2 * Copyright (c) 2013 Adrian Chadd <adrian@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * This implements some very basic bluetooth coexistence methods for
36 * the ath(4) hardware.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/sysctl.h>
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.h>
49 #include <sys/errno.h>
50 #include <machine/bus.h>
51 #include <machine/resource.h>
55 #include <sys/socket.h>
58 #include <net/if_var.h>
59 #include <net/if_media.h>
60 #include <net/if_arp.h>
61 #include <net/ethernet.h> /* XXX for ether_sprintf */
63 #include <net80211/ieee80211_var.h>
68 #include <netinet/in.h>
69 #include <netinet/if_ether.h>
72 #include <dev/ath/if_athvar.h>
73 #include <dev/ath/if_ath_btcoex.h>
74 #include <dev/ath/if_ath_btcoex_mci.h>
76 MALLOC_DECLARE(M_ATHDEV);
79 * Initial AR9285 / (WB195) bluetooth coexistence settings,
80 * just for experimentation.
82 * Return 0 for OK; errno for error.
84 * XXX TODO: There needs to be a PCIe workaround to disable ASPM if
85 * bluetooth coexistence is enabled.
88 ath_btcoex_cfg_wb195(struct ath_softc *sc)
90 HAL_BT_COEX_INFO btinfo;
91 HAL_BT_COEX_CONFIG btconfig;
92 struct ath_hal *ah = sc->sc_ah;
94 if (! ath_hal_btcoex_supported(ah))
97 bzero(&btinfo, sizeof(btinfo));
98 bzero(&btconfig, sizeof(btconfig));
100 device_printf(sc->sc_dev, "Enabling WB195 BTCOEX\n");
102 btinfo.bt_module = HAL_BT_MODULE_JANUS;
103 btinfo.bt_coex_config = HAL_BT_COEX_CFG_3WIRE;
105 * These are the three GPIO pins hooked up between the AR9285 and
108 btinfo.bt_gpio_bt_active = 6;
109 btinfo.bt_gpio_bt_priority = 7;
110 btinfo.bt_gpio_wlan_active = 5;
111 btinfo.bt_active_polarity = 1; /* XXX not used */
112 btinfo.bt_single_ant = 1; /* 1 antenna on ar9285 ? */
113 btinfo.bt_isolation = 0; /* in dB, not used */
115 ath_hal_btcoex_set_info(ah, &btinfo);
117 btconfig.bt_time_extend = 0;
118 btconfig.bt_txstate_extend = 1; /* true */
119 btconfig.bt_txframe_extend = 1; /* true */
120 btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
121 btconfig.bt_quiet_collision = 1; /* true */
122 btconfig.bt_rxclear_polarity = 1; /* true */
123 btconfig.bt_priority_time = 2;
124 btconfig.bt_first_slot_time = 5;
125 btconfig.bt_hold_rxclear = 1; /* true */
127 ath_hal_btcoex_set_config(ah, &btconfig);
130 * Enable antenna diversity.
132 ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
138 * Initial AR9485 / (WB225) bluetooth coexistence settings,
139 * just for experimentation.
141 * Return 0 for OK; errno for error.
144 ath_btcoex_cfg_wb225(struct ath_softc *sc)
146 HAL_BT_COEX_INFO btinfo;
147 HAL_BT_COEX_CONFIG btconfig;
148 struct ath_hal *ah = sc->sc_ah;
150 if (! ath_hal_btcoex_supported(ah))
153 bzero(&btinfo, sizeof(btinfo));
154 bzero(&btconfig, sizeof(btconfig));
156 device_printf(sc->sc_dev, "Enabling WB225 BTCOEX\n");
158 btinfo.bt_module = HAL_BT_MODULE_JANUS; /* XXX not used? */
159 btinfo.bt_coex_config = HAL_BT_COEX_CFG_3WIRE;
161 * These are the three GPIO pins hooked up between the AR9485 and
162 * the bluetooth module.
164 btinfo.bt_gpio_bt_active = 4;
165 btinfo.bt_gpio_bt_priority = 8;
166 btinfo.bt_gpio_wlan_active = 5;
168 btinfo.bt_active_polarity = 1; /* XXX not used */
169 btinfo.bt_single_ant = 1; /* 1 antenna on ar9285 ? */
170 btinfo.bt_isolation = 0; /* in dB, not used */
172 ath_hal_btcoex_set_info(ah, &btinfo);
174 btconfig.bt_time_extend = 0;
175 btconfig.bt_txstate_extend = 1; /* true */
176 btconfig.bt_txframe_extend = 1; /* true */
177 btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
178 btconfig.bt_quiet_collision = 1; /* true */
179 btconfig.bt_rxclear_polarity = 1; /* true */
180 btconfig.bt_priority_time = 2;
181 btconfig.bt_first_slot_time = 5;
182 btconfig.bt_hold_rxclear = 1; /* true */
184 ath_hal_btcoex_set_config(ah, &btconfig);
187 * Enable antenna diversity.
189 ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
195 ath_btcoex_cfg_mci(struct ath_softc *sc, uint32_t mci_cfg, int do_btdiv)
197 HAL_BT_COEX_INFO btinfo;
198 HAL_BT_COEX_CONFIG btconfig;
199 struct ath_hal *ah = sc->sc_ah;
201 if (! ath_hal_btcoex_supported(ah))
204 bzero(&btinfo, sizeof(btinfo));
205 bzero(&btconfig, sizeof(btconfig));
207 sc->sc_ah->ah_config.ath_hal_mci_config = mci_cfg;
209 if (ath_btcoex_mci_attach(sc) != 0) {
210 device_printf(sc->sc_dev, "Failed to setup btcoex\n");
214 btinfo.bt_module = HAL_BT_MODULE_JANUS; /* XXX not used? */
215 btinfo.bt_coex_config = HAL_BT_COEX_CFG_MCI;
218 * MCI uses a completely different interface to speak
219 * to the bluetooth module - it's a command based
220 * thing over a serial line, rather than
221 * state pins to/from the bluetooth module.
223 * So, the GPIO configuration, polarity, etc
224 * doesn't matter on MCI devices; it's just
225 * completely ignored by the HAL.
227 btinfo.bt_gpio_bt_active = 4;
228 btinfo.bt_gpio_bt_priority = 8;
229 btinfo.bt_gpio_wlan_active = 5;
231 btinfo.bt_active_polarity = 1; /* XXX not used */
232 btinfo.bt_single_ant = 0; /* 2 antenna on WB335 */
233 btinfo.bt_isolation = 0; /* in dB, not used */
235 ath_hal_btcoex_set_info(ah, &btinfo);
237 btconfig.bt_time_extend = 0;
238 btconfig.bt_txstate_extend = 1; /* true */
239 btconfig.bt_txframe_extend = 1; /* true */
240 btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
241 btconfig.bt_quiet_collision = 1; /* true */
242 btconfig.bt_rxclear_polarity = 1; /* true */
243 btconfig.bt_priority_time = 2;
244 btconfig.bt_first_slot_time = 5;
245 btconfig.bt_hold_rxclear = 1; /* true */
247 ath_hal_btcoex_set_config(ah, &btconfig);
250 ath_hal_btcoex_enable(sc->sc_ah);
253 ath_hal_btcoex_set_weights(ah, HAL_BT_COEX_STOMP_NONE);
256 * Enable antenna diversity.
258 ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY,
265 * Initial AR9462 / (WB222) bluetooth coexistence settings.
267 * Return 0 for OK; errno for error.
270 ath_btcoex_cfg_wb222(struct ath_softc *sc)
273 device_printf(sc->sc_dev, "Enabling WB222 BTCOEX\n");
275 return (ath_btcoex_cfg_mci(sc, 0x2201, 1));
279 * Initial QCA9565 / (WB335B) bluetooth coexistence settings.
281 * Return 0 for OK; errno for error.
284 ath_btcoex_cfg_wb335b(struct ath_softc *sc)
292 /* 1-ant and 2-ant AR9565 */
294 * XXX TODO: ensure these actually make it down to the
297 if (sc->sc_pci_devinfo & ATH_PCI_AR9565_1ANT) {
298 flags &= ~ATH_MCI_CONFIG_ANT_ARCH;
299 flags |= ATH_MCI_ANT_ARCH_1_ANT_PA_LNA_SHARED <<
300 ATH_MCI_CONFIG_ANT_ARCH_S;
301 } else if (sc->sc_pci_devinfo & ATH_PCI_AR9565_2ANT) {
302 flags &= ~ATH_MCI_CONFIG_ANT_ARCH;
303 flags |= ATH_MCI_ANT_ARCH_2_ANT_PA_LNA_NON_SHARED <<
304 ATH_MCI_CONFIG_ANT_ARCH_S;
307 if (sc->sc_pci_devinfo & ATH_PCI_BT_ANT_DIV) {
311 device_printf(sc->sc_dev, "Enabling WB335 BTCOEX\n");
313 return (ath_btcoex_cfg_mci(sc, flags, do_btdiv));
318 * When using bluetooth coexistence, ASPM needs to be disabled
319 * otherwise the sleeping interferes with the bluetooth (USB)
320 * operation and the MAC sleep/wakeup hardware.
322 * The PCIe powersave routine also needs to not be called
323 * by the driver during suspend/resume, else things will get
324 * a little odd. Check Linux ath9k for more details.
327 ath_btcoex_aspm_wb195(struct ath_softc *sc)
330 /* XXX TODO: clear device ASPM L0S and L1 */
331 /* XXX TODO: clear _parent_ ASPM L0S and L1 */
336 * Methods which are required
340 * Attach btcoex to the given interface
343 ath_btcoex_attach(struct ath_softc *sc)
346 struct ath_hal *ah = sc->sc_ah;
347 const char *profname;
350 * No chipset bluetooth coexistence? Then do nothing.
352 if (! ath_hal_btcoex_supported(ah))
356 * Look at the hints to determine which bluetooth
357 * profile to configure.
359 ret = resource_string_value(device_get_name(sc->sc_dev),
360 device_get_unit(sc->sc_dev),
368 if (strncmp(profname, "wb195", 5) == 0) {
369 ret = ath_btcoex_cfg_wb195(sc);
370 } else if (strncmp(profname, "wb222", 5) == 0) {
371 ret = ath_btcoex_cfg_wb222(sc);
372 } else if (strncmp(profname, "wb225", 5) == 0) {
373 ret = ath_btcoex_cfg_wb225(sc);
374 } else if (strncmp(profname, "wb335", 5) == 0) {
375 ret = ath_btcoex_cfg_wb335b(sc);
381 * Propagate up failure from the actual attach phase.
390 * Detach btcoex from the given interface
393 ath_btcoex_detach(struct ath_softc *sc)
395 if (sc->sc_btcoex_mci) {
396 ath_btcoex_mci_detach(sc);
403 * Configure or disable bluetooth coexistence on the given channel.
405 * For AR9285/AR9287/AR9485, we'll never see a 5GHz channel, so we just
406 * assume bluetooth coexistence is always on.
408 * For AR9462, we may see a 5GHz channel; bluetooth coexistence should
409 * not be enabled on those channels.
412 ath_btcoex_enable(struct ath_softc *sc, const struct ieee80211_channel *chan)
414 if (sc->sc_btcoex_mci) {
415 ath_btcoex_mci_enable(sc, chan);
422 * Handle ioctl requests from the diagnostic interface.
424 * The initial part of this code resembles ath_ioctl_diag();
425 * it's likely a good idea to reduce duplication between
426 * these two routines.
429 ath_btcoex_ioctl(struct ath_softc *sc, struct ath_diag *ad)
431 unsigned int id = ad->ad_id & ATH_DIAG_ID;
433 void *outdata = NULL;
434 u_int32_t insize = ad->ad_in_size;
435 u_int32_t outsize = ad->ad_out_size;
439 if (ad->ad_id & ATH_DIAG_IN) {
443 indata = malloc(insize, M_TEMP, M_NOWAIT);
444 if (indata == NULL) {
448 error = copyin(ad->ad_in_data, indata, insize);
452 if (ad->ad_id & ATH_DIAG_DYN) {
454 * Allocate a buffer for the results (otherwise the HAL
455 * returns a pointer to a buffer where we can read the
456 * results). Note that we depend on the HAL leaving this
457 * pointer for us to use below in reclaiming the buffer;
458 * may want to be more defensive.
460 outdata = malloc(outsize, M_TEMP, M_NOWAIT);
461 if (outdata == NULL) {
470 if (outsize < ad->ad_out_size)
471 ad->ad_out_size = outsize;
472 if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
475 if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
476 free(indata, M_TEMP);
477 if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
478 free(outdata, M_TEMP);