- Store tx rate control parameters in drivers, so switching between tx rate
[dragonfly.git] / sys / dev / netif / rtw / if_rtw_pci.c
blobc403ce06c9d706e317e05fda746cb857319e7104
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:
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.
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.
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 $
39 * Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc.
40 * All rights reserved.
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.
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.
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.
76 * PCI bus front-end for the Realtek RTL8180 802.11 MAC/BBP chip.
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>
85 #include <bus/pci/pcivar.h>
86 #include <bus/pci/pcireg.h>
87 #include <bus/pci/pcidevs.h>
89 #include <net/if.h>
90 #include <net/if_arp.h>
91 #include <net/if_media.h>
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>
97 #include <dev/netif/rtw/rtwreg.h>
98 #include <dev/netif/rtw/sa2400reg.h>
99 #include <dev/netif/rtw/rtwvar.h>
102 * PCI configuration space registers
104 #define RTW_PCI_IOBA 0x10 /* i/o mapped base */
105 #define RTW_PCI_MMBA 0x14 /* memory mapped base */
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 }
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" },
126 { 0, 0, NULL }
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);
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 }
146 static driver_t rtw_pci_driver = {
147 "rtw",
148 rtw_pci_methods,
149 sizeof(struct rtw_softc)
152 DRIVER_MODULE(rtw, pci, rtw_pci_driver, rtw_devclass, 0, 0);
153 DRIVER_MODULE(rtw, cardbus, rtw_pci_driver, rtw_devclass, 0, 0);
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);
160 static int
161 rtw_pci_probe(device_t dev)
163 const struct rtw_pci_product *app;
164 uint16_t vid, did;
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;
174 return ENXIO;
177 static int
178 rtw_pci_attach(device_t dev)
180 struct rtw_softc *sc = device_get_softc(dev);
181 struct rtw_regs *regs = &sc->sc_regs;
182 int i, error;
185 * No power management hooks.
186 * XXX Maybe we should add some!
188 sc->sc_flags |= RTW_F_ENABLED;
190 sc->sc_rev = pci_get_revid(dev);
192 #ifndef BURN_BRIDGES
193 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
194 uint32_t mem, port, irq;
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);
200 device_printf(dev, "chip is in D%d power mode "
201 "-- setting to D0\n", pci_get_powerstate(dev));
203 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
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);
209 #endif /* !BURN_BRIDGES */
211 /* Enable PCI bus master */
212 pci_enable_busmaster(dev);
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;
224 #undef N
225 if (regs->r_res == NULL) {
226 device_printf(dev, "can't allocate IO mem/port\n");
227 return ENXIO;
229 regs->r_bh = rman_get_bushandle(regs->r_res);
230 regs->r_bt = rman_get_bustag(regs->r_res);
232 error = rtw_attach(dev);
233 if (error)
234 rtw_pci_detach(dev);
235 return error;
238 static int
239 rtw_pci_detach(device_t dev)
241 struct rtw_softc *sc = device_get_softc(dev);
242 struct rtw_regs *regs = &sc->sc_regs;
244 if (device_is_attached(dev))
245 rtw_detach(dev);
247 if (regs->r_res != NULL) {
248 bus_release_resource(dev, regs->r_type, regs->r_rid,
249 regs->r_res);
251 return 0;
254 static int
255 rtw_pci_shutdown(device_t dev)
257 struct rtw_softc *sc = device_get_softc(dev);
258 struct ifnet *ifp = &sc->sc_ic.ic_if;
260 lwkt_serialize_enter(ifp->if_serializer);
261 rtw_stop(sc, 1);
262 lwkt_serialize_exit(ifp->if_serializer);
263 return 0;