2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 * $NetBSD: if_rtw_pci.c,v 1.4 2005/12/04 17:44:02 christos Exp $
38 * Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc.
39 * All rights reserved.
41 * This code is derived from software contributed to The NetBSD Foundation
42 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
43 * NASA Ames Research Center; Charles M. Hannum; and David Young.
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by the NetBSD
56 * Foundation, Inc. and its contributors.
57 * 4. Neither the name of The NetBSD Foundation nor the names of its
58 * contributors may be used to endorse or promote products derived
59 * from this software without specific prior written permission.
61 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
62 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
63 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
64 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
65 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
66 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
67 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
68 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
69 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
70 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
71 * POSSIBILITY OF SUCH DAMAGE.
75 * PCI bus front-end for the Realtek RTL8180 802.11 MAC/BBP chip.
78 #include <sys/param.h>
79 #include <sys/kernel.h>
82 #include <sys/socket.h>
84 #include <bus/pci/pcivar.h>
85 #include <bus/pci/pcireg.h>
89 #include <net/if_arp.h>
90 #include <net/if_media.h>
92 #include <netproto/802_11/ieee80211_var.h>
93 #include <netproto/802_11/ieee80211_radiotap.h>
94 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
96 #include <dev/netif/rtw/rtwreg.h>
97 #include <dev/netif/rtw/sa2400reg.h>
98 #include <dev/netif/rtw/rtwvar.h>
101 * PCI configuration space registers
103 #define RTW_PCI_IOBA 0x10 /* i/o mapped base */
104 #define RTW_PCI_MMBA 0x14 /* memory mapped base */
106 static const struct rtw_pci_reg {
110 /* Prefer IO memory over IO port */
111 { SYS_RES_MEMORY, RTW_PCI_MMBA },
112 { SYS_RES_IOPORT, RTW_PCI_IOBA }
115 static const struct rtw_pci_product {
116 uint16_t app_vendor; /* PCI vendor ID */
117 uint16_t app_product; /* PCI product ID */
118 const char *app_product_name;
119 } rtw_pci_products[] = {
120 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8180,
121 "Realtek RTL8180 802.11 MAC/BBP" },
122 { PCI_VENDOR_BELKIN, PCI_PRODUCT_BELKIN_F5D6001,
128 static int rtw_pci_probe(device_t);
129 static int rtw_pci_attach(device_t);
130 static int rtw_pci_detach(device_t);
131 static int rtw_pci_shutdown(device_t);
133 static device_method_t rtw_pci_methods[] = {
134 DEVMETHOD(device_probe, rtw_pci_probe),
135 DEVMETHOD(device_attach, rtw_pci_attach),
136 DEVMETHOD(device_detach, rtw_pci_detach),
137 DEVMETHOD(device_shutdown, rtw_pci_shutdown),
139 DEVMETHOD(device_suspend, rtw_pci_suspend),
140 DEVMETHOD(device_resume, rtw_pci_resume),
145 static driver_t rtw_pci_driver = {
148 sizeof(struct rtw_softc)
151 DRIVER_MODULE(rtw, pci, rtw_pci_driver, rtw_devclass, NULL, NULL);
152 DRIVER_MODULE(rtw, cardbus, rtw_pci_driver, rtw_devclass, NULL, NULL);
154 MODULE_DEPEND(rtw, wlan, 1, 1, 1);
155 MODULE_DEPEND(rtw, wlan_ratectl_onoe, 1, 1, 1);
156 MODULE_DEPEND(rtw, pci, 1, 1, 1);
157 MODULE_DEPEND(rtw, cardbus, 1, 1, 1);
160 rtw_pci_probe(device_t dev)
162 const struct rtw_pci_product *app;
165 vid = pci_get_vendor(dev);
166 did = pci_get_device(dev);
167 for (app = rtw_pci_products; app->app_product_name != NULL; app++) {
168 if (vid == app->app_vendor && did == app->app_product) {
169 device_set_desc(dev, app->app_product_name);
177 rtw_pci_attach(device_t dev)
179 struct rtw_softc *sc = device_get_softc(dev);
180 struct rtw_regs *regs = &sc->sc_regs;
184 * No power management hooks.
185 * XXX Maybe we should add some!
187 sc->sc_flags |= RTW_F_ENABLED;
189 sc->sc_rev = pci_get_revid(dev);
192 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
193 uint32_t mem, port, irq;
195 mem = pci_read_config(dev, RTW_PCI_MMBA, 4);
196 port = pci_read_config(dev, RTW_PCI_IOBA, 4);
197 irq = pci_read_config(dev, PCIR_INTLINE, 4);
199 device_printf(dev, "chip is in D%d power mode "
200 "-- setting to D0\n", pci_get_powerstate(dev));
202 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
204 pci_write_config(dev, RTW_PCI_MMBA, mem, 4);
205 pci_write_config(dev, RTW_PCI_IOBA, port, 4);
206 pci_write_config(dev, PCIR_INTLINE, irq, 4);
208 #endif /* !BURN_BRIDGES */
210 /* Enable PCI bus master */
211 pci_enable_busmaster(dev);
213 /* Allocate IO memory/port */
214 for (i = 0; i < NELEM(rtw_pci_regs); ++i) {
215 regs->r_rid = rtw_pci_regs[i].reg_rid;
216 regs->r_type = rtw_pci_regs[i].reg_type;
217 regs->r_res = bus_alloc_resource_any(dev, regs->r_type,
218 ®s->r_rid, RF_ACTIVE);
219 if (regs->r_res != NULL)
222 if (regs->r_res == NULL) {
223 device_printf(dev, "can't allocate IO mem/port\n");
226 regs->r_bh = rman_get_bushandle(regs->r_res);
227 regs->r_bt = rman_get_bustag(regs->r_res);
229 error = rtw_attach(dev);
236 rtw_pci_detach(device_t dev)
238 struct rtw_softc *sc = device_get_softc(dev);
239 struct rtw_regs *regs = &sc->sc_regs;
241 if (device_is_attached(dev))
244 if (regs->r_res != NULL) {
245 bus_release_resource(dev, regs->r_type, regs->r_rid,
252 rtw_pci_shutdown(device_t dev)
254 struct rtw_softc *sc = device_get_softc(dev);
255 struct ifnet *ifp = &sc->sc_ic.ic_if;
257 lwkt_serialize_enter(ifp->if_serializer);
259 lwkt_serialize_exit(ifp->if_serializer);