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