#include <sys/cdefs.h>
#include "opt_ath.h"
#include "opt_inet.h"
#include "opt_wlan.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/errno.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/bus.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_media.h>
#include <net/if_arp.h>
#include <net/ethernet.h>
#include <net80211/ieee80211_var.h>
#include <net/bpf.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/if_ether.h>
#endif
#include <dev/ath/if_athvar.h>
#include <dev/ath/if_ath_btcoex.h>
#include <dev/ath/if_ath_btcoex_mci.h>
MALLOC_DECLARE(M_ATHDEV);
static int
ath_btcoex_cfg_wb195(struct ath_softc *sc)
{
HAL_BT_COEX_INFO btinfo;
HAL_BT_COEX_CONFIG btconfig;
struct ath_hal *ah = sc->sc_ah;
if (! ath_hal_btcoex_supported(ah))
return (EINVAL);
bzero(&btinfo, sizeof(btinfo));
bzero(&btconfig, sizeof(btconfig));
device_printf(sc->sc_dev, "Enabling WB195 BTCOEX\n");
btinfo.bt_module = HAL_BT_MODULE_JANUS;
btinfo.bt_coex_config = HAL_BT_COEX_CFG_3WIRE;
btinfo.bt_gpio_bt_active = 6;
btinfo.bt_gpio_bt_priority = 7;
btinfo.bt_gpio_wlan_active = 5;
btinfo.bt_active_polarity = 1;
btinfo.bt_single_ant = 1;
btinfo.bt_isolation = 0;
ath_hal_btcoex_set_info(ah, &btinfo);
btconfig.bt_time_extend = 0;
btconfig.bt_txstate_extend = 1;
btconfig.bt_txframe_extend = 1;
btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
btconfig.bt_quiet_collision = 1;
btconfig.bt_rxclear_polarity = 1;
btconfig.bt_priority_time = 2;
btconfig.bt_first_slot_time = 5;
btconfig.bt_hold_rxclear = 1;
ath_hal_btcoex_set_config(ah, &btconfig);
ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
return (0);
}
static int
ath_btcoex_cfg_wb225(struct ath_softc *sc)
{
HAL_BT_COEX_INFO btinfo;
HAL_BT_COEX_CONFIG btconfig;
struct ath_hal *ah = sc->sc_ah;
if (! ath_hal_btcoex_supported(ah))
return (EINVAL);
bzero(&btinfo, sizeof(btinfo));
bzero(&btconfig, sizeof(btconfig));
device_printf(sc->sc_dev, "Enabling WB225 BTCOEX\n");
btinfo.bt_module = HAL_BT_MODULE_JANUS;
btinfo.bt_coex_config = HAL_BT_COEX_CFG_3WIRE;
btinfo.bt_gpio_bt_active = 4;
btinfo.bt_gpio_bt_priority = 8;
btinfo.bt_gpio_wlan_active = 5;
btinfo.bt_active_polarity = 1;
btinfo.bt_single_ant = 1;
btinfo.bt_isolation = 0;
ath_hal_btcoex_set_info(ah, &btinfo);
btconfig.bt_time_extend = 0;
btconfig.bt_txstate_extend = 1;
btconfig.bt_txframe_extend = 1;
btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
btconfig.bt_quiet_collision = 1;
btconfig.bt_rxclear_polarity = 1;
btconfig.bt_priority_time = 2;
btconfig.bt_first_slot_time = 5;
btconfig.bt_hold_rxclear = 1;
ath_hal_btcoex_set_config(ah, &btconfig);
ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY, 1);
return (0);
}
static int
ath_btcoex_cfg_mci(struct ath_softc *sc, uint32_t mci_cfg, int do_btdiv)
{
HAL_BT_COEX_INFO btinfo;
HAL_BT_COEX_CONFIG btconfig;
struct ath_hal *ah = sc->sc_ah;
if (! ath_hal_btcoex_supported(ah))
return (EINVAL);
bzero(&btinfo, sizeof(btinfo));
bzero(&btconfig, sizeof(btconfig));
sc->sc_ah->ah_config.ath_hal_mci_config = mci_cfg;
if (ath_btcoex_mci_attach(sc) != 0) {
device_printf(sc->sc_dev, "Failed to setup btcoex\n");
return (EINVAL);
}
btinfo.bt_module = HAL_BT_MODULE_JANUS;
btinfo.bt_coex_config = HAL_BT_COEX_CFG_MCI;
btinfo.bt_gpio_bt_active = 4;
btinfo.bt_gpio_bt_priority = 8;
btinfo.bt_gpio_wlan_active = 5;
btinfo.bt_active_polarity = 1;
btinfo.bt_single_ant = 0;
btinfo.bt_isolation = 0;
ath_hal_btcoex_set_info(ah, &btinfo);
btconfig.bt_time_extend = 0;
btconfig.bt_txstate_extend = 1;
btconfig.bt_txframe_extend = 1;
btconfig.bt_mode = HAL_BT_COEX_MODE_SLOTTED;
btconfig.bt_quiet_collision = 1;
btconfig.bt_rxclear_polarity = 1;
btconfig.bt_priority_time = 2;
btconfig.bt_first_slot_time = 5;
btconfig.bt_hold_rxclear = 1;
ath_hal_btcoex_set_config(ah, &btconfig);
ath_hal_btcoex_enable(sc->sc_ah);
ath_hal_btcoex_set_weights(ah, HAL_BT_COEX_STOMP_NONE);
ath_hal_btcoex_set_parameter(ah, HAL_BT_COEX_ANTENNA_DIVERSITY,
do_btdiv);
return (0);
}
static int
ath_btcoex_cfg_wb222(struct ath_softc *sc)
{
device_printf(sc->sc_dev, "Enabling WB222 BTCOEX\n");
return (ath_btcoex_cfg_mci(sc, 0x2201, 1));
}
static int
ath_btcoex_cfg_wb335b(struct ath_softc *sc)
{
uint32_t flags;
int do_btdiv = 0;
flags = 0xa4c1;
if (sc->sc_pci_devinfo & ATH_PCI_AR9565_1ANT) {
flags &= ~ATH_MCI_CONFIG_ANT_ARCH;
flags |= ATH_MCI_ANT_ARCH_1_ANT_PA_LNA_SHARED <<
ATH_MCI_CONFIG_ANT_ARCH_S;
} else if (sc->sc_pci_devinfo & ATH_PCI_AR9565_2ANT) {
flags &= ~ATH_MCI_CONFIG_ANT_ARCH;
flags |= ATH_MCI_ANT_ARCH_2_ANT_PA_LNA_NON_SHARED <<
ATH_MCI_CONFIG_ANT_ARCH_S;
}
if (sc->sc_pci_devinfo & ATH_PCI_BT_ANT_DIV) {
do_btdiv = 1;
}
device_printf(sc->sc_dev, "Enabling WB335 BTCOEX\n");
return (ath_btcoex_cfg_mci(sc, flags, do_btdiv));
}
#if 0
static int
ath_btcoex_aspm_wb195(struct ath_softc *sc)
{
}
#endif
int
ath_btcoex_attach(struct ath_softc *sc)
{
int ret;
struct ath_hal *ah = sc->sc_ah;
const char *profname;
if (! ath_hal_btcoex_supported(ah))
return (0);
ret = resource_string_value(device_get_name(sc->sc_dev),
device_get_unit(sc->sc_dev),
"btcoex_profile",
&profname);
if (ret != 0) {
return (0);
}
if (strncmp(profname, "wb195", 5) == 0) {
ret = ath_btcoex_cfg_wb195(sc);
} else if (strncmp(profname, "wb222", 5) == 0) {
ret = ath_btcoex_cfg_wb222(sc);
} else if (strncmp(profname, "wb225", 5) == 0) {
ret = ath_btcoex_cfg_wb225(sc);
} else if (strncmp(profname, "wb335", 5) == 0) {
ret = ath_btcoex_cfg_wb335b(sc);
} else {
return (0);
}
if (ret != 0)
return (ret);
return (0);
}
int
ath_btcoex_detach(struct ath_softc *sc)
{
if (sc->sc_btcoex_mci) {
ath_btcoex_mci_detach(sc);
}
return (0);
}
int
ath_btcoex_enable(struct ath_softc *sc, const struct ieee80211_channel *chan)
{
if (sc->sc_btcoex_mci) {
ath_btcoex_mci_enable(sc, chan);
}
return (0);
}
int
ath_btcoex_ioctl(struct ath_softc *sc, struct ath_diag *ad)
{
unsigned int id = ad->ad_id & ATH_DIAG_ID;
void *indata = NULL;
void *outdata = NULL;
u_int32_t insize = ad->ad_in_size;
u_int32_t outsize = ad->ad_out_size;
int error = 0;
if (ad->ad_id & ATH_DIAG_IN) {
indata = malloc(insize, M_TEMP, M_NOWAIT);
if (indata == NULL) {
error = ENOMEM;
goto bad;
}
error = copyin(ad->ad_in_data, indata, insize);
if (error)
goto bad;
}
if (ad->ad_id & ATH_DIAG_DYN) {
outdata = malloc(outsize, M_TEMP, M_NOWAIT | M_ZERO);
if (outdata == NULL) {
error = ENOMEM;
goto bad;
}
}
switch (id) {
default:
error = EINVAL;
goto bad;
}
if (outsize < ad->ad_out_size)
ad->ad_out_size = outsize;
if (outdata && copyout(outdata, ad->ad_out_data, ad->ad_out_size))
error = EFAULT;
bad:
if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
free(indata, M_TEMP);
if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
free(outdata, M_TEMP);
return (error);
}