xref: /freebsd/sys/dev/bwi/if_bwi_pci.c (revision 830940567b49bb0c08dfaed40418999e76616909)
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 };
96 
97 static int
98 bwi_pci_probe(device_t dev)
99 {
100 	const struct bwi_dev *b;
101 	uint16_t did, vid;
102 
103 	did = pci_get_device(dev);
104 	vid = pci_get_vendor(dev);
105 
106 	for (b = bwi_devices; b->desc != NULL; ++b) {
107 		if (b->did == did && b->vid == vid) {
108 			device_set_desc(dev, b->desc);
109 			return BUS_PROBE_DEFAULT;
110 		}
111 	}
112 	return ENXIO;
113 }
114 
115 static int
116 bwi_pci_attach(device_t dev)
117 {
118 	struct bwi_pci_softc *psc = device_get_softc(dev);
119 	struct bwi_softc *sc = &psc->sc_sc;
120 	int error = ENXIO;
121 
122 	sc->sc_dev = dev;
123 
124 	/*
125 	 * Enable bus mastering.
126 	 */
127 	pci_enable_busmaster(dev);
128 
129 	/*
130 	 * Setup memory-mapping of PCI registers.
131 	 */
132 	sc->sc_mem_rid = BWI_PCIR_BAR;
133 	sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
134 		&sc->sc_mem_rid, RF_ACTIVE);
135 	if (sc->sc_mem_res == NULL) {
136 		device_printf(dev, "cannot map register space\n");
137 		goto bad;
138 	}
139 	sc->sc_mem_bt = rman_get_bustag(sc->sc_mem_res);
140 	sc->sc_mem_bh = rman_get_bushandle(sc->sc_mem_res);
141 	/*
142 	 * Mark device invalid so any interrupts (shared or otherwise)
143 	 * that arrive before the card is setup are discarded.
144 	 */
145 	sc->sc_invalid = 1;
146 
147 	/*
148 	 * Arrange interrupt line.
149 	 */
150 	sc->sc_irq_rid = 0;
151 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
152 						&sc->sc_irq_rid,
153 						RF_SHAREABLE|RF_ACTIVE);
154 	if (sc->sc_irq_res == NULL) {
155 		device_printf(dev, "could not map interrupt\n");
156 		goto bad1;
157 	}
158 	if (bus_setup_intr(dev, sc->sc_irq_res,
159 			   INTR_TYPE_NET | INTR_MPSAFE,
160 			   NULL, bwi_intr, sc, &sc->sc_irq_handle)) {
161 		device_printf(dev, "could not establish interrupt\n");
162 		goto bad2;
163 	}
164 
165 	/* Get more PCI information */
166 	sc->sc_pci_did = pci_get_device(dev);
167 	sc->sc_pci_revid = pci_get_revid(dev);
168 	sc->sc_pci_subvid = pci_get_subvendor(dev);
169 	sc->sc_pci_subdid = pci_get_subdevice(dev);
170 
171 	error = bwi_attach(sc);
172 	if (error == 0)					/* success */
173 		return 0;
174 
175 	bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_handle);
176 bad2:
177 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
178 bad1:
179 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_mem_res);
180 bad:
181 	return (error);
182 }
183 
184 static int
185 bwi_pci_detach(device_t dev)
186 {
187 	struct bwi_pci_softc *psc = device_get_softc(dev);
188 	struct bwi_softc *sc = &psc->sc_sc;
189 
190 	/* check if device was removed */
191 	sc->sc_invalid = !bus_child_present(dev);
192 
193 	bwi_detach(sc);
194 
195 	bus_generic_detach(dev);
196 	bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_irq_handle);
197 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
198 
199 	bus_release_resource(dev, SYS_RES_MEMORY, BS_BAR, sc->sc_mem_res);
200 
201 	return (0);
202 }
203 
204 static int
205 bwi_pci_shutdown(device_t dev)
206 {
207 	struct bwi_pci_softc *psc = device_get_softc(dev);
208 
209 	bwi_shutdown(&psc->sc_sc);
210 	return (0);
211 }
212 
213 static int
214 bwi_pci_suspend(device_t dev)
215 {
216 	struct bwi_pci_softc *psc = device_get_softc(dev);
217 
218 	bwi_suspend(&psc->sc_sc);
219 
220 	return (0);
221 }
222 
223 static int
224 bwi_pci_resume(device_t dev)
225 {
226 	struct bwi_pci_softc *psc = device_get_softc(dev);
227 
228 	bwi_resume(&psc->sc_sc);
229 
230 	return (0);
231 }
232 
233 static device_method_t bwi_pci_methods[] = {
234 	/* Device interface */
235 	DEVMETHOD(device_probe,		bwi_pci_probe),
236 	DEVMETHOD(device_attach,	bwi_pci_attach),
237 	DEVMETHOD(device_detach,	bwi_pci_detach),
238 	DEVMETHOD(device_shutdown,	bwi_pci_shutdown),
239 	DEVMETHOD(device_suspend,	bwi_pci_suspend),
240 	DEVMETHOD(device_resume,	bwi_pci_resume),
241 
242 	{ 0,0 }
243 };
244 static driver_t bwi_driver = {
245 	"bwi",
246 	bwi_pci_methods,
247 	sizeof (struct bwi_pci_softc)
248 };
249 static	devclass_t bwi_devclass;
250 DRIVER_MODULE(bwi, pci, bwi_driver, bwi_devclass, 0, 0);
251 MODULE_DEPEND(bwi, wlan, 1, 1, 1);		/* 802.11 media layer */
252 MODULE_DEPEND(bwi, firmware, 1, 1, 1);		/* firmware support */
253 MODULE_DEPEND(bwi, wlan_amrr, 1, 1, 1);
254