mailer.conf - Add missing send-mail for dma.
[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  */
36
37 /*
38  * Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc.
39  * All rights reserved.
40  *
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.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
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.
60  *
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.
72  */
73
74 /*
75  * PCI bus front-end for the Realtek RTL8180 802.11 MAC/BBP chip.
76  */
77
78 #include <sys/param.h>
79 #include <sys/kernel.h>
80 #include <sys/bus.h>
81 #include <sys/rman.h>
82 #include <sys/socket.h>
83
84 #include <bus/pci/pcivar.h>
85 #include <bus/pci/pcireg.h>
86 #include "pcidevs.h"
87
88 #include <net/if.h>
89 #include <net/if_arp.h>
90 #include <net/if_media.h>
91
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>
95
96 #include <dev/netif/rtw/rtwreg.h>
97 #include <dev/netif/rtw/sa2400reg.h>
98 #include <dev/netif/rtw/rtwvar.h>
99
100 /*
101  * PCI configuration space registers
102  */
103 #define RTW_PCI_IOBA            0x10    /* i/o mapped base */
104 #define RTW_PCI_MMBA            0x14    /* memory mapped base */
105
106 static const struct rtw_pci_reg {
107         int     reg_type;
108         int     reg_rid;
109 } rtw_pci_regs[] = {
110         /* Prefer IO memory over IO port */
111         { SYS_RES_MEMORY, RTW_PCI_MMBA },
112         { SYS_RES_IOPORT, RTW_PCI_IOBA }
113 };
114
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,
123           "Belkin F5D6001" },
124
125         { 0, 0, NULL }
126 };
127
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);
132
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),
138 #if 0
139         DEVMETHOD(device_suspend,       rtw_pci_suspend),
140         DEVMETHOD(device_resume,        rtw_pci_resume),
141 #endif
142         DEVMETHOD_END
143 };
144
145 static driver_t rtw_pci_driver = {
146         "rtw",
147         rtw_pci_methods,
148         sizeof(struct rtw_softc)
149 };
150
151 DRIVER_MODULE(rtw, pci, rtw_pci_driver, rtw_devclass, NULL, NULL);
152 DRIVER_MODULE(rtw, cardbus, rtw_pci_driver, rtw_devclass, NULL, NULL);
153
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);
158
159 static int
160 rtw_pci_probe(device_t dev)
161 {
162         const struct rtw_pci_product *app;
163         uint16_t vid, did;
164
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);
170                         return 0;
171                 }
172         }
173         return ENXIO;
174 }
175
176 static int
177 rtw_pci_attach(device_t dev)
178 {
179         struct rtw_softc *sc = device_get_softc(dev);
180         struct rtw_regs *regs = &sc->sc_regs;
181         int i, error;
182
183         /*
184          * No power management hooks.
185          * XXX Maybe we should add some!
186          */
187         sc->sc_flags |= RTW_F_ENABLED;
188
189         sc->sc_rev = pci_get_revid(dev);
190
191 #ifndef BURN_BRIDGES
192         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
193                 uint32_t mem, port, irq;
194                                                 
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);
198                 
199                 device_printf(dev, "chip is in D%d power mode "
200                     "-- setting to D0\n", pci_get_powerstate(dev));
201
202                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
203
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);
207         }
208 #endif  /* !BURN_BRIDGES */
209
210         /* Enable PCI bus master */
211         pci_enable_busmaster(dev);
212
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                                                      &regs->r_rid, RF_ACTIVE);
219                 if (regs->r_res != NULL)
220                         break;
221         }
222         if (regs->r_res == NULL) {
223                 device_printf(dev, "can't allocate IO mem/port\n");
224                 return ENXIO;
225         }
226         regs->r_bh = rman_get_bushandle(regs->r_res);
227         regs->r_bt = rman_get_bustag(regs->r_res);
228
229         error = rtw_attach(dev);
230         if (error)
231                 rtw_pci_detach(dev);
232         return error;
233 }
234
235 static int
236 rtw_pci_detach(device_t dev)
237 {
238         struct rtw_softc *sc = device_get_softc(dev);
239         struct rtw_regs *regs = &sc->sc_regs;
240
241         if (device_is_attached(dev))
242                 rtw_detach(dev);
243
244         if (regs->r_res != NULL) {
245                 bus_release_resource(dev, regs->r_type, regs->r_rid,
246                                      regs->r_res);
247         }
248         return 0;
249 }
250
251 static int
252 rtw_pci_shutdown(device_t dev)
253 {
254         struct rtw_softc *sc = device_get_softc(dev);
255         struct ifnet *ifp = &sc->sc_ic.ic_if;
256
257         lwkt_serialize_enter(ifp->if_serializer);
258         rtw_stop(sc, 1);
259         lwkt_serialize_exit(ifp->if_serializer);
260         return 0;
261 }