xref: /freebsd/sys/dev/bwi/if_bwi_pci.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
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 <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/module.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/errno.h>
44 
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47 #include <sys/bus.h>
48 #include <sys/rman.h>
49 
50 #include <sys/socket.h>
51 
52 #include <net/if.h>
53 #include <net/if_media.h>
54 #include <net/if_arp.h>
55 
56 #include <net80211/ieee80211_var.h>
57 #include <net80211/ieee80211_radiotap.h>
58 #include <net80211/ieee80211_amrr.h>
59 
60 #include <dev/pci/pcivar.h>
61 #include <dev/pci/pcireg.h>
62 
63 #include <dev/bwi/if_bwivar.h>
64 #include <dev/bwi/if_bwireg.h>
65 #include <dev/bwi/bitops.h>
66 
67 /*
68  * PCI glue.
69  */
70 
71 struct bwi_pci_softc {
72 	struct bwi_softc	sc_sc;
73 };
74 
75 #define	BS_BAR	0x10
76 #define	PCIR_RETRY_TIMEOUT	0x41
77 
78 static const struct bwi_dev {
79 	uint16_t	vid;
80 	uint16_t	did;
81 	const char	*desc;
82 } bwi_devices[] = {
83 	{ PCI_VENDOR_BROADCOM, 0x4301,"Broadcom BCM4301 802.11b Wireless Lan" },
84 	{ PCI_VENDOR_BROADCOM, 0x4307,"Broadcom BCM4307 802.11b Wireless Lan" },
85 	{ PCI_VENDOR_BROADCOM, 0x4311,"Broadcom BCM4311 802.11b/g Wireless Lan" },
86 	{ PCI_VENDOR_BROADCOM, 0x4312,"Broadcom BCM4312 802.11a/b/g Wireless Lan" },
87 	{ PCI_VENDOR_BROADCOM, 0x4313,"Broadcom BCM4312 802.11a Wireless Lan" },
88 	{ PCI_VENDOR_BROADCOM, 0x4320,"Broadcom BCM4306 802.11b/g Wireless Lan"},
89 	{ PCI_VENDOR_BROADCOM, 0x4321,"Broadcom BCM4306 802.11a Wireless Lan"},
90 	{ PCI_VENDOR_BROADCOM, 0x4325,"Broadcom BCM4306 802.11b/g Wireless Lan"},
91 	{ PCI_VENDOR_BROADCOM, 0x4324,"Broadcom BCM4309 802.11a/b/g Wireless Lan" },
92 	{ PCI_VENDOR_BROADCOM, 0x4318,"Broadcom BCM4318 802.11b/g Wireless Lan" },
93 	{ PCI_VENDOR_BROADCOM, 0x4319,"Broadcom BCM4318 802.11a/b/g Wireless Lan" },
94 	{ PCI_VENDOR_BROADCOM, 0x431a,"Broadcom BCM4318 802.11a Wireless Lan" },
95 	{ 0, 0, NULL }
96 };
97 
98 static int
99 bwi_pci_probe(device_t dev)
100 {
101 	const struct bwi_dev *b;
102 	uint16_t did, vid;
103 
104 	did = pci_get_device(dev);
105 	vid = pci_get_vendor(dev);
106 
107 	for (b = bwi_devices; b->desc != NULL; ++b) {
108 		if (b->did == did && b->vid == vid) {
109 			device_set_desc(dev, b->desc);
110 			return BUS_PROBE_DEFAULT;
111 		}
112 	}
113 	return ENXIO;
114 }
115 
116 static int
117 bwi_pci_attach(device_t dev)
118 {
119 	struct bwi_pci_softc *psc = device_get_softc(dev);
120 	struct bwi_softc *sc = &psc->sc_sc;
121 	int error = ENXIO;
122 
123 	sc->sc_dev = dev;
124 
125 	/*
126 	 * Enable bus mastering.
127 	 */
128 	pci_enable_busmaster(dev);
129 
130 	/*
131 	 * Setup memory-mapping of PCI registers.
132 	 */
133 	sc->sc_mem_rid = BWI_PCIR_BAR;
134 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
135 		&sc->sc_mem_rid, RF_ACTIVE);
136 	if (sc->sc_mem_res == NULL) {
137 		device_printf(dev, "cannot map register space\n");
138 		goto bad;
139 	}
140 	sc->sc_mem_bt = rman_get_bustag(sc->sc_mem_res);
141 	sc->sc_mem_bh = rman_get_bushandle(sc->sc_mem_res);
142 	/*
143 	 * Mark device invalid so any interrupts (shared or otherwise)
144 	 * that arrive before the card is setup are discarded.
145 	 */
146 	sc->sc_invalid = 1;
147 
148 	/*
149 	 * Arrange interrupt line.
150 	 */
151 	sc->sc_irq_rid = 0;
152 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
153 						&sc->sc_irq_rid,
154 						RF_SHAREABLE|RF_ACTIVE);
155 	if (sc->sc_irq_res == NULL) {
156 		device_printf(dev, "could not map interrupt\n");
157 		goto bad1;
158 	}
159 	if (bus_setup_intr(dev, sc->sc_irq_res,
160 			   INTR_TYPE_NET | INTR_MPSAFE,
161 			   NULL, bwi_intr, sc, &sc->sc_irq_handle)) {
162 		device_printf(dev, "could not establish interrupt\n");
163 		goto bad2;
164 	}
165 
166 	/* Get more PCI information */
167 	sc->sc_pci_did = pci_get_device(dev);
168 	sc->sc_pci_revid = pci_get_revid(dev);
169 	sc->sc_pci_subvid = pci_get_subvendor(dev);
170 	sc->sc_pci_subdid = pci_get_subdevice(dev);
171 
172 	error = bwi_attach(sc);
173 	if (error == 0)					/* success */
174 		return 0;
175 
176 	bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_handle);
177 bad2:
178 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
179 bad1:
180 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_mem_res);
181 bad:
182 	return (error);
183 }
184 
185 static int
186 bwi_pci_detach(device_t dev)
187 {
188 	struct bwi_pci_softc *psc = device_get_softc(dev);
189 	struct bwi_softc *sc = &psc->sc_sc;
190 
191 	/* check if device was removed */
192 	sc->sc_invalid = !bus_child_present(dev);
193 
194 	bwi_detach(sc);
195 
196 	bus_generic_detach(dev);
197 	bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_handle);
198 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
199 
200 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_mem_res);
201 
202 	return (0);
203 }
204 
205 static int
206 bwi_pci_shutdown(device_t dev)
207 {
208 	struct bwi_pci_softc *psc = device_get_softc(dev);
209 
210 	bwi_shutdown(&psc->sc_sc);
211 	return (0);
212 }
213 
214 static int
215 bwi_pci_suspend(device_t dev)
216 {
217 	struct bwi_pci_softc *psc = device_get_softc(dev);
218 
219 	bwi_suspend(&psc->sc_sc);
220 
221 	return (0);
222 }
223 
224 static int
225 bwi_pci_resume(device_t dev)
226 {
227 	struct bwi_pci_softc *psc = device_get_softc(dev);
228 
229 	bwi_resume(&psc->sc_sc);
230 
231 	return (0);
232 }
233 
234 static device_method_t bwi_pci_methods[] = {
235 	/* Device interface */
236 	DEVMETHOD(device_probe,		bwi_pci_probe),
237 	DEVMETHOD(device_attach,	bwi_pci_attach),
238 	DEVMETHOD(device_detach,	bwi_pci_detach),
239 	DEVMETHOD(device_shutdown,	bwi_pci_shutdown),
240 	DEVMETHOD(device_suspend,	bwi_pci_suspend),
241 	DEVMETHOD(device_resume,	bwi_pci_resume),
242 
243 	{ 0,0 }
244 };
245 static driver_t bwi_driver = {
246 	"bwi",
247 	bwi_pci_methods,
248 	sizeof (struct bwi_pci_softc)
249 };
250 static	devclass_t bwi_devclass;
251 DRIVER_MODULE(bwi, pci, bwi_driver, bwi_devclass, 0, 0);
252 MODULE_DEPEND(bwi, wlan, 1, 1, 1);		/* 802.11 media layer */
253 MODULE_DEPEND(bwi, firmware, 1, 1, 1);		/* firmware support */
254 MODULE_DEPEND(bwi, wlan_amrr, 1, 1, 1);
255