2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2013 Adrian Chadd <adrian@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 * redistribution must be conditioned upon including a substantially
16 * similar Disclaimer requirement for further binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * This implements some very basic bluetooth coexistence methods for
38 * the ath(4) hardware.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysctl.h>
47 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mutex.h>
51 #include <sys/errno.h>
52 #include <machine/bus.h>
53 #include <machine/resource.h>
57 #include <sys/socket.h>
60 #include <net/if_var.h>
61 #include <net/if_media.h>
62 #include <net/if_arp.h>
63 #include <net/ethernet.h> /* XXX for ether_sprintf */
65 #include <net80211/ieee80211_var.h>
70 #include <netinet/in.h>
71 #include <netinet/if_ether.h>
74 #include <dev/ath/if_athvar.h>
75 #include <dev/ath/if_ath_btcoex.h>
76 #include <dev/ath/if_ath_btcoex_mci.h>
78 MALLOC_DECLARE(M_ATHDEV);
81 * Initial AR9285 / (WB195) bluetooth coexistence settings,
82 * just for experimentation.
84 * Return 0 for OK; errno for error.
86 * XXX TODO: There needs to be a PCIe workaround to disable ASPM if
87 * bluetooth coexistence is enabled.
90 ath_btcoex_cfg_wb195(struct ath_softc *sc)
92 HAL_BT_COEX_INFO btinfo;
93 HAL_BT_COEX_CONFIG btconfig;
94 struct ath_hal *ah = sc->sc_ah;
96 if (! ath_hal_btcoex_supported(ah))
99 bzero(&btinfo, sizeof(btinfo));
100 bzero(&btconfig, sizeof(btconfig));
102 device_printf(sc->sc_dev, "Enabling WB195 BTCOEX\n");
104 btinfo.bt_module = HAL_BT_MODULE_JANUS;
105 btinfo.bt_coex_config = HAL_BT_COEX_CFG_3WIRE;
107 * These are the three GPIO pins hooked up between the AR9285 and
110 btinfo.bt_gpio_bt_active = 6;
111 btinfo.bt_gpio_bt_priority = 7;
112 btinfo.bt_gpio_wlan_active = 5;
113 btinfo.bt_active_polarity = 1; /* XXX not used */
114 btinfo.bt_single_ant = 1; /* 1 antenna on ar9285 ? */
115 btinfo.bt_isolation = 0; /* in dB, not used */
117 ath_hal_btcoex_set_info(ah, &btinfo);
119 btconfig.bt_time_extend = 0;
120 btconfig.bt_txstate_extend = 1; /* true */
121 btconfig.bt_txframe_extend = 1; /* true */
122 btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
123 btconfig.bt_quiet_collision = 1; /* true */
124 btconfig.bt_rxclear_polarity = 1; /* true */
125 btconfig.bt_priority_time = 2;
126 btconfig.bt_first_slot_time = 5;
127 btconfig.bt_hold_rxclear = 1; /* true */
129 ath_hal_btcoex_set_config(ah, &btconfig);
132 * Enable antenna diversity.
134 ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
140 * Initial AR9485 / (WB225) bluetooth coexistence settings,
141 * just for experimentation.
143 * Return 0 for OK; errno for error.
146 ath_btcoex_cfg_wb225(struct ath_softc *sc)
148 HAL_BT_COEX_INFO btinfo;
149 HAL_BT_COEX_CONFIG btconfig;
150 struct ath_hal *ah = sc->sc_ah;
152 if (! ath_hal_btcoex_supported(ah))
155 bzero(&btinfo, sizeof(btinfo));
156 bzero(&btconfig, sizeof(btconfig));
158 device_printf(sc->sc_dev, "Enabling WB225 BTCOEX\n");
160 btinfo.bt_module = HAL_BT_MODULE_JANUS; /* XXX not used? */
161 btinfo.bt_coex_config = HAL_BT_COEX_CFG_3WIRE;
163 * These are the three GPIO pins hooked up between the AR9485 and
164 * the bluetooth module.
166 btinfo.bt_gpio_bt_active = 4;
167 btinfo.bt_gpio_bt_priority = 8;
168 btinfo.bt_gpio_wlan_active = 5;
170 btinfo.bt_active_polarity = 1; /* XXX not used */
171 btinfo.bt_single_ant = 1; /* 1 antenna on ar9285 ? */
172 btinfo.bt_isolation = 0; /* in dB, not used */
174 ath_hal_btcoex_set_info(ah, &btinfo);
176 btconfig.bt_time_extend = 0;
177 btconfig.bt_txstate_extend = 1; /* true */
178 btconfig.bt_txframe_extend = 1; /* true */
179 btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
180 btconfig.bt_quiet_collision = 1; /* true */
181 btconfig.bt_rxclear_polarity = 1; /* true */
182 btconfig.bt_priority_time = 2;
183 btconfig.bt_first_slot_time = 5;
184 btconfig.bt_hold_rxclear = 1; /* true */
186 ath_hal_btcoex_set_config(ah, &btconfig);
189 * Enable antenna diversity.
191 ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
197 ath_btcoex_cfg_mci(struct ath_softc *sc, uint32_t mci_cfg, int do_btdiv)
199 HAL_BT_COEX_INFO btinfo;
200 HAL_BT_COEX_CONFIG btconfig;
201 struct ath_hal *ah = sc->sc_ah;
203 if (! ath_hal_btcoex_supported(ah))
206 bzero(&btinfo, sizeof(btinfo));
207 bzero(&btconfig, sizeof(btconfig));
209 sc->sc_ah->ah_config.ath_hal_mci_config = mci_cfg;
211 if (ath_btcoex_mci_attach(sc) != 0) {
212 device_printf(sc->sc_dev, "Failed to setup btcoex\n");
216 btinfo.bt_module = HAL_BT_MODULE_JANUS; /* XXX not used? */
217 btinfo.bt_coex_config = HAL_BT_COEX_CFG_MCI;
220 * MCI uses a completely different interface to speak
221 * to the bluetooth module - it's a command based
222 * thing over a serial line, rather than
223 * state pins to/from the bluetooth module.
225 * So, the GPIO configuration, polarity, etc
226 * doesn't matter on MCI devices; it's just
227 * completely ignored by the HAL.
229 btinfo.bt_gpio_bt_active = 4;
230 btinfo.bt_gpio_bt_priority = 8;
231 btinfo.bt_gpio_wlan_active = 5;
233 btinfo.bt_active_polarity = 1; /* XXX not used */
234 btinfo.bt_single_ant = 0; /* 2 antenna on WB335 */
235 btinfo.bt_isolation = 0; /* in dB, not used */
237 ath_hal_btcoex_set_info(ah, &btinfo);
239 btconfig.bt_time_extend = 0;
240 btconfig.bt_txstate_extend = 1; /* true */
241 btconfig.bt_txframe_extend = 1; /* true */
242 btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
243 btconfig.bt_quiet_collision = 1; /* true */
244 btconfig.bt_rxclear_polarity = 1; /* true */
245 btconfig.bt_priority_time = 2;
246 btconfig.bt_first_slot_time = 5;
247 btconfig.bt_hold_rxclear = 1; /* true */
249 ath_hal_btcoex_set_config(ah, &btconfig);
252 ath_hal_btcoex_enable(sc->sc_ah);
255 ath_hal_btcoex_set_weights(ah, HAL_BT_COEX_STOMP_NONE);
258 * Enable antenna diversity.
260 ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY,
267 * Initial AR9462 / (WB222) bluetooth coexistence settings.
269 * Return 0 for OK; errno for error.
272 ath_btcoex_cfg_wb222(struct ath_softc *sc)
275 device_printf(sc->sc_dev, "Enabling WB222 BTCOEX\n");
277 return (ath_btcoex_cfg_mci(sc, 0x2201, 1));
281 * Initial QCA9565 / (WB335B) bluetooth coexistence settings.
283 * Return 0 for OK; errno for error.
286 ath_btcoex_cfg_wb335b(struct ath_softc *sc)
294 /* 1-ant and 2-ant AR9565 */
296 * XXX TODO: ensure these actually make it down to the
299 if (sc->sc_pci_devinfo & ATH_PCI_AR9565_1ANT) {
300 flags &= ~ATH_MCI_CONFIG_ANT_ARCH;
301 flags |= ATH_MCI_ANT_ARCH_1_ANT_PA_LNA_SHARED <<
302 ATH_MCI_CONFIG_ANT_ARCH_S;
303 } else if (sc->sc_pci_devinfo & ATH_PCI_AR9565_2ANT) {
304 flags &= ~ATH_MCI_CONFIG_ANT_ARCH;
305 flags |= ATH_MCI_ANT_ARCH_2_ANT_PA_LNA_NON_SHARED <<
306 ATH_MCI_CONFIG_ANT_ARCH_S;
309 if (sc->sc_pci_devinfo & ATH_PCI_BT_ANT_DIV) {
313 device_printf(sc->sc_dev, "Enabling WB335 BTCOEX\n");
315 return (ath_btcoex_cfg_mci(sc, flags, do_btdiv));
320 * When using bluetooth coexistence, ASPM needs to be disabled
321 * otherwise the sleeping interferes with the bluetooth (USB)
322 * operation and the MAC sleep/wakeup hardware.
324 * The PCIe powersave routine also needs to not be called
325 * by the driver during suspend/resume, else things will get
326 * a little odd. Check Linux ath9k for more details.
329 ath_btcoex_aspm_wb195(struct ath_softc *sc)
332 /* XXX TODO: clear device ASPM L0S and L1 */
333 /* XXX TODO: clear _parent_ ASPM L0S and L1 */
338 * Methods which are required
342 * Attach btcoex to the given interface
345 ath_btcoex_attach(struct ath_softc *sc)
348 struct ath_hal *ah = sc->sc_ah;
349 const char *profname;
352 * No chipset bluetooth coexistence? Then do nothing.
354 if (! ath_hal_btcoex_supported(ah))
358 * Look at the hints to determine which bluetooth
359 * profile to configure.
361 ret = resource_string_value(device_get_name(sc->sc_dev),
362 device_get_unit(sc->sc_dev),
370 if (strncmp(profname, "wb195", 5) == 0) {
371 ret = ath_btcoex_cfg_wb195(sc);
372 } else if (strncmp(profname, "wb222", 5) == 0) {
373 ret = ath_btcoex_cfg_wb222(sc);
374 } else if (strncmp(profname, "wb225", 5) == 0) {
375 ret = ath_btcoex_cfg_wb225(sc);
376 } else if (strncmp(profname, "wb335", 5) == 0) {
377 ret = ath_btcoex_cfg_wb335b(sc);
383 * Propagate up failure from the actual attach phase.
392 * Detach btcoex from the given interface
395 ath_btcoex_detach(struct ath_softc *sc)
397 if (sc->sc_btcoex_mci) {
398 ath_btcoex_mci_detach(sc);
405 * Configure or disable bluetooth coexistence on the given channel.
407 * For AR9285/AR9287/AR9485, we'll never see a 5GHz channel, so we just
408 * assume bluetooth coexistence is always on.
410 * For AR9462, we may see a 5GHz channel; bluetooth coexistence should
411 * not be enabled on those channels.
414 ath_btcoex_enable(struct ath_softc *sc, const struct ieee80211_channel *chan)
416 if (sc->sc_btcoex_mci) {
417 ath_btcoex_mci_enable(sc, chan);
424 * Handle ioctl requests from the diagnostic interface.
426 * The initial part of this code resembles ath_ioctl_diag();
427 * it's likely a good idea to reduce duplication between
428 * these two routines.
431 ath_btcoex_ioctl(struct ath_softc *sc, struct ath_diag *ad)
433 unsigned int id = ad->ad_id & ATH_DIAG_ID;
435 void *outdata = NULL;
436 u_int32_t insize = ad->ad_in_size;
437 u_int32_t outsize = ad->ad_out_size;
441 if (ad->ad_id & ATH_DIAG_IN) {
445 indata = malloc(insize, M_TEMP, M_NOWAIT);
446 if (indata == NULL) {
450 error = copyin(ad->ad_in_data, indata, insize);
454 if (ad->ad_id & ATH_DIAG_DYN) {
456 * Allocate a buffer for the results (otherwise the HAL
457 * returns a pointer to a buffer where we can read the
458 * results). Note that we depend on the HAL leaving this
459 * pointer for us to use below in reclaiming the buffer;
460 * may want to be more defensive.
462 outdata = malloc(outsize, M_TEMP, M_NOWAIT | M_ZERO);
463 if (outdata == NULL) {
473 if (outsize < ad->ad_out_size)
474 ad->ad_out_size = outsize;
475 if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
478 if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
479 free(indata, M_TEMP);
480 if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
481 free(outdata, M_TEMP);