xref: /freebsd/sys/dev/bwi/if_bwi_pci.c (revision 09d325677d53a12c79a43664ff29871e92247629)
1 /*-
2  * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
3  * 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  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*
34  * PCI/Cardbus front-end for the Broadcom Wireless LAN controller driver.
35  */
36 
37 #include "opt_wlan.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/module.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
46 #include <sys/errno.h>
47 
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <sys/bus.h>
51 #include <sys/rman.h>
52 
53 #include <sys/socket.h>
54 
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/if_media.h>
58 #include <net/if_arp.h>
59 #include <net/ethernet.h>
60 
61 #include <net80211/ieee80211_var.h>
62 #include <net80211/ieee80211_radiotap.h>
63 #include <net80211/ieee80211_amrr.h>
64 
65 #include <dev/pci/pcivar.h>
66 #include <dev/pci/pcireg.h>
67 
68 #include <dev/bwi/if_bwivar.h>
69 #include <dev/bwi/if_bwireg.h>
70 #include <dev/bwi/bitops.h>
71 
72 /*
73  * PCI glue.
74  */
75 
76 struct bwi_pci_softc {
77 	struct bwi_softc	sc_sc;
78 };
79 
80 #define	BS_BAR	0x10
81 #define	PCIR_RETRY_TIMEOUT	0x41
82 
83 static const struct bwi_dev {
84 	uint16_t	vid;
85 	uint16_t	did;
86 	const char	*desc;
87 } bwi_devices[] = {
88 	{ PCI_VENDOR_BROADCOM, 0x4301,"Broadcom BCM4301 802.11b Wireless Lan" },
89 	{ PCI_VENDOR_BROADCOM, 0x4307,"Broadcom BCM4307 802.11b Wireless Lan" },
90 	{ PCI_VENDOR_BROADCOM, 0x4311,"Broadcom BCM4311 802.11b/g Wireless Lan" },
91 	{ PCI_VENDOR_BROADCOM, 0x4312,"Broadcom BCM4312 802.11a/b/g Wireless Lan" },
92 	{ PCI_VENDOR_BROADCOM, 0x4313,"Broadcom BCM4312 802.11a Wireless Lan" },
93 	{ PCI_VENDOR_BROADCOM, 0x4320,"Broadcom BCM4306 802.11b/g Wireless Lan"},
94 	{ PCI_VENDOR_BROADCOM, 0x4321,"Broadcom BCM4306 802.11a Wireless Lan"},
95 	{ PCI_VENDOR_BROADCOM, 0x4325,"Broadcom BCM4306 802.11b/g Wireless Lan"},
96 	{ PCI_VENDOR_BROADCOM, 0x4324,"Broadcom BCM4309 802.11a/b/g Wireless Lan" },
97 	{ PCI_VENDOR_BROADCOM, 0x4318,"Broadcom BCM4318 802.11b/g Wireless Lan" },
98 	{ PCI_VENDOR_BROADCOM, 0x4319,"Broadcom BCM4318 802.11a/b/g Wireless Lan" },
99 	{ PCI_VENDOR_BROADCOM, 0x431a,"Broadcom BCM4318 802.11a Wireless Lan" },
100 	{ 0, 0, NULL }
101 };
102 
103 static int
104 bwi_pci_probe(device_t dev)
105 {
106 	const struct bwi_dev *b;
107 	uint16_t did, vid;
108 
109 	did = pci_get_device(dev);
110 	vid = pci_get_vendor(dev);
111 
112 	for (b = bwi_devices; b->desc != NULL; ++b) {
113 		if (b->did == did && b->vid == vid) {
114 			device_set_desc(dev, b->desc);
115 			return BUS_PROBE_DEFAULT;
116 		}
117 	}
118 	return ENXIO;
119 }
120 
121 static int
122 bwi_pci_attach(device_t dev)
123 {
124 	struct bwi_pci_softc *psc = device_get_softc(dev);
125 	struct bwi_softc *sc = &psc->sc_sc;
126 	int error = ENXIO;
127 
128 	sc->sc_dev = dev;
129 
130 	/*
131 	 * Enable bus mastering.
132 	 */
133 	pci_enable_busmaster(dev);
134 
135 	/*
136 	 * Setup memory-mapping of PCI registers.
137 	 */
138 	sc->sc_mem_rid = BWI_PCIR_BAR;
139 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
140 		&sc->sc_mem_rid, RF_ACTIVE);
141 	if (sc->sc_mem_res == NULL) {
142 		device_printf(dev, "cannot map register space\n");
143 		goto bad;
144 	}
145 	sc->sc_mem_bt = rman_get_bustag(sc->sc_mem_res);
146 	sc->sc_mem_bh = rman_get_bushandle(sc->sc_mem_res);
147 	/*
148 	 * Mark device invalid so any interrupts (shared or otherwise)
149 	 * that arrive before the card is setup are discarded.
150 	 */
151 	sc->sc_invalid = 1;
152 
153 	/*
154 	 * Arrange interrupt line.
155 	 */
156 	sc->sc_irq_rid = 0;
157 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
158 						&sc->sc_irq_rid,
159 						RF_SHAREABLE|RF_ACTIVE);
160 	if (sc->sc_irq_res == NULL) {
161 		device_printf(dev, "could not map interrupt\n");
162 		goto bad1;
163 	}
164 	if (bus_setup_intr(dev, sc->sc_irq_res,
165 			   INTR_TYPE_NET | INTR_MPSAFE,
166 			   NULL, bwi_intr, sc, &sc->sc_irq_handle)) {
167 		device_printf(dev, "could not establish interrupt\n");
168 		goto bad2;
169 	}
170 
171 	/* Get more PCI information */
172 	sc->sc_pci_did = pci_get_device(dev);
173 	sc->sc_pci_revid = pci_get_revid(dev);
174 	sc->sc_pci_subvid = pci_get_subvendor(dev);
175 	sc->sc_pci_subdid = pci_get_subdevice(dev);
176 
177 	error = bwi_attach(sc);
178 	if (error == 0)					/* success */
179 		return 0;
180 
181 	bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_handle);
182 bad2:
183 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
184 bad1:
185 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_mem_res);
186 bad:
187 	return (error);
188 }
189 
190 static int
191 bwi_pci_detach(device_t dev)
192 {
193 	struct bwi_pci_softc *psc = device_get_softc(dev);
194 	struct bwi_softc *sc = &psc->sc_sc;
195 
196 	/* check if device was removed */
197 	sc->sc_invalid = !bus_child_present(dev);
198 
199 	bwi_detach(sc);
200 
201 	bus_generic_detach(dev);
202 	bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_handle);
203 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
204 
205 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_mem_res);
206 
207 	return (0);
208 }
209 
210 static int
211 bwi_pci_shutdown(device_t dev)
212 {
213 	struct bwi_pci_softc *psc = device_get_softc(dev);
214 
215 	bwi_shutdown(&psc->sc_sc);
216 	return (0);
217 }
218 
219 static int
220 bwi_pci_suspend(device_t dev)
221 {
222 	struct bwi_pci_softc *psc = device_get_softc(dev);
223 
224 	bwi_suspend(&psc->sc_sc);
225 
226 	return (0);
227 }
228 
229 static int
230 bwi_pci_resume(device_t dev)
231 {
232 	struct bwi_pci_softc *psc = device_get_softc(dev);
233 
234 	bwi_resume(&psc->sc_sc);
235 
236 	return (0);
237 }
238 
239 static device_method_t bwi_pci_methods[] = {
240 	/* Device interface */
241 	DEVMETHOD(device_probe,		bwi_pci_probe),
242 	DEVMETHOD(device_attach,	bwi_pci_attach),
243 	DEVMETHOD(device_detach,	bwi_pci_detach),
244 	DEVMETHOD(device_shutdown,	bwi_pci_shutdown),
245 	DEVMETHOD(device_suspend,	bwi_pci_suspend),
246 	DEVMETHOD(device_resume,	bwi_pci_resume),
247 
248 	{ 0,0 }
249 };
250 static driver_t bwi_driver = {
251 	"bwi",
252 	bwi_pci_methods,
253 	sizeof (struct bwi_pci_softc)
254 };
255 static	devclass_t bwi_devclass;
256 DRIVER_MODULE(bwi, pci, bwi_driver, bwi_devclass, 0, 0);
257 MODULE_DEPEND(bwi, wlan, 1, 1, 1);		/* 802.11 media layer */
258 MODULE_DEPEND(bwi, firmware, 1, 1, 1);		/* firmware support */
259 MODULE_DEPEND(bwi, wlan_amrr, 1, 1, 1);
260