diff --git a/hal/HalPwrSeqCmd.c b/hal/HalPwrSeqCmd.c new file mode 100644 index 0000000..389785c --- /dev/null +++ b/hal/HalPwrSeqCmd.c @@ -0,0 +1,185 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +/*++ +Copyright (c) Realtek Semiconductor Corp. All rights reserved. + +Module Name: + HalPwrSeqCmd.c + +Abstract: + Implement HW Power sequence configuration CMD handling routine for Realtek devices. + +Major Change History: + When Who What + ---------- --------------- ------------------------------- + 2011-10-26 Lucas Modify to be compatible with SD4-CE driver. + 2011-07-07 Roger Create. + +--*/ +#include + + +/* + * Description: + * This routine deal with the Power Configuration CMDs parsing for RTL8723/RTL8188E Series IC. + * + * Assumption: + * We should follow specific format which was released from HW SD. + * + * 2011.07.07, added by Roger. + * */ +u8 HalPwrSeqCmdParsing( + PADAPTER padapter, + u8 CutVersion, + u8 FabVersion, + u8 InterfaceType, + WLAN_PWR_CFG PwrSeqCmd[]) +{ + WLAN_PWR_CFG PwrCfgCmd = {0}; + u8 bPollingBit = _FALSE; + u8 bHWICSupport = _FALSE; + u32 AryIdx = 0; + u8 value = 0; + u32 offset = 0; + u8 flag = 0; + u32 pollingCount = 0; /* polling autoload done. */ + u32 maxPollingCnt = 5000; + + do { + PwrCfgCmd = PwrSeqCmd[AryIdx]; + + + /* 2 Only Handle the command whose FAB, CUT, and Interface are matched */ + if ((GET_PWR_CFG_FAB_MASK(PwrCfgCmd) & FabVersion) && + (GET_PWR_CFG_CUT_MASK(PwrCfgCmd) & CutVersion) && + (GET_PWR_CFG_INTF_MASK(PwrCfgCmd) & InterfaceType)) { + switch (GET_PWR_CFG_CMD(PwrCfgCmd)) { + case PWR_CMD_READ: + break; + + case PWR_CMD_WRITE: + offset = GET_PWR_CFG_OFFSET(PwrCfgCmd); + +#ifdef CONFIG_SDIO_HCI + /* */ + /* We should deal with interface specific address mapping for some interfaces, e.g., SDIO interface */ + /* 2011.07.07. */ + /* */ + if (GET_PWR_CFG_BASE(PwrCfgCmd) == PWR_BASEADDR_SDIO) { + /* Read Back SDIO Local value */ + value = SdioLocalCmd52Read1Byte(padapter, offset); + + value &= ~(GET_PWR_CFG_MASK(PwrCfgCmd)); + value |= (GET_PWR_CFG_VALUE(PwrCfgCmd) & GET_PWR_CFG_MASK(PwrCfgCmd)); + + /* Write Back SDIO Local value */ + SdioLocalCmd52Write1Byte(padapter, offset, value); + } else +#endif + { +#ifdef CONFIG_GSPI_HCI + if (GET_PWR_CFG_BASE(PwrCfgCmd) == PWR_BASEADDR_SDIO) + offset = SPI_LOCAL_OFFSET | offset; +#endif + /* Read the value from system register */ + value = rtw_read8(padapter, offset); + + value = value & (~(GET_PWR_CFG_MASK(PwrCfgCmd))); + value = value | (GET_PWR_CFG_VALUE(PwrCfgCmd) & GET_PWR_CFG_MASK(PwrCfgCmd)); + + /* Write the value back to sytem register */ + rtw_write8(padapter, offset, value); + } + break; + + case PWR_CMD_POLLING: + + bPollingBit = _FALSE; + offset = GET_PWR_CFG_OFFSET(PwrCfgCmd); + + rtw_hal_get_hwreg(padapter, HW_VAR_PWR_CMD, &bHWICSupport); + if (bHWICSupport && offset == 0x06) { + flag = 0; + maxPollingCnt = 100000; + } else + maxPollingCnt = 5000; + +#ifdef CONFIG_GSPI_HCI + if (GET_PWR_CFG_BASE(PwrCfgCmd) == PWR_BASEADDR_SDIO) + offset = SPI_LOCAL_OFFSET | offset; +#endif + do { +#ifdef CONFIG_SDIO_HCI + if (GET_PWR_CFG_BASE(PwrCfgCmd) == PWR_BASEADDR_SDIO) + value = SdioLocalCmd52Read1Byte(padapter, offset); + else +#endif + value = rtw_read8(padapter, offset); + + value = value & GET_PWR_CFG_MASK(PwrCfgCmd); + if (value == (GET_PWR_CFG_VALUE(PwrCfgCmd) & GET_PWR_CFG_MASK(PwrCfgCmd))) + bPollingBit = _TRUE; + else + rtw_udelay_os(10); + + if (pollingCount++ > maxPollingCnt) { + RTW_ERR("HalPwrSeqCmdParsing: Fail to polling Offset[%#x]=%02x\n", offset, value); + + /* For PCIE + USB package poll power bit timeout issue only modify 8821AE and 8723BE */ + if (bHWICSupport && offset == 0x06 && flag == 0) { + + RTW_ERR("[WARNING] PCIE polling(0x%X) timeout(%d), Toggle 0x04[3] and try again.\n", offset, maxPollingCnt); + if (IS_HARDWARE_TYPE_8723DE(padapter)) + PlatformEFIOWrite1Byte(padapter, 0x40, (PlatformEFIORead1Byte(padapter, 0x40)) & (~BIT3)); + + PlatformEFIOWrite1Byte(padapter, 0x04, PlatformEFIORead1Byte(padapter, 0x04) | BIT3); + PlatformEFIOWrite1Byte(padapter, 0x04, PlatformEFIORead1Byte(padapter, 0x04) & ~BIT3); + + if (IS_HARDWARE_TYPE_8723DE(padapter)) + PlatformEFIOWrite1Byte(padapter, 0x40, PlatformEFIORead1Byte(padapter, 0x40)|BIT3); + + /* Retry Polling Process one more time */ + pollingCount = 0; + flag = 1; + } else { + return _FALSE; + } + } + } while (!bPollingBit); + + break; + + case PWR_CMD_DELAY: + if (GET_PWR_CFG_VALUE(PwrCfgCmd) == PWRSEQ_DELAY_US) + rtw_udelay_os(GET_PWR_CFG_OFFSET(PwrCfgCmd)); + else + rtw_udelay_os(GET_PWR_CFG_OFFSET(PwrCfgCmd) * 1000); + break; + + case PWR_CMD_END: + /* When this command is parsed, end the process */ + return _TRUE; + break; + + default: + break; + } + } + + AryIdx++;/* Add Array Index */ + } while (1); + + return _TRUE; +} diff --git a/hal/hal_btcoex.c b/hal/hal_btcoex.c new file mode 100644 index 0000000..32ef8ec --- /dev/null +++ b/hal/hal_btcoex.c @@ -0,0 +1,6651 @@ +/****************************************************************************** + * + * Copyright(c) 2013 - 2019 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#define __HAL_BTCOEX_C__ + +#ifdef CONFIG_BT_COEXIST + +#include +#include +#include "btc/mp_precomp.h" + +/* ************************************ + * Global variables + * ************************************ */ +const char *const BtProfileString[] = { + "NONE", + "A2DP", + "PAN", + "HID", + "SCO", +}; + +const char *const BtSpecString[] = { + "1.0b", + "1.1", + "1.2", + "2.0+EDR", + "2.1+EDR", + "3.0+HS", + "4.0", +}; + +const char *const BtLinkRoleString[] = { + "Master", + "Slave", +}; + +const char *const h2cStaString[] = { + "successful", + "h2c busy", + "rf off", + "fw not read", +}; + +const char *const ioStaString[] = { + "success", + "can not IO", + "rf off", + "fw not read", + "wait io timeout", + "invalid len", + "idle Q empty", + "insert waitQ fail", + "unknown fail", + "wrong level", + "h2c stopped", +}; + +const char *const GLBtcWifiBwString[] = { + "11bg", + "HT20", + "HT40", + "VHT80", + "VHT160" +}; + +const char *const GLBtcWifiFreqString[] = { + "2.4G", + "5G", + "2.4G+5G" +}; + +const char *const GLBtcIotPeerString[] = { + "UNKNOWN", + "REALTEK", + "REALTEK_92SE", + "BROADCOM", + "RALINK", + "ATHEROS", + "CISCO", + "MERU", + "MARVELL", + "REALTEK_SOFTAP", /* peer is RealTek SOFT_AP, by Bohn, 2009.12.17 */ + "SELF_SOFTAP", /* Self is SoftAP */ + "AIRGO", + "INTEL", + "RTK_APCLIENT", + "REALTEK_81XX", + "REALTEK_WOW", + "REALTEK_JAGUAR_BCUTAP", + "REALTEK_JAGUAR_CCUTAP" +}; + +const char *const coexOpcodeString[] = { + "Wifi status notify", + "Wifi progress", + "Wifi info", + "Power state", + "Set Control", + "Get Control" +}; + +const char *const coexIndTypeString[] = { + "bt info", + "pstdma", + "limited tx/rx", + "coex table", + "request" +}; + +const char *const coexH2cResultString[] = { + "ok", + "unknown", + "un opcode", + "opVer MM", + "par Err", + "par OoR", + "reqNum MM", + "halMac Fail", + "h2c TimeOut", + "Invalid c2h Len", + "data overflow" +}; + +#define HALBTCOUTSRC_AGG_CHK_WINDOW_IN_MS 8000 + +struct btc_coexist GLBtCoexist; +BTC_OFFLOAD gl_coex_offload; +u8 GLBtcWiFiInScanState; +u8 GLBtcWiFiInIQKState; +u8 GLBtcWiFiInIPS; +u8 GLBtcWiFiInLPS; +u8 GLBtcBtCoexAliveRegistered; + +/* + * BT control H2C/C2H + */ +/* EXT_EID */ +typedef enum _bt_ext_eid { + C2H_WIFI_FW_ACTIVE_RSP = 0, + C2H_TRIG_BY_BT_FW +} BT_EXT_EID; + +/* C2H_STATUS */ +typedef enum _bt_c2h_status { + BT_STATUS_OK = 0, + BT_STATUS_VERSION_MISMATCH, + BT_STATUS_UNKNOWN_OPCODE, + BT_STATUS_ERROR_PARAMETER +} BT_C2H_STATUS; + +/* C2H BT OP CODES */ +typedef enum _bt_op_code { + BT_OP_GET_BT_VERSION = 0x00, + BT_OP_WRITE_REG_ADDR = 0x0c, + BT_OP_WRITE_REG_VALUE = 0x0d, + + BT_OP_READ_REG = 0x11, + + BT_LO_OP_GET_AFH_MAP_L = 0x1e, + BT_LO_OP_GET_AFH_MAP_M = 0x1f, + BT_LO_OP_GET_AFH_MAP_H = 0x20, + + BT_OP_SET_BT_TRX_MASK = 0x29, + BT_OP_GET_BT_COEX_SUPPORTED_FEATURE = 0x2a, + BT_OP_GET_BT_COEX_SUPPORTED_VERSION = 0x2b, + BT_OP_GET_BT_ANT_DET_VAL = 0x2c, + BT_OP_GET_BT_BLE_SCAN_TYPE = 0x2d, + BT_OP_GET_BT_BLE_SCAN_PARA = 0x2e, + BT_OP_GET_BT_DEVICE_INFO = 0x30, + BT_OP_GET_BT_FORBIDDEN_SLOT_VAL = 0x31, + BT_OP_SET_BT_LANCONSTRAIN_LEVEL = 0x32, + BT_OP_SET_BT_TEST_MODE_VAL = 0x33, + BT_OP_MAX +} BT_OP_CODE; + +#define BTC_MPOPER_TIMEOUT 50 /* unit: ms */ + +#define C2H_MAX_SIZE 16 +u8 GLBtcBtMpOperSeq; +_mutex GLBtcBtMpOperLock; +_timer GLBtcBtMpOperTimer; +_sema GLBtcBtMpRptSema; +u8 GLBtcBtMpRptSeq; +u8 GLBtcBtMpRptStatus; +u8 GLBtcBtMpRptRsp[C2H_MAX_SIZE]; +u8 GLBtcBtMpRptRspSize; +u8 GLBtcBtMpRptWait; +u8 GLBtcBtMpRptWiFiOK; +u8 GLBtcBtMpRptBTOK; + +/* + * Debug + */ +u32 GLBtcDbgType[COMP_MAX]; +u8 GLBtcDbgBuf[BT_TMP_BUF_SIZE]; +u8 gl_btc_trace_buf[BT_TMP_BUF_SIZE]; + +typedef struct _btcoexdbginfo { + u8 *info; + u32 size; /* buffer total size */ + u32 len; /* now used length */ +} BTCDBGINFO, *PBTCDBGINFO; + +BTCDBGINFO GLBtcDbgInfo; + +#define BT_Operation(Adapter) _FALSE + +static void DBG_BT_INFO_INIT(PBTCDBGINFO pinfo, u8 *pbuf, u32 size) +{ + if (NULL == pinfo) + return; + + _rtw_memset(pinfo, 0, sizeof(BTCDBGINFO)); + + if (pbuf && size) { + pinfo->info = pbuf; + pinfo->size = size; + } +} + +void DBG_BT_INFO(u8 *dbgmsg) +{ + PBTCDBGINFO pinfo; + u32 msglen, buflen; + u8 *pbuf; + + + pinfo = &GLBtcDbgInfo; + + if (NULL == pinfo->info) + return; + + msglen = strlen(dbgmsg); + if (pinfo->len + msglen > pinfo->size) + return; + + pbuf = pinfo->info + pinfo->len; + _rtw_memcpy(pbuf, dbgmsg, msglen); + pinfo->len += msglen; +} + +/* ************************************ + * Debug related function + * ************************************ */ +static u8 halbtcoutsrc_IsBtCoexistAvailable(PBTC_COEXIST pBtCoexist) +{ + if (!pBtCoexist->bBinded || + NULL == pBtCoexist->Adapter) + return _FALSE; + return _TRUE; +} + +static void halbtcoutsrc_DbgInit(void) +{ + u8 i; + + for (i = 0; i < COMP_MAX; i++) + GLBtcDbgType[i] = 0; +} + +static void halbtcoutsrc_EnterPwrLock(PBTC_COEXIST pBtCoexist) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj((PADAPTER)pBtCoexist->Adapter); + struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj); + + _enter_pwrlock(&pwrpriv->lock); +} + +static void halbtcoutsrc_ExitPwrLock(PBTC_COEXIST pBtCoexist) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj((PADAPTER)pBtCoexist->Adapter); + struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj); + + _exit_pwrlock(&pwrpriv->lock); +} + +static u8 halbtcoutsrc_IsHwMailboxExist(PBTC_COEXIST pBtCoexist) +{ + if (pBtCoexist->board_info.bt_chip_type == BTC_CHIP_CSR_BC4 + || pBtCoexist->board_info.bt_chip_type == BTC_CHIP_CSR_BC8 + ) + return _FALSE; + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) + return _FALSE; + else + return _TRUE; +} + +static u8 halbtcoutsrc_LeaveLps(PBTC_COEXIST pBtCoexist) +{ + PADAPTER padapter; + + + padapter = pBtCoexist->Adapter; + + pBtCoexist->bt_info.bt_ctrl_lps = _TRUE; + pBtCoexist->bt_info.bt_lps_on = _FALSE; + + return rtw_btcoex_LPS_Leave(padapter); +} + +void halbtcoutsrc_EnterLps(PBTC_COEXIST pBtCoexist) +{ + PADAPTER padapter; + + + padapter = pBtCoexist->Adapter; + + if (pBtCoexist->bdontenterLPS == _FALSE) { + pBtCoexist->bt_info.bt_ctrl_lps = _TRUE; + pBtCoexist->bt_info.bt_lps_on = _TRUE; + + rtw_btcoex_LPS_Enter(padapter); + } +} + +void halbtcoutsrc_NormalLps(PBTC_COEXIST pBtCoexist) +{ + PADAPTER padapter; + + + + padapter = pBtCoexist->Adapter; + + if (pBtCoexist->bt_info.bt_ctrl_lps) { + pBtCoexist->bt_info.bt_lps_on = _FALSE; + rtw_btcoex_LPS_Leave(padapter); + pBtCoexist->bt_info.bt_ctrl_lps = _FALSE; + + /* recover the LPS state to the original */ +#if 0 + padapter->hal_func.UpdateLPSStatusHandler( + padapter, + pPSC->RegLeisurePsMode, + pPSC->RegPowerSaveMode); +#endif + } +} + +void halbtcoutsrc_Pre_NormalLps(PBTC_COEXIST pBtCoexist) +{ + PADAPTER padapter; + + padapter = pBtCoexist->Adapter; + + if (pBtCoexist->bt_info.bt_ctrl_lps) { + pBtCoexist->bt_info.bt_lps_on = _FALSE; + rtw_btcoex_LPS_Leave(padapter); + } +} + +void halbtcoutsrc_Post_NormalLps(PBTC_COEXIST pBtCoexist) +{ + if (pBtCoexist->bt_info.bt_ctrl_lps) + pBtCoexist->bt_info.bt_ctrl_lps = _FALSE; +} + +/* + * Constraint: + * 1. this function will request pwrctrl->lock + */ +void halbtcoutsrc_LeaveLowPower(PBTC_COEXIST pBtCoexist) +{ +#ifdef CONFIG_LPS_LCLK + PADAPTER padapter; + PHAL_DATA_TYPE pHalData; + struct pwrctrl_priv *pwrctrl; + s32 ready; + systime stime; + s32 utime; + u32 timeout; /* unit: ms */ + + + padapter = pBtCoexist->Adapter; + pHalData = GET_HAL_DATA(padapter); + pwrctrl = adapter_to_pwrctl(padapter); + ready = _FAIL; +#ifdef LPS_RPWM_WAIT_MS + timeout = LPS_RPWM_WAIT_MS; +#else /* !LPS_RPWM_WAIT_MS */ + timeout = 30; +#endif /* !LPS_RPWM_WAIT_MS */ + + if (GLBtcBtCoexAliveRegistered == _TRUE) + return; + + stime = rtw_get_current_time(); + do { + ready = rtw_register_task_alive(padapter, BTCOEX_ALIVE); + if (_SUCCESS == ready) + break; + + utime = rtw_get_passing_time_ms(stime); + if (utime > timeout) + break; + + rtw_msleep_os(1); + } while (1); + + GLBtcBtCoexAliveRegistered = _TRUE; +#endif /* CONFIG_LPS_LCLK */ +} + +/* + * Constraint: + * 1. this function will request pwrctrl->lock + */ +void halbtcoutsrc_NormalLowPower(PBTC_COEXIST pBtCoexist) +{ +#ifdef CONFIG_LPS_LCLK + PADAPTER padapter; + + if (GLBtcBtCoexAliveRegistered == _FALSE) + return; + + padapter = pBtCoexist->Adapter; + rtw_unregister_task_alive(padapter, BTCOEX_ALIVE); + + GLBtcBtCoexAliveRegistered = _FALSE; +#endif /* CONFIG_LPS_LCLK */ +} + +void halbtcoutsrc_DisableLowPower(PBTC_COEXIST pBtCoexist, u8 bLowPwrDisable) +{ + pBtCoexist->bt_info.bt_disable_low_pwr = bLowPwrDisable; + if (bLowPwrDisable) + halbtcoutsrc_LeaveLowPower(pBtCoexist); /* leave 32k low power. */ + else + halbtcoutsrc_NormalLowPower(pBtCoexist); /* original 32k low power behavior. */ +} + +void halbtcoutsrc_AggregationCheck(PBTC_COEXIST pBtCoexist) +{ + PADAPTER padapter; + BOOLEAN bNeedToAct = _FALSE; + static u32 preTime = 0; + u32 curTime = 0; + + padapter = pBtCoexist->Adapter; + + /* ===================================== */ + /* To void continuous deleteBA=>addBA=>deleteBA=>addBA */ + /* This function is not allowed to continuous called. */ + /* It can only be called after 8 seconds. */ + /* ===================================== */ + + curTime = rtw_systime_to_ms(rtw_get_current_time()); + if ((curTime - preTime) < HALBTCOUTSRC_AGG_CHK_WINDOW_IN_MS) /* over 8 seconds you can execute this function again. */ + return; + else + preTime = curTime; + + if (pBtCoexist->bt_info.reject_agg_pkt) { + bNeedToAct = _TRUE; + pBtCoexist->bt_info.pre_reject_agg_pkt = pBtCoexist->bt_info.reject_agg_pkt; + } else { + if (pBtCoexist->bt_info.pre_reject_agg_pkt) { + bNeedToAct = _TRUE; + pBtCoexist->bt_info.pre_reject_agg_pkt = pBtCoexist->bt_info.reject_agg_pkt; + } + + if (pBtCoexist->bt_info.pre_bt_ctrl_agg_buf_size != + pBtCoexist->bt_info.bt_ctrl_agg_buf_size) { + bNeedToAct = _TRUE; + pBtCoexist->bt_info.pre_bt_ctrl_agg_buf_size = pBtCoexist->bt_info.bt_ctrl_agg_buf_size; + } + + if (pBtCoexist->bt_info.bt_ctrl_agg_buf_size) { + if (pBtCoexist->bt_info.pre_agg_buf_size != + pBtCoexist->bt_info.agg_buf_size) + bNeedToAct = _TRUE; + pBtCoexist->bt_info.pre_agg_buf_size = pBtCoexist->bt_info.agg_buf_size; + } + } + + if (bNeedToAct) + rtw_btcoex_rx_ampdu_apply(padapter); +} + +u8 halbtcoutsrc_is_autoload_fail(PBTC_COEXIST pBtCoexist) +{ + PADAPTER padapter; + PHAL_DATA_TYPE pHalData; + + padapter = pBtCoexist->Adapter; + pHalData = GET_HAL_DATA(padapter); + + return pHalData->bautoload_fail_flag; +} + +u8 halbtcoutsrc_is_fw_ready(PBTC_COEXIST pBtCoexist) +{ + PADAPTER padapter; + + padapter = pBtCoexist->Adapter; + + return GET_HAL_DATA(padapter)->bFWReady; +} + +u8 halbtcoutsrc_IsDualBandConnected(PADAPTER padapter) +{ + u8 ret = BTC_MULTIPORT_SCC; + +#ifdef CONFIG_MCC_MODE + if (MCC_EN(padapter) && (rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC))) { + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *mccobjpriv = &(dvobj->mcc_objpriv); + u8 band0 = mccobjpriv->iface[0]->mlmeextpriv.cur_channel > 14 ? BAND_ON_5G : BAND_ON_2_4G; + u8 band1 = mccobjpriv->iface[1]->mlmeextpriv.cur_channel > 14 ? BAND_ON_5G : BAND_ON_2_4G; + + if (band0 != band1) + ret = BTC_MULTIPORT_MCC_DUAL_BAND; + else + ret = BTC_MULTIPORT_MCC_DUAL_CHANNEL; + } +#endif + + return ret; +} + +u8 halbtcoutsrc_IsWifiBusy(PADAPTER padapter) +{ + if (rtw_mi_check_status(padapter, MI_AP_ASSOC)) + return _TRUE; + if (rtw_mi_busy_traffic_check(padapter)) + return _TRUE; + + return _FALSE; +} + +static u32 _halbtcoutsrc_GetWifiLinkStatus(PADAPTER padapter) +{ + struct mlme_priv *pmlmepriv; + u8 bp2p; + u32 portConnectedStatus; + + + pmlmepriv = &padapter->mlmepriv; + bp2p = _FALSE; + portConnectedStatus = 0; + +#ifdef CONFIG_P2P + if (!rtw_p2p_chk_state(&padapter->wdinfo, P2P_STATE_NONE)) + bp2p = _TRUE; +#endif /* CONFIG_P2P */ + + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE) { + if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == _TRUE) { + if (_TRUE == bp2p) + portConnectedStatus |= WIFI_P2P_GO_CONNECTED; + else + portConnectedStatus |= WIFI_AP_CONNECTED; + } else { + if (_TRUE == bp2p) + portConnectedStatus |= WIFI_P2P_GC_CONNECTED; + else + portConnectedStatus |= WIFI_STA_CONNECTED; + } + } + + return portConnectedStatus; +} + +u32 halbtcoutsrc_GetWifiLinkStatus(PBTC_COEXIST pBtCoexist) +{ + /* ================================= */ + /* return value: */ + /* [31:16]=> connected port number */ + /* [15:0]=> port connected bit define */ + /* ================================ */ + + PADAPTER padapter; + u32 retVal; + u32 portConnectedStatus, numOfConnectedPort; + struct dvobj_priv *dvobj; + _adapter *iface; + int i; + + padapter = pBtCoexist->Adapter; + retVal = 0; + portConnectedStatus = 0; + numOfConnectedPort = 0; + dvobj = adapter_to_dvobj(padapter); + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if ((iface) && rtw_is_adapter_up(iface)) { + retVal = _halbtcoutsrc_GetWifiLinkStatus(iface); + if (retVal) { + portConnectedStatus |= retVal; + numOfConnectedPort++; + } + } + } + retVal = (numOfConnectedPort << 16) | portConnectedStatus; + + return retVal; +} + +struct btc_wifi_link_info halbtcoutsrc_getwifilinkinfo(PBTC_COEXIST pBtCoexist) +{ + u8 n_assoc_iface = 0, i =0, mcc_en = _FALSE; + PADAPTER adapter = NULL; + PADAPTER iface = NULL; + PADAPTER sta_iface = NULL, p2p_iface = NULL, ap_iface = NULL; + BTC_LINK_MODE btc_link_moe = BTC_LINK_MAX; + struct dvobj_priv *dvobj = NULL; + struct mlme_ext_priv *mlmeext = NULL; + struct btc_wifi_link_info wifi_link_info; + + adapter = (PADAPTER)pBtCoexist->Adapter; + dvobj = adapter_to_dvobj(adapter); + n_assoc_iface = rtw_mi_get_assoc_if_num(adapter); + + /* init value */ + wifi_link_info.link_mode = BTC_LINK_NONE; + wifi_link_info.sta_center_channel = 0; + wifi_link_info.p2p_center_channel = 0; + wifi_link_info.bany_client_join_go = _FALSE; + wifi_link_info.benable_noa = _FALSE; + wifi_link_info.bhotspot = _FALSE; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (!iface) + continue; + + mlmeext = &iface->mlmeextpriv; + if (MLME_IS_GO(iface)) { + wifi_link_info.link_mode = BTC_LINK_ONLY_GO; + wifi_link_info.p2p_center_channel = + rtw_get_center_ch(mlmeext->cur_channel, mlmeext->cur_bwmode, mlmeext->cur_ch_offset); + p2p_iface = iface; + if (rtw_linked_check(iface)) + wifi_link_info.bany_client_join_go = _TRUE; + } else if (MLME_IS_GC(iface)) { + wifi_link_info.link_mode = BTC_LINK_ONLY_GC; + wifi_link_info.p2p_center_channel = + rtw_get_center_ch(mlmeext->cur_channel, mlmeext->cur_bwmode, mlmeext->cur_ch_offset); + p2p_iface = iface; + } else if (MLME_IS_AP(iface)) { + wifi_link_info.link_mode = BTC_LINK_ONLY_AP; + ap_iface = iface; + wifi_link_info.p2p_center_channel = + rtw_get_center_ch(mlmeext->cur_channel, mlmeext->cur_bwmode, mlmeext->cur_ch_offset); + } else if (MLME_IS_STA(iface) && rtw_linked_check(iface)) { + wifi_link_info.link_mode = BTC_LINK_ONLY_STA; + wifi_link_info.sta_center_channel = + rtw_get_center_ch(mlmeext->cur_channel, mlmeext->cur_bwmode, mlmeext->cur_ch_offset); + sta_iface = iface; + } + } + +#ifdef CONFIG_MCC_MODE + if (MCC_EN(adapter)) { + if (rtw_hal_check_mcc_status(adapter, MCC_STATUS_DOING_MCC)) + mcc_en = _TRUE; + } +#endif/* CONFIG_MCC_MODE */ + + if (n_assoc_iface == 0) { + wifi_link_info.link_mode = BTC_LINK_NONE; + } else if (n_assoc_iface == 1) { + /* by pass */ + } else if (n_assoc_iface == 2) { + if (sta_iface && p2p_iface) { + u8 band_sta = sta_iface->mlmeextpriv.cur_channel > 14 ? BAND_ON_5G : BAND_ON_2_4G; + u8 band_p2p = p2p_iface->mlmeextpriv.cur_channel > 14 ? BAND_ON_5G : BAND_ON_2_4G; + if (band_sta == band_p2p) { + switch (band_sta) { + case BAND_ON_2_4G: + if (MLME_IS_GO(p2p_iface)) { + #ifdef CONFIG_MCC_MODE + wifi_link_info.link_mode = + mcc_en == _TRUE ? BTC_LINK_2G_MCC_GO_STA : BTC_LINK_2G_SCC_GO_STA; + #else /* !CONFIG_MCC_MODE */ + wifi_link_info.link_mode = BTC_LINK_2G_SCC_GO_STA; + #endif /* CONFIG_MCC_MODE */ + } else if (MLME_IS_GC(p2p_iface)) { + #ifdef CONFIG_MCC_MODE + wifi_link_info.link_mode = + mcc_en == _TRUE ? BTC_LINK_2G_MCC_GC_STA : BTC_LINK_2G_SCC_GC_STA; + #else /* !CONFIG_MCC_MODE */ + wifi_link_info.link_mode = BTC_LINK_2G_SCC_GC_STA; + #endif /* CONFIG_MCC_MODE */ + } + break; + case BAND_ON_5G: + if (MLME_IS_GO(p2p_iface)) { + #ifdef CONFIG_MCC_MODE + wifi_link_info.link_mode = + mcc_en == _TRUE ? BTC_LINK_5G_MCC_GO_STA : BTC_LINK_5G_SCC_GO_STA; + #else /* !CONFIG_MCC_MODE */ + wifi_link_info.link_mode = BTC_LINK_5G_SCC_GO_STA; + #endif /* CONFIG_MCC_MODE */ + } else if (MLME_IS_GC(p2p_iface)) { + #ifdef CONFIG_MCC_MODE + wifi_link_info.link_mode = + mcc_en == _TRUE ? BTC_LINK_5G_MCC_GC_STA : BTC_LINK_5G_SCC_GC_STA; + #else /* !CONFIG_MCC_MODE */ + wifi_link_info.link_mode = BTC_LINK_5G_SCC_GC_STA; + #endif /* CONFIG_MCC_MODE */ + } + break; + } + } else { + if (MLME_IS_GO(p2p_iface)) + wifi_link_info.link_mode = BTC_LINK_25G_MCC_GO_STA; + else if (MLME_IS_GC(p2p_iface)) + wifi_link_info.link_mode = BTC_LINK_25G_MCC_GC_STA; + } + } + + if (sta_iface && ap_iface) { + u8 band_sta = sta_iface->mlmeextpriv.cur_channel > 14 ? BAND_ON_5G : BAND_ON_2_4G; + u8 band_ap = ap_iface->mlmeextpriv.cur_channel > 14 ? BAND_ON_5G : BAND_ON_2_4G; + + if (band_sta == band_ap) { + switch (band_sta) { + case BAND_ON_2_4G: + #ifdef CONFIG_MCC_MODE + wifi_link_info.link_mode = + mcc_en == _TRUE ? BTC_LINK_2G_MCC_GO_STA : BTC_LINK_2G_SCC_GO_STA; + #else /* !CONFIG_MCC_MODE */ + wifi_link_info.link_mode = BTC_LINK_2G_SCC_GO_STA; + #endif /* CONFIG_MCC_MODE */ + break; + case BAND_ON_5G: + #ifdef CONFIG_MCC_MODE + wifi_link_info.link_mode = + mcc_en == _TRUE ? BTC_LINK_5G_MCC_GO_STA : BTC_LINK_5G_SCC_GO_STA; + #else /* !CONFIG_MCC_MODE */ + wifi_link_info.link_mode = BTC_LINK_5G_SCC_GO_STA; + #endif /* CONFIG_MCC_MODE */ + break; + } + } else { + wifi_link_info.link_mode = BTC_LINK_25G_MCC_GO_STA; + } + } + } else { + if (pBtCoexist->board_info.btdm_ant_num == 1) + RTW_ERR("%s do not support n_assoc_iface > 2 (ant_num == 1)", __func__); + } + + return wifi_link_info; +} + + +static void _btmpoper_timer_hdl(void *p) +{ + if (GLBtcBtMpRptWait == _TRUE) { + GLBtcBtMpRptWait = _FALSE; + _rtw_up_sema(&GLBtcBtMpRptSema); + } +} + +/* + * !IMPORTANT! + * Before call this function, caller should acquire "GLBtcBtMpOperLock"! + * Othrewise there will be racing problem and something may go wrong. + */ +static u8 _btmpoper_cmd(PBTC_COEXIST pBtCoexist, u8 opcode, u8 opcodever, u8 *cmd, u8 size) +{ + PADAPTER padapter; + u8 buf[H2C_BTMP_OPER_LEN] = {0}; + u8 buflen; + u8 seq; + s32 ret; + + + if (!cmd && size) + size = 0; + if ((size + 2) > H2C_BTMP_OPER_LEN) + return BT_STATUS_H2C_LENGTH_EXCEEDED; + buflen = size + 2; + + seq = GLBtcBtMpOperSeq & 0xF; + GLBtcBtMpOperSeq++; + + buf[0] = (opcodever & 0xF) | (seq << 4); + buf[1] = opcode; + if (cmd && size) + _rtw_memcpy(buf + 2, cmd, size); + + GLBtcBtMpRptWait = _TRUE; + GLBtcBtMpRptWiFiOK = _FALSE; + GLBtcBtMpRptBTOK = _FALSE; + GLBtcBtMpRptStatus = 0; + padapter = pBtCoexist->Adapter; + _set_timer(&GLBtcBtMpOperTimer, BTC_MPOPER_TIMEOUT); + if (rtw_hal_fill_h2c_cmd(padapter, H2C_BT_MP_OPER, buflen, buf) == _FAIL) { + _cancel_timer_ex(&GLBtcBtMpOperTimer); + ret = BT_STATUS_H2C_FAIL; + goto exit; + } + + _rtw_down_sema(&GLBtcBtMpRptSema); + /* GLBtcBtMpRptWait should be _FALSE here*/ + + if (GLBtcBtMpRptWiFiOK == _FALSE) { + RTW_DBG("%s: Didn't get H2C Rsp Event!\n", __FUNCTION__); + ret = BT_STATUS_H2C_TIMTOUT; + goto exit; + } + if (GLBtcBtMpRptBTOK == _FALSE) { + RTW_DBG("%s: Didn't get BT response!\n", __FUNCTION__); + ret = BT_STATUS_H2C_BT_NO_RSP; + goto exit; + } + + if (seq != GLBtcBtMpRptSeq) { + RTW_ERR("%s: Sequence number not match!(%d!=%d)!\n", + __FUNCTION__, seq, GLBtcBtMpRptSeq); + ret = BT_STATUS_C2H_REQNUM_MISMATCH; + goto exit; + } + + switch (GLBtcBtMpRptStatus) { + /* Examine the status reported from C2H */ + case BT_STATUS_OK: + ret = BT_STATUS_BT_OP_SUCCESS; + RTW_DBG("%s: C2H status = BT_STATUS_BT_OP_SUCCESS\n", __FUNCTION__); + break; + case BT_STATUS_VERSION_MISMATCH: + ret = BT_STATUS_OPCODE_L_VERSION_MISMATCH; + RTW_DBG("%s: C2H status = BT_STATUS_OPCODE_L_VERSION_MISMATCH\n", __FUNCTION__); + break; + case BT_STATUS_UNKNOWN_OPCODE: + ret = BT_STATUS_UNKNOWN_OPCODE_L; + RTW_DBG("%s: C2H status = MP_BT_STATUS_UNKNOWN_OPCODE_L\n", __FUNCTION__); + break; + case BT_STATUS_ERROR_PARAMETER: + ret = BT_STATUS_PARAMETER_FORMAT_ERROR_L; + RTW_DBG("%s: C2H status = MP_BT_STATUS_PARAMETER_FORMAT_ERROR_L\n", __FUNCTION__); + break; + default: + ret = BT_STATUS_UNKNOWN_STATUS_L; + RTW_DBG("%s: C2H status = MP_BT_STATUS_UNKNOWN_STATUS_L\n", __FUNCTION__); + break; + } + +exit: + return ret; +} + +u32 halbtcoutsrc_GetBtPatchVer(PBTC_COEXIST pBtCoexist) +{ + if (pBtCoexist->bt_info.get_bt_fw_ver_cnt <= 5) { + if (halbtcoutsrc_IsHwMailboxExist(pBtCoexist) == _TRUE) { + _irqL irqL; + u8 ret; + + _enter_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + ret = _btmpoper_cmd(pBtCoexist, BT_OP_GET_BT_VERSION, 0, NULL, 0); + if (BT_STATUS_BT_OP_SUCCESS == ret) { + pBtCoexist->bt_info.bt_real_fw_ver = le32_to_cpu(*(u32 *)GLBtcBtMpRptRsp); + pBtCoexist->bt_info.get_bt_fw_ver_cnt++; + } + + _exit_critical_mutex(&GLBtcBtMpOperLock, &irqL); + } else { +#ifdef CONFIG_BT_COEXIST_SOCKET_TRX + u8 dataLen = 2; + u8 buf[4] = {0}; + + buf[0] = 0x0; /* OP_Code */ + buf[1] = 0x0; /* OP_Code_Length */ + BT_SendEventExtBtCoexControl(pBtCoexist->Adapter, _FALSE, dataLen, &buf[0]); +#endif /* !CONFIG_BT_COEXIST_SOCKET_TRX */ + } + } + + return pBtCoexist->bt_info.bt_real_fw_ver; +} + +s32 halbtcoutsrc_GetWifiRssi(PADAPTER padapter) +{ + return rtw_dm_get_min_rssi(padapter); +} + +u32 halbtcoutsrc_GetBtCoexSupportedFeature(void *pBtcContext) +{ + PBTC_COEXIST pBtCoexist; + u32 ret = BT_STATUS_BT_OP_SUCCESS; + u32 data = 0; + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + + if (halbtcoutsrc_IsHwMailboxExist(pBtCoexist) == _TRUE) { + u8 buf[3] = {0}; + _irqL irqL; + u8 op_code; + u8 status; + + _enter_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + op_code = BT_OP_GET_BT_COEX_SUPPORTED_FEATURE; + status = _btmpoper_cmd(pBtCoexist, op_code, 0, buf, 0); + if (status == BT_STATUS_BT_OP_SUCCESS) + data = le16_to_cpu(*(u16 *)GLBtcBtMpRptRsp); + else + ret = SET_BT_MP_OPER_RET(op_code, status); + + _exit_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + } else + ret = BT_STATUS_NOT_IMPLEMENT; + + return data; +} + +u32 halbtcoutsrc_GetBtCoexSupportedVersion(void *pBtcContext) +{ + PBTC_COEXIST pBtCoexist; + u32 ret = BT_STATUS_BT_OP_SUCCESS; + u32 data = 0xFFFF; + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + + if (halbtcoutsrc_IsHwMailboxExist(pBtCoexist) == _TRUE) { + u8 buf[3] = {0}; + _irqL irqL; + u8 op_code; + u8 status; + + _enter_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + op_code = BT_OP_GET_BT_COEX_SUPPORTED_VERSION; + status = _btmpoper_cmd(pBtCoexist, op_code, 0, buf, 0); + if (status == BT_STATUS_BT_OP_SUCCESS) + data = le16_to_cpu(*(u16 *)GLBtcBtMpRptRsp); + else + ret = SET_BT_MP_OPER_RET(op_code, status); + + _exit_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + } else + ret = BT_STATUS_NOT_IMPLEMENT; + + return data; +} + +u32 halbtcoutsrc_GetBtDeviceInfo(void *pBtcContext) +{ + PBTC_COEXIST pBtCoexist; + u32 ret = BT_STATUS_BT_OP_SUCCESS; + u32 btDeviceInfo = 0; + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + + if (halbtcoutsrc_IsHwMailboxExist(pBtCoexist) == _TRUE) { + u8 buf[3] = {0}; + _irqL irqL; + u8 op_code; + u8 status; + + _enter_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + op_code = BT_OP_GET_BT_DEVICE_INFO; + status = _btmpoper_cmd(pBtCoexist, op_code, 0, buf, 0); + if (status == BT_STATUS_BT_OP_SUCCESS) + btDeviceInfo = le32_to_cpu(*(u32 *)GLBtcBtMpRptRsp); + else + ret = SET_BT_MP_OPER_RET(op_code, status); + + _exit_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + } else + ret = BT_STATUS_NOT_IMPLEMENT; + + return btDeviceInfo; +} + +u32 halbtcoutsrc_GetBtForbiddenSlotVal(void *pBtcContext) +{ + PBTC_COEXIST pBtCoexist; + u32 ret = BT_STATUS_BT_OP_SUCCESS; + u32 btForbiddenSlotVal = 0; + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + + if (halbtcoutsrc_IsHwMailboxExist(pBtCoexist) == _TRUE) { + u8 buf[3] = {0}; + _irqL irqL; + u8 op_code; + u8 status; + + _enter_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + op_code = BT_OP_GET_BT_FORBIDDEN_SLOT_VAL; + status = _btmpoper_cmd(pBtCoexist, op_code, 0, buf, 0); + if (status == BT_STATUS_BT_OP_SUCCESS) + btForbiddenSlotVal = le32_to_cpu(*(u32 *)GLBtcBtMpRptRsp); + else + ret = SET_BT_MP_OPER_RET(op_code, status); + + _exit_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + } else + ret = BT_STATUS_NOT_IMPLEMENT; + + return btForbiddenSlotVal; +} + +static u8 halbtcoutsrc_GetWifiScanAPNum(PADAPTER padapter) +{ + struct mlme_priv *pmlmepriv; + struct mlme_ext_priv *pmlmeext; + static u8 scan_AP_num = 0; + + + pmlmepriv = &padapter->mlmepriv; + pmlmeext = &padapter->mlmeextpriv; + + if (GLBtcWiFiInScanState == _FALSE) { + if (pmlmepriv->num_of_scanned > 0xFF) + scan_AP_num = 0xFF; + else + scan_AP_num = (u8)pmlmepriv->num_of_scanned; + } + + return scan_AP_num; +} + +u32 halbtcoutsrc_GetPhydmVersion(void *pBtcContext) +{ + struct btc_coexist *pBtCoexist = (struct btc_coexist *)pBtcContext; + PADAPTER Adapter = pBtCoexist->Adapter; + +#ifdef CONFIG_RTL8192E + return RELEASE_VERSION_8192E; +#endif + +#ifdef CONFIG_RTL8821A + return RELEASE_VERSION_8821A; +#endif + +#ifdef CONFIG_RTL8723B + return RELEASE_VERSION_8723B; +#endif + +#ifdef CONFIG_RTL8812A + return RELEASE_VERSION_8812A; +#endif + +#ifdef CONFIG_RTL8703B + return RELEASE_VERSION_8703B; +#endif + +#ifdef CONFIG_RTL8822B + return RELEASE_VERSION_8822B; +#endif + +#ifdef CONFIG_RTL8723D + return RELEASE_VERSION_8723D; +#endif + +#ifdef CONFIG_RTL8821C + return RELEASE_VERSION_8821C; +#endif + +#ifdef CONFIG_RTL8192F + return RELEASE_VERSION_8192F; +#endif + +#ifdef CONFIG_RTL8822C + return RELEASE_VERSION_8822C; +#endif + +#ifdef CONFIG_RTL8814A + return RELEASE_VERSION_8814A; +#endif + +#ifdef CONFIG_RTL8814B + return RELEASE_VERSION_8814B; +#endif + +#ifdef CONFIG_RTL8723F + return RELEASE_VERSION_8723F; +#endif + +} + +u8 halbtcoutsrc_Get(void *pBtcContext, u8 getType, void *pOutBuf) +{ + PBTC_COEXIST pBtCoexist; + PADAPTER padapter; + PHAL_DATA_TYPE pHalData; + struct mlme_ext_priv *mlmeext; + struct btc_wifi_link_info *wifi_link_info; + u8 bSoftApExist, bVwifiExist; + u8 *pu8; + s32 *pS4Tmp; + u32 *pU4Tmp; + u8 *pU1Tmp; + u16 *pU2Tmp; + u8 ret; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return _FALSE; + + padapter = pBtCoexist->Adapter; + pHalData = GET_HAL_DATA(padapter); + mlmeext = &padapter->mlmeextpriv; + bSoftApExist = _FALSE; + bVwifiExist = _FALSE; + pu8 = (u8 *)pOutBuf; + pS4Tmp = (s32 *)pOutBuf; + pU4Tmp = (u32 *)pOutBuf; + pU1Tmp = (u8 *)pOutBuf; + pU2Tmp = (u16*)pOutBuf; + wifi_link_info = (struct btc_wifi_link_info *)pOutBuf; + ret = _TRUE; + + switch (getType) { + case BTC_GET_BL_HS_OPERATION: + *pu8 = _FALSE; + ret = _FALSE; + break; + + case BTC_GET_BL_HS_CONNECTING: + *pu8 = _FALSE; + ret = _FALSE; + break; + + case BTC_GET_BL_WIFI_FW_READY: + *pu8 = halbtcoutsrc_is_fw_ready(pBtCoexist); + break; + + case BTC_GET_BL_WIFI_CONNECTED: + *pu8 = (rtw_mi_check_status(padapter, MI_LINKED)) ? _TRUE : _FALSE; + break; + + case BTC_GET_BL_WIFI_DUAL_BAND_CONNECTED: + *pu8 = halbtcoutsrc_IsDualBandConnected(padapter); + break; + + case BTC_GET_BL_WIFI_BUSY: + *pu8 = halbtcoutsrc_IsWifiBusy(padapter); + break; + + case BTC_GET_BL_WIFI_SCAN: +#if 0 + *pu8 = (rtw_mi_check_fwstate(padapter, WIFI_UNDER_SURVEY)) ? _TRUE : _FALSE; +#else + /* Use the value of the new variable GLBtcWiFiInScanState to judge whether WiFi is in scan state or not, since the originally used flag + WIFI_UNDER_SURVEY in fwstate may not be cleared in time */ + *pu8 = GLBtcWiFiInScanState; +#endif + break; + + case BTC_GET_BL_WIFI_LINK: + *pu8 = (rtw_mi_check_status(padapter, MI_STA_LINKING)) ? _TRUE : _FALSE; + break; + + case BTC_GET_BL_WIFI_ROAM: + *pu8 = (rtw_mi_check_status(padapter, MI_STA_LINKING)) ? _TRUE : _FALSE; + break; + + case BTC_GET_BL_WIFI_4_WAY_PROGRESS: + *pu8 = _FALSE; + break; + + case BTC_GET_BL_WIFI_BSSID: + _rtw_memcpy(pu8, get_bssid(&padapter->mlmepriv), ETH_ALEN); + break; + + case BTC_GET_BL_WIFI_UNDER_5G: + *pu8 = (pHalData->current_band_type == BAND_ON_5G) ? _TRUE : _FALSE; + break; + + case BTC_GET_BL_WIFI_AP_MODE_ENABLE: + *pu8 = (rtw_mi_check_status(padapter, MI_AP_MODE)) ? _TRUE : _FALSE; + break; + + case BTC_GET_BL_WIFI_ENABLE_ENCRYPTION: + *pu8 = padapter->securitypriv.dot11PrivacyAlgrthm == 0 ? _FALSE : _TRUE; + break; + + case BTC_GET_BL_WIFI_UNDER_B_MODE: + if (mlmeext->cur_wireless_mode == WIRELESS_11B) + *pu8 = _TRUE; + else + *pu8 = _FALSE; + break; + + case BTC_GET_BL_WIFI_IS_IN_MP_MODE: + if (padapter->registrypriv.mp_mode == 0) + *pu8 = _FALSE; + else + *pu8 = _TRUE; + break; + + case BTC_GET_BL_EXT_SWITCH: + *pu8 = _FALSE; + break; + case BTC_GET_BL_IS_ASUS_8723B: + /* Always return FALSE in linux driver since this case is added only for windows driver */ + *pu8 = _FALSE; + break; + + case BTC_GET_BL_RF4CE_CONNECTED: +#ifdef CONFIG_RF4CE_COEXIST + if (hal_btcoex_get_rf4ce_link_state() == 0) + *pu8 = FALSE; + else + *pu8 = TRUE; +#else + *pu8 = FALSE; +#endif + break; + + case BTC_GET_BL_WIFI_LW_PWR_STATE: + /* return false due to coex do not run during 32K */ + *pu8 = FALSE; + break; + + case BTC_GET_S4_WIFI_RSSI: + *pS4Tmp = halbtcoutsrc_GetWifiRssi(padapter); + break; + + case BTC_GET_S4_HS_RSSI: + *pS4Tmp = 0; + ret = _FALSE; + break; + + case BTC_GET_U4_WIFI_BW: + if (IsLegacyOnly(mlmeext->cur_wireless_mode)) + *pU4Tmp = BTC_WIFI_BW_LEGACY; + else { + switch (pHalData->current_channel_bw) { + case CHANNEL_WIDTH_20: + *pU4Tmp = BTC_WIFI_BW_HT20; + break; + case CHANNEL_WIDTH_40: + *pU4Tmp = BTC_WIFI_BW_HT40; + break; + case CHANNEL_WIDTH_80: + *pU4Tmp = BTC_WIFI_BW_HT80; + break; + case CHANNEL_WIDTH_160: + *pU4Tmp = BTC_WIFI_BW_HT160; + break; + default: + RTW_INFO("[BTCOEX] unknown bandwidth(%d)\n", pHalData->current_channel_bw); + *pU4Tmp = BTC_WIFI_BW_HT40; + break; + } + + } + break; + + case BTC_GET_U4_WIFI_TRAFFIC_DIRECTION: + case BTC_GET_U4_WIFI_TRAFFIC_DIR: + { + PRT_LINK_DETECT_T plinkinfo; + plinkinfo = &padapter->mlmepriv.LinkDetectInfo; + + if (plinkinfo->NumTxOkInPeriod > plinkinfo->NumRxOkInPeriod) + *pU4Tmp = BTC_WIFI_TRAFFIC_TX; + else + *pU4Tmp = BTC_WIFI_TRAFFIC_RX; + } + break; + + case BTC_GET_U4_WIFI_FW_VER: + *pU4Tmp = pHalData->firmware_version << 16; + *pU4Tmp |= pHalData->firmware_sub_version; + break; + + case BTC_GET_U4_WIFI_PHY_VER: + *pU4Tmp = halbtcoutsrc_GetPhydmVersion(pBtCoexist); + break; + + case BTC_GET_U4_WIFI_LINK_STATUS: + *pU4Tmp = halbtcoutsrc_GetWifiLinkStatus(pBtCoexist); + break; + case BTC_GET_BL_WIFI_LINK_INFO: + *wifi_link_info = halbtcoutsrc_getwifilinkinfo(pBtCoexist); + break; + case BTC_GET_U4_BT_PATCH_VER: + *pU4Tmp = halbtcoutsrc_GetBtPatchVer(pBtCoexist); + break; + + case BTC_GET_U4_VENDOR: + *pU4Tmp = BTC_VENDOR_OTHER; + break; + + case BTC_GET_U4_SUPPORTED_VERSION: + *pU4Tmp = halbtcoutsrc_GetBtCoexSupportedVersion(pBtCoexist); + break; + case BTC_GET_U4_SUPPORTED_FEATURE: + *pU4Tmp = halbtcoutsrc_GetBtCoexSupportedFeature(pBtCoexist); + break; + + case BTC_GET_U4_BT_DEVICE_INFO: + *pU4Tmp = halbtcoutsrc_GetBtDeviceInfo(pBtCoexist); + break; + + case BTC_GET_U4_BT_FORBIDDEN_SLOT_VAL: + case BTC_GET_U4_BT_A2DP_FLUSH_VAL: + *pU4Tmp = halbtcoutsrc_GetBtForbiddenSlotVal(pBtCoexist); + break; + + case BTC_GET_U4_WIFI_IQK_TOTAL: + *pU4Tmp = pHalData->odmpriv.n_iqk_cnt; + break; + + case BTC_GET_U4_WIFI_IQK_OK: + *pU4Tmp = pHalData->odmpriv.n_iqk_ok_cnt; + break; + + case BTC_GET_U4_WIFI_IQK_FAIL: + *pU4Tmp = pHalData->odmpriv.n_iqk_fail_cnt; + break; + + case BTC_GET_U1_WIFI_DOT11_CHNL: + *pU1Tmp = padapter->mlmeextpriv.cur_channel; + break; + + case BTC_GET_U1_WIFI_CENTRAL_CHNL: + *pU1Tmp = pHalData->current_channel; + break; + + case BTC_GET_U1_WIFI_HS_CHNL: + *pU1Tmp = 0; + ret = _FALSE; + break; + + case BTC_GET_U1_WIFI_P2P_CHNL: +#ifdef CONFIG_P2P + { + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + *pU1Tmp = pwdinfo->operating_channel; + } +#else + *pU1Tmp = 0; +#endif + break; + + case BTC_GET_U1_MAC_PHY_MODE: + /* *pU1Tmp = BTC_SMSP; + * *pU1Tmp = BTC_DMSP; + * *pU1Tmp = BTC_DMDP; + * *pU1Tmp = BTC_MP_UNKNOWN; */ + break; + + case BTC_GET_U1_AP_NUM: + *pU1Tmp = halbtcoutsrc_GetWifiScanAPNum(padapter); + break; + case BTC_GET_U1_ANT_TYPE: + switch (pHalData->bt_coexist.btAntisolation) { + case 0: + *pU1Tmp = (u8)BTC_ANT_TYPE_0; + pBtCoexist->board_info.ant_type = (u8)BTC_ANT_TYPE_0; + break; + case 1: + *pU1Tmp = (u8)BTC_ANT_TYPE_1; + pBtCoexist->board_info.ant_type = (u8)BTC_ANT_TYPE_1; + break; + case 2: + *pU1Tmp = (u8)BTC_ANT_TYPE_2; + pBtCoexist->board_info.ant_type = (u8)BTC_ANT_TYPE_2; + break; + case 3: + *pU1Tmp = (u8)BTC_ANT_TYPE_3; + pBtCoexist->board_info.ant_type = (u8)BTC_ANT_TYPE_3; + break; + case 4: + *pU1Tmp = (u8)BTC_ANT_TYPE_4; + pBtCoexist->board_info.ant_type = (u8)BTC_ANT_TYPE_4; + break; + } + break; + case BTC_GET_U1_IOT_PEER: + *pU1Tmp = mlmeext->mlmext_info.assoc_AP_vendor; + break; + + /* =======1Ant=========== */ + case BTC_GET_U1_LPS_MODE: + *pU1Tmp = padapter->dvobj->pwrctl_priv.pwr_mode; + break; + + case BTC_GET_U2_BEACON_PERIOD: + *pU2Tmp = mlmeext->mlmext_info.bcn_interval; + break; + + default: + ret = _FALSE; + break; + } + + return ret; +} + +u16 halbtcoutsrc_LnaConstrainLvl(void *pBtcContext, u8 *lna_constrain_level) +{ + PBTC_COEXIST pBtCoexist; + u16 ret = BT_STATUS_BT_OP_SUCCESS; + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + + if (halbtcoutsrc_IsHwMailboxExist(pBtCoexist) == _TRUE) { + _irqL irqL; + u8 op_code; + + _enter_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + ret = _btmpoper_cmd(pBtCoexist, BT_OP_SET_BT_LANCONSTRAIN_LEVEL, 0, lna_constrain_level, 1); + + _exit_critical_mutex(&GLBtcBtMpOperLock, &irqL); + } else { + ret = BT_STATUS_NOT_IMPLEMENT; + RTW_INFO("%s halbtcoutsrc_IsHwMailboxExist(pBtCoexist) == FALSE\n", __func__); + } + + return ret; +} + +u8 halbtcoutsrc_SetBtGoldenRxRange(void *pBtcContext, u8 profile, u8 range_shift) +{ + /* wait for implementation if necessary */ + + return 0; +} + +u8 halbtcoutsrc_Set(void *pBtcContext, u8 setType, void *pInBuf) +{ + PBTC_COEXIST pBtCoexist; + PADAPTER padapter; + PHAL_DATA_TYPE pHalData; + u8 *pu8; + u8 *pU1Tmp; + u16 *pU2Tmp; + u32 *pU4Tmp; + u8 ret; + u8 result = _TRUE; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return _FALSE; + + padapter = pBtCoexist->Adapter; + pHalData = GET_HAL_DATA(padapter); + pu8 = (u8 *)pInBuf; + pU1Tmp = (u8 *)pInBuf; + pU2Tmp = (u16*)pInBuf; + pU4Tmp = (u32 *)pInBuf; + ret = _TRUE; + + switch (setType) { + /* set some u8 type variables. */ + case BTC_SET_BL_BT_DISABLE: + pBtCoexist->bt_info.bt_disabled = *pu8; + break; + + case BTC_SET_BL_BT_ENABLE_DISABLE_CHANGE: + pBtCoexist->bt_info.bt_enable_disable_change = *pu8; + break; + + case BTC_SET_BL_BT_TRAFFIC_BUSY: + pBtCoexist->bt_info.bt_busy = *pu8; + break; + + case BTC_SET_BL_BT_LIMITED_DIG: + pBtCoexist->bt_info.limited_dig = *pu8; + break; + + case BTC_SET_BL_FORCE_TO_ROAM: + pBtCoexist->bt_info.force_to_roam = *pu8; + break; + + case BTC_SET_BL_TO_REJ_AP_AGG_PKT: + pBtCoexist->bt_info.reject_agg_pkt = *pu8; + break; + + case BTC_SET_BL_BT_CTRL_AGG_SIZE: + pBtCoexist->bt_info.bt_ctrl_agg_buf_size = *pu8; + break; + + case BTC_SET_BL_INC_SCAN_DEV_NUM: + pBtCoexist->bt_info.increase_scan_dev_num = *pu8; + break; + + case BTC_SET_BL_BT_TX_RX_MASK: + pBtCoexist->bt_info.bt_tx_rx_mask = *pu8; + break; + + case BTC_SET_BL_MIRACAST_PLUS_BT: + pBtCoexist->bt_info.miracast_plus_bt = *pu8; + break; + + /* set some u8 type variables. */ + case BTC_SET_U1_RSSI_ADJ_VAL_FOR_AGC_TABLE_ON: + pBtCoexist->bt_info.rssi_adjust_for_agc_table_on = *pU1Tmp; + break; + + case BTC_SET_U1_AGG_BUF_SIZE: + pBtCoexist->bt_info.agg_buf_size = *pU1Tmp; + break; + + /* the following are some action which will be triggered */ + case BTC_SET_ACT_GET_BT_RSSI: +#if 0 + BT_SendGetBtRssiEvent(padapter); +#else + ret = _FALSE; +#endif + break; + + case BTC_SET_ACT_AGGREGATE_CTRL: + halbtcoutsrc_AggregationCheck(pBtCoexist); + break; + + /* =======1Ant=========== */ + /* set some u8 type variables. */ + case BTC_SET_U1_RSSI_ADJ_VAL_FOR_1ANT_COEX_TYPE: + pBtCoexist->bt_info.rssi_adjust_for_1ant_coex_type = *pU1Tmp; + break; + + case BTC_SET_U1_LPS_VAL: + pBtCoexist->bt_info.lps_val = *pU1Tmp; + break; + + case BTC_SET_U1_RPWM_VAL: + pBtCoexist->bt_info.rpwm_val = *pU1Tmp; + break; + + /* the following are some action which will be triggered */ + case BTC_SET_ACT_LEAVE_LPS: + result = halbtcoutsrc_LeaveLps(pBtCoexist); + break; + + case BTC_SET_ACT_ENTER_LPS: + halbtcoutsrc_EnterLps(pBtCoexist); + break; + + case BTC_SET_ACT_NORMAL_LPS: + halbtcoutsrc_NormalLps(pBtCoexist); + break; + + case BTC_SET_ACT_PRE_NORMAL_LPS: + halbtcoutsrc_Pre_NormalLps(pBtCoexist); + break; + + case BTC_SET_ACT_POST_NORMAL_LPS: + halbtcoutsrc_Post_NormalLps(pBtCoexist); + break; + + case BTC_SET_ACT_DISABLE_LOW_POWER: + halbtcoutsrc_DisableLowPower(pBtCoexist, *pu8); + break; + + case BTC_SET_ACT_UPDATE_RAMASK: + /* + pBtCoexist->bt_info.ra_mask = *pU4Tmp; + + if (check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE) == _TRUE) { + struct sta_info *psta; + PWLAN_BSSID_EX cur_network; + + cur_network = &padapter->mlmeextpriv.mlmext_info.network; + psta = rtw_get_stainfo(&padapter->stapriv, cur_network->MacAddress); + rtw_hal_update_ra_mask(psta); + } + */ + break; + + case BTC_SET_ACT_SEND_MIMO_PS: { + u8 newMimoPsMode = 3; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + + /* *pU1Tmp = 0 use SM_PS static type */ + /* *pU1Tmp = 1 disable SM_PS */ + if (*pU1Tmp == 0) + newMimoPsMode = WLAN_HT_CAP_SM_PS_STATIC; + else if (*pU1Tmp == 1) + newMimoPsMode = WLAN_HT_CAP_SM_PS_DISABLED; + + if (check_fwstate(&padapter->mlmepriv , WIFI_ASOC_STATE) == _TRUE) { + /* issue_action_SM_PS(padapter, get_my_bssid(&(pmlmeinfo->network)), newMimoPsMode); */ + issue_action_SM_PS_wait_ack(padapter , get_my_bssid(&(pmlmeinfo->network)) , newMimoPsMode, 3 , 1); + } + } + break; + + case BTC_SET_ACT_CTRL_BT_INFO: +#ifdef CONFIG_BT_COEXIST_SOCKET_TRX + { + u8 dataLen = *pU1Tmp; + u8 tmpBuf[BTC_TMP_BUF_SHORT]; + if (dataLen) + _rtw_memcpy(tmpBuf, pU1Tmp + 1, dataLen); + BT_SendEventExtBtInfoControl(padapter, dataLen, &tmpBuf[0]); + } +#else /* !CONFIG_BT_COEXIST_SOCKET_TRX */ + ret = _FALSE; +#endif /* CONFIG_BT_COEXIST_SOCKET_TRX */ + break; + + case BTC_SET_ACT_CTRL_BT_COEX: +#ifdef CONFIG_BT_COEXIST_SOCKET_TRX + { + u8 dataLen = *pU1Tmp; + u8 tmpBuf[BTC_TMP_BUF_SHORT]; + if (dataLen) + _rtw_memcpy(tmpBuf, pU1Tmp + 1, dataLen); + BT_SendEventExtBtCoexControl(padapter, _FALSE, dataLen, &tmpBuf[0]); + } +#else /* !CONFIG_BT_COEXIST_SOCKET_TRX */ + ret = _FALSE; +#endif /* CONFIG_BT_COEXIST_SOCKET_TRX */ + break; + case BTC_SET_ACT_CTRL_8723B_ANT: +#if 0 + { + u8 dataLen = *pU1Tmp; + u8 tmpBuf[BTC_TMP_BUF_SHORT]; + if (dataLen) + PlatformMoveMemory(&tmpBuf[0], pU1Tmp + 1, dataLen); + BT_Set8723bAnt(Adapter, dataLen, &tmpBuf[0]); + } +#else + ret = _FALSE; +#endif + break; + case BTC_SET_BL_BT_LNA_CONSTRAIN_LEVEL: + halbtcoutsrc_LnaConstrainLvl(pBtCoexist, pu8); + break; + case BTC_SET_BL_BT_GOLDEN_RX_RANGE: + halbtcoutsrc_SetBtGoldenRxRange(pBtCoexist, (*pU2Tmp & 0xff00) >> 8, (*pU2Tmp & 0xff)); + break; + case BTC_SET_RESET_COEX_VAR: + _rtw_memset(&pBtCoexist->coex_dm, 0x00, sizeof(pBtCoexist->coex_dm)); + _rtw_memset(&pBtCoexist->coex_sta, 0x00, sizeof(pBtCoexist->coex_sta)); + + switch(pBtCoexist->chip_type) { +#ifdef CONFIG_RTL8822B + case BTC_CHIP_RTL8822B: + _rtw_memset(&pBtCoexist->coex_dm_8822b_1ant, 0x00, sizeof(pBtCoexist->coex_dm_8822b_1ant)); + _rtw_memset(&pBtCoexist->coex_dm_8822b_2ant, 0x00, sizeof(pBtCoexist->coex_dm_8822b_2ant)); + _rtw_memset(&pBtCoexist->coex_sta_8822b_1ant, 0x00, sizeof(pBtCoexist->coex_sta_8822b_1ant)); + _rtw_memset(&pBtCoexist->coex_sta_8822b_2ant, 0x00, sizeof(pBtCoexist->coex_sta_8822b_2ant)); + break; +#endif +#ifdef CONFIG_RTL8821C + case BTC_CHIP_RTL8821C: + _rtw_memset(&pBtCoexist->coex_dm_8821c_1ant, 0x00, sizeof(pBtCoexist->coex_dm_8821c_1ant)); + _rtw_memset(&pBtCoexist->coex_dm_8821c_2ant, 0x00, sizeof(pBtCoexist->coex_dm_8821c_2ant)); + _rtw_memset(&pBtCoexist->coex_sta_8821c_1ant, 0x00, sizeof(pBtCoexist->coex_sta_8821c_1ant)); + _rtw_memset(&pBtCoexist->coex_sta_8821c_2ant, 0x00, sizeof(pBtCoexist->coex_sta_8821c_2ant)); + break; +#endif +#ifdef CONFIG_RTL8723D + case BTC_CHIP_RTL8723D: + _rtw_memset(&pBtCoexist->coex_dm_8723d_1ant, 0x00, sizeof(pBtCoexist->coex_dm_8723d_1ant)); + _rtw_memset(&pBtCoexist->coex_dm_8723d_2ant, 0x00, sizeof(pBtCoexist->coex_dm_8723d_2ant)); + _rtw_memset(&pBtCoexist->coex_sta_8723d_1ant, 0x00, sizeof(pBtCoexist->coex_sta_8723d_1ant)); + _rtw_memset(&pBtCoexist->coex_sta_8723d_2ant, 0x00, sizeof(pBtCoexist->coex_sta_8723d_2ant)); + break; +#endif + } + break; + /* ===================== */ + default: + ret = _FALSE; + break; + } + + return result; +} + +u8 halbtcoutsrc_UnderIps(PBTC_COEXIST pBtCoexist) +{ + PADAPTER padapter; + struct pwrctrl_priv *pwrpriv; + u8 bMacPwrCtrlOn; + + padapter = pBtCoexist->Adapter; + pwrpriv = &padapter->dvobj->pwrctl_priv; + bMacPwrCtrlOn = _FALSE; + + if ((_TRUE == pwrpriv->bips_processing) + && (IPS_NONE != pwrpriv->ips_mode_req) + ) + return _TRUE; + + if (rf_off == pwrpriv->rf_pwrstate) + return _TRUE; + + rtw_hal_get_hwreg(padapter, HW_VAR_APFM_ON_MAC, &bMacPwrCtrlOn); + if (_FALSE == bMacPwrCtrlOn) + return _TRUE; + + return _FALSE; +} + +u8 halbtcoutsrc_UnderLps(PBTC_COEXIST pBtCoexist) +{ + return GLBtcWiFiInLPS; +} + +u8 halbtcoutsrc_Under32K(PBTC_COEXIST pBtCoexist) +{ + /* todo: the method to check whether wifi is under 32K or not */ + return _FALSE; +} + +void halbtcoutsrc_DisplayCoexStatistics(PBTC_COEXIST pBtCoexist) +{ +#if 0 + PADAPTER padapter = (PADAPTER)pBtCoexist->Adapter; + PBT_MGNT pBtMgnt = &padapter->MgntInfo.BtInfo.BtMgnt; + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); + u8 *cliBuf = pBtCoexist->cliBuf; + u8 i, j; + u8 tmpbuf[BTC_TMP_BUF_SHORT]; + + + if (gl_coex_offload.cnt_h2c_sent) { + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Coex h2c notify]============"); + CL_PRINTF(cliBuf); + + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = H2c(%d)/Ack(%d)", "Coex h2c/c2h overall statistics", + gl_coex_offload.cnt_h2c_sent, gl_coex_offload.cnt_c2h_ack); + for (j = 0; j < COL_STATUS_MAX; j++) { + if (gl_coex_offload.status[j]) { + CL_SPRINTF(tmpbuf, BTC_TMP_BUF_SHORT, ", %s:%d", coexH2cResultString[j], gl_coex_offload.status[j]); + CL_STRNCAT(cliBuf, BT_TMP_BUF_SIZE, tmpbuf, BTC_TMP_BUF_SHORT); + } + } + CL_PRINTF(cliBuf); + } + for (i = 0; i < COL_OP_WIFI_OPCODE_MAX; i++) { + if (gl_coex_offload.h2c_record[i].count) { + /*==========================================*/ + /* H2C result statistics*/ + /*==========================================*/ + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = total:%d", coexOpcodeString[i], gl_coex_offload.h2c_record[i].count); + for (j = 0; j < COL_STATUS_MAX; j++) { + if (gl_coex_offload.h2c_record[i].status[j]) { + CL_SPRINTF(tmpbuf, BTC_TMP_BUF_SHORT, ", %s:%d", coexH2cResultString[j], gl_coex_offload.h2c_record[i].status[j]); + CL_STRNCAT(cliBuf, BT_TMP_BUF_SIZE, tmpbuf, BTC_TMP_BUF_SHORT); + } + } + CL_PRINTF(cliBuf); + /*==========================================*/ + /* H2C/C2H content*/ + /*==========================================*/ + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = ", "H2C / C2H content"); + for (j = 0; j < gl_coex_offload.h2c_record[i].h2c_len; j++) { + CL_SPRINTF(tmpbuf, BTC_TMP_BUF_SHORT, "%02x ", gl_coex_offload.h2c_record[i].h2c_buf[j]); + CL_STRNCAT(cliBuf, BT_TMP_BUF_SIZE, tmpbuf, 3); + } + if (gl_coex_offload.h2c_record[i].c2h_ack_len) { + CL_STRNCAT(cliBuf, BT_TMP_BUF_SIZE, "/ ", 2); + for (j = 0; j < gl_coex_offload.h2c_record[i].c2h_ack_len; j++) { + CL_SPRINTF(tmpbuf, BTC_TMP_BUF_SHORT, "%02x ", gl_coex_offload.h2c_record[i].c2h_ack_buf[j]); + CL_STRNCAT(cliBuf, BT_TMP_BUF_SIZE, tmpbuf, 3); + } + } + CL_PRINTF(cliBuf); + /*==========================================*/ + } + } + + if (gl_coex_offload.cnt_c2h_ind) { + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Coex c2h indication]============"); + CL_PRINTF(cliBuf); + + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = Ind(%d)", "C2H indication statistics", + gl_coex_offload.cnt_c2h_ind); + for (j = 0; j < COL_STATUS_MAX; j++) { + if (gl_coex_offload.c2h_ind_status[j]) { + CL_SPRINTF(tmpbuf, BTC_TMP_BUF_SHORT, ", %s:%d", coexH2cResultString[j], gl_coex_offload.c2h_ind_status[j]); + CL_STRNCAT(cliBuf, BT_TMP_BUF_SIZE, tmpbuf, BTC_TMP_BUF_SHORT); + } + } + CL_PRINTF(cliBuf); + } + for (i = 0; i < COL_IND_MAX; i++) { + if (gl_coex_offload.c2h_ind_record[i].count) { + /*==========================================*/ + /* H2C result statistics*/ + /*==========================================*/ + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = total:%d", coexIndTypeString[i], gl_coex_offload.c2h_ind_record[i].count); + for (j = 0; j < COL_STATUS_MAX; j++) { + if (gl_coex_offload.c2h_ind_record[i].status[j]) { + CL_SPRINTF(tmpbuf, BTC_TMP_BUF_SHORT, ", %s:%d", coexH2cResultString[j], gl_coex_offload.c2h_ind_record[i].status[j]); + CL_STRNCAT(cliBuf, BT_TMP_BUF_SIZE, tmpbuf, BTC_TMP_BUF_SHORT); + } + } + CL_PRINTF(cliBuf); + /*==========================================*/ + /* content*/ + /*==========================================*/ + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = ", "C2H indication content"); + for (j = 0; j < gl_coex_offload.c2h_ind_record[i].ind_len; j++) { + CL_SPRINTF(tmpbuf, BTC_TMP_BUF_SHORT, "%02x ", gl_coex_offload.c2h_ind_record[i].ind_buf[j]); + CL_STRNCAT(cliBuf, BT_TMP_BUF_SIZE, tmpbuf, 3); + } + CL_PRINTF(cliBuf); + /*==========================================*/ + } + } + + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Statistics]============"); + CL_PRINTF(cliBuf); + +#if (H2C_USE_IO_THREAD != 1) + for (i = 0; i < H2C_STATUS_MAX; i++) { + if (pHalData->h2cStatistics[i]) { + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = [%s] = %d", "H2C statistics", \ + h2cStaString[i], pHalData->h2cStatistics[i]); + CL_PRINTF(cliBuf); + } + } +#else + for (i = 0; i < IO_STATUS_MAX; i++) { + if (Adapter->ioComStr.ioH2cStatistics[i]) { + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = [%s] = %d", "H2C statistics", \ + ioStaString[i], Adapter->ioComStr.ioH2cStatistics[i]); + CL_PRINTF(cliBuf); + } + } +#endif +#if 0 + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "lastHMEBoxNum", \ + pHalData->LastHMEBoxNum); + CL_PRINTF(cliBuf); + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x / 0x%x", "LastOkH2c/FirstFailH2c(fwNotRead)", \ + pHalData->lastSuccessH2cEid, pHalData->firstFailedH2cEid); + CL_PRINTF(cliBuf); + + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d/ %d", "c2hIsr/c2hIntr/clr1AF/noRdy/noBuf", \ + pHalData->InterruptLog.nIMR_C2HCMD, DBG_Var.c2hInterruptCnt, DBG_Var.c2hClrReadC2hCnt, + DBG_Var.c2hNotReadyCnt, DBG_Var.c2hBufAlloFailCnt); + CL_PRINTF(cliBuf); + + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d", "c2hPacket", \ + DBG_Var.c2hPacketCnt); + CL_PRINTF(cliBuf); +#endif + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d", "Periodical/ DbgCtrl", \ + pBtCoexist->statistics.cntPeriodical, pBtCoexist->statistics.cntDbgCtrl); + CL_PRINTF(cliBuf); + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d", "PowerOn/InitHw/InitCoexDm/RfStatus", \ + pBtCoexist->statistics.cntPowerOn, pBtCoexist->statistics.cntInitHwConfig, pBtCoexist->statistics.cntInitCoexDm, + pBtCoexist->statistics.cntRfStatusNotify); + CL_PRINTF(cliBuf); + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d/ %d", "Ips/Lps/Scan/Connect/Mstatus", \ + pBtCoexist->statistics.cntIpsNotify, pBtCoexist->statistics.cntLpsNotify, + pBtCoexist->statistics.cntScanNotify, pBtCoexist->statistics.cntConnectNotify, + pBtCoexist->statistics.cntMediaStatusNotify); + CL_PRINTF(cliBuf); + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d", "Special pkt/Bt info/ bind", + pBtCoexist->statistics.cntSpecialPacketNotify, pBtCoexist->statistics.cntBtInfoNotify, + pBtCoexist->statistics.cntBind); + CL_PRINTF(cliBuf); +#endif + PADAPTER padapter = pBtCoexist->Adapter; + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); + u8 *cliBuf = pBtCoexist->cli_buf; + + if (pHalData->EEPROMBluetoothCoexist == 1) { + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Coex Status]============"); + CL_PRINTF(cliBuf); + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d ", "IsBtDisabled", rtw_btcoex_IsBtDisabled(padapter)); + CL_PRINTF(cliBuf); + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d ", "IsBtControlLps", rtw_btcoex_IsBtControlLps(padapter)); + CL_PRINTF(cliBuf); + } +} + +void halbtcoutsrc_DisplayBtLinkInfo(PBTC_COEXIST pBtCoexist) +{ +#if 0 + PADAPTER padapter = (PADAPTER)pBtCoexist->Adapter; + PBT_MGNT pBtMgnt = &padapter->MgntInfo.BtInfo.BtMgnt; + u8 *cliBuf = pBtCoexist->cliBuf; + u8 i; + + + if (pBtCoexist->stack_info.profile_notified) { + for (i = 0; i < pBtMgnt->ExtConfig.NumberOfACL; i++) { + if (pBtMgnt->ExtConfig.HCIExtensionVer >= 1) { + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %s/ %s", "Bt link type/spec/role", \ + BtProfileString[pBtMgnt->ExtConfig.aclLink[i].BTProfile], + BtSpecString[pBtMgnt->ExtConfig.aclLink[i].BTCoreSpec], + BtLinkRoleString[pBtMgnt->ExtConfig.aclLink[i].linkRole]); + CL_PRINTF(cliBuf); + } else { + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %s", "Bt link type/spec", \ + BtProfileString[pBtMgnt->ExtConfig.aclLink[i].BTProfile], + BtSpecString[pBtMgnt->ExtConfig.aclLink[i].BTCoreSpec]); + CL_PRINTF(cliBuf); + } + } + } +#endif +} + +void halbtcoutsrc_DisplayWifiStatus(PBTC_COEXIST pBtCoexist) +{ + PADAPTER padapter = pBtCoexist->Adapter; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + u8 *cliBuf = pBtCoexist->cli_buf; + s32 wifiRssi = 0, btHsRssi = 0; + BOOLEAN bScan = _FALSE, bLink = _FALSE, bRoam = _FALSE, bWifiBusy = _FALSE, bWifiUnderBMode = _FALSE; + u32 wifiBw = BTC_WIFI_BW_HT20, wifiTrafficDir = BTC_WIFI_TRAFFIC_TX, wifiFreq = BTC_FREQ_2_4G; + u32 wifiLinkStatus = 0x0; + BOOLEAN bBtHsOn = _FALSE, bLowPower = _FALSE; + u8 wifiChnl = 0, wifiP2PChnl = 0, nScanAPNum = 0, FwPSState; + u32 iqk_cnt_total = 0, iqk_cnt_ok = 0, iqk_cnt_fail = 0; + u16 wifiBcnInterval = 0; + PHAL_DATA_TYPE hal = GET_HAL_DATA(padapter); + struct btc_wifi_link_info wifi_link_info; + + wifi_link_info = halbtcoutsrc_getwifilinkinfo(pBtCoexist); + + switch (wifi_link_info.link_mode) { + case BTC_LINK_NONE: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "None", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = hal->current_channel > 14 ? BTC_FREQ_5G : BTC_FREQ_2_4G; + break; + case BTC_LINK_ONLY_GO: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "ONLY_GO", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = hal->current_channel > 14 ? BTC_FREQ_5G : BTC_FREQ_2_4G; + break; + case BTC_LINK_ONLY_GC: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "ONLY_GC", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = hal->current_channel > 14 ? BTC_FREQ_5G : BTC_FREQ_2_4G; + break; + case BTC_LINK_ONLY_STA: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "ONLY_STA", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = hal->current_channel > 14 ? BTC_FREQ_5G : BTC_FREQ_2_4G; + break; + case BTC_LINK_ONLY_AP: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "ONLY_AP", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = hal->current_channel > 14 ? BTC_FREQ_5G : BTC_FREQ_2_4G; + break; + case BTC_LINK_2G_MCC_GO_STA: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "24G_MCC_GO_STA", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = BTC_FREQ_2_4G; + break; + case BTC_LINK_5G_MCC_GO_STA: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "5G_MCC_GO_STA", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = BTC_FREQ_5G; + break; + case BTC_LINK_25G_MCC_GO_STA: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "2BANDS_MCC_GO_STA", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = BTC_FREQ_25G; + break; + case BTC_LINK_2G_MCC_GC_STA: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "24G_MCC_GC_STA", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = BTC_FREQ_2_4G; + break; + case BTC_LINK_5G_MCC_GC_STA: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "5G_MCC_GC_STA", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = BTC_FREQ_5G; + break; + case BTC_LINK_25G_MCC_GC_STA: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "2BANDS_MCC_GC_STA", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = BTC_FREQ_25G; + break; + case BTC_LINK_2G_SCC_GO_STA: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "24G_SCC_GO_STA", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = BTC_FREQ_2_4G; + break; + case BTC_LINK_5G_SCC_GO_STA: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "5G_SCC_GO_STA", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = BTC_FREQ_5G; + break; + case BTC_LINK_2G_SCC_GC_STA: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "24G_SCC_GC_STA", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = BTC_FREQ_2_4G; + break; + case BTC_LINK_5G_SCC_GC_STA: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "5G_SCC_GC_STA", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = BTC_FREQ_5G; + break; + default: + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %d/ %d", "WifiLinkMode/HotSpa/Noa/ClientJoin", + "UNKNOWN", wifi_link_info.bhotspot, wifi_link_info.benable_noa, wifi_link_info.bany_client_join_go); + wifiFreq = hal->current_channel > 14 ? BTC_FREQ_5G : BTC_FREQ_2_4G; + break; + } + + CL_PRINTF(cliBuf); + + wifiLinkStatus = halbtcoutsrc_GetWifiLinkStatus(pBtCoexist); + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d/ %d", "STA/vWifi/HS/p2pGo/p2pGc", + ((wifiLinkStatus & WIFI_STA_CONNECTED) ? 1 : 0), ((wifiLinkStatus & WIFI_AP_CONNECTED) ? 1 : 0), + ((wifiLinkStatus & WIFI_HS_CONNECTED) ? 1 : 0), ((wifiLinkStatus & WIFI_P2P_GO_CONNECTED) ? 1 : 0), + ((wifiLinkStatus & WIFI_P2P_GC_CONNECTED) ? 1 : 0)); + CL_PRINTF(cliBuf); + + pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_SCAN, &bScan); + pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_LINK, &bLink); + pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_ROAM, &bRoam); + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d ", "Link/ Roam/ Scan", + bLink, bRoam, bScan); + CL_PRINTF(cliBuf); + + pBtCoexist->btc_get(pBtCoexist, BTC_GET_U4_WIFI_IQK_TOTAL, &iqk_cnt_total); + pBtCoexist->btc_get(pBtCoexist, BTC_GET_U4_WIFI_IQK_OK, &iqk_cnt_ok); + pBtCoexist->btc_get(pBtCoexist, BTC_GET_U4_WIFI_IQK_FAIL, &iqk_cnt_fail); + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d %s %s", + "IQK All/ OK/ Fail/AutoLoad/FWDL", iqk_cnt_total, iqk_cnt_ok, iqk_cnt_fail, + ((halbtcoutsrc_is_autoload_fail(pBtCoexist) == _TRUE) ? "fail":"ok"), ((halbtcoutsrc_is_fw_ready(pBtCoexist) == _TRUE) ? "ok":"fail")); + CL_PRINTF(cliBuf); + + if (wifiLinkStatus & WIFI_STA_CONNECTED) { + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ "MAC_FMT"", "IOT Peer/BSSID", GLBtcIotPeerString[padapter->mlmeextpriv.mlmext_info.assoc_AP_vendor], MAC_ARG(get_bssid(&padapter->mlmepriv))); + CL_PRINTF(cliBuf); + } + + pBtCoexist->btc_get(pBtCoexist, BTC_GET_S4_WIFI_RSSI, &wifiRssi); + pBtCoexist->btc_get(pBtCoexist, BTC_GET_U2_BEACON_PERIOD, &wifiBcnInterval); + wifiChnl = wifi_link_info.sta_center_channel; + wifiP2PChnl = wifi_link_info.p2p_center_channel; + + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d dBm/ %d/ %d/ %d", "RSSI/ STA_Chnl/ P2P_Chnl/ BI", + wifiRssi-100, wifiChnl, wifiP2PChnl, wifiBcnInterval); + CL_PRINTF(cliBuf); + + pBtCoexist->btc_get(pBtCoexist, BTC_GET_U4_WIFI_BW, &wifiBw); + pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &bWifiBusy); + pBtCoexist->btc_get(pBtCoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION, &wifiTrafficDir); + pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_UNDER_B_MODE, &bWifiUnderBMode); + pBtCoexist->btc_get(pBtCoexist, BTC_GET_U1_AP_NUM, &nScanAPNum); + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s / %s/ %s/ %d ", "Band/ BW/ Traffic/ APCnt", + GLBtcWifiFreqString[wifiFreq], ((bWifiUnderBMode) ? "11b" : GLBtcWifiBwString[wifiBw]), + ((!bWifiBusy) ? "idle" : ((BTC_WIFI_TRAFFIC_TX == wifiTrafficDir) ? "uplink" : "downlink")), + nScanAPNum); + CL_PRINTF(cliBuf); + + /* power status */ + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s%s%s", "Power Status", \ + ((halbtcoutsrc_UnderIps(pBtCoexist) == _TRUE) ? "IPS ON" : "IPS OFF"), + ((halbtcoutsrc_UnderLps(pBtCoexist) == _TRUE) ? ", LPS ON" : ", LPS OFF"), + ((halbtcoutsrc_Under32K(pBtCoexist) == _TRUE) ? ", 32k" : "")); + CL_PRINTF(cliBuf); + + CL_SPRINTF(cliBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x %02x %02x %02x (0x%x/0x%x)", "Power mode cmd(lps/rpwm)", + pBtCoexist->pwrModeVal[0], pBtCoexist->pwrModeVal[1], + pBtCoexist->pwrModeVal[2], pBtCoexist->pwrModeVal[3], + pBtCoexist->pwrModeVal[4], pBtCoexist->pwrModeVal[5], + pBtCoexist->bt_info.lps_val, + pBtCoexist->bt_info.rpwm_val); + CL_PRINTF(cliBuf); +} + +void halbtcoutsrc_DisplayDbgMsg(void *pBtcContext, u8 dispType) +{ + PBTC_COEXIST pBtCoexist; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + switch (dispType) { + case BTC_DBG_DISP_COEX_STATISTICS: + halbtcoutsrc_DisplayCoexStatistics(pBtCoexist); + break; + case BTC_DBG_DISP_BT_LINK_INFO: + halbtcoutsrc_DisplayBtLinkInfo(pBtCoexist); + break; + case BTC_DBG_DISP_WIFI_STATUS: + halbtcoutsrc_DisplayWifiStatus(pBtCoexist); + break; + default: + break; + } +} + +/* ************************************ + * IO related function + * ************************************ */ +u8 halbtcoutsrc_Read1Byte(void *pBtcContext, u32 RegAddr) +{ + PBTC_COEXIST pBtCoexist; + PADAPTER padapter; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + padapter = pBtCoexist->Adapter; + + return rtw_read8(padapter, RegAddr); +} + +u16 halbtcoutsrc_Read2Byte(void *pBtcContext, u32 RegAddr) +{ + PBTC_COEXIST pBtCoexist; + PADAPTER padapter; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + padapter = pBtCoexist->Adapter; + + return rtw_read16(padapter, RegAddr); +} + +u32 halbtcoutsrc_Read4Byte(void *pBtcContext, u32 RegAddr) +{ + PBTC_COEXIST pBtCoexist; + PADAPTER padapter; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + padapter = pBtCoexist->Adapter; + + return rtw_read32(padapter, RegAddr); +} + +void halbtcoutsrc_Write1Byte(void *pBtcContext, u32 RegAddr, u8 Data) +{ + PBTC_COEXIST pBtCoexist; + PADAPTER padapter; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + padapter = pBtCoexist->Adapter; + + rtw_write8(padapter, RegAddr, Data); +} + +void halbtcoutsrc_BitMaskWrite1Byte(void *pBtcContext, u32 regAddr, u8 bitMask, u8 data1b) +{ + PBTC_COEXIST pBtCoexist; + PADAPTER padapter; + u8 originalValue, bitShift; + u8 i; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + padapter = pBtCoexist->Adapter; + originalValue = 0; + bitShift = 0; + + if (bitMask != 0xff) { + originalValue = rtw_read8(padapter, regAddr); + + for (i = 0; i <= 7; i++) { + if ((bitMask >> i) & 0x1) + break; + } + bitShift = i; + + data1b = (originalValue & ~bitMask) | ((data1b << bitShift) & bitMask); + } + + rtw_write8(padapter, regAddr, data1b); +} + +void halbtcoutsrc_Write2Byte(void *pBtcContext, u32 RegAddr, u16 Data) +{ + PBTC_COEXIST pBtCoexist; + PADAPTER padapter; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + padapter = pBtCoexist->Adapter; + + rtw_write16(padapter, RegAddr, Data); +} + +void halbtcoutsrc_Write4Byte(void *pBtcContext, u32 RegAddr, u32 Data) +{ + PBTC_COEXIST pBtCoexist; + PADAPTER padapter; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + padapter = pBtCoexist->Adapter; + + rtw_write32(padapter, RegAddr, Data); +} + +void halbtcoutsrc_WriteLocalReg1Byte(void *pBtcContext, u32 RegAddr, u8 Data) +{ + PBTC_COEXIST pBtCoexist = (PBTC_COEXIST)pBtcContext; + PADAPTER Adapter = pBtCoexist->Adapter; + + if (BTC_INTF_SDIO == pBtCoexist->chip_interface) + rtw_write8(Adapter, SDIO_LOCAL_BASE | RegAddr, Data); + else + rtw_write8(Adapter, RegAddr, Data); +} + +u32 halbtcoutsrc_WaitLIndirectReg_Ready(void *pBtcContext) +{ + PBTC_COEXIST btc = (PBTC_COEXIST)pBtcContext; + u32 delay_count = 0, reg = 0; + + if (!btc->chip_para->lte_indirect_access) + return 0; + + switch (btc->chip_para->indirect_type) { + case BTC_INDIRECT_1700: + reg = 0x1703; + break; + case BTC_INDIRECT_7C0: + reg = 0x7C3; + break; + default: + return 0; + } + + /* wait for ready bit before access */ + while (1) { + if ((halbtcoutsrc_Read1Byte(btc, reg) & BIT(5)) == 0) { + rtw_mdelay_os(10); + if (++delay_count >= 10) + break; + } else { + break; + } + } + + return delay_count; +} + +u32 halbtcoutsrc_ReadLIndirectReg(void *pBtcContext, u16 reg_addr) +{ + PBTC_COEXIST btc = (PBTC_COEXIST)pBtcContext; + u32 val = 0; + + if (!btc->chip_para->lte_indirect_access) + return 0; + + /* wait for ready bit before access */ + halbtcoutsrc_WaitLIndirectReg_Ready(btc); + + switch (btc->chip_para->indirect_type) { + case BTC_INDIRECT_1700: + halbtcoutsrc_Write4Byte(btc, 0x1700, 0x800F0000 | reg_addr); + val = halbtcoutsrc_Read4Byte(btc, 0x1708); /* get read data */ + break; + case BTC_INDIRECT_7C0: + halbtcoutsrc_Write4Byte(btc, 0x7c0, 0x800F0000 | reg_addr); + val = halbtcoutsrc_Read4Byte(btc, 0x7c8); /* get read data */ + break; + } + + return val; +} + +void halbtcoutsrc_WriteLIndirectReg(void *pBtcContext, u16 reg_addr, u32 bit_mask, u32 reg_value) +{ + PBTC_COEXIST btc = (PBTC_COEXIST)pBtcContext; + u32 val, i = 0, bitpos = 0, reg0, reg1; + + if (!btc->chip_para->lte_indirect_access) + return; + + if (bit_mask == 0x0) + return; + + switch (btc->chip_para->indirect_type) { + case BTC_INDIRECT_1700: + reg0 = 0x1700; + reg1 = 0x1704; + break; + case BTC_INDIRECT_7C0: + reg0 = 0x7C0; + reg1 = 0x7C4; + break; + default: + return; + } + + if (bit_mask == 0xffffffff) { + /* wait for ready bit before access 0x1700 */ + halbtcoutsrc_WaitLIndirectReg_Ready(btc); + + /* put write data */ + halbtcoutsrc_Write4Byte(btc, reg1, reg_value); + halbtcoutsrc_Write4Byte(btc, reg0, 0xc00F0000 | reg_addr); + } else { + for (i = 0; i <= 31; i++) { + if (((bit_mask >> i) & 0x1) == 0x1) { + bitpos = i; + break; + } + } + + /* read back register value before write */ + val = halbtcoutsrc_ReadLIndirectReg(btc, reg_addr); + val = (val & (~bit_mask)) | (reg_value << bitpos); + + /* wait for ready bit before access 0x1700 */ + halbtcoutsrc_WaitLIndirectReg_Ready(btc); + + halbtcoutsrc_Write4Byte(btc, reg1, val); /* put write data */ + halbtcoutsrc_Write4Byte(btc, reg0, 0xc00F0000 | reg_addr); + } +} + +u16 halbtcoutsrc_Read_scbd(void *pBtcContext, u16* score_board_val) +{ + PBTC_COEXIST btc = (PBTC_COEXIST)pBtcContext; + struct btc_coex_sta *coex_sta = &btc->coex_sta; + const struct btc_chip_para *chip_para = btc->chip_para; + + if (!chip_para->scbd_support) + return 0; + + *score_board_val = (btc->btc_read_2byte(btc, chip_para->scbd_reg)) + & 0x7fff; + coex_sta->score_board_BW = *score_board_val; + + return coex_sta->score_board_BW; +} + +u32 halbtcoutsrc_Read_scbd_32bit(void *pBtcContext, u32* score_board_val) +{ + PBTC_COEXIST btc = (PBTC_COEXIST)pBtcContext; + struct btc_coex_sta *coex_sta = &btc->coex_sta; + const struct btc_chip_para *chip_para = btc->chip_para; + + if (!chip_para->scbd_support) + return 0; + + *score_board_val = (btc->btc_read_4byte(btc, chip_para->scbd_reg)) + & 0x7fffffff; + coex_sta->score_board_BW_32bit = *score_board_val; + + return coex_sta->score_board_BW_32bit; +} + +void halbtcoutsrc_Write_scbd(void *pBtcContext, u16 bitpos, u8 state) +{ + PBTC_COEXIST btc = (PBTC_COEXIST)pBtcContext; + struct btc_coex_sta *coex_sta = &btc->coex_sta; + const struct btc_chip_para *chip_para = btc->chip_para; + u16 val = 0x2; + + if (!chip_para->scbd_support) + return; + + val = val | coex_sta->score_board_WB; + + /* for 8822b, Scoreboard[10]: 0: CQDDR off, 1: CQDDR on + * for 8822c, Scoreboard[10]: 0: CQDDR on, 1:CQDDR fix 2M + */ + if (!btc->chip_para->new_scbd10_def && (bitpos & BTC_SCBD_FIX2M)) { + if (state) + val = val & (~BTC_SCBD_FIX2M); + else + val = val | BTC_SCBD_FIX2M; + } else { + if (state) + val = val | bitpos; + else + val = val & (~bitpos); + } + + if (val != coex_sta->score_board_WB) { + coex_sta->score_board_WB = val; + val = val | 0x8000; + + btc->btc_write_2byte(btc, chip_para->scbd_reg, val); + + RTW_DBG("[BTC], write scoreboard 0x%x\n", val); + } else { + RTW_DBG("[BTC], return for nochange\n"); + } +} + +void halbtcoutsrc_Write_scbd_32bit(void *pBtcContext, u32 bitpos, u8 state) +{ + PBTC_COEXIST btc = (PBTC_COEXIST)pBtcContext; + struct btc_coex_sta *coex_sta = &btc->coex_sta; + const struct btc_chip_para *chip_para = btc->chip_para; + u32 val = 0x2; + + if (!chip_para->scbd_support) + return; + + val = val | coex_sta->score_board_WB_32bit; + + /* for 8822b, Scoreboard[10]: 0: CQDDR off, 1: CQDDR on + * for 8822c, Scoreboard[10]: 0: CQDDR on, 1:CQDDR fix 2M + */ + if (!btc->chip_para->new_scbd10_def && (bitpos & BTC_SCBD_FIX2M)) { + if (state) + val = val & (~BTC_SCBD_FIX2M); + else + val = val | BTC_SCBD_FIX2M; + } else { + if (state) + val = val | bitpos; + else + val = val & (~bitpos); + } + + if (val != coex_sta->score_board_WB_32bit) { + coex_sta->score_board_WB_32bit = val; + val = val | 0x80000000; + + btc->btc_write_4byte(btc, chip_para->scbd_reg, val); + + RTW_DBG("[BTC], write scoreboard 0x%x\n", val); + } else { + RTW_DBG("[BTC], return for nochange\n"); + } +} + +void halbtcoutsrc_SetBbReg(void *pBtcContext, u32 RegAddr, u32 BitMask, u32 Data) +{ + PBTC_COEXIST pBtCoexist; + PADAPTER padapter; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + padapter = pBtCoexist->Adapter; + + phy_set_bb_reg(padapter, RegAddr, BitMask, Data); +} + + +u32 halbtcoutsrc_GetBbReg(void *pBtcContext, u32 RegAddr, u32 BitMask) +{ + PBTC_COEXIST pBtCoexist; + PADAPTER padapter; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + padapter = pBtCoexist->Adapter; + + return phy_query_bb_reg(padapter, RegAddr, BitMask); +} + +void halbtcoutsrc_SetRfReg(void *pBtcContext, enum rf_path eRFPath, u32 RegAddr, u32 BitMask, u32 Data) +{ + PBTC_COEXIST pBtCoexist; + PADAPTER padapter; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + padapter = pBtCoexist->Adapter; + + phy_set_rf_reg(padapter, eRFPath, RegAddr, BitMask, Data); +} + +u32 halbtcoutsrc_GetRfReg(void *pBtcContext, enum rf_path eRFPath, u32 RegAddr, u32 BitMask) +{ + PBTC_COEXIST pBtCoexist; + PADAPTER padapter; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + padapter = pBtCoexist->Adapter; + + return phy_query_rf_reg(padapter, eRFPath, RegAddr, BitMask); +} + +u16 halbtcoutsrc_SetBtReg(void *pBtcContext, u8 RegType, u32 RegAddr, u32 Data) +{ + PBTC_COEXIST pBtCoexist; + u16 ret = BT_STATUS_BT_OP_SUCCESS; + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + + if (halbtcoutsrc_IsHwMailboxExist(pBtCoexist) == _TRUE) { + u8 buf[3] = {0}; + _irqL irqL; + u8 op_code; + u8 status; + + _enter_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + Data = cpu_to_le32(Data); + op_code = BT_OP_WRITE_REG_VALUE; + status = _btmpoper_cmd(pBtCoexist, op_code, 0, (u8 *)&Data, 3); + if (status != BT_STATUS_BT_OP_SUCCESS) + ret = SET_BT_MP_OPER_RET(op_code, status); + else { + buf[0] = RegType; + *(u16 *)(buf + 1) = cpu_to_le16((u16)RegAddr); + op_code = BT_OP_WRITE_REG_ADDR; + status = _btmpoper_cmd(pBtCoexist, op_code, 0, buf, 3); + if (status != BT_STATUS_BT_OP_SUCCESS) + ret = SET_BT_MP_OPER_RET(op_code, status); + } + + _exit_critical_mutex(&GLBtcBtMpOperLock, &irqL); + } else + ret = BT_STATUS_NOT_IMPLEMENT; + + return ret; +} + +u8 halbtcoutsrc_SetBtAntDetection(void *pBtcContext, u8 txTime, u8 btChnl) +{ + /* Always return _FALSE since we don't implement this yet */ +#if 0 + PBTC_COEXIST pBtCoexist = (PBTC_COEXIST)pBtcContext; + PADAPTER Adapter = pBtCoexist->Adapter; + u8 btCanTx = 0; + BOOLEAN bStatus = FALSE; + + bStatus = NDBG_SetBtAntDetection(Adapter, txTime, btChnl, &btCanTx); + if (bStatus && btCanTx) + return _TRUE; + else + return _FALSE; +#else + return _FALSE; +#endif +} + +u8 halbtcoutsrc_SetBtTRXMASK(void *pBtcContext, u8 bt_trx_mask) +{ + PBTC_COEXIST pBtCoexist; + u8 bStatus = _FALSE; + u8 btCanTx = 0; + u16 ret = BT_STATUS_BT_OP_SUCCESS; + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + + if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter) || IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter) + || IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + + if (halbtcoutsrc_IsHwMailboxExist(pBtCoexist) == _TRUE) { + u8 buf[3] = {0}; + u8 len = 0; + _irqL irqL; + u8 op_code; + u8 status; + + if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + buf[0] = bt_trx_mask; + len = 1; + } else { + buf[0] = (bt_trx_mask & 0x80) >> 7; + buf[1] = bt_trx_mask & 0x7f; + len = 2; + } + + _enter_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + op_code = BT_OP_SET_BT_TRX_MASK; + status = _btmpoper_cmd(pBtCoexist, op_code, 0, buf, len); + if (status != BT_STATUS_BT_OP_SUCCESS) + ret = SET_BT_MP_OPER_RET(op_code, status); + + _exit_critical_mutex(&GLBtcBtMpOperLock, &irqL); + } else + ret = BT_STATUS_NOT_IMPLEMENT; + } + + if (ret == BT_STATUS_BT_OP_SUCCESS) + return _TRUE; + else + return _FALSE; +} + +u16 halbtcoutsrc_GetBtReg_with_status(void *pBtcContext, u8 RegType, u32 RegAddr, u32 *data) +{ + PBTC_COEXIST pBtCoexist; + u16 ret = BT_STATUS_BT_OP_SUCCESS; + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + + if (halbtcoutsrc_IsHwMailboxExist(pBtCoexist) == _TRUE) { + u8 buf[3] = {0}; + _irqL irqL; + u8 op_code; + u8 status; + + buf[0] = RegType; + *(u16 *)(buf + 1) = cpu_to_le16((u16)RegAddr); + + _enter_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + op_code = BT_OP_READ_REG; + status = _btmpoper_cmd(pBtCoexist, op_code, 0, buf, 3); + if (status == BT_STATUS_BT_OP_SUCCESS) + *data = le16_to_cpu(*(u16 *)GLBtcBtMpRptRsp); + else + ret = SET_BT_MP_OPER_RET(op_code, status); + + _exit_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + } else + ret = BT_STATUS_NOT_IMPLEMENT; + + return ret; +} + +u32 halbtcoutsrc_GetBtReg(void *pBtcContext, u8 RegType, u32 RegAddr) +{ + u32 regVal; + + return (BT_STATUS_BT_OP_SUCCESS == halbtcoutsrc_GetBtReg_with_status(pBtcContext, RegType, RegAddr, ®Val)) ? regVal : 0xffffffff; +} + +u16 halbtcoutsrc_setbttestmode(void *pBtcContext, u8 Type) +{ + PBTC_COEXIST pBtCoexist; + u16 ret = BT_STATUS_BT_OP_SUCCESS; + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + + if (halbtcoutsrc_IsHwMailboxExist(pBtCoexist) == _TRUE) { + _irqL irqL; + u8 op_code; + u8 status; + + _enter_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + Type = cpu_to_le32(Type); + op_code = BT_OP_SET_BT_TEST_MODE_VAL; + status = _btmpoper_cmd(pBtCoexist, op_code, 0, (u8 *)&Type, 3); + if (status != BT_STATUS_BT_OP_SUCCESS) + ret = SET_BT_MP_OPER_RET(op_code, status); + + _exit_critical_mutex(&GLBtcBtMpOperLock, &irqL); + } else + ret = BT_STATUS_NOT_IMPLEMENT; + + return ret; + +} + + +void halbtcoutsrc_FillH2cCmd(void *pBtcContext, u8 elementId, u32 cmdLen, u8 *pCmdBuffer) +{ + PBTC_COEXIST pBtCoexist; + PADAPTER padapter; + s32 ret = 0; + + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + padapter = pBtCoexist->Adapter; + + ret = rtw_hal_fill_h2c_cmd(padapter, elementId, cmdLen, pCmdBuffer); + +#ifdef CONFIG_RTL8192F + if (ret == _SUCCESS) { + switch (elementId) { + case H2C_BT_INFO: + case H2C_BT_IGNORE_WLANACT: + case H2C_WL_OPMODE: + case H2C_BT_MP_OPER: + case H2C_BT_CONTROL: + rtw_msleep_os(20); + break; + } + } +#endif +} + +static void halbtcoutsrc_coex_offload_init(void) +{ + u8 i; + + gl_coex_offload.h2c_req_num = 0; + gl_coex_offload.cnt_h2c_sent = 0; + gl_coex_offload.cnt_c2h_ack = 0; + gl_coex_offload.cnt_c2h_ind = 0; + + for (i = 0; i < COL_MAX_H2C_REQ_NUM; i++) + init_completion(&gl_coex_offload.c2h_event[i]); +} + +static COL_H2C_STATUS halbtcoutsrc_send_h2c(PADAPTER Adapter, PCOL_H2C pcol_h2c, u16 h2c_cmd_len) +{ + COL_H2C_STATUS h2c_status = COL_STATUS_C2H_OK; + u8 i; + +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)) + reinit_completion(&gl_coex_offload.c2h_event[pcol_h2c->req_num]); /* set event to un signaled state */ +#else + INIT_COMPLETION(gl_coex_offload.c2h_event[pcol_h2c->req_num]); +#endif + + if (TRUE) { +#if 0 /*(USE_HAL_MAC_API == 1) */ + if (RT_STATUS_SUCCESS == HAL_MAC_Send_BT_COEX(&GET_HAL_MAC_INFO(Adapter), (u8 *)(pcol_h2c), (u32)h2c_cmd_len, 1)) { + if (!wait_for_completion_timeout(&gl_coex_offload.c2h_event[pcol_h2c->req_num], 20)) { + h2c_status = COL_STATUS_H2C_TIMTOUT; + } + } else { + h2c_status = COL_STATUS_H2C_HALMAC_FAIL; + } +#endif + } + + return h2c_status; +} + +static COL_H2C_STATUS halbtcoutsrc_check_c2h_ack(PADAPTER Adapter, PCOL_SINGLE_H2C_RECORD pH2cRecord) +{ + COL_H2C_STATUS c2h_status = COL_STATUS_C2H_OK; + PCOL_H2C p_h2c_cmd = (PCOL_H2C)&pH2cRecord->h2c_buf[0]; + u8 req_num = p_h2c_cmd->req_num; + PCOL_C2H_ACK p_c2h_ack = (PCOL_C2H_ACK)&gl_coex_offload.c2h_ack_buf[req_num]; + + + if ((COL_C2H_ACK_HDR_LEN + p_c2h_ack->ret_len) > gl_coex_offload.c2h_ack_len[req_num]) { + c2h_status = COL_STATUS_COEX_DATA_OVERFLOW; + return c2h_status; + } + /* else */ + { + _rtw_memmove(&pH2cRecord->c2h_ack_buf[0], &gl_coex_offload.c2h_ack_buf[req_num], gl_coex_offload.c2h_ack_len[req_num]); + pH2cRecord->c2h_ack_len = gl_coex_offload.c2h_ack_len[req_num]; + } + + + if (p_c2h_ack->req_num != p_h2c_cmd->req_num) { + c2h_status = COL_STATUS_C2H_REQ_NUM_MISMATCH; + } else if (p_c2h_ack->opcode_ver != p_h2c_cmd->opcode_ver) { + c2h_status = COL_STATUS_C2H_OPCODE_VER_MISMATCH; + } else { + c2h_status = p_c2h_ack->status; + } + + return c2h_status; +} + +COL_H2C_STATUS halbtcoutsrc_CoexH2cProcess(void *pBtCoexist, + u8 opcode, u8 opcode_ver, u8 *ph2c_par, u8 h2c_par_len) +{ + PADAPTER Adapter = ((struct btc_coexist *)pBtCoexist)->Adapter; + u8 H2C_Parameter[BTC_TMP_BUF_SHORT] = {0}; + PCOL_H2C pcol_h2c = (PCOL_H2C)&H2C_Parameter[0]; + u16 paraLen = 0; + COL_H2C_STATUS h2c_status = COL_STATUS_C2H_OK, c2h_status = COL_STATUS_C2H_OK; + COL_H2C_STATUS ret_status = COL_STATUS_C2H_OK; + u16 i, col_h2c_len = 0; + + pcol_h2c->opcode = opcode; + pcol_h2c->opcode_ver = opcode_ver; + pcol_h2c->req_num = gl_coex_offload.h2c_req_num; + gl_coex_offload.h2c_req_num++; + gl_coex_offload.h2c_req_num %= 16; + + _rtw_memmove(&pcol_h2c->buf[0], ph2c_par, h2c_par_len); + + + col_h2c_len = h2c_par_len + 2; /* 2=sizeof(OPCode, OPCode_version and Request number) */ + BT_PrintData(Adapter, "[COL], H2C cmd: ", col_h2c_len, H2C_Parameter); + + gl_coex_offload.cnt_h2c_sent++; + + gl_coex_offload.h2c_record[opcode].count++; + gl_coex_offload.h2c_record[opcode].h2c_len = col_h2c_len; + _rtw_memmove((void *)&gl_coex_offload.h2c_record[opcode].h2c_buf[0], (void *)pcol_h2c, col_h2c_len); + + h2c_status = halbtcoutsrc_send_h2c(Adapter, pcol_h2c, col_h2c_len); + + gl_coex_offload.h2c_record[opcode].c2h_ack_len = 0; + + if (COL_STATUS_C2H_OK == h2c_status) { + /* if reach here, it means H2C get the correct c2h response, */ + c2h_status = halbtcoutsrc_check_c2h_ack(Adapter, &gl_coex_offload.h2c_record[opcode]); + ret_status = c2h_status; + } else { + /* check h2c status error, return error status code to upper layer. */ + ret_status = h2c_status; + } + gl_coex_offload.h2c_record[opcode].status[ret_status]++; + gl_coex_offload.status[ret_status]++; + + return ret_status; +} + +u8 halbtcoutsrc_GetAntDetValFromBt(void *pBtcContext) +{ + /* Always return 0 since we don't implement this yet */ +#if 0 + struct btc_coexist *pBtCoexist = (struct btc_coexist *)pBtcContext; + PADAPTER Adapter = pBtCoexist->Adapter; + u8 AntDetVal = 0x0; + u8 opcodeVer = 1; + BOOLEAN status = false; + + status = NDBG_GetAntDetValFromBt(Adapter, opcodeVer, &AntDetVal); + + RT_TRACE(COMP_DBG, DBG_LOUD, ("$$$ halbtcoutsrc_GetAntDetValFromBt(): status = %d, feature = %x\n", status, AntDetVal)); + + return AntDetVal; +#else + return 0; +#endif +} + +u8 halbtcoutsrc_GetBleScanTypeFromBt(void *pBtcContext) +{ + PBTC_COEXIST pBtCoexist; + u32 ret = BT_STATUS_BT_OP_SUCCESS; + u8 data = 0; + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + + if (halbtcoutsrc_IsHwMailboxExist(pBtCoexist) == _TRUE) { + u8 buf[3] = {0}; + _irqL irqL; + u8 op_code; + u8 status; + + + _enter_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + op_code = BT_OP_GET_BT_BLE_SCAN_TYPE; + status = _btmpoper_cmd(pBtCoexist, op_code, 0, buf, 0); + if (status == BT_STATUS_BT_OP_SUCCESS) + data = *(u8 *)GLBtcBtMpRptRsp; + else + ret = SET_BT_MP_OPER_RET(op_code, status); + + _exit_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + } else + ret = BT_STATUS_NOT_IMPLEMENT; + + return data; +} + +u32 halbtcoutsrc_GetBleScanParaFromBt(void *pBtcContext, u8 scanType) +{ + PBTC_COEXIST pBtCoexist; + u32 ret = BT_STATUS_BT_OP_SUCCESS; + u32 data = 0; + + pBtCoexist = (PBTC_COEXIST)pBtcContext; + + if (halbtcoutsrc_IsHwMailboxExist(pBtCoexist) == _TRUE) { + u8 buf[3] = {0}; + _irqL irqL; + u8 op_code; + u8 status; + + buf[0] = scanType; + + _enter_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + op_code = BT_OP_GET_BT_BLE_SCAN_PARA; + status = _btmpoper_cmd(pBtCoexist, op_code, 0, buf, 1); + if (status == BT_STATUS_BT_OP_SUCCESS) + data = le32_to_cpu(*(u32 *)GLBtcBtMpRptRsp); + else + ret = SET_BT_MP_OPER_RET(op_code, status); + + _exit_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + } else + ret = BT_STATUS_NOT_IMPLEMENT; + + return data; +} + +u8 halbtcoutsrc_GetBtAFHMapFromBt(void *pBtcContext, u8 mapType, u8 *afhMap) +{ + struct btc_coexist *pBtCoexist = (struct btc_coexist *)pBtcContext; + u8 buf[2] = {0}; + _irqL irqL; + u8 op_code; + u32 *AfhMapL = (u32 *)&(afhMap[0]); + u32 *AfhMapM = (u32 *)&(afhMap[4]); + u16 *AfhMapH = (u16 *)&(afhMap[8]); + u8 status; + u32 ret = BT_STATUS_BT_OP_SUCCESS; + + if (halbtcoutsrc_IsHwMailboxExist(pBtCoexist) == _FALSE) + return _FALSE; + + buf[0] = 0; + buf[1] = mapType; + + _enter_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + op_code = BT_LO_OP_GET_AFH_MAP_L; + status = _btmpoper_cmd(pBtCoexist, op_code, 0, buf, 0); + if (status == BT_STATUS_BT_OP_SUCCESS) + *AfhMapL = le32_to_cpu(*(u32 *)GLBtcBtMpRptRsp); + else { + ret = SET_BT_MP_OPER_RET(op_code, status); + goto exit; + } + + op_code = BT_LO_OP_GET_AFH_MAP_M; + status = _btmpoper_cmd(pBtCoexist, op_code, 0, buf, 0); + if (status == BT_STATUS_BT_OP_SUCCESS) + *AfhMapM = le32_to_cpu(*(u32 *)GLBtcBtMpRptRsp); + else { + ret = SET_BT_MP_OPER_RET(op_code, status); + goto exit; + } + + op_code = BT_LO_OP_GET_AFH_MAP_H; + status = _btmpoper_cmd(pBtCoexist, op_code, 0, buf, 0); + if (status == BT_STATUS_BT_OP_SUCCESS) + *AfhMapH = le16_to_cpu(*(u16 *)GLBtcBtMpRptRsp); + else { + ret = SET_BT_MP_OPER_RET(op_code, status); + goto exit; + } + +exit: + + _exit_critical_mutex(&GLBtcBtMpOperLock, &irqL); + + return (ret == BT_STATUS_BT_OP_SUCCESS) ? _TRUE : _FALSE; +} + +u8 halbtcoutsrc_SetTimer(void *pBtcContext, u32 type, u32 val) +{ + struct btc_coexist *pBtCoexist=(struct btc_coexist *)pBtcContext; + + if (type >= BTC_TIMER_MAX) + return _FALSE; + + pBtCoexist->coex_sta.cnt_timer[type] = val; + + RTW_DBG("[BTC], Set Timer: type = %d, val = %d\n", type, val); + + return _TRUE; +} + +u32 halbtcoutsrc_SetAtomic (void *btc_ctx, u32 *target, u32 val) +{ + *target = val; + return _SUCCESS; +} + +void halbtcoutsrc_phydm_modify_AntDiv_HwSw(void *pBtcContext, u8 is_hw) +{ + /* empty function since we don't need it */ +} + +void halbtcoutsrc_phydm_modify_RA_PCR_threshold(void *pBtcContext, u8 RA_offset_direction, u8 RA_threshold_offset) +{ + struct btc_coexist *pBtCoexist = (struct btc_coexist *)pBtcContext; + +/* switch to #if 0 in case the phydm version does not provide the function */ +#if 1 + phydm_modify_RA_PCR_threshold(pBtCoexist->odm_priv, RA_offset_direction, RA_threshold_offset); +#endif +} + +u32 halbtcoutsrc_phydm_query_PHY_counter(void *pBtcContext, u8 info_type) +{ + struct btc_coexist *pBtCoexist = (struct btc_coexist *)pBtcContext; + +/* switch to #if 0 in case the phydm version does not provide the function */ +#if 1 + return phydm_cmn_info_query((struct dm_struct *)pBtCoexist->odm_priv, (enum phydm_info_query)info_type); +#else + return 0; +#endif +} + +void halbtcoutsrc_reduce_wl_tx_power(void *pBtcContext, s8 tx_power) +{ + struct btc_coexist *pBtCoexist = (struct btc_coexist *)pBtcContext; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA((PADAPTER)pBtCoexist->Adapter); + + /* The reduction of wl tx pwr should be processed inside the set tx pwr lvl function */ + if (IS_HARDWARE_TYPE_8822C(pBtCoexist->Adapter) || IS_HARDWARE_TYPE_8723F(pBtCoexist->Adapter)) + rtw_hal_set_tx_power_level(pBtCoexist->Adapter, pHalData->current_channel); +} + +#if 0 +static void BT_CoexOffloadRecordErrC2hAck(PADAPTER Adapter) +{ + PADAPTER pDefaultAdapter = GetDefaultAdapter(Adapter); + + if (pDefaultAdapter != Adapter) + return; + + if (!hal_btcoex_IsBtExist(Adapter)) + return; + + gl_coex_offload.cnt_c2h_ack++; + + gl_coex_offload.status[COL_STATUS_INVALID_C2H_LEN]++; +} + +static void BT_CoexOffloadC2hAckCheck(PADAPTER Adapter, u8 *tmpBuf, u8 length) +{ + PADAPTER pDefaultAdapter = GetDefaultAdapter(Adapter); + PCOL_C2H_ACK p_c2h_ack = NULL; + u8 req_num = 0xff; + + if (pDefaultAdapter != Adapter) + return; + + if (!hal_btcoex_IsBtExist(Adapter)) + return; + + gl_coex_offload.cnt_c2h_ack++; + + if (length < COL_C2H_ACK_HDR_LEN) { /* c2h ack length must >= 3 (status, opcode_ver, req_num and ret_len) */ + gl_coex_offload.status[COL_STATUS_INVALID_C2H_LEN]++; + } else { + BT_PrintData(Adapter, "[COL], c2h ack:", length, tmpBuf); + + p_c2h_ack = (PCOL_C2H_ACK)tmpBuf; + req_num = p_c2h_ack->req_num; + + _rtw_memmove(&gl_coex_offload.c2h_ack_buf[req_num][0], tmpBuf, length); + gl_coex_offload.c2h_ack_len[req_num] = length; + + complete(&gl_coex_offload.c2h_event[req_num]); + } +} + +static void BT_CoexOffloadC2hIndCheck(PADAPTER Adapter, u8 *tmpBuf, u8 length) +{ + PADAPTER pDefaultAdapter = GetDefaultAdapter(Adapter); + PCOL_C2H_IND p_c2h_ind = NULL; + u8 ind_type = 0, ind_version = 0, ind_length = 0; + + if (pDefaultAdapter != Adapter) + return; + + if (!hal_btcoex_IsBtExist(Adapter)) + return; + + gl_coex_offload.cnt_c2h_ind++; + + if (length < COL_C2H_IND_HDR_LEN) { /* c2h indication length must >= 3 (type, version and length) */ + gl_coex_offload.c2h_ind_status[COL_STATUS_INVALID_C2H_LEN]++; + } else { + BT_PrintData(Adapter, "[COL], c2h indication:", length, tmpBuf); + + p_c2h_ind = (PCOL_C2H_IND)tmpBuf; + ind_type = p_c2h_ind->type; + ind_version = p_c2h_ind->version; + ind_length = p_c2h_ind->length; + + _rtw_memmove(&gl_coex_offload.c2h_ind_buf[0], tmpBuf, length); + gl_coex_offload.c2h_ind_len = length; + + /* log */ + gl_coex_offload.c2h_ind_record[ind_type].count++; + gl_coex_offload.c2h_ind_record[ind_type].status[COL_STATUS_C2H_OK]++; + _rtw_memmove(&gl_coex_offload.c2h_ind_record[ind_type].ind_buf[0], tmpBuf, length); + gl_coex_offload.c2h_ind_record[ind_type].ind_len = length; + + gl_coex_offload.c2h_ind_status[COL_STATUS_C2H_OK]++; + /*TODO: need to check c2h indication length*/ + /* TODO: Notification */ + } +} + +void BT_CoexOffloadC2hCheck(PADAPTER Adapter, u8 *Buffer, u8 Length) +{ +#if 0 /*(USE_HAL_MAC_API == 1)*/ + u8 c2hSubCmdId = 0, c2hAckLen = 0, h2cCmdId = 0, h2cSubCmdId = 0, c2hIndLen = 0; + + BT_PrintData(Adapter, "[COL], c2h packet:", Length - 2, Buffer + 2); + c2hSubCmdId = (u8)C2H_HDR_GET_C2H_SUB_CMD_ID(Buffer); + + if (c2hSubCmdId == C2H_SUB_CMD_ID_H2C_ACK_HDR || + c2hSubCmdId == C2H_SUB_CMD_ID_BT_COEX_INFO) { + if (c2hSubCmdId == C2H_SUB_CMD_ID_H2C_ACK_HDR) { + /* coex c2h ack */ + h2cCmdId = (u8)H2C_ACK_HDR_GET_H2C_CMD_ID(Buffer); + h2cSubCmdId = (u8)H2C_ACK_HDR_GET_H2C_SUB_CMD_ID(Buffer); + if (h2cCmdId == 0xff && h2cSubCmdId == 0x60) { + c2hAckLen = (u8)C2H_HDR_GET_LEN(Buffer); + if (c2hAckLen >= 8) + BT_CoexOffloadC2hAckCheck(Adapter, &Buffer[12], (u8)(c2hAckLen - 8)); + else + BT_CoexOffloadRecordErrC2hAck(Adapter); + } + } else if (c2hSubCmdId == C2H_SUB_CMD_ID_BT_COEX_INFO) { + /* coex c2h indication */ + c2hIndLen = (u8)C2H_HDR_GET_LEN(Buffer); + BT_CoexOffloadC2hIndCheck(Adapter, &Buffer[4], (u8)c2hIndLen); + } + } +#endif +} +#endif + +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) +static void halbtcoutsrc_wl_noisy_detect(struct btc_coexist *btc) +{ + struct btc_coex_sta *coex_sta = &btc->coex_sta; + u32 cnt_cck, ok_11b, err_11b; + + ok_11b = btc->btc_phydm_query_PHY_counter(btc, PHYDM_INFO_CRC32_OK_CCK); + err_11b = btc->btc_phydm_query_PHY_counter(btc, + PHYDM_INFO_CRC32_ERROR_CCK); + + /* WiFi environment noisy identification */ + cnt_cck = ok_11b + err_11b; + + if (!coex_sta->wl_gl_busy && !coex_sta->wl_cck_lock) { + if (cnt_cck > 250) { + if (coex_sta->cnt_wl[BTC_CNT_WL_NOISY2] < 5) + coex_sta->cnt_wl[BTC_CNT_WL_NOISY2]++; + + if (coex_sta->cnt_wl[BTC_CNT_WL_NOISY2] == 5) { + coex_sta->cnt_wl[BTC_CNT_WL_NOISY0] = 0; + coex_sta->cnt_wl[BTC_CNT_WL_NOISY1] = 0; + } + } else if (cnt_cck < 100) { + if (coex_sta->cnt_wl[BTC_CNT_WL_NOISY0] < 5) + coex_sta->cnt_wl[BTC_CNT_WL_NOISY0]++; + + if (coex_sta->cnt_wl[BTC_CNT_WL_NOISY0] == 5) { + coex_sta->cnt_wl[BTC_CNT_WL_NOISY1] = 0; + coex_sta->cnt_wl[BTC_CNT_WL_NOISY2] = 0; + } + } else { + if (coex_sta->cnt_wl[BTC_CNT_WL_NOISY1] < 5) + coex_sta->cnt_wl[BTC_CNT_WL_NOISY1]++; + + if (coex_sta->cnt_wl[BTC_CNT_WL_NOISY1] == 5) { + coex_sta->cnt_wl[BTC_CNT_WL_NOISY0] = 0; + coex_sta->cnt_wl[BTC_CNT_WL_NOISY2] = 0; + } + } + + if (coex_sta->cnt_wl[BTC_CNT_WL_NOISY2] == 5) + coex_sta->wl_noisy_level = 2; + else if (coex_sta->cnt_wl[BTC_CNT_WL_NOISY1] == 5) + coex_sta->wl_noisy_level = 1; + else + coex_sta->wl_noisy_level = 0; + + RTW_DBG("[BTC], wl_noisy_level = %d\n", + coex_sta->wl_noisy_level); + } +} + +static boolean halbtcoutsrc_btc_monitor_bt_ctr(struct btc_coexist *btc) +{ + struct btc_coex_sta *coex_sta = &btc->coex_sta; + struct btc_coex_dm *coex_dm = &btc->coex_dm; + u32 cnt_bt_hi_pri, cnt_bt_lo_pri, cnt_bt_all; + boolean is_run_coex = _FALSE; + + cnt_bt_hi_pri = btc->btc_read_4byte(btc, REG_BT_ACT_STATISTICS); + coex_sta->hi_pri_tx = cnt_bt_hi_pri & MASKLWORD; + coex_sta->hi_pri_rx = (cnt_bt_hi_pri & MASKHWORD) >> 16; + + cnt_bt_lo_pri = btc->btc_read_4byte(btc, REG_BT_ACT_STATISTICS_1); + coex_sta->lo_pri_tx = cnt_bt_lo_pri & MASKLWORD; + coex_sta->lo_pri_rx = (cnt_bt_lo_pri & MASKHWORD) >> 16; + + RTW_DBG("[BTC], Hi-Pri Rx/Tx: %d/%d, Lo-Pri Rx/Tx: %d/%d\n", + coex_sta->hi_pri_rx, coex_sta->hi_pri_tx, + coex_sta->lo_pri_rx, coex_sta->lo_pri_tx); + + /* reset counter */ + btc->btc_write_1byte(btc, 0x76e, 0xc); + + if (coex_sta->wl_under_lps || coex_sta->wl_under_ips || + (coex_sta->hi_pri_rx == 65535 && coex_sta->hi_pri_tx == 65535 && + coex_sta->lo_pri_rx == 65535 && coex_sta->lo_pri_tx == 65535)) + coex_sta->bt_ctr_ok = _FALSE; + else + coex_sta->bt_ctr_ok = _TRUE; + + if (!coex_sta->bt_ctr_ok) + return _FALSE; + + if (coex_sta->hi_pri_rx == 0 && coex_sta->hi_pri_tx == 0 && + coex_sta->lo_pri_rx == 0 && coex_sta->lo_pri_tx == 0) { + coex_sta->cnt_bt[BTC_CNT_BT_DISABLE]++; + + if (coex_sta->cnt_bt[BTC_CNT_BT_DISABLE] > 2) + coex_sta->cnt_bt[BTC_CNT_BT_DISABLE] = 2; + } else { + coex_sta->cnt_bt[BTC_CNT_BT_DISABLE] = 0; + } + + cnt_bt_all = coex_sta->hi_pri_rx + coex_sta->hi_pri_tx + + coex_sta->lo_pri_rx + coex_sta->lo_pri_tx; + + if ((coex_sta->cnt_bt[BTC_CNT_BT_TRX] > (cnt_bt_all + 50) || + cnt_bt_all > (coex_sta->cnt_bt[BTC_CNT_BT_TRX] + 50)) && + coex_dm->bt_status == BTC_BTSTATUS_NCON_IDLE) + is_run_coex = _TRUE; + + coex_sta->cnt_bt[BTC_CNT_BT_TRX] = cnt_bt_all; + + return is_run_coex; +} +#endif + +/* ************************************ + * Extern functions called by other module + * ************************************ */ +u8 EXhalbtcoutsrc_BindBtCoexWithAdapter(void *padapter) +{ + PBTC_COEXIST pBtCoexist = &GLBtCoexist; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA((PADAPTER)padapter); + + if (pBtCoexist->bBinded) + return _FALSE; + else + pBtCoexist->bBinded = _TRUE; + + pBtCoexist->statistics.cnt_bind++; + + pBtCoexist->Adapter = padapter; + pBtCoexist->odm_priv = (void *)&(pHalData->odmpriv); + + pBtCoexist->stack_info.profile_notified = _FALSE; + + pBtCoexist->bt_info.bt_ctrl_agg_buf_size = _FALSE; + pBtCoexist->bt_info.agg_buf_size = 5; + + pBtCoexist->bt_info.increase_scan_dev_num = _FALSE; + pBtCoexist->bt_info.miracast_plus_bt = _FALSE; + + /* for btc common architecture, inform chip type to coex. mechanism */ + if(IS_HARDWARE_TYPE_8822C(padapter)) { +#ifdef CONFIG_RTL8822C + pBtCoexist->chip_type = BTC_CHIP_RTL8822C; + pBtCoexist->chip_para = &btc_chip_para_8822c; +#endif + } +#ifdef CONFIG_RTL8192F + else if (IS_HARDWARE_TYPE_8192F(padapter)) { + pBtCoexist->chip_type = BTC_CHIP_RTL8725A; + pBtCoexist->chip_para = &btc_chip_para_8192f; + } +#endif +#ifdef PLATFORM_LINUX +#ifdef CONFIG_RTL8723F + else if (IS_HARDWARE_TYPE_8723F(padapter)) { + pBtCoexist->chip_type = BTC_CHIP_RTL8723F; + pBtCoexist->chip_para = &btc_chip_para_8723f; + } +#endif +#endif + else { + pBtCoexist->chip_type = BTC_CHIP_UNDEF; + pBtCoexist->chip_para = NULL; + } + + return _TRUE; +} + +void EXhalbtcoutsrc_AntInfoSetting(void *padapter) +{ + PBTC_COEXIST pBtCoexist = &GLBtCoexist; + u8 antNum = 1, singleAntPath = 0; + + antNum = rtw_btcoex_get_pg_ant_num((PADAPTER)padapter); + EXhalbtcoutsrc_SetAntNum(BT_COEX_ANT_TYPE_PG, antNum); + + if (antNum == 1) { + singleAntPath = rtw_btcoex_get_pg_single_ant_path((PADAPTER)padapter); + EXhalbtcoutsrc_SetSingleAntPath(singleAntPath); + } + + pBtCoexist->board_info.customerID = RT_CID_DEFAULT; + pBtCoexist->board_info.customer_id = RT_CID_DEFAULT; + + /* set default antenna position to main port */ + pBtCoexist->board_info.btdm_ant_pos = BTC_ANTENNA_AT_MAIN_PORT; + + pBtCoexist->board_info.btdm_ant_det_finish = _FALSE; + pBtCoexist->board_info.btdm_ant_num_by_ant_det = 1; + + pBtCoexist->board_info.tfbga_package = rtw_btcoex_is_tfbga_package_type((PADAPTER)padapter); + + pBtCoexist->board_info.rfe_type = rtw_btcoex_get_pg_rfe_type((PADAPTER)padapter); + + pBtCoexist->board_info.ant_div_cfg = rtw_btcoex_get_ant_div_cfg((PADAPTER)padapter); + + pBtCoexist->board_info.ant_distance = 10; +} + +u8 EXhalbtcoutsrc_InitlizeVariables(void *padapter) +{ + PBTC_COEXIST pBtCoexist = &GLBtCoexist; + + /* pBtCoexist->statistics.cntBind++; */ + + halbtcoutsrc_DbgInit(); + + halbtcoutsrc_coex_offload_init(); + +#ifdef CONFIG_PCI_HCI + pBtCoexist->chip_interface = BTC_INTF_PCI; +#elif defined(CONFIG_USB_HCI) + pBtCoexist->chip_interface = BTC_INTF_USB; +#elif defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) + pBtCoexist->chip_interface = BTC_INTF_SDIO; +#else + pBtCoexist->chip_interface = BTC_INTF_UNKNOWN; +#endif + + EXhalbtcoutsrc_BindBtCoexWithAdapter(padapter); + + pBtCoexist->btc_read_1byte = halbtcoutsrc_Read1Byte; + pBtCoexist->btc_write_1byte = halbtcoutsrc_Write1Byte; + pBtCoexist->btc_write_1byte_bitmask = halbtcoutsrc_BitMaskWrite1Byte; + pBtCoexist->btc_read_2byte = halbtcoutsrc_Read2Byte; + pBtCoexist->btc_write_2byte = halbtcoutsrc_Write2Byte; + pBtCoexist->btc_read_4byte = halbtcoutsrc_Read4Byte; + pBtCoexist->btc_write_4byte = halbtcoutsrc_Write4Byte; + pBtCoexist->btc_write_local_reg_1byte = halbtcoutsrc_WriteLocalReg1Byte; + + pBtCoexist->btc_read_linderct = halbtcoutsrc_ReadLIndirectReg; + pBtCoexist->btc_write_linderct = halbtcoutsrc_WriteLIndirectReg; + + pBtCoexist->btc_read_scbd = halbtcoutsrc_Read_scbd; + pBtCoexist->btc_read_scbd_32bit = halbtcoutsrc_Read_scbd_32bit; + pBtCoexist->btc_write_scbd = halbtcoutsrc_Write_scbd; + pBtCoexist->btc_write_scbd_32bit = halbtcoutsrc_Write_scbd_32bit; + + pBtCoexist->btc_set_bb_reg = halbtcoutsrc_SetBbReg; + pBtCoexist->btc_get_bb_reg = halbtcoutsrc_GetBbReg; + + pBtCoexist->btc_set_rf_reg = halbtcoutsrc_SetRfReg; + pBtCoexist->btc_get_rf_reg = halbtcoutsrc_GetRfReg; + + pBtCoexist->btc_fill_h2c = halbtcoutsrc_FillH2cCmd; + pBtCoexist->btc_disp_dbg_msg = halbtcoutsrc_DisplayDbgMsg; + + pBtCoexist->btc_get = halbtcoutsrc_Get; + pBtCoexist->btc_set = halbtcoutsrc_Set; + pBtCoexist->btc_get_bt_reg = halbtcoutsrc_GetBtReg; + pBtCoexist->btc_set_bt_reg = halbtcoutsrc_SetBtReg; + pBtCoexist->btc_set_bt_ant_detection = halbtcoutsrc_SetBtAntDetection; + pBtCoexist->btc_set_bt_trx_mask = halbtcoutsrc_SetBtTRXMASK; + pBtCoexist->btc_coex_h2c_process = halbtcoutsrc_CoexH2cProcess; + pBtCoexist->btc_get_bt_coex_supported_feature = halbtcoutsrc_GetBtCoexSupportedFeature; + pBtCoexist->btc_get_bt_coex_supported_version= halbtcoutsrc_GetBtCoexSupportedVersion; + pBtCoexist->btc_get_ant_det_val_from_bt = halbtcoutsrc_GetAntDetValFromBt; + pBtCoexist->btc_get_ble_scan_type_from_bt = halbtcoutsrc_GetBleScanTypeFromBt; + pBtCoexist->btc_get_ble_scan_para_from_bt = halbtcoutsrc_GetBleScanParaFromBt; + pBtCoexist->btc_get_bt_afh_map_from_bt = halbtcoutsrc_GetBtAFHMapFromBt; + pBtCoexist->btc_get_bt_phydm_version = halbtcoutsrc_GetPhydmVersion; + pBtCoexist->btc_set_timer = halbtcoutsrc_SetTimer; + pBtCoexist->btc_set_atomic= halbtcoutsrc_SetAtomic; + pBtCoexist->btc_phydm_modify_RA_PCR_threshold = halbtcoutsrc_phydm_modify_RA_PCR_threshold; + pBtCoexist->btc_phydm_query_PHY_counter = halbtcoutsrc_phydm_query_PHY_counter; + pBtCoexist->btc_reduce_wl_tx_power = halbtcoutsrc_reduce_wl_tx_power; + pBtCoexist->btc_phydm_modify_antdiv_hwsw = halbtcoutsrc_phydm_modify_AntDiv_HwSw; + + pBtCoexist->cli_buf = &GLBtcDbgBuf[0]; + + GLBtcWiFiInScanState = _FALSE; + + GLBtcWiFiInIQKState = _FALSE; + + GLBtcWiFiInIPS = _FALSE; + + GLBtcWiFiInLPS = _FALSE; + + GLBtcBtCoexAliveRegistered = _FALSE; + + /* BT Control H2C/C2H*/ + GLBtcBtMpOperSeq = 0; + _rtw_mutex_init(&GLBtcBtMpOperLock); + rtw_init_timer(&GLBtcBtMpOperTimer, padapter, _btmpoper_timer_hdl, pBtCoexist); + _rtw_init_sema(&GLBtcBtMpRptSema, 0); + GLBtcBtMpRptSeq = 0; + GLBtcBtMpRptStatus = 0; + _rtw_memset(GLBtcBtMpRptRsp, 0, C2H_MAX_SIZE); + GLBtcBtMpRptRspSize = 0; + GLBtcBtMpRptWait = _FALSE; + GLBtcBtMpRptWiFiOK = _FALSE; + GLBtcBtMpRptBTOK = _FALSE; + + return _TRUE; +} + +void EXhalbtcoutsrc_PowerOnSetting(PBTC_COEXIST pBtCoexist) +{ + HAL_DATA_TYPE *pHalData = NULL; + + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + pHalData = GET_HAL_DATA((PADAPTER)pBtCoexist->Adapter); + +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_power_on_setting(pBtCoexist); + +#else + if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8723B + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_power_on_setting(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_power_on_setting(pBtCoexist); +#endif + } + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_power_on_setting(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_power_on_setting(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_power_on_setting(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8821A + else if (IS_HARDWARE_TYPE_8821(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821a1ant_power_on_setting(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821a2ant_power_on_setting(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8822B + else if ((IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) && (pHalData->EEPROMBluetoothCoexist == _TRUE)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_power_on_setting(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_power_on_setting(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8821C + else if ((IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) && (pHalData->EEPROMBluetoothCoexist == _TRUE)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_power_on_setting(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_power_on_setting(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8814A + if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_power_on_setting(pBtCoexist); + /* else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8814a1ant_power_on_setting(pBtCoexist); */ + } +#endif + +#endif +} + +void EXhalbtcoutsrc_PreLoadFirmware(PBTC_COEXIST pBtCoexist) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + pBtCoexist->statistics.cnt_pre_load_firmware++; + + if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8723B + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_pre_load_firmware(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_pre_load_firmware(pBtCoexist); +#endif + } + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_pre_load_firmware(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_pre_load_firmware(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_pre_load_firmware(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_pre_load_firmware(pBtCoexist); + } +#endif +} + +void EXhalbtcoutsrc_init_hw_config(PBTC_COEXIST pBtCoexist, u8 bWifiOnly) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + pBtCoexist->statistics.cnt_init_hw_config++; + +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_init_hw_config(pBtCoexist, bWifiOnly); +#else + + if (IS_HARDWARE_TYPE_8821(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8821A + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821a2ant_init_hw_config(pBtCoexist, bWifiOnly); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821a1ant_init_hw_config(pBtCoexist, bWifiOnly); +#endif + } + +#ifdef CONFIG_RTL8723B + else if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_init_hw_config(pBtCoexist, bWifiOnly); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_init_hw_config(pBtCoexist, bWifiOnly); + } +#endif + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_init_hw_config(pBtCoexist, bWifiOnly); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_init_hw_config(pBtCoexist, bWifiOnly); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_init_hw_config(pBtCoexist, bWifiOnly); + } +#endif + +#ifdef CONFIG_RTL8192E + else if (IS_HARDWARE_TYPE_8192E(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8192e2ant_init_hw_config(pBtCoexist, bWifiOnly); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8192e1ant_init_hw_config(pBtCoexist, bWifiOnly); + } +#endif + +#ifdef CONFIG_RTL8812A + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8812a2ant_init_hw_config(pBtCoexist, bWifiOnly); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8812a1ant_init_hw_config(pBtCoexist, bWifiOnly); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_init_hw_config(pBtCoexist, bWifiOnly); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_init_hw_config(pBtCoexist, bWifiOnly); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_init_hw_config(pBtCoexist, bWifiOnly); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_init_hw_config(pBtCoexist, bWifiOnly); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_init_hw_config(pBtCoexist, bWifiOnly); + } +#endif + +#endif +} + +void EXhalbtcoutsrc_init_coex_dm(PBTC_COEXIST pBtCoexist) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + pBtCoexist->statistics.cnt_init_coex_dm++; + +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_init_coex_dm(pBtCoexist); +#else + + if (IS_HARDWARE_TYPE_8821(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8821A + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821a2ant_init_coex_dm(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821a1ant_init_coex_dm(pBtCoexist); +#endif + } + +#ifdef CONFIG_RTL8723B + else if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_init_coex_dm(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_init_coex_dm(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_init_coex_dm(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_init_coex_dm(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_init_coex_dm(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8192E + else if (IS_HARDWARE_TYPE_8192E(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8192e2ant_init_coex_dm(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8192e1ant_init_coex_dm(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8812A + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8812a2ant_init_coex_dm(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8812a1ant_init_coex_dm(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_init_coex_dm(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_init_coex_dm(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_init_coex_dm(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_init_coex_dm(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_init_coex_dm(pBtCoexist); + } +#endif + +#endif + + pBtCoexist->initilized = _TRUE; +} + +void EXhalbtcoutsrc_ips_notify(PBTC_COEXIST pBtCoexist, u8 type) +{ + u8 ipsType; + + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + pBtCoexist->statistics.cnt_ips_notify++; + if (pBtCoexist->manual_control) + return; + + if (IPS_NONE == type) { + ipsType = BTC_IPS_LEAVE; + GLBtcWiFiInIPS = _FALSE; + } else { + ipsType = BTC_IPS_ENTER; + GLBtcWiFiInIPS = _TRUE; + } + + /* All notify is called in cmd thread, don't need to leave low power again + * halbtcoutsrc_LeaveLowPower(pBtCoexist); */ + +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_ips_notify(pBtCoexist, ipsType); +#else + + if (IS_HARDWARE_TYPE_8821(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8821A + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821a2ant_ips_notify(pBtCoexist, ipsType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821a1ant_ips_notify(pBtCoexist, ipsType); +#endif + } + +#ifdef CONFIG_RTL8723B + else if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_ips_notify(pBtCoexist, ipsType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_ips_notify(pBtCoexist, ipsType); + } +#endif + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_ips_notify(pBtCoexist, ipsType); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_ips_notify(pBtCoexist, ipsType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_ips_notify(pBtCoexist, ipsType); + } +#endif + +#ifdef CONFIG_RTL8192E + else if (IS_HARDWARE_TYPE_8192E(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8192e2ant_ips_notify(pBtCoexist, ipsType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8192e1ant_ips_notify(pBtCoexist, ipsType); + } +#endif + +#ifdef CONFIG_RTL8812A + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8812a2ant_ips_notify(pBtCoexist, ipsType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8812a1ant_ips_notify(pBtCoexist, ipsType); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_ips_notify(pBtCoexist, ipsType); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_ips_notify(pBtCoexist, ipsType); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_ips_notify(pBtCoexist, ipsType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_ips_notify(pBtCoexist, ipsType); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_ips_notify(pBtCoexist, ipsType); + } +#endif + +#endif + /* halbtcoutsrc_NormalLowPower(pBtCoexist); */ +} + +void EXhalbtcoutsrc_lps_notify(PBTC_COEXIST pBtCoexist, u8 type) +{ + u8 lpsType; + + + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + pBtCoexist->statistics.cnt_lps_notify++; + if (pBtCoexist->manual_control) + return; + + if (PS_MODE_ACTIVE == type) { + lpsType = BTC_LPS_DISABLE; + GLBtcWiFiInLPS = _FALSE; + } else { + lpsType = BTC_LPS_ENABLE; + GLBtcWiFiInLPS = _TRUE; + } + +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_lps_notify(pBtCoexist, lpsType); +#else + + if (IS_HARDWARE_TYPE_8821(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8821A + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821a2ant_lps_notify(pBtCoexist, lpsType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821a1ant_lps_notify(pBtCoexist, lpsType); +#endif + } + +#ifdef CONFIG_RTL8723B + else if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_lps_notify(pBtCoexist, lpsType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_lps_notify(pBtCoexist, lpsType); + } +#endif + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_lps_notify(pBtCoexist, lpsType); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_lps_notify(pBtCoexist, lpsType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_lps_notify(pBtCoexist, lpsType); + } +#endif + +#ifdef CONFIG_RTL8192E + else if (IS_HARDWARE_TYPE_8192E(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8192e2ant_lps_notify(pBtCoexist, lpsType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8192e1ant_lps_notify(pBtCoexist, lpsType); + } +#endif + +#ifdef CONFIG_RTL8812A + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8812a2ant_lps_notify(pBtCoexist, lpsType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8812a1ant_lps_notify(pBtCoexist, lpsType); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_lps_notify(pBtCoexist, lpsType); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_lps_notify(pBtCoexist, lpsType); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_lps_notify(pBtCoexist, lpsType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_lps_notify(pBtCoexist, lpsType); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_lps_notify(pBtCoexist, lpsType); + } +#endif + +#endif +} + +void EXhalbtcoutsrc_scan_notify(PBTC_COEXIST pBtCoexist, u8 type) +{ + u8 scanType; + + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + pBtCoexist->statistics.cnt_scan_notify++; + if (pBtCoexist->manual_control) + return; + + if (type) { + scanType = BTC_SCAN_START; + GLBtcWiFiInScanState = _TRUE; + } else { + scanType = BTC_SCAN_FINISH; + GLBtcWiFiInScanState = _FALSE; + } + + /* All notify is called in cmd thread, don't need to leave low power again + * halbtcoutsrc_LeaveLowPower(pBtCoexist); */ + +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_scan_notify(pBtCoexist, scanType); +#else + + if (IS_HARDWARE_TYPE_8821(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8821A + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821a2ant_scan_notify(pBtCoexist, scanType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821a1ant_scan_notify(pBtCoexist, scanType); +#endif + } + +#ifdef CONFIG_RTL8723B + else if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_scan_notify(pBtCoexist, scanType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_scan_notify(pBtCoexist, scanType); + } +#endif + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_scan_notify(pBtCoexist, scanType); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_scan_notify(pBtCoexist, scanType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_scan_notify(pBtCoexist, scanType); + } +#endif + +#ifdef CONFIG_RTL8192E + else if (IS_HARDWARE_TYPE_8192E(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8192e2ant_scan_notify(pBtCoexist, scanType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8192e1ant_scan_notify(pBtCoexist, scanType); + } +#endif + +#ifdef CONFIG_RTL8812A + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8812a2ant_scan_notify(pBtCoexist, scanType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8812a1ant_scan_notify(pBtCoexist, scanType); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_scan_notify(pBtCoexist, scanType); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_scan_notify(pBtCoexist, scanType); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_scan_notify(pBtCoexist, scanType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_scan_notify(pBtCoexist, scanType); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_scan_notify(pBtCoexist, scanType); + } +#endif + +#endif + + /* halbtcoutsrc_NormalLowPower(pBtCoexist); */ +} + +void EXhalbtcoutsrc_SetAntennaPathNotify(PBTC_COEXIST pBtCoexist, u8 type) +{ +#if 0 + u8 switchType; + + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + if (pBtCoexist->manual_control) + return; + + halbtcoutsrc_LeaveLowPower(pBtCoexist); + + switchType = type; + + if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_set_antenna_notify(pBtCoexist, type); + } + if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_set_antenna_notify(pBtCoexist, type); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_set_antenna_notify(pBtCoexist, type); + } + + halbtcoutsrc_NormalLowPower(pBtCoexist); +#endif +} + +void EXhalbtcoutsrc_connect_notify(PBTC_COEXIST pBtCoexist, u8 assoType) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + pBtCoexist->statistics.cnt_connect_notify++; + if (pBtCoexist->manual_control) + return; + + /* All notify is called in cmd thread, don't need to leave low power again + * halbtcoutsrc_LeaveLowPower(pBtCoexist); */ +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_connect_notify(pBtCoexist, assoType); +#else + + if (IS_HARDWARE_TYPE_8821(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8821A + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821a2ant_connect_notify(pBtCoexist, assoType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821a1ant_connect_notify(pBtCoexist, assoType); +#endif + } + +#ifdef CONFIG_RTL8723B + else if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_connect_notify(pBtCoexist, assoType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_connect_notify(pBtCoexist, assoType); + } +#endif + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_connect_notify(pBtCoexist, assoType); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_connect_notify(pBtCoexist, assoType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_connect_notify(pBtCoexist, assoType); + } +#endif + +#ifdef CONFIG_RTL8192E + else if (IS_HARDWARE_TYPE_8192E(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8192e2ant_connect_notify(pBtCoexist, assoType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8192e1ant_connect_notify(pBtCoexist, assoType); + } +#endif + +#ifdef CONFIG_RTL8812A + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8812a2ant_connect_notify(pBtCoexist, assoType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8812a1ant_connect_notify(pBtCoexist, assoType); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_connect_notify(pBtCoexist, assoType); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_connect_notify(pBtCoexist, assoType); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_connect_notify(pBtCoexist, assoType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_connect_notify(pBtCoexist, assoType); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_connect_notify(pBtCoexist, assoType); + } +#endif + +#endif + + /* halbtcoutsrc_NormalLowPower(pBtCoexist); */ +} + +void EXhalbtcoutsrc_media_status_notify(PBTC_COEXIST pBtCoexist, RT_MEDIA_STATUS mediaStatus) +{ + u8 mStatus = BTC_MEDIA_MAX; + PADAPTER adapter = NULL; + HAL_DATA_TYPE *hal = NULL; + + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + if (pBtCoexist->manual_control) + return; + + pBtCoexist->statistics.cnt_media_status_notify++; + adapter = (PADAPTER)pBtCoexist->Adapter; + hal = GET_HAL_DATA(adapter); + + if (RT_MEDIA_CONNECT == mediaStatus) { + if (hal->current_band_type == BAND_ON_2_4G) + mStatus = BTC_MEDIA_CONNECT; + else if (hal->current_band_type == BAND_ON_5G) + mStatus = BTC_MEDIA_CONNECT_5G; + else { + mStatus = BTC_MEDIA_CONNECT; + RTW_ERR("%s unknow band type\n", __func__); + } + } else + mStatus = BTC_MEDIA_DISCONNECT; + + /* All notify is called in cmd thread, don't need to leave low power again + * halbtcoutsrc_LeaveLowPower(pBtCoexist); */ +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_media_status_notify(pBtCoexist, mStatus); +#else + + if (IS_HARDWARE_TYPE_8821(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8821A + /* compatible for 8821A */ + if (mStatus == BTC_MEDIA_CONNECT_5G) + mStatus = BTC_MEDIA_CONNECT; + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821a2ant_media_status_notify(pBtCoexist, mStatus); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821a1ant_media_status_notify(pBtCoexist, mStatus); +#endif + } + +#ifdef CONFIG_RTL8723B + else if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_media_status_notify(pBtCoexist, mStatus); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_media_status_notify(pBtCoexist, mStatus); + } +#endif + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_media_status_notify(pBtCoexist, mStatus); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_media_status_notify(pBtCoexist, mStatus); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_media_status_notify(pBtCoexist, mStatus); + } +#endif + +#ifdef CONFIG_RTL8192E + else if (IS_HARDWARE_TYPE_8192E(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8192e2ant_media_status_notify(pBtCoexist, mStatus); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8192e1ant_media_status_notify(pBtCoexist, mStatus); + } +#endif + +#ifdef CONFIG_RTL8812A + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { + /* compatible for 8812A */ + if (mStatus == BTC_MEDIA_CONNECT_5G) + mStatus = BTC_MEDIA_CONNECT; + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8812a2ant_media_status_notify(pBtCoexist, mStatus); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8812a1ant_media_status_notify(pBtCoexist, mStatus); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_media_status_notify(pBtCoexist, mStatus); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_media_status_notify(pBtCoexist, mStatus); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_media_status_notify(pBtCoexist, mStatus); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_media_status_notify(pBtCoexist, mStatus); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_media_status_notify(pBtCoexist, mStatus); + } +#endif + +#endif + + /* halbtcoutsrc_NormalLowPower(pBtCoexist); */ +} + +void EXhalbtcoutsrc_specific_packet_notify(PBTC_COEXIST pBtCoexist, u8 pktType) +{ + u8 packetType; + PADAPTER adapter = NULL; + HAL_DATA_TYPE *hal = NULL; + + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + if (pBtCoexist->manual_control) + return; + + pBtCoexist->statistics.cnt_specific_packet_notify++; + adapter = (PADAPTER)pBtCoexist->Adapter; + hal = GET_HAL_DATA(adapter); + + if (PACKET_DHCP == pktType) + packetType = BTC_PACKET_DHCP; + else if (PACKET_EAPOL == pktType) + packetType = BTC_PACKET_EAPOL; + else if (PACKET_ARP == pktType) + packetType = BTC_PACKET_ARP; + else { + packetType = BTC_PACKET_UNKNOWN; + return; + } + + if (hal->current_band_type == BAND_ON_5G) + packetType |= BTC_5G_BAND; + + /* All notify is called in cmd thread, don't need to leave low power again + * halbtcoutsrc_LeaveLowPower(pBtCoexist); */ +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_specific_packet_notify(pBtCoexist, packetType); +#else + + if (IS_HARDWARE_TYPE_8821(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8821A + /* compatible for 8821A */ + if (hal->current_band_type == BAND_ON_5G) + packetType &= ~BTC_5G_BAND; + + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821a2ant_specific_packet_notify(pBtCoexist, packetType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821a1ant_specific_packet_notify(pBtCoexist, packetType); +#endif + } + +#ifdef CONFIG_RTL8723B + else if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_specific_packet_notify(pBtCoexist, packetType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_specific_packet_notify(pBtCoexist, packetType); + } +#endif + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_specific_packet_notify(pBtCoexist, packetType); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_specific_packet_notify(pBtCoexist, packetType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_specific_packet_notify(pBtCoexist, packetType); + } +#endif + +#ifdef CONFIG_RTL8192E + else if (IS_HARDWARE_TYPE_8192E(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8192e2ant_specific_packet_notify(pBtCoexist, packetType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8192e1ant_specific_packet_notify(pBtCoexist, packetType); + } +#endif + +#ifdef CONFIG_RTL8812A + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { + /* compatible for 8812A */ + if (hal->current_band_type == BAND_ON_5G) + packetType &= ~BTC_5G_BAND; + + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8812a2ant_specific_packet_notify(pBtCoexist, packetType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8812a1ant_specific_packet_notify(pBtCoexist, packetType); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_specific_packet_notify(pBtCoexist, packetType); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_specific_packet_notify(pBtCoexist, packetType); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_specific_packet_notify(pBtCoexist, packetType); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_specific_packet_notify(pBtCoexist, packetType); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_specific_packet_notify(pBtCoexist, packetType); + } +#endif + +#endif + + /* halbtcoutsrc_NormalLowPower(pBtCoexist); */ +} + +void EXhalbtcoutsrc_bt_info_notify(PBTC_COEXIST pBtCoexist, u8 *tmpBuf, u8 length) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + pBtCoexist->statistics.cnt_bt_info_notify++; + + /* All notify is called in cmd thread, don't need to leave low power again + * halbtcoutsrc_LeaveLowPower(pBtCoexist); */ +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_bt_info_notify(pBtCoexist, tmpBuf, length); +#else + + if (IS_HARDWARE_TYPE_8821(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8821A + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821a2ant_bt_info_notify(pBtCoexist, tmpBuf, length); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821a1ant_bt_info_notify(pBtCoexist, tmpBuf, length); +#endif + } + +#ifdef CONFIG_RTL8723B + else if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_bt_info_notify(pBtCoexist, tmpBuf, length); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_bt_info_notify(pBtCoexist, tmpBuf, length); + } +#endif + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_bt_info_notify(pBtCoexist, tmpBuf, length); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_bt_info_notify(pBtCoexist, tmpBuf, length); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_bt_info_notify(pBtCoexist, tmpBuf, length); + } +#endif + +#ifdef CONFIG_RTL8192E + else if (IS_HARDWARE_TYPE_8192E(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8192e2ant_bt_info_notify(pBtCoexist, tmpBuf, length); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8192e1ant_bt_info_notify(pBtCoexist, tmpBuf, length); + } +#endif + +#ifdef CONFIG_RTL8812A + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8812a2ant_bt_info_notify(pBtCoexist, tmpBuf, length); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8812a1ant_bt_info_notify(pBtCoexist, tmpBuf, length); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_bt_info_notify(pBtCoexist, tmpBuf, length); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_bt_info_notify(pBtCoexist, tmpBuf, length); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_bt_info_notify(pBtCoexist, tmpBuf, length); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_bt_info_notify(pBtCoexist, tmpBuf, length); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_bt_info_notify(pBtCoexist, tmpBuf, length); + } +#endif + +#endif + + /* halbtcoutsrc_NormalLowPower(pBtCoexist); */ +} + +void EXhalbtcoutsrc_WlFwDbgInfoNotify(PBTC_COEXIST pBtCoexist, u8* tmpBuf, u8 length) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_wl_fwdbginfo_notify(pBtCoexist, tmpBuf, length); +#else + + if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8703B + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_wl_fwdbginfo_notify(pBtCoexist, tmpBuf, length); +#endif + } + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_wl_fwdbginfo_notify(pBtCoexist, tmpBuf, length); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_wl_fwdbginfo_notify(pBtCoexist, tmpBuf, length); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_wl_fwdbginfo_notify(pBtCoexist, tmpBuf, length); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_wl_fwdbginfo_notify(pBtCoexist, tmpBuf, length); + } +#endif + +#endif +} + +void EXhalbtcoutsrc_rx_rate_change_notify(PBTC_COEXIST pBtCoexist, u8 is_data_frame, u8 btc_rate_id) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + pBtCoexist->statistics.cnt_rate_id_notify++; + +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_rx_rate_change_notify(pBtCoexist, is_data_frame, btc_rate_id); +#else + + if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8703B + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_rx_rate_change_notify(pBtCoexist, is_data_frame, btc_rate_id); +#endif + } + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_rx_rate_change_notify(pBtCoexist, is_data_frame, btc_rate_id); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_rx_rate_change_notify(pBtCoexist, is_data_frame, btc_rate_id); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_rx_rate_change_notify(pBtCoexist, is_data_frame, btc_rate_id); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_rx_rate_change_notify(pBtCoexist, is_data_frame, btc_rate_id); + } +#endif + +#endif +} + +void +EXhalbtcoutsrc_RfStatusNotify( + PBTC_COEXIST pBtCoexist, + u8 type +) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + pBtCoexist->statistics.cnt_rf_status_notify++; + +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_rf_status_notify(pBtCoexist, type); +#else + + if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8723B + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_rf_status_notify(pBtCoexist, type); +#endif + } + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_rf_status_notify(pBtCoexist, type); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_rf_status_notify(pBtCoexist, type); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_rf_status_notify(pBtCoexist, type); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_rf_status_notify(pBtCoexist, type); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_rf_status_notify(pBtCoexist, type); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_rf_status_notify(pBtCoexist, type); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_rf_status_notify(pBtCoexist, type); + } +#endif + +#endif +} + +void EXhalbtcoutsrc_StackOperationNotify(PBTC_COEXIST pBtCoexist, u8 type) +{ +#if 0 + u8 stackOpType; + + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + pBtCoexist->statistics.cntStackOperationNotify++; + if (pBtCoexist->manual_control) + return; + + if ((HCI_BT_OP_INQUIRY_START == type) || + (HCI_BT_OP_PAGING_START == type) || + (HCI_BT_OP_PAIRING_START == type)) + stackOpType = BTC_STACK_OP_INQ_PAGE_PAIR_START; + else if ((HCI_BT_OP_INQUIRY_FINISH == type) || + (HCI_BT_OP_PAGING_SUCCESS == type) || + (HCI_BT_OP_PAGING_UNSUCCESS == type) || + (HCI_BT_OP_PAIRING_FINISH == type)) + stackOpType = BTC_STACK_OP_INQ_PAGE_PAIR_FINISH; + else + stackOpType = BTC_STACK_OP_NONE; + +#endif +} + +void EXhalbtcoutsrc_halt_notify(PBTC_COEXIST pBtCoexist) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + pBtCoexist->statistics.cnt_halt_notify++; + +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_halt_notify(pBtCoexist); +#else + + if (IS_HARDWARE_TYPE_8821(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8821A + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821a2ant_halt_notify(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821a1ant_halt_notify(pBtCoexist); +#endif + } + +#ifdef CONFIG_RTL8723B + else if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_halt_notify(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_halt_notify(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_halt_notify(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_halt_notify(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_halt_notify(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8192E + else if (IS_HARDWARE_TYPE_8192E(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8192e2ant_halt_notify(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8192e1ant_halt_notify(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8812A + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8812a2ant_halt_notify(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8812a1ant_halt_notify(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_halt_notify(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_halt_notify(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_halt_notify(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_halt_notify(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_halt_notify(pBtCoexist); + } +#endif + +#endif +} + +void EXhalbtcoutsrc_SwitchBtTRxMask(PBTC_COEXIST pBtCoexist) +{ + if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) { + halbtcoutsrc_SetBtReg(pBtCoexist, 0, 0x3c, 0x01); /* BT goto standby while GNT_BT 1-->0 */ + } else if (pBtCoexist->board_info.btdm_ant_num == 1) { + halbtcoutsrc_SetBtReg(pBtCoexist, 0, 0x3c, 0x15); /* BT goto standby while GNT_BT 1-->0 */ + } + } +} + +void EXhalbtcoutsrc_pnp_notify(PBTC_COEXIST pBtCoexist, u8 pnpState) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + pBtCoexist->statistics.cnt_pnp_notify++; + + /* */ + /* currently only 1ant we have to do the notification, */ + /* once pnp is notified to sleep state, we have to leave LPS that we can sleep normally. */ + /* */ +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_pnp_notify(pBtCoexist, pnpState); +#else + + if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8723B + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_pnp_notify(pBtCoexist, pnpState); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_pnp_notify(pBtCoexist, pnpState); +#endif + } + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_pnp_notify(pBtCoexist, pnpState); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_pnp_notify(pBtCoexist, pnpState); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_pnp_notify(pBtCoexist, pnpState); + } +#endif + +#ifdef CONFIG_RTL8821A + else if (IS_HARDWARE_TYPE_8821(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821a1ant_pnp_notify(pBtCoexist, pnpState); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821a2ant_pnp_notify(pBtCoexist, pnpState); + } +#endif + +#ifdef CONFIG_RTL8192E + else if (IS_HARDWARE_TYPE_8192E(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8192e1ant_pnp_notify(pBtCoexist, pnpState); + } +#endif + +#ifdef CONFIG_RTL8812A + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8812a1ant_pnp_notify(pBtCoexist, pnpState); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_pnp_notify(pBtCoexist, pnpState); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_pnp_notify(pBtCoexist, pnpState); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_pnp_notify(pBtCoexist, pnpState); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_pnp_notify(pBtCoexist, pnpState); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_pnp_notify(pBtCoexist, pnpState); + } +#endif + +#endif +} + +void EXhalbtcoutsrc_CoexDmSwitch(PBTC_COEXIST pBtCoexist) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + pBtCoexist->statistics.cnt_coex_dm_switch++; + + halbtcoutsrc_LeaveLowPower(pBtCoexist); + + if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8723B + if (pBtCoexist->board_info.btdm_ant_num == 1) { + pBtCoexist->stop_coex_dm = TRUE; + ex_halbtc8723b1ant_coex_dm_reset(pBtCoexist); + EXhalbtcoutsrc_SetAntNum(BT_COEX_ANT_TYPE_DETECTED, 2); + ex_halbtc8723b2ant_init_hw_config(pBtCoexist, FALSE); + ex_halbtc8723b2ant_init_coex_dm(pBtCoexist); + pBtCoexist->stop_coex_dm = FALSE; + } +#endif + } + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) { + pBtCoexist->stop_coex_dm = TRUE; + ex_halbtc8723d1ant_coex_dm_reset(pBtCoexist); + EXhalbtcoutsrc_SetAntNum(BT_COEX_ANT_TYPE_DETECTED, 2); + ex_halbtc8723d2ant_init_hw_config(pBtCoexist, FALSE); + ex_halbtc8723d2ant_init_coex_dm(pBtCoexist); + pBtCoexist->stop_coex_dm = FALSE; + } + } +#endif + + halbtcoutsrc_NormalLowPower(pBtCoexist); +} + +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) +void EXhalbtcoutsrc_TimerNotify(PBTC_COEXIST pBtCoexist, u32 timer_type) +{ + rtw_btc_ex_timerup_notify(pBtCoexist, timer_type); +} + +void EXhalbtcoutsrc_WLStatusChangeNotify(PBTC_COEXIST pBtCoexist, u32 change_type) +{ + rtw_btc_ex_wl_status_change_notify(pBtCoexist, change_type); +} + +u32 EXhalbtcoutsrc_CoexTimerCheck(PBTC_COEXIST pBtCoexist) +{ + u32 i, timer_map = 0; + + for (i = 0; i < BTC_TIMER_MAX; i++) { + if (pBtCoexist->coex_sta.cnt_timer[i] > 0) { + if (pBtCoexist->coex_sta.cnt_timer[i] == 1) { + timer_map |= BIT(i); + RTW_DBG("[BTC], %s(): timer_map = 0x%x\n", __func__, timer_map); + } + + pBtCoexist->coex_sta.cnt_timer[i]--; + } + } + + return timer_map; +} + +u32 EXhalbtcoutsrc_WLStatusCheck(PBTC_COEXIST pBtCoexist) +{ + struct btc_wifi_link_info link_info; + struct btc_coex_sta *coex_sta = &pBtCoexist->coex_sta; + const struct btc_chip_para *chip_para = pBtCoexist->chip_para; + u32 change_map = 0; + static bool wl_busy_pre; + bool wl_busy = _FALSE, bt_ctr_change = _FALSE; + s32 wl_rssi; + u32 traffic_dir; + u8 i, tmp; + static u8 rssi_step_pre = 5, wl_noisy_level_pre = 4; + + halbtcoutsrc_wl_noisy_detect(pBtCoexist); + bt_ctr_change = halbtcoutsrc_btc_monitor_bt_ctr(pBtCoexist); + + /* WL busy to idle or idle to busy */ + pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_BUSY, &wl_busy); + if (wl_busy != wl_busy_pre) { + if (wl_busy) + change_map |= BIT(BTC_WLSTATUS_CHANGE_TOBUSY); + else + change_map |= BIT(BTC_WLSTATUS_CHANGE_TOIDLE); + + wl_busy_pre = wl_busy; + } + + /* WL RSSI */ + pBtCoexist->btc_get(pBtCoexist, BTC_GET_S4_WIFI_RSSI, &wl_rssi); + tmp = (u8)(wl_rssi & 0xff); + for (i = 0; i < 4; i++) { + if (tmp >= chip_para->wl_rssi_step[i]) + break; + } + + if (rssi_step_pre != i) { + rssi_step_pre = i; + change_map |= BIT(BTC_WLSTATUS_CHANGE_RSSI); + } + + /* WL Link info */ + pBtCoexist->btc_get(pBtCoexist, BTC_GET_BL_WIFI_LINK_INFO, &link_info); + if (link_info.link_mode != pBtCoexist->wifi_link_info.link_mode || + link_info.sta_center_channel != + pBtCoexist->wifi_link_info.sta_center_channel || + link_info.p2p_center_channel != + pBtCoexist->wifi_link_info.p2p_center_channel || + link_info.bany_client_join_go != + pBtCoexist->wifi_link_info.bany_client_join_go) { + change_map |= BIT(BTC_WLSTATUS_CHANGE_LINKINFO); + pBtCoexist->wifi_link_info = link_info; + } + + /* WL Traffic Direction */ + pBtCoexist->btc_get(pBtCoexist, BTC_GET_U4_WIFI_TRAFFIC_DIR, &traffic_dir); + if (wl_busy && traffic_dir != pBtCoexist->wifi_link_info_ext.traffic_dir) { + change_map |= BIT(BTC_WLSTATUS_CHANGE_DIR); + pBtCoexist->wifi_link_info_ext.traffic_dir = traffic_dir; + } + + /* Noisy Detect */ + if (coex_sta->wl_noisy_level != wl_noisy_level_pre) { + change_map |= BIT(BTC_WLSTATUS_CHANGE_NOISY); + wl_noisy_level_pre = coex_sta->wl_noisy_level; + } + + /* BT Counter change > 50 */ + if (bt_ctr_change) + change_map |= BIT(BTC_WLSTATUS_CHANGE_BTCNT); + + /* CCK Lock Try */ + if (coex_sta->wl_coex_mode == BTC_WLINK_2GFREE) + coex_sta->cnt_wl[BTC_CNT_WL_2G_FDDSTAY]++; + + if (coex_sta->wl_coex_mode == BTC_WLINK_2GFREE && + coex_sta->cnt_wl[BTC_CNT_WL_2G_FDDSTAY] > BTC_2GFDD_MAX_STAY && + coex_sta->cnt_wl[BTC_CNT_WL_2G_TDDTRY] < BTC_2GTDD_MAX_TRY) { + coex_sta->cnt_wl[BTC_CNT_WL_2G_TDDTRY]++; + + RTW_DBG("[BTC], Try 2.4G coex from FDD to TDD (FDD:%d, TRY:%d)\n", + coex_sta->cnt_wl[BTC_CNT_WL_2G_FDDSTAY], + coex_sta->cnt_wl[BTC_CNT_WL_2G_TDDTRY]); + + coex_sta->cnt_wl[BTC_CNT_WL_2G_FDDSTAY] = 0; + change_map |= BIT(BTC_WLSTATUS_CHANGE_LOCKTRY); + } + + RTW_DBG("[BTC], %s(): change_map = 0x%x\n", __func__, change_map); + + return change_map; +} + +void EXhalbtcoutsrc_status_monitor(PBTC_COEXIST pBtCoexist) +{ + u32 timer_up_type = 0, wl_status_change_type = 0; + + timer_up_type = EXhalbtcoutsrc_CoexTimerCheck(pBtCoexist); + if (timer_up_type != 0) + EXhalbtcoutsrc_TimerNotify(pBtCoexist, timer_up_type); + + wl_status_change_type = EXhalbtcoutsrc_WLStatusCheck(pBtCoexist); + if (wl_status_change_type != 0) + EXhalbtcoutsrc_WLStatusChangeNotify(pBtCoexist, wl_status_change_type); + + rtw_btc_ex_periodical(pBtCoexist); +} +#endif + +void EXhalbtcoutsrc_WL_RFK_Notify(PBTC_COEXIST pBtCoexist, u8 path, u8 type, u8 state) +{ + #if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_wl_rfk_notify(pBtCoexist, path, type, state); + #endif + return; +} + +void EXhalbtcoutsrc_periodical(PBTC_COEXIST pBtCoexist) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + pBtCoexist->statistics.cnt_periodical++; + + /* Periodical should be called in cmd thread, */ + /* don't need to leave low power again + * halbtcoutsrc_LeaveLowPower(pBtCoexist); */ +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + EXhalbtcoutsrc_status_monitor(pBtCoexist); +#else + + if (IS_HARDWARE_TYPE_8821(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8821A + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821a2ant_periodical(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) { + if (!halbtcoutsrc_UnderIps(pBtCoexist)) + ex_halbtc8821a1ant_periodical(pBtCoexist); + } +#endif + } + +#ifdef CONFIG_RTL8723B + else if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_periodical(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_periodical(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_periodical(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_periodical(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_periodical(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8192E + else if (IS_HARDWARE_TYPE_8192E(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8192e2ant_periodical(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8192e1ant_periodical(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8812A + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8812a2ant_periodical(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8812a1ant_periodical(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_periodical(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_periodical(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_periodical(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_periodical(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_periodical(pBtCoexist); + } +#endif + +#endif + + /* halbtcoutsrc_NormalLowPower(pBtCoexist); */ +} + +void EXhalbtcoutsrc_dbg_control(PBTC_COEXIST pBtCoexist, u8 opCode, u8 opLen, u8 *pData) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + pBtCoexist->statistics.cnt_dbg_ctrl++; + + /* This function doesn't be called yet, */ + /* default no need to leave low power to avoid deadlock + * halbtcoutsrc_LeaveLowPower(pBtCoexist); */ +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + /* rtw_btc_ex_dbg_control(pBtCoexist, opCode, opLen, pData); */ +#else + + if (IS_HARDWARE_TYPE_8192E(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8192E + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8192e1ant_dbg_control(pBtCoexist, opCode, opLen, pData); +#endif + } + +#ifdef CONFIG_RTL8812A + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8812a2ant_dbg_control(pBtCoexist, opCode, opLen, pData); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8812a1ant_dbg_control(pBtCoexist, opCode, opLen, pData); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) + if(pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_dbg_control(pBtCoexist, opCode, opLen, pData); +#endif + +#endif + + /* halbtcoutsrc_NormalLowPower(pBtCoexist); */ +} + +#if 0 +void +EXhalbtcoutsrc_AntennaDetection( + PBTC_COEXIST pBtCoexist, + u32 centFreq, + u32 offset, + u32 span, + u32 seconds +) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + /* Need to refine the following power save operations to enable this function in the future */ +#if 0 + IPSDisable(pBtCoexist->Adapter, FALSE, 0); + LeisurePSLeave(pBtCoexist->Adapter, LPS_DISABLE_BT_COEX); +#endif + + if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_AntennaDetection(pBtCoexist, centFreq, offset, span, seconds); + } + + /* IPSReturn(pBtCoexist->Adapter, 0xff); */ +} +#endif + +void EXhalbtcoutsrc_StackUpdateProfileInfo(void) +{ +#ifdef CONFIG_BT_COEXIST_SOCKET_TRX + PBTC_COEXIST pBtCoexist = &GLBtCoexist; + PADAPTER padapter = NULL; + PBT_MGNT pBtMgnt = NULL; + u8 i; + + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + padapter = (PADAPTER)pBtCoexist->Adapter; + pBtMgnt = &padapter->coex_info.BtMgnt; + + pBtCoexist->stack_info.profile_notified = _TRUE; + + pBtCoexist->stack_info.num_of_link = + pBtMgnt->ExtConfig.NumberOfACL + pBtMgnt->ExtConfig.NumberOfSCO; + + /* reset first */ + pBtCoexist->stack_info.bt_link_exist = _FALSE; + pBtCoexist->stack_info.sco_exist = _FALSE; + pBtCoexist->stack_info.acl_exist = _FALSE; + pBtCoexist->stack_info.a2dp_exist = _FALSE; + pBtCoexist->stack_info.hid_exist = _FALSE; + pBtCoexist->stack_info.num_of_hid = 0; + pBtCoexist->stack_info.pan_exist = _FALSE; + + if (!pBtMgnt->ExtConfig.NumberOfACL) + pBtCoexist->stack_info.min_bt_rssi = 0; + + if (pBtCoexist->stack_info.num_of_link) { + pBtCoexist->stack_info.bt_link_exist = _TRUE; + if (pBtMgnt->ExtConfig.NumberOfSCO) + pBtCoexist->stack_info.sco_exist = _TRUE; + if (pBtMgnt->ExtConfig.NumberOfACL) + pBtCoexist->stack_info.acl_exist = _TRUE; + } + + for (i = 0; i < pBtMgnt->ExtConfig.NumberOfACL; i++) { + if (BT_PROFILE_A2DP == pBtMgnt->ExtConfig.aclLink[i].BTProfile) + pBtCoexist->stack_info.a2dp_exist = _TRUE; + else if (BT_PROFILE_PAN == pBtMgnt->ExtConfig.aclLink[i].BTProfile) + pBtCoexist->stack_info.pan_exist = _TRUE; + else if (BT_PROFILE_HID == pBtMgnt->ExtConfig.aclLink[i].BTProfile) { + pBtCoexist->stack_info.hid_exist = _TRUE; + pBtCoexist->stack_info.num_of_hid++; + } else + pBtCoexist->stack_info.unknown_acl_exist = _TRUE; + } +#endif /* CONFIG_BT_COEXIST_SOCKET_TRX */ +} + +void EXhalbtcoutsrc_UpdateMinBtRssi(s8 btRssi) +{ + PBTC_COEXIST pBtCoexist = &GLBtCoexist; + + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + pBtCoexist->stack_info.min_bt_rssi = btRssi; +} + +void EXhalbtcoutsrc_SetHciVersion(u16 hciVersion) +{ + PBTC_COEXIST pBtCoexist = &GLBtCoexist; + + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + pBtCoexist->stack_info.hci_version = hciVersion; +} + +void EXhalbtcoutsrc_SetBtPatchVersion(u16 btHciVersion, u16 btPatchVersion) +{ + PBTC_COEXIST pBtCoexist = &GLBtCoexist; + + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + pBtCoexist->bt_info.bt_real_fw_ver = btPatchVersion; + pBtCoexist->bt_info.bt_hci_ver = btHciVersion; +} + +#if 0 +void EXhalbtcoutsrc_SetBtExist(u8 bBtExist) +{ + GLBtCoexist.boardInfo.bBtExist = bBtExist; +} +#endif +void EXhalbtcoutsrc_SetChipType(u8 chipType) +{ + switch (chipType) { + default: + case BT_2WIRE: + case BT_ISSC_3WIRE: + case BT_ACCEL: + case BT_RTL8756: + GLBtCoexist.board_info.bt_chip_type = BTC_CHIP_UNDEF; + break; + case BT_CSR_BC4: + GLBtCoexist.board_info.bt_chip_type = BTC_CHIP_CSR_BC4; + break; + case BT_CSR_BC8: + GLBtCoexist.board_info.bt_chip_type = BTC_CHIP_CSR_BC8; + break; + case BT_RTL8723A: + GLBtCoexist.board_info.bt_chip_type = BTC_CHIP_RTL8723A; + break; + case BT_RTL8821: + GLBtCoexist.board_info.bt_chip_type = BTC_CHIP_RTL8821; + break; + case BT_RTL8723B: + GLBtCoexist.board_info.bt_chip_type = BTC_CHIP_RTL8723B; + break; + } +} + +void EXhalbtcoutsrc_SetAntNum(u8 type, u8 antNum) +{ + if (BT_COEX_ANT_TYPE_PG == type) { + GLBtCoexist.board_info.pg_ant_num = antNum; + GLBtCoexist.board_info.btdm_ant_num = antNum; +#if 0 + /* The antenna position: Main (default) or Aux for pgAntNum=2 && btdmAntNum =1 */ + /* The antenna position should be determined by auto-detect mechanism */ + /* The following is assumed to main, and those must be modified if y auto-detect mechanism is ready */ + if ((GLBtCoexist.board_info.pg_ant_num == 2) && (GLBtCoexist.board_info.btdm_ant_num == 1)) + GLBtCoexist.board_info.btdm_ant_pos = BTC_ANTENNA_AT_MAIN_PORT; + else + GLBtCoexist.board_info.btdm_ant_pos = BTC_ANTENNA_AT_MAIN_PORT; +#endif + } else if (BT_COEX_ANT_TYPE_ANTDIV == type) { + GLBtCoexist.board_info.btdm_ant_num = antNum; + /* GLBtCoexist.boardInfo.btdmAntPos = BTC_ANTENNA_AT_MAIN_PORT; */ + } else if (BT_COEX_ANT_TYPE_DETECTED == type) { + GLBtCoexist.board_info.btdm_ant_num = antNum; + /* GLBtCoexist.boardInfo.btdmAntPos = BTC_ANTENNA_AT_MAIN_PORT; */ + } +} + +/* + * Currently used by 8723b only, S0 or S1 + * */ +void EXhalbtcoutsrc_SetSingleAntPath(u8 singleAntPath) +{ + GLBtCoexist.board_info.single_ant_path = singleAntPath; +} + +void EXhalbtcoutsrc_DisplayBtCoexInfo(PBTC_COEXIST pBtCoexist) +{ + HAL_DATA_TYPE *pHalData = NULL; + + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + halbtcoutsrc_LeaveLowPower(pBtCoexist); + + /* To prevent the racing with IPS enter */ + halbtcoutsrc_EnterPwrLock(pBtCoexist); + +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + pHalData = GET_HAL_DATA((PADAPTER)pBtCoexist->Adapter); + + if (pHalData->EEPROMBluetoothCoexist == _TRUE) + rtw_btc_ex_display_coex_info(pBtCoexist); +#else + + if (IS_HARDWARE_TYPE_8821(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8821A + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821a2ant_display_coex_info(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821a1ant_display_coex_info(pBtCoexist); +#endif + } + +#ifdef CONFIG_RTL8723B + else if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723b2ant_display_coex_info(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_display_coex_info(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8703B + else if (IS_HARDWARE_TYPE_8703B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8703b1ant_display_coex_info(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8723D + else if (IS_HARDWARE_TYPE_8723D(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8723d2ant_display_coex_info(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723d1ant_display_coex_info(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8192E + else if (IS_HARDWARE_TYPE_8192E(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8192e2ant_display_coex_info(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8192e1ant_display_coex_info(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8812A + else if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8812a2ant_display_coex_info(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8812a1ant_display_coex_info(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_display_coex_info(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_display_coex_info(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_display_coex_info(pBtCoexist); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_display_coex_info(pBtCoexist); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_display_coex_info(pBtCoexist); + } +#endif + +#endif + + halbtcoutsrc_ExitPwrLock(pBtCoexist); + + halbtcoutsrc_NormalLowPower(pBtCoexist); +} + +void EXhalbtcoutsrc_DisplayAntDetection(PBTC_COEXIST pBtCoexist) +{ + if (!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + halbtcoutsrc_LeaveLowPower(pBtCoexist); + + if (IS_HARDWARE_TYPE_8723B(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8723B + if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8723b1ant_display_ant_detection(pBtCoexist); +#endif + } + + halbtcoutsrc_NormalLowPower(pBtCoexist); +} + +void ex_halbtcoutsrc_pta_off_on_notify(PBTC_COEXIST pBtCoexist, u8 bBTON) +{ + if (IS_HARDWARE_TYPE_8812(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8812A + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8812a2ant_pta_off_on_notify(pBtCoexist, (bBTON == _TRUE) ? BTC_BT_ON : BTC_BT_OFF); +#endif + } + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_pta_off_on_notify(pBtCoexist, (bBTON == _TRUE) ? BTC_BT_ON : BTC_BT_OFF); + } +#endif +} + +void EXhalbtcoutsrc_set_rfe_type(u8 type) +{ + GLBtCoexist.board_info.rfe_type= type; +} + +#ifdef CONFIG_RF4CE_COEXIST +void EXhalbtcoutsrc_set_rf4ce_link_state(u8 state) +{ + GLBtCoexist.rf4ce_info.link_state = state; +} + +u8 EXhalbtcoutsrc_get_rf4ce_link_state(void) +{ + return GLBtCoexist.rf4ce_info.link_state; +} +#endif + +void EXhalbtcoutsrc_switchband_notify(struct btc_coexist *pBtCoexist, u8 type) +{ + if(!halbtcoutsrc_IsBtCoexistAvailable(pBtCoexist)) + return; + + if(pBtCoexist->manual_control) + return; + + /* Driver should guarantee that the HW status isn't in low power mode */ + /* halbtcoutsrc_LeaveLowPower(pBtCoexist); */ +#if (CONFIG_BTCOEX_SUPPORT_BTC_CMN == 1) + rtw_btc_ex_switchband_notify(pBtCoexist, type); +#else + + if(IS_HARDWARE_TYPE_8822B(pBtCoexist->Adapter)) { +#ifdef CONFIG_RTL8822B + if(pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8822b1ant_switchband_notify(pBtCoexist, type); + else if(pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8822b2ant_switchband_notify(pBtCoexist, type); +#endif + } + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8821c2ant_switchband_notify(pBtCoexist, type); + else if (pBtCoexist->board_info.btdm_ant_num == 1) + ex_halbtc8821c1ant_switchband_notify(pBtCoexist, type); + } +#endif + +#ifdef CONFIG_RTL8814A + else if (IS_HARDWARE_TYPE_8814A(pBtCoexist->Adapter)) { + if (pBtCoexist->board_info.btdm_ant_num == 2) + ex_halbtc8814a2ant_switchband_notify(pBtCoexist, type); + } +#endif + +#endif + + /* halbtcoutsrc_NormalLowPower(pBtCoexist); */ +} + +u8 EXhalbtcoutsrc_rate_id_to_btc_rate_id(u8 rate_id) +{ + u8 btc_rate_id = BTC_UNKNOWN; + + switch (rate_id) { + /* CCK rates */ + case DESC_RATE1M: + btc_rate_id = BTC_CCK_1; + break; + case DESC_RATE2M: + btc_rate_id = BTC_CCK_2; + break; + case DESC_RATE5_5M: + btc_rate_id = BTC_CCK_5_5; + break; + case DESC_RATE11M: + btc_rate_id = BTC_CCK_11; + break; + + /* OFDM rates */ + case DESC_RATE6M: + btc_rate_id = BTC_OFDM_6; + break; + case DESC_RATE9M: + btc_rate_id = BTC_OFDM_9; + break; + case DESC_RATE12M: + btc_rate_id = BTC_OFDM_12; + break; + case DESC_RATE18M: + btc_rate_id = BTC_OFDM_18; + break; + case DESC_RATE24M: + btc_rate_id = BTC_OFDM_24; + break; + case DESC_RATE36M: + btc_rate_id = BTC_OFDM_36; + break; + case DESC_RATE48M: + btc_rate_id = BTC_OFDM_48; + break; + case DESC_RATE54M: + btc_rate_id = BTC_OFDM_54; + break; + + /* MCS rates */ + case DESC_RATEMCS0: + btc_rate_id = BTC_MCS_0; + break; + case DESC_RATEMCS1: + btc_rate_id = BTC_MCS_1; + break; + case DESC_RATEMCS2: + btc_rate_id = BTC_MCS_2; + break; + case DESC_RATEMCS3: + btc_rate_id = BTC_MCS_3; + break; + case DESC_RATEMCS4: + btc_rate_id = BTC_MCS_4; + break; + case DESC_RATEMCS5: + btc_rate_id = BTC_MCS_5; + break; + case DESC_RATEMCS6: + btc_rate_id = BTC_MCS_6; + break; + case DESC_RATEMCS7: + btc_rate_id = BTC_MCS_7; + break; + case DESC_RATEMCS8: + btc_rate_id = BTC_MCS_8; + break; + case DESC_RATEMCS9: + btc_rate_id = BTC_MCS_9; + break; + case DESC_RATEMCS10: + btc_rate_id = BTC_MCS_10; + break; + case DESC_RATEMCS11: + btc_rate_id = BTC_MCS_11; + break; + case DESC_RATEMCS12: + btc_rate_id = BTC_MCS_12; + break; + case DESC_RATEMCS13: + btc_rate_id = BTC_MCS_13; + break; + case DESC_RATEMCS14: + btc_rate_id = BTC_MCS_14; + break; + case DESC_RATEMCS15: + btc_rate_id = BTC_MCS_15; + break; + case DESC_RATEMCS16: + btc_rate_id = BTC_MCS_16; + break; + case DESC_RATEMCS17: + btc_rate_id = BTC_MCS_17; + break; + case DESC_RATEMCS18: + btc_rate_id = BTC_MCS_18; + break; + case DESC_RATEMCS19: + btc_rate_id = BTC_MCS_19; + break; + case DESC_RATEMCS20: + btc_rate_id = BTC_MCS_20; + break; + case DESC_RATEMCS21: + btc_rate_id = BTC_MCS_21; + break; + case DESC_RATEMCS22: + btc_rate_id = BTC_MCS_22; + break; + case DESC_RATEMCS23: + btc_rate_id = BTC_MCS_23; + break; + case DESC_RATEMCS24: + btc_rate_id = BTC_MCS_24; + break; + case DESC_RATEMCS25: + btc_rate_id = BTC_MCS_25; + break; + case DESC_RATEMCS26: + btc_rate_id = BTC_MCS_26; + break; + case DESC_RATEMCS27: + btc_rate_id = BTC_MCS_27; + break; + case DESC_RATEMCS28: + btc_rate_id = BTC_MCS_28; + break; + case DESC_RATEMCS29: + btc_rate_id = BTC_MCS_29; + break; + case DESC_RATEMCS30: + btc_rate_id = BTC_MCS_30; + break; + case DESC_RATEMCS31: + btc_rate_id = BTC_MCS_31; + break; + + case DESC_RATEVHTSS1MCS0: + btc_rate_id = BTC_VHT_1SS_MCS_0; + break; + case DESC_RATEVHTSS1MCS1: + btc_rate_id = BTC_VHT_1SS_MCS_1; + break; + case DESC_RATEVHTSS1MCS2: + btc_rate_id = BTC_VHT_1SS_MCS_2; + break; + case DESC_RATEVHTSS1MCS3: + btc_rate_id = BTC_VHT_1SS_MCS_3; + break; + case DESC_RATEVHTSS1MCS4: + btc_rate_id = BTC_VHT_1SS_MCS_4; + break; + case DESC_RATEVHTSS1MCS5: + btc_rate_id = BTC_VHT_1SS_MCS_5; + break; + case DESC_RATEVHTSS1MCS6: + btc_rate_id = BTC_VHT_1SS_MCS_6; + break; + case DESC_RATEVHTSS1MCS7: + btc_rate_id = BTC_VHT_1SS_MCS_7; + break; + case DESC_RATEVHTSS1MCS8: + btc_rate_id = BTC_VHT_1SS_MCS_8; + break; + case DESC_RATEVHTSS1MCS9: + btc_rate_id = BTC_VHT_1SS_MCS_9; + break; + + case DESC_RATEVHTSS2MCS0: + btc_rate_id = BTC_VHT_2SS_MCS_0; + break; + case DESC_RATEVHTSS2MCS1: + btc_rate_id = BTC_VHT_2SS_MCS_1; + break; + case DESC_RATEVHTSS2MCS2: + btc_rate_id = BTC_VHT_2SS_MCS_2; + break; + case DESC_RATEVHTSS2MCS3: + btc_rate_id = BTC_VHT_2SS_MCS_3; + break; + case DESC_RATEVHTSS2MCS4: + btc_rate_id = BTC_VHT_2SS_MCS_4; + break; + case DESC_RATEVHTSS2MCS5: + btc_rate_id = BTC_VHT_2SS_MCS_5; + break; + case DESC_RATEVHTSS2MCS6: + btc_rate_id = BTC_VHT_2SS_MCS_6; + break; + case DESC_RATEVHTSS2MCS7: + btc_rate_id = BTC_VHT_2SS_MCS_7; + break; + case DESC_RATEVHTSS2MCS8: + btc_rate_id = BTC_VHT_2SS_MCS_8; + break; + case DESC_RATEVHTSS2MCS9: + btc_rate_id = BTC_VHT_2SS_MCS_9; + break; + + case DESC_RATEVHTSS3MCS0: + btc_rate_id = BTC_VHT_3SS_MCS_0; + break; + case DESC_RATEVHTSS3MCS1: + btc_rate_id = BTC_VHT_3SS_MCS_1; + break; + case DESC_RATEVHTSS3MCS2: + btc_rate_id = BTC_VHT_3SS_MCS_2; + break; + case DESC_RATEVHTSS3MCS3: + btc_rate_id = BTC_VHT_3SS_MCS_3; + break; + case DESC_RATEVHTSS3MCS4: + btc_rate_id = BTC_VHT_3SS_MCS_4; + break; + case DESC_RATEVHTSS3MCS5: + btc_rate_id = BTC_VHT_3SS_MCS_5; + break; + case DESC_RATEVHTSS3MCS6: + btc_rate_id = BTC_VHT_3SS_MCS_6; + break; + case DESC_RATEVHTSS3MCS7: + btc_rate_id = BTC_VHT_3SS_MCS_7; + break; + case DESC_RATEVHTSS3MCS8: + btc_rate_id = BTC_VHT_3SS_MCS_8; + break; + case DESC_RATEVHTSS3MCS9: + btc_rate_id = BTC_VHT_3SS_MCS_9; + break; + + case DESC_RATEVHTSS4MCS0: + btc_rate_id = BTC_VHT_4SS_MCS_0; + break; + case DESC_RATEVHTSS4MCS1: + btc_rate_id = BTC_VHT_4SS_MCS_1; + break; + case DESC_RATEVHTSS4MCS2: + btc_rate_id = BTC_VHT_4SS_MCS_2; + break; + case DESC_RATEVHTSS4MCS3: + btc_rate_id = BTC_VHT_4SS_MCS_3; + break; + case DESC_RATEVHTSS4MCS4: + btc_rate_id = BTC_VHT_4SS_MCS_4; + break; + case DESC_RATEVHTSS4MCS5: + btc_rate_id = BTC_VHT_4SS_MCS_5; + break; + case DESC_RATEVHTSS4MCS6: + btc_rate_id = BTC_VHT_4SS_MCS_6; + break; + case DESC_RATEVHTSS4MCS7: + btc_rate_id = BTC_VHT_4SS_MCS_7; + break; + case DESC_RATEVHTSS4MCS8: + btc_rate_id = BTC_VHT_4SS_MCS_8; + break; + case DESC_RATEVHTSS4MCS9: + btc_rate_id = BTC_VHT_4SS_MCS_9; + break; + } + + return btc_rate_id; +} + +/* + * Description: + * Run BT-Coexist mechansim or not + * + */ +void hal_btcoex_SetBTCoexist(PADAPTER padapter, u8 bBtExist) +{ + PHAL_DATA_TYPE pHalData; + + + pHalData = GET_HAL_DATA(padapter); + pHalData->bt_coexist.bBtExist = bBtExist; +} + +/* + * Dewcription: + * Check is co-exist mechanism enabled or not + * + * Return: + * _TRUE Enable BT co-exist mechanism + * _FALSE Disable BT co-exist mechanism + */ +u8 hal_btcoex_IsBtExist(PADAPTER padapter) +{ + PHAL_DATA_TYPE pHalData; + + + pHalData = GET_HAL_DATA(padapter); + return pHalData->bt_coexist.bBtExist; +} + +u8 hal_btcoex_IsBtDisabled(PADAPTER padapter) +{ + if (!hal_btcoex_IsBtExist(padapter)) + return _TRUE; + + if (GLBtCoexist.bt_info.bt_disabled) + return _TRUE; + else + return _FALSE; +} + +void hal_btcoex_SetChipType(PADAPTER padapter, u8 chipType) +{ + PHAL_DATA_TYPE pHalData; + + pHalData = GET_HAL_DATA(padapter); + pHalData->bt_coexist.btChipType = chipType; +} + +void hal_btcoex_SetPgAntNum(PADAPTER padapter, u8 antNum) +{ + PHAL_DATA_TYPE pHalData; + + pHalData = GET_HAL_DATA(padapter); + + pHalData->bt_coexist.btTotalAntNum = antNum; +} + +u8 hal_btcoex_Initialize(PADAPTER padapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + u8 ret; + + _rtw_memset(&GLBtCoexist, 0, sizeof(GLBtCoexist)); + + ret = EXhalbtcoutsrc_InitlizeVariables((void *)padapter); + + return ret; +} + +void hal_btcoex_PowerOnSetting(PADAPTER padapter) +{ + EXhalbtcoutsrc_PowerOnSetting(&GLBtCoexist); +} + +void hal_btcoex_AntInfoSetting(PADAPTER padapter) +{ + hal_btcoex_SetBTCoexist(padapter, rtw_btcoex_get_bt_coexist(padapter)); + hal_btcoex_SetChipType(padapter, rtw_btcoex_get_chip_type(padapter)); + hal_btcoex_SetPgAntNum(padapter, rtw_btcoex_get_pg_ant_num(padapter)); + + EXhalbtcoutsrc_AntInfoSetting(padapter); +} + +void hal_btcoex_PowerOffSetting(PADAPTER padapter) +{ + /* Clear the WiFi on/off bit in scoreboard reg. if necessary */ + if (IS_HARDWARE_TYPE_8703B(padapter) || IS_HARDWARE_TYPE_8723D(padapter) + || IS_HARDWARE_TYPE_8821C(padapter) || IS_HARDWARE_TYPE_8822B(padapter) + || IS_HARDWARE_TYPE_8822C(padapter)) + rtw_write16(padapter, 0xaa, 0x8000); +} + +void hal_btcoex_PreLoadFirmware(PADAPTER padapter) +{ + EXhalbtcoutsrc_PreLoadFirmware(&GLBtCoexist); +} + +void hal_btcoex_InitHwConfig(PADAPTER padapter, u8 bWifiOnly) +{ + if (!hal_btcoex_IsBtExist(padapter)) + return; + + EXhalbtcoutsrc_init_hw_config(&GLBtCoexist, bWifiOnly); + EXhalbtcoutsrc_init_coex_dm(&GLBtCoexist); +} + +void hal_btcoex_IpsNotify(PADAPTER padapter, u8 type) +{ + EXhalbtcoutsrc_ips_notify(&GLBtCoexist, type); +} + +void hal_btcoex_LpsNotify(PADAPTER padapter, u8 type) +{ + EXhalbtcoutsrc_lps_notify(&GLBtCoexist, type); +} + +void hal_btcoex_ScanNotify(PADAPTER padapter, u8 type) +{ + EXhalbtcoutsrc_scan_notify(&GLBtCoexist, type); +} + +void hal_btcoex_ConnectNotify(PADAPTER padapter, u8 action) +{ + u8 assoType = 0; + u8 is_5g_band = _FALSE; + + is_5g_band = (padapter->mlmeextpriv.cur_channel > 14) ? _TRUE : _FALSE; + + if (action == _TRUE) { + if (is_5g_band == _TRUE) + assoType = BTC_ASSOCIATE_5G_START; + else + assoType = BTC_ASSOCIATE_START; + } + else { + if (is_5g_band == _TRUE) + assoType = BTC_ASSOCIATE_5G_FINISH; + else + assoType = BTC_ASSOCIATE_FINISH; + } + + EXhalbtcoutsrc_connect_notify(&GLBtCoexist, assoType); +} + +void hal_btcoex_MediaStatusNotify(PADAPTER padapter, u8 mediaStatus) +{ + EXhalbtcoutsrc_media_status_notify(&GLBtCoexist, mediaStatus); +} + +void hal_btcoex_SpecialPacketNotify(PADAPTER padapter, u8 pktType) +{ + EXhalbtcoutsrc_specific_packet_notify(&GLBtCoexist, pktType); +} + +void hal_btcoex_IQKNotify(PADAPTER padapter, u8 state) +{ + GLBtcWiFiInIQKState = state; +} + +void hal_btcoex_WLRFKNotify(PADAPTER padapter, u8 path, u8 type, u8 state) +{ + EXhalbtcoutsrc_WL_RFK_Notify(&GLBtCoexist, path, type, state); +} + +void hal_btcoex_BtInfoNotify(PADAPTER padapter, u8 length, u8 *tmpBuf) +{ + if (GLBtcWiFiInIQKState == _TRUE) + return; + + EXhalbtcoutsrc_bt_info_notify(&GLBtCoexist, tmpBuf, length); +} + +void hal_btcoex_BtMpRptNotify(PADAPTER padapter, u8 length, u8 *tmpBuf) +{ + u8 extid, status, len, seq; + + + if (GLBtcBtMpRptWait == _FALSE) + return; + + if ((length < 3) || (!tmpBuf)) + return; + + extid = tmpBuf[0]; + /* not response from BT FW then exit*/ + switch (extid) { + case C2H_WIFI_FW_ACTIVE_RSP: + GLBtcBtMpRptWiFiOK = _TRUE; + break; + + case C2H_TRIG_BY_BT_FW: + GLBtcBtMpRptBTOK = _TRUE; + + status = tmpBuf[1] & 0xF; + len = length - 3; + seq = tmpBuf[2] >> 4; + + GLBtcBtMpRptSeq = seq; + GLBtcBtMpRptStatus = status; + _rtw_memcpy(GLBtcBtMpRptRsp, tmpBuf + 3, len); + GLBtcBtMpRptRspSize = len; + + break; + + default: + return; + } + + if ((GLBtcBtMpRptWiFiOK == _TRUE) && (GLBtcBtMpRptBTOK == _TRUE)) { + GLBtcBtMpRptWait = _FALSE; + _cancel_timer_ex(&GLBtcBtMpOperTimer); + _rtw_up_sema(&GLBtcBtMpRptSema); + } +} + +void hal_btcoex_SuspendNotify(PADAPTER padapter, u8 state) +{ + switch (state) { + case BTCOEX_SUSPEND_STATE_SUSPEND: + EXhalbtcoutsrc_pnp_notify(&GLBtCoexist, BTC_WIFI_PNP_SLEEP); + break; + case BTCOEX_SUSPEND_STATE_SUSPEND_KEEP_ANT: + /* should switch to "#if 1" once all ICs' coex. revision are upgraded to support the KEEP_ANT case */ +#if 0 + EXhalbtcoutsrc_pnp_notify(&GLBtCoexist, BTC_WIFI_PNP_SLEEP_KEEP_ANT); +#else + EXhalbtcoutsrc_pnp_notify(&GLBtCoexist, BTC_WIFI_PNP_SLEEP); + EXhalbtcoutsrc_pnp_notify(&GLBtCoexist, BTC_WIFI_PNP_SLEEP_KEEP_ANT); +#endif + break; + case BTCOEX_SUSPEND_STATE_RESUME: +#ifdef CONFIG_FW_MULTI_PORT_SUPPORT + /* re-download FW after resume, inform WL FW port number */ + rtw_hal_set_wifi_btc_port_id_cmd(GLBtCoexist.Adapter); +#endif + EXhalbtcoutsrc_pnp_notify(&GLBtCoexist, BTC_WIFI_PNP_WAKE_UP); + break; + } +} + +void hal_btcoex_HaltNotify(PADAPTER padapter, u8 do_halt) +{ + if (do_halt == 1) + EXhalbtcoutsrc_halt_notify(&GLBtCoexist); + + GLBtCoexist.bBinded = _FALSE; + GLBtCoexist.Adapter = NULL; +} + +void hal_btcoex_SwitchBtTRxMask(PADAPTER padapter) +{ + EXhalbtcoutsrc_SwitchBtTRxMask(&GLBtCoexist); +} + +void hal_btcoex_Hanlder(PADAPTER padapter) +{ + u32 bt_patch_ver; + + EXhalbtcoutsrc_periodical(&GLBtCoexist); + + if (GLBtCoexist.bt_info.bt_get_fw_ver == 0) { + GLBtCoexist.btc_get(&GLBtCoexist, BTC_GET_U4_BT_PATCH_VER, &bt_patch_ver); + GLBtCoexist.bt_info.bt_get_fw_ver = bt_patch_ver; + } +} + +s32 hal_btcoex_IsBTCoexRejectAMPDU(PADAPTER padapter) +{ + return (s32)GLBtCoexist.bt_info.reject_agg_pkt; +} + +s32 hal_btcoex_IsBTCoexCtrlAMPDUSize(PADAPTER padapter) +{ + return (s32)GLBtCoexist.bt_info.bt_ctrl_agg_buf_size; +} + +u32 hal_btcoex_GetAMPDUSize(PADAPTER padapter) +{ + return (u32)GLBtCoexist.bt_info.agg_buf_size; +} + +void hal_btcoex_SetManualControl(PADAPTER padapter, u8 bmanual) +{ + GLBtCoexist.manual_control = bmanual; +} + +void hal_btcoex_set_policy_control(PADAPTER padapter, u8 btc_policy) +{ + switch (btc_policy) { + case BTCOEX_POLICY_CONTROL_AUTO: + GLBtCoexist.coex_sta.force_freerun = _FALSE; + GLBtCoexist.coex_sta.force_tdd = _FALSE; + break; + case BTCOEX_POLICY_CONTROL_FORCE_FREERUN: + GLBtCoexist.coex_sta.force_freerun = _TRUE; + GLBtCoexist.coex_sta.force_tdd = _FALSE; + break; + case BTCOEX_POLICY_CONTROL_FORCE_TDMA: + GLBtCoexist.coex_sta.force_freerun = _FALSE; + GLBtCoexist.coex_sta.force_tdd = _TRUE; + break; + } +} + +u8 hal_btcoex_1Ant(PADAPTER padapter) +{ + if (hal_btcoex_IsBtExist(padapter) == _FALSE) + return _FALSE; + + if (GLBtCoexist.board_info.btdm_ant_num == 1) + return _TRUE; + + return _FALSE; +} + +u8 hal_btcoex_IsBtControlLps(PADAPTER padapter) +{ + if (GLBtCoexist.bdontenterLPS == _TRUE) + return _TRUE; + + if (hal_btcoex_IsBtExist(padapter) == _FALSE) + return _FALSE; + + if (GLBtCoexist.bt_info.bt_disabled) + return _FALSE; + + if (GLBtCoexist.bt_info.bt_ctrl_lps) + return _TRUE; + + return _FALSE; +} + +u8 hal_btcoex_IsLpsOn(PADAPTER padapter) +{ + if (GLBtCoexist.bdontenterLPS == _TRUE) + return _FALSE; + + if (hal_btcoex_IsBtExist(padapter) == _FALSE) + return _FALSE; + + if (GLBtCoexist.bt_info.bt_disabled) + return _FALSE; + + if (GLBtCoexist.bt_info.bt_lps_on) + return _TRUE; + + return _FALSE; +} + +u8 hal_btcoex_RpwmVal(PADAPTER padapter) +{ + return GLBtCoexist.bt_info.rpwm_val; +} + +u8 hal_btcoex_LpsVal(PADAPTER padapter) +{ + return GLBtCoexist.bt_info.lps_val; +} + +u32 hal_btcoex_GetRaMask(PADAPTER padapter) +{ + if (!hal_btcoex_IsBtExist(padapter)) + return 0; + + if (GLBtCoexist.bt_info.bt_disabled) + return 0; + + /* Modify by YiWei , suggest by Cosa and Jenyu + * Remove the limit antenna number , because 2 antenna case (ex: 8192eu)also want to get BT coex report rate mask. + */ + /*if (GLBtCoexist.board_info.btdm_ant_num != 1) + return 0;*/ + + return GLBtCoexist.bt_info.ra_mask; +} + +u8 hal_btcoex_query_reduced_wl_pwr_lvl(PADAPTER padapter) +{ + return GLBtCoexist.coex_dm.cur_wl_pwr_lvl; +} + +void hal_btcoex_set_reduced_wl_pwr_lvl(PADAPTER padapter, u8 val) +{ + GLBtCoexist.coex_dm.cur_wl_pwr_lvl = val; +} + +void hal_btcoex_do_reduce_wl_pwr_lvl(PADAPTER padapter) +{ + halbtcoutsrc_reduce_wl_tx_power(&GLBtCoexist, 0); +} + +void hal_btcoex_RecordPwrMode(PADAPTER padapter, u8 *pCmdBuf, u8 cmdLen) +{ + + _rtw_memcpy(GLBtCoexist.pwrModeVal, pCmdBuf, cmdLen); +} + +void hal_btcoex_DisplayBtCoexInfo(PADAPTER padapter, u8 *pbuf, u32 bufsize) +{ + PBTCDBGINFO pinfo; + + + pinfo = &GLBtcDbgInfo; + DBG_BT_INFO_INIT(pinfo, pbuf, bufsize); + EXhalbtcoutsrc_DisplayBtCoexInfo(&GLBtCoexist); + DBG_BT_INFO_INIT(pinfo, NULL, 0); +} + +void hal_btcoex_SetDBG(PADAPTER padapter, u32 *pDbgModule) +{ + u32 i; + + + if (NULL == pDbgModule) + return; + + for (i = 0; i < COMP_MAX; i++) + GLBtcDbgType[i] = pDbgModule[i]; +} + +u32 hal_btcoex_GetDBG(PADAPTER padapter, u8 *pStrBuf, u32 bufSize) +{ + s32 count; + u8 *pstr; + u32 leftSize; + + + if ((NULL == pStrBuf) || (0 == bufSize)) + return 0; + + count = 0; + pstr = pStrBuf; + leftSize = bufSize; + /* RTW_INFO(FUNC_ADPT_FMT ": bufsize=%d\n", FUNC_ADPT_ARG(padapter), bufSize); */ + + count = rtw_sprintf(pstr, leftSize, "#define DBG\t%d\n", DBG); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + + count = rtw_sprintf(pstr, leftSize, "BTCOEX Debug Setting:\n"); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + + count = rtw_sprintf(pstr, leftSize, + "COMP_COEX: 0x%08X\n\n", + GLBtcDbgType[COMP_COEX]); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + +#if 0 + count = rtw_sprintf(pstr, leftSize, "INTERFACE Debug Setting Definition:\n"); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + count = rtw_sprintf(pstr, leftSize, "\tbit[0]=%d for INTF_INIT\n", + GLBtcDbgType[BTC_MSG_INTERFACE] & INTF_INIT ? 1 : 0); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + count = rtw_sprintf(pstr, leftSize, "\tbit[2]=%d for INTF_NOTIFY\n\n", + GLBtcDbgType[BTC_MSG_INTERFACE] & INTF_NOTIFY ? 1 : 0); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + + count = rtw_sprintf(pstr, leftSize, "ALGORITHM Debug Setting Definition:\n"); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + count = rtw_sprintf(pstr, leftSize, "\tbit[0]=%d for BT_RSSI_STATE\n", + GLBtcDbgType[BTC_MSG_ALGORITHM] & ALGO_BT_RSSI_STATE ? 1 : 0); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + count = rtw_sprintf(pstr, leftSize, "\tbit[1]=%d for WIFI_RSSI_STATE\n", + GLBtcDbgType[BTC_MSG_ALGORITHM] & ALGO_WIFI_RSSI_STATE ? 1 : 0); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + count = rtw_sprintf(pstr, leftSize, "\tbit[2]=%d for BT_MONITOR\n", + GLBtcDbgType[BTC_MSG_ALGORITHM] & ALGO_BT_MONITOR ? 1 : 0); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + count = rtw_sprintf(pstr, leftSize, "\tbit[3]=%d for TRACE\n", + GLBtcDbgType[BTC_MSG_ALGORITHM] & ALGO_TRACE ? 1 : 0); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + count = rtw_sprintf(pstr, leftSize, "\tbit[4]=%d for TRACE_FW\n", + GLBtcDbgType[BTC_MSG_ALGORITHM] & ALGO_TRACE_FW ? 1 : 0); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + count = rtw_sprintf(pstr, leftSize, "\tbit[5]=%d for TRACE_FW_DETAIL\n", + GLBtcDbgType[BTC_MSG_ALGORITHM] & ALGO_TRACE_FW_DETAIL ? 1 : 0); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + count = rtw_sprintf(pstr, leftSize, "\tbit[6]=%d for TRACE_FW_EXEC\n", + GLBtcDbgType[BTC_MSG_ALGORITHM] & ALGO_TRACE_FW_EXEC ? 1 : 0); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + count = rtw_sprintf(pstr, leftSize, "\tbit[7]=%d for TRACE_SW\n", + GLBtcDbgType[BTC_MSG_ALGORITHM] & ALGO_TRACE_SW ? 1 : 0); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + count = rtw_sprintf(pstr, leftSize, "\tbit[8]=%d for TRACE_SW_DETAIL\n", + GLBtcDbgType[BTC_MSG_ALGORITHM] & ALGO_TRACE_SW_DETAIL ? 1 : 0); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; + count = rtw_sprintf(pstr, leftSize, "\tbit[9]=%d for TRACE_SW_EXEC\n", + GLBtcDbgType[BTC_MSG_ALGORITHM] & ALGO_TRACE_SW_EXEC ? 1 : 0); + if ((count < 0) || (count >= leftSize)) + goto exit; + pstr += count; + leftSize -= count; +#endif + +exit: + count = pstr - pStrBuf; + /* RTW_INFO(FUNC_ADPT_FMT ": usedsize=%d\n", FUNC_ADPT_ARG(padapter), count); */ + + return count; +} + +u8 hal_btcoex_IncreaseScanDeviceNum(PADAPTER padapter) +{ + if (!hal_btcoex_IsBtExist(padapter)) + return _FALSE; + + if (GLBtCoexist.bt_info.increase_scan_dev_num) + return _TRUE; + + return _FALSE; +} + +u8 hal_btcoex_IsBtLinkExist(PADAPTER padapter) +{ + if (GLBtCoexist.bt_link_info.bt_link_exist) + return _TRUE; + + return _FALSE; +} + +void hal_btcoex_SetBtPatchVersion(PADAPTER padapter, u16 btHciVer, u16 btPatchVer) +{ + EXhalbtcoutsrc_SetBtPatchVersion(btHciVer, btPatchVer); +} + +void hal_btcoex_SetHciVersion(PADAPTER padapter, u16 hciVersion) +{ + EXhalbtcoutsrc_SetHciVersion(hciVersion); +} + +void hal_btcoex_StackUpdateProfileInfo(void) +{ + EXhalbtcoutsrc_StackUpdateProfileInfo(); +} + +void hal_btcoex_pta_off_on_notify(PADAPTER padapter, u8 bBTON) +{ + ex_halbtcoutsrc_pta_off_on_notify(&GLBtCoexist, bBTON); +} + +/* + * Description: + * Setting BT coex antenna isolation type . + * coex mechanisn/ spital stream/ best throughput + * anttype = 0 , PSTDMA / 2SS / 0.5T , bad isolation , WiFi/BT ANT Distance<15cm , (<20dB) for 2,3 antenna + * anttype = 1 , PSTDMA / 1SS / 0.5T , normal isolaiton , 50cm>WiFi/BT ANT Distance>15cm , (>20dB) for 2 antenna + * anttype = 2 , TDMA / 2SS / T , normal isolaiton , 50cm>WiFi/BT ANT Distance>15cm , (>20dB) for 3 antenna + * anttype = 3 , no TDMA / 1SS / 0.5T , good isolation , WiFi/BT ANT Distance >50cm , (>40dB) for 2 antenna + * anttype = 4 , no TDMA / 2SS / T , good isolation , WiFi/BT ANT Distance >50cm , (>40dB) for 3 antenna + * wifi only throughput ~ T + * wifi/BT share one antenna with SPDT + */ +void hal_btcoex_SetAntIsolationType(PADAPTER padapter, u8 anttype) +{ + PHAL_DATA_TYPE pHalData; + PBTC_COEXIST pBtCoexist = &GLBtCoexist; + + /*RTW_INFO("####%s , anttype = %d , %d\n" , __func__ , anttype , __LINE__); */ + pHalData = GET_HAL_DATA(padapter); + + + pHalData->bt_coexist.btAntisolation = anttype; + + switch (pHalData->bt_coexist.btAntisolation) { + case 0: + pBtCoexist->board_info.ant_type = (u8)BTC_ANT_TYPE_0; + break; + case 1: + pBtCoexist->board_info.ant_type = (u8)BTC_ANT_TYPE_1; + break; + case 2: + pBtCoexist->board_info.ant_type = (u8)BTC_ANT_TYPE_2; + break; + case 3: + pBtCoexist->board_info.ant_type = (u8)BTC_ANT_TYPE_3; + break; + case 4: + pBtCoexist->board_info.ant_type = (u8)BTC_ANT_TYPE_4; + break; + } + +} + +#ifdef CONFIG_LOAD_PHY_PARA_FROM_FILE +int +hal_btcoex_ParseAntIsolationConfigFile( + PADAPTER Adapter, + char *buffer +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + u32 i = 0 , j = 0; + char *szLine , *ptmp; + int rtStatus = _SUCCESS; + char param_value_string[10]; + u8 param_value; + u8 anttype = 4; + + u8 ant_num = 3 , ant_distance = 50 , rfe_type = 1; + + typedef struct ant_isolation { + char *param_name; /* antenna isolation config parameter name */ + u8 *value; /* antenna isolation config parameter value */ + } ANT_ISOLATION; + + ANT_ISOLATION ant_isolation_param[] = { + {"ANT_NUMBER" , &ant_num}, + {"ANT_DISTANCE" , &ant_distance}, + {"RFE_TYPE" , &rfe_type}, + {NULL , 0} + }; + + + + /* RTW_INFO("===>Hal_ParseAntIsolationConfigFile()\n" ); */ + + ptmp = buffer; + for (szLine = GetLineFromBuffer(ptmp) ; szLine != NULL; szLine = GetLineFromBuffer(ptmp)) { + /* skip comment */ + if (IsCommentString(szLine)) + continue; + + /* RTW_INFO("%s : szLine = %s , strlen(szLine) = %d\n" , __func__ , szLine , strlen(szLine));*/ + for (j = 0 ; ant_isolation_param[j].param_name != NULL ; j++) { + if (strstr(szLine , ant_isolation_param[j].param_name) != NULL) { + i = 0; + while (i < strlen(szLine)) { + if (szLine[i] != '"') + ++i; + else { + /* skip only has one " */ + if (strpbrk(szLine , "\"") == strrchr(szLine , '"')) { + RTW_INFO("Fail to parse parameters , format error!\n"); + break; + } + _rtw_memset((void *)param_value_string , 0 , 10); + if (!ParseQualifiedString(szLine , &i , param_value_string , '"' , '"')) { + RTW_INFO("Fail to parse parameters\n"); + return _FAIL; + } else if (!GetU1ByteIntegerFromStringInDecimal(param_value_string , ant_isolation_param[j].value)) + RTW_INFO("Fail to GetU1ByteIntegerFromStringInDecimal\n"); + + break; + } + } + } + } + } + + /* YiWei 20140716 , for BT coex antenna isolation control */ + /* rfe_type = 0 was SPDT , rfe_type = 1 was coupler */ + if (ant_num == 3 && ant_distance >= 50) + anttype = 3; + else if (ant_num == 2 && ant_distance >= 50 && rfe_type == 1) + anttype = 2; + else if (ant_num == 3 && ant_distance >= 15 && ant_distance < 50) + anttype = 2; + else if (ant_num == 2 && ant_distance >= 15 && ant_distance < 50 && rfe_type == 1) + anttype = 2; + else if ((ant_num == 2 && ant_distance < 15 && rfe_type == 1) || (ant_num == 3 && ant_distance < 15)) + anttype = 1; + else if (ant_num == 2 && rfe_type == 0) + anttype = 0; + else + anttype = 0; + + hal_btcoex_SetAntIsolationType(Adapter, anttype); + + RTW_INFO("%s : ant_num = %d\n" , __func__ , ant_num); + RTW_INFO("%s : ant_distance = %d\n" , __func__ , ant_distance); + RTW_INFO("%s : rfe_type = %d\n" , __func__ , rfe_type); + /* RTW_INFO("<===Hal_ParseAntIsolationConfigFile()\n"); */ + return rtStatus; +} + + +int +hal_btcoex_AntIsolationConfig_ParaFile( + PADAPTER Adapter, + char *pFileName +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + int rlen = 0 , rtStatus = _FAIL; + + _rtw_memset(pHalData->para_file_buf , 0 , MAX_PARA_FILE_BUF_LEN); + + rtw_get_phy_file_path(Adapter, pFileName); + if (rtw_is_file_readable(rtw_phy_para_file_path) == _TRUE) { + rlen = rtw_retrieve_from_file(rtw_phy_para_file_path, pHalData->para_file_buf, MAX_PARA_FILE_BUF_LEN); + if (rlen > 0) + rtStatus = _SUCCESS; + } + + + if (rtStatus == _SUCCESS) { + /*RTW_INFO("%s(): read %s ok\n", __func__ , pFileName);*/ + rtStatus = hal_btcoex_ParseAntIsolationConfigFile(Adapter , pHalData->para_file_buf); + } else + RTW_INFO("%s(): No File %s, Load from *** Array!\n" , __func__ , pFileName); + + return rtStatus; +} +#endif /* CONFIG_LOAD_PHY_PARA_FROM_FILE */ + +u16 hal_btcoex_btreg_read(PADAPTER padapter, u8 type, u16 addr, u32 *data) +{ + u16 ret = 0; + + halbtcoutsrc_LeaveLowPower(&GLBtCoexist); + + ret = halbtcoutsrc_GetBtReg_with_status(&GLBtCoexist, type, addr, data); + + halbtcoutsrc_NormalLowPower(&GLBtCoexist); + + return ret; +} + +u16 hal_btcoex_btreg_write(PADAPTER padapter, u8 type, u16 addr, u16 val) +{ + u16 ret = 0; + + halbtcoutsrc_LeaveLowPower(&GLBtCoexist); + + ret = halbtcoutsrc_SetBtReg(&GLBtCoexist, type, addr, val); + + halbtcoutsrc_NormalLowPower(&GLBtCoexist); + + return ret; +} + +void hal_btcoex_set_rfe_type(u8 type) +{ + EXhalbtcoutsrc_set_rfe_type(type); +} + +#ifdef CONFIG_RF4CE_COEXIST +void hal_btcoex_set_rf4ce_link_state(u8 state) +{ + EXhalbtcoutsrc_set_rf4ce_link_state(state); +} + +u8 hal_btcoex_get_rf4ce_link_state(void) +{ + return EXhalbtcoutsrc_get_rf4ce_link_state(); +} +#endif /* CONFIG_RF4CE_COEXIST */ + +void hal_btcoex_switchband_notify(u8 under_scan, u8 band_type) +{ + switch (band_type) { + case BAND_ON_2_4G: + if (under_scan) + EXhalbtcoutsrc_switchband_notify(&GLBtCoexist, BTC_SWITCH_TO_24G); + else + EXhalbtcoutsrc_switchband_notify(&GLBtCoexist, BTC_SWITCH_TO_24G_NOFORSCAN); + break; + case BAND_ON_5G: + EXhalbtcoutsrc_switchband_notify(&GLBtCoexist, BTC_SWITCH_TO_5G); + break; + default: + RTW_INFO("[BTCOEX] unkown switch band type\n"); + break; + } +} + +void hal_btcoex_WlFwDbgInfoNotify(PADAPTER padapter, u8* tmpBuf, u8 length) +{ + EXhalbtcoutsrc_WlFwDbgInfoNotify(&GLBtCoexist, tmpBuf, length); +} + +void hal_btcoex_rx_rate_change_notify(PADAPTER padapter, u8 is_data_frame, u8 rate_id) +{ + EXhalbtcoutsrc_rx_rate_change_notify(&GLBtCoexist, is_data_frame, EXhalbtcoutsrc_rate_id_to_btc_rate_id(rate_id)); +} + +u16 hal_btcoex_btset_testode(PADAPTER padapter, u8 type) +{ + u16 ret = 0; + + halbtcoutsrc_LeaveLowPower(&GLBtCoexist); + + ret = halbtcoutsrc_setbttestmode(&GLBtCoexist, type); + + halbtcoutsrc_NormalLowPower(&GLBtCoexist); + + return ret; +} + +#endif /* CONFIG_BT_COEXIST */ diff --git a/hal/hal_btcoex_wifionly.c b/hal/hal_btcoex_wifionly.c new file mode 100644 index 0000000..a9ef07f --- /dev/null +++ b/hal/hal_btcoex_wifionly.c @@ -0,0 +1,279 @@ +/****************************************************************************** + * + * Copyright(c) 2016 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#include + +#if (CONFIG_BTCOEX_SUPPORT_WIFI_ONLY_CFG == 1) + +#include "btc/mp_precomp.h" + +struct wifi_only_cfg GLBtCoexistWifiOnly; + +void halwifionly_write1byte(void *pwifionlyContext, u32 RegAddr, u8 Data) +{ + struct wifi_only_cfg *pwifionlycfg = (struct wifi_only_cfg *)pwifionlyContext; + PADAPTER Adapter = pwifionlycfg->Adapter; + + rtw_write8(Adapter, RegAddr, Data); +} + +void halwifionly_write2byte(void *pwifionlyContext, u32 RegAddr, u16 Data) +{ + struct wifi_only_cfg *pwifionlycfg = (struct wifi_only_cfg *)pwifionlyContext; + PADAPTER Adapter = pwifionlycfg->Adapter; + + rtw_write16(Adapter, RegAddr, Data); +} + +void halwifionly_write4byte(void *pwifionlyContext, u32 RegAddr, u32 Data) +{ + struct wifi_only_cfg *pwifionlycfg = (struct wifi_only_cfg *)pwifionlyContext; + PADAPTER Adapter = pwifionlycfg->Adapter; + + rtw_write32(Adapter, RegAddr, Data); +} + +u8 halwifionly_read1byte(void *pwifionlyContext, u32 RegAddr) +{ + struct wifi_only_cfg *pwifionlycfg = (struct wifi_only_cfg *)pwifionlyContext; + PADAPTER Adapter = pwifionlycfg->Adapter; + + return rtw_read8(Adapter, RegAddr); +} + +u16 halwifionly_read2byte(void * pwifionlyContext, u32 RegAddr) +{ + struct wifi_only_cfg *pwifionlycfg = (struct wifi_only_cfg *)pwifionlyContext; + PADAPTER Adapter = pwifionlycfg->Adapter; + + return rtw_read16(Adapter, RegAddr); +} + +u32 halwifionly_read4byte(void *pwifionlyContext, u32 RegAddr) +{ + struct wifi_only_cfg *pwifionlycfg = (struct wifi_only_cfg *)pwifionlyContext; + PADAPTER Adapter = pwifionlycfg->Adapter; + + return rtw_read32(Adapter, RegAddr); +} + +void halwifionly_bitmaskwrite1byte(void *pwifionlyContext, u32 regAddr, u8 bitMask, u8 data) +{ + u8 originalValue, bitShift = 0; + u8 i; + + struct wifi_only_cfg *pwifionlycfg = (struct wifi_only_cfg *)pwifionlyContext; + PADAPTER Adapter = pwifionlycfg->Adapter; + + if (bitMask != 0xff) { + originalValue = rtw_read8(Adapter, regAddr); + for (i = 0; i <= 7; i++) { + if ((bitMask >> i) & 0x1) + break; + } + bitShift = i; + data = ((originalValue) & (~bitMask)) | (((data << bitShift)) & bitMask); + } + rtw_write8(Adapter, regAddr, data); +} + +void halwifionly_phy_set_rf_reg(void *pwifionlyContext, enum rf_path eRFPath, u32 RegAddr, u32 BitMask, u32 Data) +{ + struct wifi_only_cfg *pwifionlycfg = (struct wifi_only_cfg *)pwifionlyContext; + PADAPTER Adapter = pwifionlycfg->Adapter; + + phy_set_rf_reg(Adapter, eRFPath, RegAddr, BitMask, Data); +} + +void halwifionly_phy_set_bb_reg(void *pwifionlyContext, u32 RegAddr, u32 BitMask, u32 Data) +{ + struct wifi_only_cfg *pwifionlycfg = (struct wifi_only_cfg *)pwifionlyContext; + PADAPTER Adapter = pwifionlycfg->Adapter; + + phy_set_bb_reg(Adapter, RegAddr, BitMask, Data); +} + +void hal_btcoex_wifionly_switchband_notify(PADAPTER padapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + u8 is_5g = _FALSE; + + if (pHalData->current_band_type == BAND_ON_5G) + is_5g = _TRUE; + + if (IS_HARDWARE_TYPE_8822B(padapter)) { +#ifdef CONFIG_RTL8822B + ex_hal8822b_wifi_only_switchbandnotify(&GLBtCoexistWifiOnly, is_5g); +#endif + } + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(padapter)) + ex_hal8821c_wifi_only_switchbandnotify(&GLBtCoexistWifiOnly, is_5g); +#endif + +#ifdef CONFIG_RTL8822C + else if (IS_HARDWARE_TYPE_8822C(padapter)) + ex_hal8822c_wifi_only_switchbandnotify(&GLBtCoexistWifiOnly, is_5g); +#endif + +#ifdef CONFIG_RTL8814B + else if (IS_HARDWARE_TYPE_8814B(padapter)) + ex_hal8814b_wifi_only_switchbandnotify(&GLBtCoexistWifiOnly, is_5g); +#endif + +#ifdef CONFIG_RTL8723F + else if (IS_HARDWARE_TYPE_8723F(padapter)) + ex_hal8723f_wifi_only_switchbandnotify(&GLBtCoexistWifiOnly, is_5g); +#endif +} + +void hal_btcoex_wifionly_scan_notify(PADAPTER padapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + u8 is_5g = _FALSE; + + if (pHalData->current_band_type == BAND_ON_5G) + is_5g = _TRUE; + + if (IS_HARDWARE_TYPE_8822B(padapter)) { +#ifdef CONFIG_RTL8822B + ex_hal8822b_wifi_only_scannotify(&GLBtCoexistWifiOnly, is_5g); +#endif + } + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(padapter)) + ex_hal8821c_wifi_only_scannotify(&GLBtCoexistWifiOnly, is_5g); +#endif + +#ifdef CONFIG_RTL8822C + else if (IS_HARDWARE_TYPE_8822C(padapter)) + ex_hal8822c_wifi_only_scannotify(&GLBtCoexistWifiOnly, is_5g); +#endif + +#ifdef CONFIG_RTL8814B + else if (IS_HARDWARE_TYPE_8814B(padapter)) + ex_hal8814b_wifi_only_scannotify(&GLBtCoexistWifiOnly, is_5g); +#endif +} + +void hal_btcoex_wifionly_connect_notify(PADAPTER padapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + u8 is_5g = _FALSE; + + if (pHalData->current_band_type == BAND_ON_5G) + is_5g = _TRUE; + + if (IS_HARDWARE_TYPE_8822B(padapter)) { +#ifdef CONFIG_RTL8822B + ex_hal8822b_wifi_only_connectnotify(&GLBtCoexistWifiOnly, is_5g); +#endif + } + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(padapter)) + ex_hal8821c_wifi_only_connectnotify(&GLBtCoexistWifiOnly, is_5g); +#endif + +#ifdef CONFIG_RTL8822C + else if (IS_HARDWARE_TYPE_8822C(padapter)) + ex_hal8822c_wifi_only_connectnotify(&GLBtCoexistWifiOnly, is_5g); +#endif + +#ifdef CONFIG_RTL8814B + else if (IS_HARDWARE_TYPE_8814B(padapter)) + ex_hal8814b_wifi_only_connectnotify(&GLBtCoexistWifiOnly, is_5g); +#endif + +#ifdef CONFIG_RTL8723F + else if (IS_HARDWARE_TYPE_8723F(padapter)) + ex_hal8723f_wifi_only_connectnotify(&GLBtCoexistWifiOnly, is_5g); +#endif +} + +void hal_btcoex_wifionly_hw_config(PADAPTER padapter) +{ + struct wifi_only_cfg *pwifionlycfg = &GLBtCoexistWifiOnly; + + if (IS_HARDWARE_TYPE_8723B(padapter)) { +#ifdef CONFIG_RTL8723B + ex_hal8723b_wifi_only_hw_config(pwifionlycfg); +#endif + } + +#ifdef CONFIG_RTL8822B + else if (IS_HARDWARE_TYPE_8822B(padapter)) + ex_hal8822b_wifi_only_hw_config(pwifionlycfg); +#endif + +#ifdef CONFIG_RTL8821C + else if (IS_HARDWARE_TYPE_8821C(padapter)) + ex_hal8821c_wifi_only_hw_config(pwifionlycfg); +#endif + +#ifdef CONFIG_RTL8822C + else if (IS_HARDWARE_TYPE_8822C(padapter)) + ex_hal8822c_wifi_only_hw_config(pwifionlycfg); +#endif + +#ifdef CONFIG_RTL8814B + else if (IS_HARDWARE_TYPE_8814B(padapter)) + ex_hal8814b_wifi_only_hw_config(pwifionlycfg); +#endif + +#ifdef CONFIG_RTL8723F + else if (IS_HARDWARE_TYPE_8723F(padapter)) + ex_hal8723f_wifi_only_hw_config(pwifionlycfg); +#endif +} + +void hal_btcoex_wifionly_initlizevariables(PADAPTER padapter) +{ + struct wifi_only_cfg *pwifionlycfg = &GLBtCoexistWifiOnly; + struct wifi_only_haldata *pwifionly_haldata = &pwifionlycfg->haldata_info; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + + _rtw_memset(&GLBtCoexistWifiOnly, 0, sizeof(GLBtCoexistWifiOnly)); + + pwifionlycfg->Adapter = padapter; + +#ifdef CONFIG_PCI_HCI + pwifionlycfg->chip_interface = WIFIONLY_INTF_PCI; +#elif defined(CONFIG_USB_HCI) + pwifionlycfg->chip_interface = WIFIONLY_INTF_USB; +#elif defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) + pwifionlycfg->chip_interface = WIFIONLY_INTF_SDIO; +#else + pwifionlycfg->chip_interface = WIFIONLY_INTF_UNKNOWN; +#endif + + pwifionly_haldata->customer_id = CUSTOMER_NORMAL; +} + +void hal_btcoex_wifionly_AntInfoSetting(PADAPTER padapter) +{ + struct wifi_only_cfg *pwifionlycfg = &GLBtCoexistWifiOnly; + struct wifi_only_haldata *pwifionly_haldata = &pwifionlycfg->haldata_info; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + + pwifionly_haldata->efuse_pg_antnum = pHalData->EEPROMBluetoothAntNum; + pwifionly_haldata->efuse_pg_antpath = pHalData->ant_path; + pwifionly_haldata->rfe_type = pHalData->rfe_type; + pwifionly_haldata->ant_div_cfg = pHalData->AntDivCfg; +} + +#endif + diff --git a/hal/hal_com.c b/hal/hal_com.c new file mode 100644 index 0000000..bad695b --- /dev/null +++ b/hal/hal_com.c @@ -0,0 +1,16702 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#define _HAL_COM_C_ + +#include +#include "hal_com_h2c.h" + +#include "hal_data.h" + +#ifdef RTW_HALMAC +#include "../../hal/hal_halmac.h" +#endif + +void rtw_dump_fw_info(void *sel, _adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = NULL; + + if (!adapter) + return; + + hal_data = GET_HAL_DATA(adapter); + if (hal_data->bFWReady) + RTW_PRINT_SEL(sel, "FW VER -%d.%d\n", hal_data->firmware_version, hal_data->firmware_sub_version); + else + RTW_PRINT_SEL(sel, "FW not ready\n"); +} + +bool rsvd_page_cache_update_all(struct rsvd_page_cache_t *cache, u8 loc + , u8 txdesc_len, u32 page_size, u8 *info, u32 info_len) +{ + u8 page_num; + bool modified = 0; + bool loc_mod = 0, size_mod = 0, page_num_mod = 0; + + page_num = info_len ? (u8)PageNum(txdesc_len + info_len, page_size) : 0; + if (!info_len) + loc = 0; + + if (cache->loc != loc) { + RTW_INFO("%s %s loc change (%u -> %u)\n" + , __func__, cache->name, cache->loc, loc); + loc_mod = 1; + } + if (cache->size != info_len) { + RTW_INFO("%s %s size change (%u -> %u)\n" + , __func__, cache->name, cache->size, info_len); + size_mod = 1; + } + if (cache->page_num != page_num) { + RTW_INFO("%s %s page_num change (%u -> %u)\n" + , __func__, cache->name, cache->page_num, page_num); + page_num_mod = 1; + } + + if (info && info_len) { + if (cache->data) { + if (cache->size == info_len) { + if (_rtw_memcmp(cache->data, info, info_len) != _TRUE) { + RTW_INFO("%s %s data change\n", __func__, cache->name); + modified = 1; + } + } else + rsvd_page_cache_free_data(cache); + } + + if (!cache->data) { + cache->data = rtw_malloc(info_len); + if (!cache->data) { + RTW_ERR("%s %s alloc data with size(%u) fail\n" + , __func__, cache->name, info_len); + rtw_warn_on(1); + } else { + RTW_INFO("%s %s alloc data with size(%u)\n" + , __func__, cache->name, info_len); + } + modified = 1; + } + + if (cache->data && modified) + _rtw_memcpy(cache->data, info, info_len); + } else { + if (cache->data && size_mod) + rsvd_page_cache_free_data(cache); + } + + cache->loc = loc; + cache->page_num = page_num; + cache->size = info_len; + + return modified | loc_mod | size_mod | page_num_mod; +} + +bool rsvd_page_cache_update_data(struct rsvd_page_cache_t *cache, u8 *info, u32 info_len) +{ + bool modified = 0; + + if (!info || !info_len) { + RTW_WARN("%s %s invalid input(info:%p, info_len:%u)\n" + , __func__, cache->name, info, info_len); + goto exit; + } + + if (!cache->loc || !cache->page_num || !cache->size) { + RTW_ERR("%s %s layout not ready(loc:%u, page_num:%u, size:%u)\n" + , __func__, cache->name, cache->loc, cache->page_num, cache->size); + rtw_warn_on(1); + goto exit; + } + + if (cache->size != info_len) { + RTW_ERR("%s %s size(%u) differ with info_len(%u)\n" + , __func__, cache->name, cache->size, info_len); + rtw_warn_on(1); + goto exit; + } + + if (!cache->data) { + cache->data = rtw_zmalloc(cache->size); + if (!cache->data) { + RTW_ERR("%s %s alloc data with size(%u) fail\n" + , __func__, cache->name, cache->size); + rtw_warn_on(1); + goto exit; + } else { + RTW_INFO("%s %s alloc data with size(%u)\n" + , __func__, cache->name, info_len); + } + modified = 1; + } + + if (_rtw_memcmp(cache->data, info, cache->size) == _FALSE) { + RTW_INFO("%s %s data change\n", __func__, cache->name); + _rtw_memcpy(cache->data, info, cache->size); + modified = 1; + } + +exit: + return modified; +} + +void rsvd_page_cache_free_data(struct rsvd_page_cache_t *cache) +{ + if (cache->data) { + rtw_mfree(cache->data, cache->size); + cache->data = NULL; + } +} + +void rsvd_page_cache_free(struct rsvd_page_cache_t *cache) +{ + cache->loc = 0; + cache->page_num = 0; + rsvd_page_cache_free_data(cache); + cache->size = 0; +} + +/* #define CONFIG_GTK_OL_DBG */ + +/*#define DBG_SEC_CAM_MOVE*/ +#ifdef DBG_SEC_CAM_MOVE +void rtw_hal_move_sta_gk_to_dk(_adapter *adapter) +{ + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + int cam_id, index = 0; + u8 *addr = NULL; + + if (!MLME_IS_STA(adapter)) + return; + + addr = get_bssid(pmlmepriv); + + if (addr == NULL) { + RTW_INFO("%s: get bssid MAC addr fail!!\n", __func__); + return; + } + + rtw_clean_dk_section(adapter); + + do { + cam_id = rtw_camid_search(adapter, addr, index, 1); + + if (cam_id == -1) + RTW_INFO("%s: cam_id: %d, key_id:%d\n", __func__, cam_id, index); + else + rtw_sec_cam_swap(adapter, cam_id, index); + + index++; + } while (index < 4); + +} + +void rtw_hal_read_sta_dk_key(_adapter *adapter, u8 key_id) +{ + struct security_priv *psecuritypriv = &adapter->securitypriv; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct cam_ctl_t *cam_ctl = &dvobj->cam_ctl; + _irqL irqL; + u8 get_key[16]; + + _rtw_memset(get_key, 0, sizeof(get_key)); + + if (key_id > 4) { + RTW_INFO("%s [ERROR] gtk_keyindex:%d invalid\n", __func__, key_id); + rtw_warn_on(1); + return; + } + rtw_sec_read_cam_ent(adapter, key_id, NULL, NULL, get_key); + + /*update key into related sw variable*/ + _enter_critical_bh(&cam_ctl->lock, &irqL); + if (_rtw_camid_is_gk(adapter, key_id)) { + RTW_INFO("[HW KEY] -Key-id:%d "KEY_FMT"\n", key_id, KEY_ARG(get_key)); + RTW_INFO("[cam_cache KEY] - Key-id:%d "KEY_FMT"\n", key_id, KEY_ARG(&dvobj->cam_cache[key_id].key)); + } + _exit_critical_bh(&cam_ctl->lock, &irqL); + +} +#endif + + +#ifdef CONFIG_LOAD_PHY_PARA_FROM_FILE + char rtw_phy_para_file_path[PATH_LENGTH_MAX]; +#endif + +void dump_chip_info(HAL_VERSION ChipVersion) +{ + int cnt = 0; + u8 buf[128] = {0}; + + if (IS_8188E(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8188E_"); + else if (IS_8188F(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8188F_"); + else if (IS_8188GTV(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8188GTV_"); + else if (IS_8812_SERIES(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8812_"); + else if (IS_8192E(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8192E_"); + else if (IS_8821_SERIES(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8821_"); + else if (IS_8723B_SERIES(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8723B_"); + else if (IS_8703B_SERIES(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8703B_"); + else if (IS_8723D_SERIES(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8723D_"); + else if (IS_8814A_SERIES(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8814A_"); + else if (IS_8822B_SERIES(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8822B_"); + else if (IS_8821C_SERIES(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8821C_"); + else if (IS_8710B_SERIES(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8710B_"); + else if (IS_8192F_SERIES(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8192F_"); + else if (IS_8822C_SERIES(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8822C_"); + else if (IS_8814B_SERIES(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8814B_"); + else if (IS_8723F_SERIES(ChipVersion)) + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_8723F_"); + else + cnt += sprintf((buf + cnt), "Chip Version Info: CHIP_UNKNOWN_"); + + cnt += sprintf((buf + cnt), "%s", IS_NORMAL_CHIP(ChipVersion) ? "" : "T_"); + + if (IS_CHIP_VENDOR_TSMC(ChipVersion)) + cnt += sprintf((buf + cnt), "%s", "T"); + else if (IS_CHIP_VENDOR_UMC(ChipVersion)) + cnt += sprintf((buf + cnt), "%s", "U"); + else if (IS_CHIP_VENDOR_SMIC(ChipVersion)) + cnt += sprintf((buf + cnt), "%s", "S"); + + if (IS_A_CUT(ChipVersion)) + cnt += sprintf((buf + cnt), "1_"); + else if (IS_B_CUT(ChipVersion)) + cnt += sprintf((buf + cnt), "2_"); + else if (IS_C_CUT(ChipVersion)) + cnt += sprintf((buf + cnt), "3_"); + else if (IS_D_CUT(ChipVersion)) + cnt += sprintf((buf + cnt), "4_"); + else if (IS_E_CUT(ChipVersion)) + cnt += sprintf((buf + cnt), "5_"); + else if (IS_F_CUT(ChipVersion)) + cnt += sprintf((buf + cnt), "6_"); + else if (IS_I_CUT(ChipVersion)) + cnt += sprintf((buf + cnt), "9_"); + else if (IS_J_CUT(ChipVersion)) + cnt += sprintf((buf + cnt), "10_"); + else if (IS_K_CUT(ChipVersion)) + cnt += sprintf((buf + cnt), "11_"); + else + cnt += sprintf((buf + cnt), "UNKNOWN_Cv(%d)_", ChipVersion.CUTVersion); + + if (IS_1T1R(ChipVersion)) + cnt += sprintf((buf + cnt), "1T1R_"); + else if (IS_1T2R(ChipVersion)) + cnt += sprintf((buf + cnt), "1T2R_"); + else if (IS_2T2R(ChipVersion)) + cnt += sprintf((buf + cnt), "2T2R_"); + else if (IS_3T3R(ChipVersion)) + cnt += sprintf((buf + cnt), "3T3R_"); + else if (IS_3T4R(ChipVersion)) + cnt += sprintf((buf + cnt), "3T4R_"); + else if (IS_4T4R(ChipVersion)) + cnt += sprintf((buf + cnt), "4T4R_"); + else + cnt += sprintf((buf + cnt), "UNKNOWN_RFTYPE(%d)_", ChipVersion.RFType); + + cnt += sprintf((buf + cnt), "RomVer(%d)\n", ChipVersion.ROMVer); + + RTW_INFO("%s", buf); +} + +u8 rtw_hal_get_port(_adapter *adapter) +{ + u8 hw_port = get_hw_port(adapter); +#ifdef CONFIG_CLIENT_PORT_CFG + u8 clt_port = get_clt_port(adapter); + + if (clt_port) + hw_port = clt_port; + +#ifdef DBG_HW_PORT + if (MLME_IS_STA(adapter) && (adapter->client_id != MAX_CLIENT_PORT_NUM)) { + if(hw_port == CLT_PORT_INVALID) { + RTW_ERR(ADPT_FMT" @@@@@ Client port == 0 @@@@@\n", ADPT_ARG(adapter)); + rtw_warn_on(1); + } + } + #ifdef CONFIG_AP_MODE + else if (MLME_IS_AP(adapter) || MLME_IS_MESH(adapter)) { + if (hw_port != HW_PORT0) { + RTW_ERR(ADPT_FMT" @@@@@ AP / MESH port != 0 @@@@@\n", ADPT_ARG(adapter)); + rtw_warn_on(1); + } + } + #endif + if (0) + RTW_INFO(ADPT_FMT" - HP:%d,CP:%d\n", ADPT_ARG(adapter), get_hw_port(adapter), get_clt_port(adapter)); +#endif /*DBG_HW_PORT*/ + +#endif/*CONFIG_CLIENT_PORT_CFG*/ + + return hw_port; +} + +#define EEPROM_CHANNEL_PLAN_BY_HW_MASK 0x80 + +/* + * Description: + * Use hardware(efuse), driver parameter(registry) and default channel plan + * to decide which one should be used. + * + * Parameters: + * padapter pointer of adapter + * hw_alpha2 country code from HW (efuse/eeprom/mapfile) + * hw_chplan channel plan from HW (efuse/eeprom/mapfile) + * BIT[7] software configure mode; 0:Enable, 1:disable + * BIT[6:0] Channel Plan + * sw_alpha2 country code from HW (registry/module param) + * sw_chplan channel plan from SW (registry/module param) + * AutoLoadFail efuse autoload fail or not + * + */ +void hal_com_config_channel_plan( + PADAPTER padapter, + char *hw_alpha2, + u8 hw_chplan, + char *sw_alpha2, + u8 sw_chplan, + BOOLEAN AutoLoadFail +) +{ + struct rf_ctl_t *rfctl = adapter_to_rfctl(padapter); + PHAL_DATA_TYPE pHalData; + u8 force_hw_chplan = _FALSE; + int chplan = -1; + const struct country_chplan *country_ent = NULL, *ent; + u8 def_chplan = 0x7F; /* Realtek define, used when HW, SW both invalid */ + + pHalData = GET_HAL_DATA(padapter); + + /* treat 0xFF as invalid value, bypass hw_chplan & force_hw_chplan parsing */ + if (hw_chplan == 0xFF) + goto chk_hw_country_code; + + if (AutoLoadFail == _TRUE) + goto chk_sw_config; + +#ifndef CONFIG_FORCE_SW_CHANNEL_PLAN + if (hw_chplan & EEPROM_CHANNEL_PLAN_BY_HW_MASK) + force_hw_chplan = _TRUE; +#endif + + hw_chplan &= (~EEPROM_CHANNEL_PLAN_BY_HW_MASK); + +chk_hw_country_code: + if (hw_alpha2 && !IS_ALPHA2_NO_SPECIFIED(hw_alpha2)) { + ent = rtw_get_chplan_from_country(hw_alpha2); + if (ent) { + /* get chplan from hw country code, by pass hw chplan setting */ + country_ent = ent; + chplan = ent->chplan; + goto chk_sw_config; + } else + RTW_PRINT("%s unsupported hw_alpha2:\"%c%c\"\n", __func__, hw_alpha2[0], hw_alpha2[1]); + } + + if (rtw_is_channel_plan_valid(hw_chplan)) + chplan = hw_chplan; + else if (force_hw_chplan == _TRUE) { + RTW_PRINT("%s unsupported hw_chplan:0x%02X\n", __func__, hw_chplan); + /* hw infomaton invalid, refer to sw information */ + force_hw_chplan = _FALSE; + } + +chk_sw_config: + if (force_hw_chplan == _TRUE) + goto done; + + if (sw_alpha2 && !IS_ALPHA2_NO_SPECIFIED(sw_alpha2)) { + ent = rtw_get_chplan_from_country(sw_alpha2); + if (ent) { + /* get chplan from sw country code, by pass sw chplan setting */ + country_ent = ent; + chplan = ent->chplan; + goto done; + } else + RTW_PRINT("%s unsupported sw_alpha2:\"%c%c\"\n", __func__, sw_alpha2[0], sw_alpha2[1]); + } + + if (rtw_is_channel_plan_valid(sw_chplan)) { + /* cancel hw_alpha2 because chplan is specified by sw_chplan*/ + country_ent = NULL; + chplan = sw_chplan; + } else if (sw_chplan != RTW_CHPLAN_UNSPECIFIED) + RTW_PRINT("%s unsupported sw_chplan:0x%02X\n", __func__, sw_chplan); + +done: + if (chplan == -1) { + RTW_PRINT("%s use def_chplan:0x%02X\n", __func__, def_chplan); + chplan = def_chplan; + } else if (country_ent) { + RTW_PRINT("%s country code:\"%c%c\" with chplan:0x%02X\n", __func__ + , country_ent->alpha2[0], country_ent->alpha2[1], country_ent->chplan); + } else + RTW_PRINT("%s chplan:0x%02X\n", __func__, chplan); + + rfctl->country_ent = country_ent; + rfctl->ChannelPlan = chplan; + pHalData->bDisableSWChannelPlan = force_hw_chplan; +} + +BOOLEAN +HAL_IsLegalChannel( + PADAPTER Adapter, + u32 Channel +) +{ + BOOLEAN bLegalChannel = _TRUE; + + if (Channel > 14) { + if (is_supported_5g(Adapter->registrypriv.wireless_mode) == _FALSE) { + bLegalChannel = _FALSE; + RTW_INFO("Channel > 14 but wireless_mode do not support 5G\n"); + } + } else if ((Channel <= 14) && (Channel >= 1)) { + if (IsSupported24G(Adapter->registrypriv.wireless_mode) == _FALSE) { + bLegalChannel = _FALSE; + RTW_INFO("(Channel <= 14) && (Channel >=1) but wireless_mode do not support 2.4G\n"); + } + } else { + bLegalChannel = _FALSE; + RTW_INFO("Channel is Invalid !!!\n"); + } + + return bLegalChannel; +} + +static const u8 _MRateToHwRate[MGN_UNKNOWN] = { + [MGN_1M] = DESC_RATE1M, + [MGN_2M] = DESC_RATE2M, + [MGN_5_5M] = DESC_RATE5_5M, + [MGN_11M] = DESC_RATE11M, + [MGN_6M] = DESC_RATE6M, + [MGN_9M] = DESC_RATE9M, + [MGN_12M] = DESC_RATE12M, + [MGN_18M] = DESC_RATE18M, + [MGN_24M] = DESC_RATE24M, + [MGN_36M] = DESC_RATE36M, + [MGN_48M] = DESC_RATE48M, + [MGN_54M] = DESC_RATE54M, + [MGN_MCS0] = DESC_RATEMCS0, + [MGN_MCS1] = DESC_RATEMCS1, + [MGN_MCS2] = DESC_RATEMCS2, + [MGN_MCS3] = DESC_RATEMCS3, + [MGN_MCS4] = DESC_RATEMCS4, + [MGN_MCS5] = DESC_RATEMCS5, + [MGN_MCS6] = DESC_RATEMCS6, + [MGN_MCS7] = DESC_RATEMCS7, + [MGN_MCS8] = DESC_RATEMCS8, + [MGN_MCS9] = DESC_RATEMCS9, + [MGN_MCS10] = DESC_RATEMCS10, + [MGN_MCS11] = DESC_RATEMCS11, + [MGN_MCS12] = DESC_RATEMCS12, + [MGN_MCS13] = DESC_RATEMCS13, + [MGN_MCS14] = DESC_RATEMCS14, + [MGN_MCS15] = DESC_RATEMCS15, + [MGN_MCS16] = DESC_RATEMCS16, + [MGN_MCS17] = DESC_RATEMCS17, + [MGN_MCS18] = DESC_RATEMCS18, + [MGN_MCS19] = DESC_RATEMCS19, + [MGN_MCS20] = DESC_RATEMCS20, + [MGN_MCS21] = DESC_RATEMCS21, + [MGN_MCS22] = DESC_RATEMCS22, + [MGN_MCS23] = DESC_RATEMCS23, + [MGN_MCS24] = DESC_RATEMCS24, + [MGN_MCS25] = DESC_RATEMCS25, + [MGN_MCS26] = DESC_RATEMCS26, + [MGN_MCS27] = DESC_RATEMCS27, + [MGN_MCS28] = DESC_RATEMCS28, + [MGN_MCS29] = DESC_RATEMCS29, + [MGN_MCS30] = DESC_RATEMCS30, + [MGN_MCS31] = DESC_RATEMCS31, + [MGN_VHT1SS_MCS0] = DESC_RATEVHTSS1MCS0, + [MGN_VHT1SS_MCS1] = DESC_RATEVHTSS1MCS1, + [MGN_VHT1SS_MCS2] = DESC_RATEVHTSS1MCS2, + [MGN_VHT1SS_MCS3] = DESC_RATEVHTSS1MCS3, + [MGN_VHT1SS_MCS4] = DESC_RATEVHTSS1MCS4, + [MGN_VHT1SS_MCS5] = DESC_RATEVHTSS1MCS5, + [MGN_VHT1SS_MCS6] = DESC_RATEVHTSS1MCS6, + [MGN_VHT1SS_MCS7] = DESC_RATEVHTSS1MCS7, + [MGN_VHT1SS_MCS8] = DESC_RATEVHTSS1MCS8, + [MGN_VHT1SS_MCS9] = DESC_RATEVHTSS1MCS9, + [MGN_VHT2SS_MCS0] = DESC_RATEVHTSS2MCS0, + [MGN_VHT2SS_MCS1] = DESC_RATEVHTSS2MCS1, + [MGN_VHT2SS_MCS2] = DESC_RATEVHTSS2MCS2, + [MGN_VHT2SS_MCS3] = DESC_RATEVHTSS2MCS3, + [MGN_VHT2SS_MCS4] = DESC_RATEVHTSS2MCS4, + [MGN_VHT2SS_MCS5] = DESC_RATEVHTSS2MCS5, + [MGN_VHT2SS_MCS6] = DESC_RATEVHTSS2MCS6, + [MGN_VHT2SS_MCS7] = DESC_RATEVHTSS2MCS7, + [MGN_VHT2SS_MCS8] = DESC_RATEVHTSS2MCS8, + [MGN_VHT2SS_MCS9] = DESC_RATEVHTSS2MCS9, + [MGN_VHT3SS_MCS0] = DESC_RATEVHTSS3MCS0, + [MGN_VHT3SS_MCS1] = DESC_RATEVHTSS3MCS1, + [MGN_VHT3SS_MCS2] = DESC_RATEVHTSS3MCS2, + [MGN_VHT3SS_MCS3] = DESC_RATEVHTSS3MCS3, + [MGN_VHT3SS_MCS4] = DESC_RATEVHTSS3MCS4, + [MGN_VHT3SS_MCS5] = DESC_RATEVHTSS3MCS5, + [MGN_VHT3SS_MCS6] = DESC_RATEVHTSS3MCS6, + [MGN_VHT3SS_MCS7] = DESC_RATEVHTSS3MCS7, + [MGN_VHT3SS_MCS8] = DESC_RATEVHTSS3MCS8, + [MGN_VHT3SS_MCS9] = DESC_RATEVHTSS3MCS9, + [MGN_VHT4SS_MCS0] = DESC_RATEVHTSS4MCS0, + [MGN_VHT4SS_MCS1] = DESC_RATEVHTSS4MCS1, + [MGN_VHT4SS_MCS2] = DESC_RATEVHTSS4MCS2, + [MGN_VHT4SS_MCS3] = DESC_RATEVHTSS4MCS3, + [MGN_VHT4SS_MCS4] = DESC_RATEVHTSS4MCS4, + [MGN_VHT4SS_MCS5] = DESC_RATEVHTSS4MCS5, + [MGN_VHT4SS_MCS6] = DESC_RATEVHTSS4MCS6, + [MGN_VHT4SS_MCS7] = DESC_RATEVHTSS4MCS7, + [MGN_VHT4SS_MCS8] = DESC_RATEVHTSS4MCS8, + [MGN_VHT4SS_MCS9] = DESC_RATEVHTSS4MCS9, +}; + +u8 MRateToHwRate(enum MGN_RATE rate) +{ + u8 hw_rate = DESC_RATE1M; /* default value, also is zero */ + + if (rate < MGN_UNKNOWN) + hw_rate = _MRateToHwRate[rate]; + + if (rate != MGN_1M && hw_rate == DESC_RATE1M) + RTW_WARN("Invalid rate 0x%x in %s\n", rate, __FUNCTION__); + + return hw_rate; +} + +const char * const _HDATA_RATE[DESC_RATE_NUM + 1] = { + [DESC_RATE1M] = "CCK_1M", + [DESC_RATE2M] = "CCK_2M", + [DESC_RATE5_5M] = "CCK5_5M", + [DESC_RATE11M] = "CCK_11M", + [DESC_RATE6M] = "OFDM_6M", + [DESC_RATE9M] = "OFDM_9M", + [DESC_RATE12M] = "OFDM_12M", + [DESC_RATE18M] = "OFDM_18M", + [DESC_RATE24M] = "OFDM_24M", + [DESC_RATE36M] = "OFDM_36M", + [DESC_RATE48M] = "OFDM_48M", + [DESC_RATE54M] = "OFDM_54M", + [DESC_RATEMCS0] = "MCS0", + [DESC_RATEMCS1] = "MCS1", + [DESC_RATEMCS2] = "MCS2", + [DESC_RATEMCS3] = "MCS3", + [DESC_RATEMCS4] = "MCS4", + [DESC_RATEMCS5] = "MCS5", + [DESC_RATEMCS6] = "MCS6", + [DESC_RATEMCS7] = "MCS7", + [DESC_RATEMCS8] = "MCS8", + [DESC_RATEMCS9] = "MCS9", + [DESC_RATEMCS10] = "MCS10", + [DESC_RATEMCS11] = "MCS11", + [DESC_RATEMCS12] = "MCS12", + [DESC_RATEMCS13] = "MCS13", + [DESC_RATEMCS14] = "MCS14", + [DESC_RATEMCS15] = "MCS15", + [DESC_RATEMCS16] = "MCS16", + [DESC_RATEMCS17] = "MCS17", + [DESC_RATEMCS18] = "MCS18", + [DESC_RATEMCS19] = "MCS19", + [DESC_RATEMCS20] = "MCS20", + [DESC_RATEMCS21] = "MCS21", + [DESC_RATEMCS22] = "MCS22", + [DESC_RATEMCS23] = "MCS23", + [DESC_RATEMCS24] = "MCS24", + [DESC_RATEMCS25] = "MCS25", + [DESC_RATEMCS26] = "MCS26", + [DESC_RATEMCS27] = "MCS27", + [DESC_RATEMCS28] = "MCS28", + [DESC_RATEMCS29] = "MCS29", + [DESC_RATEMCS30] = "MCS30", + [DESC_RATEMCS31] = "MCS31", + [DESC_RATEVHTSS1MCS0] = "VHT1SMCS0", + [DESC_RATEVHTSS1MCS1] = "VHT1SMCS1", + [DESC_RATEVHTSS1MCS2] = "VHT1SMCS2", + [DESC_RATEVHTSS1MCS3] = "VHT1SMCS3", + [DESC_RATEVHTSS1MCS4] = "VHT1SMCS4", + [DESC_RATEVHTSS1MCS5] = "VHT1SMCS5", + [DESC_RATEVHTSS1MCS6] = "VHT1SMCS6", + [DESC_RATEVHTSS1MCS7] = "VHT1SMCS7", + [DESC_RATEVHTSS1MCS8] = "VHT1SMCS8", + [DESC_RATEVHTSS1MCS9] = "VHT1SMCS9", + [DESC_RATEVHTSS2MCS0] = "VHT2SMCS0", + [DESC_RATEVHTSS2MCS1] = "VHT2SMCS1", + [DESC_RATEVHTSS2MCS2] = "VHT2SMCS2", + [DESC_RATEVHTSS2MCS3] = "VHT2SMCS3", + [DESC_RATEVHTSS2MCS4] = "VHT2SMCS4", + [DESC_RATEVHTSS2MCS5] = "VHT2SMCS5", + [DESC_RATEVHTSS2MCS6] = "VHT2SMCS6", + [DESC_RATEVHTSS2MCS7] = "VHT2SMCS7", + [DESC_RATEVHTSS2MCS8] = "VHT2SMCS8", + [DESC_RATEVHTSS2MCS9] = "VHT2SMCS9", + [DESC_RATEVHTSS3MCS0] = "VHT3SMCS0", + [DESC_RATEVHTSS3MCS1] = "VHT3SMCS1", + [DESC_RATEVHTSS3MCS2] = "VHT3SMCS2", + [DESC_RATEVHTSS3MCS3] = "VHT3SMCS3", + [DESC_RATEVHTSS3MCS4] = "VHT3SMCS4", + [DESC_RATEVHTSS3MCS5] = "VHT3SMCS5", + [DESC_RATEVHTSS3MCS6] = "VHT3SMCS6", + [DESC_RATEVHTSS3MCS7] = "VHT3SMCS7", + [DESC_RATEVHTSS3MCS8] = "VHT3SMCS8", + [DESC_RATEVHTSS3MCS9] = "VHT3SMCS9", + [DESC_RATEVHTSS4MCS0] = "VHT4SMCS0", + [DESC_RATEVHTSS4MCS1] = "VHT4SMCS1", + [DESC_RATEVHTSS4MCS2] = "VHT4SMCS2", + [DESC_RATEVHTSS4MCS3] = "VHT4SMCS3", + [DESC_RATEVHTSS4MCS4] = "VHT4SMCS4", + [DESC_RATEVHTSS4MCS5] = "VHT4SMCS5", + [DESC_RATEVHTSS4MCS6] = "VHT4SMCS6", + [DESC_RATEVHTSS4MCS7] = "VHT4SMCS7", + [DESC_RATEVHTSS4MCS8] = "VHT4SMCS8", + [DESC_RATEVHTSS4MCS9] = "VHT4SMCS9", + [DESC_RATE_NUM] = "UNKNOWN", +}; + +static const u8 _hw_rate_to_m_rate[DESC_RATE_NUM] = { + [DESC_RATE1M] = MGN_1M, + [DESC_RATE2M] = MGN_2M, + [DESC_RATE5_5M] = MGN_5_5M, + [DESC_RATE11M] = MGN_11M, + [DESC_RATE6M] = MGN_6M, + [DESC_RATE9M] = MGN_9M, + [DESC_RATE12M] = MGN_12M, + [DESC_RATE18M] = MGN_18M, + [DESC_RATE24M] = MGN_24M, + [DESC_RATE36M] = MGN_36M, + [DESC_RATE48M] = MGN_48M, + [DESC_RATE54M] = MGN_54M, + [DESC_RATEMCS0] = MGN_MCS0, + [DESC_RATEMCS1] = MGN_MCS1, + [DESC_RATEMCS2] = MGN_MCS2, + [DESC_RATEMCS3] = MGN_MCS3, + [DESC_RATEMCS4] = MGN_MCS4, + [DESC_RATEMCS5] = MGN_MCS5, + [DESC_RATEMCS6] = MGN_MCS6, + [DESC_RATEMCS7] = MGN_MCS7, + [DESC_RATEMCS8] = MGN_MCS8, + [DESC_RATEMCS9] = MGN_MCS9, + [DESC_RATEMCS10] = MGN_MCS10, + [DESC_RATEMCS11] = MGN_MCS11, + [DESC_RATEMCS12] = MGN_MCS12, + [DESC_RATEMCS13] = MGN_MCS13, + [DESC_RATEMCS14] = MGN_MCS14, + [DESC_RATEMCS15] = MGN_MCS15, + [DESC_RATEMCS16] = MGN_MCS16, + [DESC_RATEMCS17] = MGN_MCS17, + [DESC_RATEMCS18] = MGN_MCS18, + [DESC_RATEMCS19] = MGN_MCS19, + [DESC_RATEMCS20] = MGN_MCS20, + [DESC_RATEMCS21] = MGN_MCS21, + [DESC_RATEMCS22] = MGN_MCS22, + [DESC_RATEMCS23] = MGN_MCS23, + [DESC_RATEMCS24] = MGN_MCS24, + [DESC_RATEMCS25] = MGN_MCS25, + [DESC_RATEMCS26] = MGN_MCS26, + [DESC_RATEMCS27] = MGN_MCS27, + [DESC_RATEMCS28] = MGN_MCS28, + [DESC_RATEMCS29] = MGN_MCS29, + [DESC_RATEMCS30] = MGN_MCS30, + [DESC_RATEMCS31] = MGN_MCS31, + [DESC_RATEVHTSS1MCS0] = MGN_VHT1SS_MCS0, + [DESC_RATEVHTSS1MCS1] = MGN_VHT1SS_MCS1, + [DESC_RATEVHTSS1MCS2] = MGN_VHT1SS_MCS2, + [DESC_RATEVHTSS1MCS3] = MGN_VHT1SS_MCS3, + [DESC_RATEVHTSS1MCS4] = MGN_VHT1SS_MCS4, + [DESC_RATEVHTSS1MCS5] = MGN_VHT1SS_MCS5, + [DESC_RATEVHTSS1MCS6] = MGN_VHT1SS_MCS6, + [DESC_RATEVHTSS1MCS7] = MGN_VHT1SS_MCS7, + [DESC_RATEVHTSS1MCS8] = MGN_VHT1SS_MCS8, + [DESC_RATEVHTSS1MCS9] = MGN_VHT1SS_MCS9, + [DESC_RATEVHTSS2MCS0] = MGN_VHT2SS_MCS0, + [DESC_RATEVHTSS2MCS1] = MGN_VHT2SS_MCS1, + [DESC_RATEVHTSS2MCS2] = MGN_VHT2SS_MCS2, + [DESC_RATEVHTSS2MCS3] = MGN_VHT2SS_MCS3, + [DESC_RATEVHTSS2MCS4] = MGN_VHT2SS_MCS4, + [DESC_RATEVHTSS2MCS5] = MGN_VHT2SS_MCS5, + [DESC_RATEVHTSS2MCS6] = MGN_VHT2SS_MCS6, + [DESC_RATEVHTSS2MCS7] = MGN_VHT2SS_MCS7, + [DESC_RATEVHTSS2MCS8] = MGN_VHT2SS_MCS8, + [DESC_RATEVHTSS2MCS9] = MGN_VHT2SS_MCS9, + [DESC_RATEVHTSS3MCS0] = MGN_VHT3SS_MCS0, + [DESC_RATEVHTSS3MCS1] = MGN_VHT3SS_MCS1, + [DESC_RATEVHTSS3MCS2] = MGN_VHT3SS_MCS2, + [DESC_RATEVHTSS3MCS3] = MGN_VHT3SS_MCS3, + [DESC_RATEVHTSS3MCS4] = MGN_VHT3SS_MCS4, + [DESC_RATEVHTSS3MCS5] = MGN_VHT3SS_MCS5, + [DESC_RATEVHTSS3MCS6] = MGN_VHT3SS_MCS6, + [DESC_RATEVHTSS3MCS7] = MGN_VHT3SS_MCS7, + [DESC_RATEVHTSS3MCS8] = MGN_VHT3SS_MCS8, + [DESC_RATEVHTSS3MCS9] = MGN_VHT3SS_MCS9, + [DESC_RATEVHTSS4MCS0] = MGN_VHT4SS_MCS0, + [DESC_RATEVHTSS4MCS1] = MGN_VHT4SS_MCS1, + [DESC_RATEVHTSS4MCS2] = MGN_VHT4SS_MCS2, + [DESC_RATEVHTSS4MCS3] = MGN_VHT4SS_MCS3, + [DESC_RATEVHTSS4MCS4] = MGN_VHT4SS_MCS4, + [DESC_RATEVHTSS4MCS5] = MGN_VHT4SS_MCS5, + [DESC_RATEVHTSS4MCS6] = MGN_VHT4SS_MCS6, + [DESC_RATEVHTSS4MCS7] = MGN_VHT4SS_MCS7, + [DESC_RATEVHTSS4MCS8] = MGN_VHT4SS_MCS8, + [DESC_RATEVHTSS4MCS9] = MGN_VHT4SS_MCS9, +}; + +u8 hw_rate_to_m_rate(u8 hw_rate) +{ + u8 rate = MGN_1M; /* default value */ + + if (hw_rate < DESC_RATE_NUM) + rate = _hw_rate_to_m_rate[hw_rate]; + else + RTW_WARN("Invalid hw_rate 0x%x in %s\n", hw_rate, __FUNCTION__); + + return rate; +} + +#ifdef CONFIG_RTW_DEBUG +void dump_hw_rate_map_test(void *sel) +{ + RATE_SECTION rs; + u8 hw_rate; + enum MGN_RATE m_rate; + int i; + + for (rs = 0; rs < RATE_SECTION_NUM; rs++) { + for (i = 0; i < rates_by_sections[rs].rate_num; i++) { + hw_rate = MRateToHwRate(rates_by_sections[rs].rates[i]); + RTW_PRINT_SEL(sel, "m_rate:%s(%d) to hw_rate:%s(%d)\n" + , MGN_RATE_STR(rates_by_sections[rs].rates[i]), rates_by_sections[rs].rates[i] + , HDATA_RATE(hw_rate), hw_rate + ); + } + if (rs == HT_4SS) { /* show MCS32 after MCS31 */ + hw_rate = MRateToHwRate(MGN_MCS32); + RTW_PRINT_SEL(sel, "m_rate:%s(%d) to hw_rate:%s(%d)\n" + , MGN_RATE_STR(MGN_MCS32), MGN_MCS32 + , HDATA_RATE(hw_rate), hw_rate + ); + } + } + hw_rate = MRateToHwRate(MGN_UNKNOWN); + RTW_PRINT_SEL(sel, "m_rate:%s(%d) to hw_rate:%s(%d)\n" + , MGN_RATE_STR(MGN_UNKNOWN), MGN_UNKNOWN + , HDATA_RATE(hw_rate), hw_rate + ); + + for (i = DESC_RATE1M; i <= DESC_RATE_NUM; i++) { + m_rate = hw_rate_to_m_rate(i); + RTW_PRINT_SEL(sel, "hw_rate:%s(%d) to m_rate:%s(%d)\n" + , HDATA_RATE(i), i + , MGN_RATE_STR(m_rate), m_rate + ); + } +} +#endif /* CONFIG_RTW_DEBUG */ + +void HalSetBrateCfg( + PADAPTER Adapter, + u8 *mBratesOS, + u16 *pBrateCfg) +{ + u8 i, is_brate, brate; + + for (i = 0; i < NDIS_802_11_LENGTH_RATES_EX; i++) { + is_brate = mBratesOS[i] & IEEE80211_BASIC_RATE_MASK; + brate = mBratesOS[i] & 0x7f; + + if (is_brate) { + switch (brate) { + case IEEE80211_CCK_RATE_1MB: + *pBrateCfg |= RATE_1M; + break; + case IEEE80211_CCK_RATE_2MB: + *pBrateCfg |= RATE_2M; + break; + case IEEE80211_CCK_RATE_5MB: + *pBrateCfg |= RATE_5_5M; + break; + case IEEE80211_CCK_RATE_11MB: + *pBrateCfg |= RATE_11M; + break; + case IEEE80211_OFDM_RATE_6MB: + *pBrateCfg |= RATE_6M; + break; + case IEEE80211_OFDM_RATE_9MB: + *pBrateCfg |= RATE_9M; + break; + case IEEE80211_OFDM_RATE_12MB: + *pBrateCfg |= RATE_12M; + break; + case IEEE80211_OFDM_RATE_18MB: + *pBrateCfg |= RATE_18M; + break; + case IEEE80211_OFDM_RATE_24MB: + *pBrateCfg |= RATE_24M; + break; + case IEEE80211_OFDM_RATE_36MB: + *pBrateCfg |= RATE_36M; + break; + case IEEE80211_OFDM_RATE_48MB: + *pBrateCfg |= RATE_48M; + break; + case IEEE80211_OFDM_RATE_54MB: + *pBrateCfg |= RATE_54M; + break; + } + } + } +} + +static void +_OneOutPipeMapping( + PADAPTER pAdapter +) +{ + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(pAdapter); + + pdvobjpriv->Queue2Pipe[0] = pdvobjpriv->RtOutPipe[0];/* VO */ + pdvobjpriv->Queue2Pipe[1] = pdvobjpriv->RtOutPipe[0];/* VI */ + pdvobjpriv->Queue2Pipe[2] = pdvobjpriv->RtOutPipe[0];/* BE */ + pdvobjpriv->Queue2Pipe[3] = pdvobjpriv->RtOutPipe[0];/* BK */ + + pdvobjpriv->Queue2Pipe[4] = pdvobjpriv->RtOutPipe[0];/* BCN */ + pdvobjpriv->Queue2Pipe[5] = pdvobjpriv->RtOutPipe[0];/* MGT */ + pdvobjpriv->Queue2Pipe[6] = pdvobjpriv->RtOutPipe[0];/* HIGH */ + pdvobjpriv->Queue2Pipe[7] = pdvobjpriv->RtOutPipe[0];/* TXCMD */ +} + +static void +_TwoOutPipeMapping( + PADAPTER pAdapter, + BOOLEAN bWIFICfg +) +{ + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(pAdapter); + + if (bWIFICfg) { /* WMM */ + + /* BK, BE, VI, VO, BCN, CMD,MGT,HIGH,HCCA */ + /* { 0, 1, 0, 1, 0, 0, 0, 0, 0 }; */ + /* 0:ep_0 num, 1:ep_1 num */ + + pdvobjpriv->Queue2Pipe[0] = pdvobjpriv->RtOutPipe[1];/* VO */ + pdvobjpriv->Queue2Pipe[1] = pdvobjpriv->RtOutPipe[0];/* VI */ + pdvobjpriv->Queue2Pipe[2] = pdvobjpriv->RtOutPipe[1];/* BE */ + pdvobjpriv->Queue2Pipe[3] = pdvobjpriv->RtOutPipe[0];/* BK */ + + pdvobjpriv->Queue2Pipe[4] = pdvobjpriv->RtOutPipe[0];/* BCN */ + pdvobjpriv->Queue2Pipe[5] = pdvobjpriv->RtOutPipe[0];/* MGT */ + pdvobjpriv->Queue2Pipe[6] = pdvobjpriv->RtOutPipe[0];/* HIGH */ + pdvobjpriv->Queue2Pipe[7] = pdvobjpriv->RtOutPipe[0];/* TXCMD */ + + } else { /* typical setting */ + + + /* BK, BE, VI, VO, BCN, CMD,MGT,HIGH,HCCA */ + /* { 1, 1, 0, 0, 0, 0, 0, 0, 0 }; */ + /* 0:ep_0 num, 1:ep_1 num */ + + pdvobjpriv->Queue2Pipe[0] = pdvobjpriv->RtOutPipe[0];/* VO */ + pdvobjpriv->Queue2Pipe[1] = pdvobjpriv->RtOutPipe[0];/* VI */ + pdvobjpriv->Queue2Pipe[2] = pdvobjpriv->RtOutPipe[1];/* BE */ + pdvobjpriv->Queue2Pipe[3] = pdvobjpriv->RtOutPipe[1];/* BK */ + + pdvobjpriv->Queue2Pipe[4] = pdvobjpriv->RtOutPipe[0];/* BCN */ + pdvobjpriv->Queue2Pipe[5] = pdvobjpriv->RtOutPipe[0];/* MGT */ + pdvobjpriv->Queue2Pipe[6] = pdvobjpriv->RtOutPipe[0];/* HIGH */ + pdvobjpriv->Queue2Pipe[7] = pdvobjpriv->RtOutPipe[0];/* TXCMD */ + + } + +} + +static void _ThreeOutPipeMapping( + PADAPTER pAdapter, + BOOLEAN bWIFICfg +) +{ + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(pAdapter); + + if (bWIFICfg) { /* for WMM */ + + /* BK, BE, VI, VO, BCN, CMD,MGT,HIGH,HCCA */ + /* { 1, 2, 1, 0, 0, 0, 0, 0, 0 }; */ + /* 0:H, 1:N, 2:L */ + + pdvobjpriv->Queue2Pipe[0] = pdvobjpriv->RtOutPipe[0];/* VO */ + pdvobjpriv->Queue2Pipe[1] = pdvobjpriv->RtOutPipe[1];/* VI */ + pdvobjpriv->Queue2Pipe[2] = pdvobjpriv->RtOutPipe[2];/* BE */ + pdvobjpriv->Queue2Pipe[3] = pdvobjpriv->RtOutPipe[1];/* BK */ + + pdvobjpriv->Queue2Pipe[4] = pdvobjpriv->RtOutPipe[0];/* BCN */ + pdvobjpriv->Queue2Pipe[5] = pdvobjpriv->RtOutPipe[0];/* MGT */ + pdvobjpriv->Queue2Pipe[6] = pdvobjpriv->RtOutPipe[0];/* HIGH */ + pdvobjpriv->Queue2Pipe[7] = pdvobjpriv->RtOutPipe[0];/* TXCMD */ + + } else { /* typical setting */ + + + /* BK, BE, VI, VO, BCN, CMD,MGT,HIGH,HCCA */ + /* { 2, 2, 1, 0, 0, 0, 0, 0, 0 }; */ + /* 0:H, 1:N, 2:L */ + + pdvobjpriv->Queue2Pipe[0] = pdvobjpriv->RtOutPipe[0];/* VO */ + pdvobjpriv->Queue2Pipe[1] = pdvobjpriv->RtOutPipe[1];/* VI */ + pdvobjpriv->Queue2Pipe[2] = pdvobjpriv->RtOutPipe[2];/* BE */ + pdvobjpriv->Queue2Pipe[3] = pdvobjpriv->RtOutPipe[2];/* BK */ + + pdvobjpriv->Queue2Pipe[4] = pdvobjpriv->RtOutPipe[0];/* BCN */ + pdvobjpriv->Queue2Pipe[5] = pdvobjpriv->RtOutPipe[0];/* MGT */ + pdvobjpriv->Queue2Pipe[6] = pdvobjpriv->RtOutPipe[0];/* HIGH */ + pdvobjpriv->Queue2Pipe[7] = pdvobjpriv->RtOutPipe[0];/* TXCMD */ + } + +} +#if 0 +static void _FourOutPipeMapping( + PADAPTER pAdapter, + BOOLEAN bWIFICfg +) +{ + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(pAdapter); + + if (bWIFICfg) { /* for WMM */ + + /* BK, BE, VI, VO, BCN, CMD,MGT,HIGH,HCCA */ + /* { 1, 2, 1, 0, 0, 0, 0, 0, 0 }; */ + /* 0:H, 1:N, 2:L ,3:E */ + + pdvobjpriv->Queue2Pipe[0] = pdvobjpriv->RtOutPipe[0];/* VO */ + pdvobjpriv->Queue2Pipe[1] = pdvobjpriv->RtOutPipe[1];/* VI */ + pdvobjpriv->Queue2Pipe[2] = pdvobjpriv->RtOutPipe[2];/* BE */ + pdvobjpriv->Queue2Pipe[3] = pdvobjpriv->RtOutPipe[1];/* BK */ + + pdvobjpriv->Queue2Pipe[4] = pdvobjpriv->RtOutPipe[0];/* BCN */ + pdvobjpriv->Queue2Pipe[5] = pdvobjpriv->RtOutPipe[0];/* MGT */ + pdvobjpriv->Queue2Pipe[6] = pdvobjpriv->RtOutPipe[3];/* HIGH */ + pdvobjpriv->Queue2Pipe[7] = pdvobjpriv->RtOutPipe[0];/* TXCMD */ + + } else { /* typical setting */ + + + /* BK, BE, VI, VO, BCN, CMD,MGT,HIGH,HCCA */ + /* { 2, 2, 1, 0, 0, 0, 0, 0, 0 }; */ + /* 0:H, 1:N, 2:L */ + + pdvobjpriv->Queue2Pipe[0] = pdvobjpriv->RtOutPipe[0];/* VO */ + pdvobjpriv->Queue2Pipe[1] = pdvobjpriv->RtOutPipe[1];/* VI */ + pdvobjpriv->Queue2Pipe[2] = pdvobjpriv->RtOutPipe[2];/* BE */ + pdvobjpriv->Queue2Pipe[3] = pdvobjpriv->RtOutPipe[2];/* BK */ + + pdvobjpriv->Queue2Pipe[4] = pdvobjpriv->RtOutPipe[0];/* BCN */ + pdvobjpriv->Queue2Pipe[5] = pdvobjpriv->RtOutPipe[0];/* MGT */ + pdvobjpriv->Queue2Pipe[6] = pdvobjpriv->RtOutPipe[3];/* HIGH */ + pdvobjpriv->Queue2Pipe[7] = pdvobjpriv->RtOutPipe[0];/* TXCMD */ + } + +} +#endif +BOOLEAN +Hal_MappingOutPipe( + PADAPTER pAdapter, + u8 NumOutPipe +) +{ + struct registry_priv *pregistrypriv = &pAdapter->registrypriv; + + BOOLEAN bWIFICfg = (pregistrypriv->wifi_spec) ? _TRUE : _FALSE; + + BOOLEAN result = _TRUE; + + switch (NumOutPipe) { + case 2: + _TwoOutPipeMapping(pAdapter, bWIFICfg); + break; + case 3: + case 4: + case 5: + case 6: + _ThreeOutPipeMapping(pAdapter, bWIFICfg); + break; + case 1: + _OneOutPipeMapping(pAdapter); + break; + default: + result = _FALSE; + break; + } + + return result; + +} + +void rtw_hal_reqtxrpt(_adapter *padapter, u8 macid) +{ + if (padapter->hal_func.reqtxrpt) + padapter->hal_func.reqtxrpt(padapter, macid); +} + +void rtw_hal_dump_macaddr(void *sel, _adapter *adapter) +{ + int i; + _adapter *iface; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + u8 mac_addr[ETH_ALEN]; + +#ifdef CONFIG_MI_WITH_MBSSID_CAM + rtw_mbid_cam_dump(sel, __func__, adapter); +#else + rtw_mi_hal_dump_macaddr(sel, adapter); +#endif +} + +#ifdef RTW_HALMAC +void rtw_hal_hw_port_enable(_adapter *adapter) +{ +#if 1 + u8 port_enable = _TRUE; + + rtw_hal_set_hwreg(adapter, HW_VAR_PORT_CFG, &port_enable); +#else + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct rtw_halmac_bcn_ctrl bcn_ctrl; + + _rtw_memset(&bcn_ctrl, 0, sizeof(struct rtw_halmac_bcn_ctrl)); + bcn_ctrl.enable_bcn = 1; + bcn_ctrl.rx_bssid_fit = 1; + bcn_ctrl.rxbcn_rpt = 1; + + /*rtw_halmac_get_bcn_ctrl(struct dvobj_priv *d, enum _hw_port hwport, + struct rtw_halmac_bcn_ctrl *bcn_ctrl)*/ + if (rtw_halmac_set_bcn_ctrl(dvobj, get_hw_port(adapter), &bcn_ctrl) == -1) { + RTW_ERR(ADPT_FMT" - hw port(%d) enable fail!!\n", ADPT_ARG(adapter), get_hw_port(adapter)); + rtw_warn_on(1); + } +#endif +} +void rtw_hal_hw_port_disable(_adapter *adapter) +{ + u8 port_enable = _FALSE; + + rtw_hal_set_hwreg(adapter, HW_VAR_PORT_CFG, &port_enable); +} + +void rtw_restore_hw_port_cfg(_adapter *adapter) +{ +#ifdef CONFIG_MI_WITH_MBSSID_CAM + +#else + int i; + _adapter *iface; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (iface) + rtw_hal_hw_port_enable(iface); + } +#endif +} +#endif + +void rtw_mi_set_mac_addr(_adapter *adapter) +{ +#ifdef CONFIG_MI_WITH_MBSSID_CAM + rtw_mi_set_mbid_cam(adapter); +#else + int i; + _adapter *iface; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (iface) + rtw_hal_set_hwreg(iface, HW_VAR_MAC_ADDR, adapter_mac_addr(iface)); + } +#endif + if (0) + rtw_hal_dump_macaddr(RTW_DBGDUMP, adapter); +} + +void rtw_init_hal_com_default_value(PADAPTER Adapter) +{ + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(Adapter); + struct registry_priv *regsty = adapter_to_regsty(Adapter); + + pHalData->AntDetection = 1; + pHalData->antenna_test = _FALSE; + pHalData->RegIQKFWOffload = regsty->iqk_fw_offload; + pHalData->ch_switch_offload = regsty->ch_switch_offload; + pHalData->multi_ch_switch_mode = 0; +#ifdef RTW_REDUCE_SCAN_SWITCH_CH_TIME + if (pHalData->ch_switch_offload == 0) + pHalData->ch_switch_offload = 1; +#endif +} + +#ifdef CONFIG_FW_C2H_REG +void c2h_evt_clear(_adapter *adapter) +{ + rtw_write8(adapter, REG_C2HEVT_CLEAR, C2H_EVT_HOST_CLOSE); +} + +s32 c2h_evt_read_88xx(_adapter *adapter, u8 *buf) +{ + s32 ret = _FAIL; + int i; + u8 trigger; + + if (buf == NULL) + goto exit; + + trigger = rtw_read8(adapter, REG_C2HEVT_CLEAR); + + if (trigger == C2H_EVT_HOST_CLOSE) { + goto exit; /* Not ready */ + } else if (trigger != C2H_EVT_FW_CLOSE) { + goto clear_evt; /* Not a valid value */ + } + + _rtw_memset(buf, 0, C2H_REG_LEN); + + /* Read ID, LEN, SEQ */ + SET_C2H_ID_88XX(buf, rtw_read8(adapter, REG_C2HEVT_MSG_NORMAL)); + SET_C2H_SEQ_88XX(buf, rtw_read8(adapter, REG_C2HEVT_CMD_SEQ_88XX)); + SET_C2H_PLEN_88XX(buf, rtw_read8(adapter, REG_C2HEVT_CMD_LEN_88XX)); + + if (0) { + RTW_INFO("%s id=0x%02x, seq=%u, plen=%u, trigger=0x%02x\n", __func__ + , C2H_ID_88XX(buf), C2H_SEQ_88XX(buf), C2H_PLEN_88XX(buf), trigger); + } + + /* Read the content */ + for (i = 0; i < C2H_PLEN_88XX(buf); i++) + *(C2H_PAYLOAD_88XX(buf) + i) = rtw_read8(adapter, REG_C2HEVT_MSG_NORMAL + 2 + i); + + RTW_DBG_DUMP("payload: ", C2H_PAYLOAD_88XX(buf), C2H_PLEN_88XX(buf)); + + ret = _SUCCESS; + +clear_evt: + /* + * Clear event to notify FW we have read the command. + * If this field isn't clear, the FW won't update the next command message. + */ + c2h_evt_clear(adapter); + +exit: + return ret; +} +#endif /* CONFIG_FW_C2H_REG */ + +#ifdef CONFIG_FW_C2H_PKT +#ifndef DBG_C2H_PKT_PRE_HDL +#define DBG_C2H_PKT_PRE_HDL 0 +#endif +#ifndef DBG_C2H_PKT_HDL +#define DBG_C2H_PKT_HDL 0 +#endif +void rtw_hal_c2h_pkt_pre_hdl(_adapter *adapter, u8 *buf, u16 len) +{ +#ifdef RTW_HALMAC + /* TODO: extract hal_mac IC's code here*/ +#else + u8 parse_fail = 0; + u8 hdl_here = 0; + s32 ret = _FAIL; + u8 id, seq, plen; + u8 *payload; + + if (rtw_hal_c2h_pkt_hdr_parse(adapter, buf, len, &id, &seq, &plen, &payload) != _SUCCESS) { + parse_fail = 1; + goto exit; + } + + hdl_here = rtw_hal_c2h_id_handle_directly(adapter, id, seq, plen, payload) == _TRUE ? 1 : 0; + if (hdl_here) + ret = rtw_hal_c2h_handler(adapter, id, seq, plen, payload); + else + ret = rtw_c2h_packet_wk_cmd(adapter, buf, len); + +exit: + if (parse_fail) + RTW_ERR("%s parse fail, buf=%p, len=:%u\n", __func__, buf, len); + else if (ret != _SUCCESS || DBG_C2H_PKT_PRE_HDL > 0) { + RTW_PRINT("%s: id=0x%02x, seq=%u, plen=%u, %s %s\n", __func__, id, seq, plen + , hdl_here ? "handle" : "enqueue" + , ret == _SUCCESS ? "ok" : "fail" + ); + if (DBG_C2H_PKT_PRE_HDL >= 2) + RTW_PRINT_DUMP("dump: ", buf, len); + } +#endif +} + +void rtw_hal_c2h_pkt_hdl(_adapter *adapter, u8 *buf, u16 len) +{ +#ifdef RTW_HALMAC + adapter->hal_func.hal_mac_c2h_handler(adapter, buf, len); +#else + u8 parse_fail = 0; + u8 bypass = 0; + s32 ret = _FAIL; + u8 id, seq, plen; + u8 *payload; + + if (rtw_hal_c2h_pkt_hdr_parse(adapter, buf, len, &id, &seq, &plen, &payload) != _SUCCESS) { + parse_fail = 1; + goto exit; + } + +#ifdef CONFIG_WOWLAN + if (adapter_to_pwrctl(adapter)->wowlan_mode == _TRUE) { + bypass = 1; + ret = _SUCCESS; + goto exit; + } +#endif + + ret = rtw_hal_c2h_handler(adapter, id, seq, plen, payload); + +exit: + if (parse_fail) + RTW_ERR("%s parse fail, buf=%p, len=:%u\n", __func__, buf, len); + else if (ret != _SUCCESS || bypass || DBG_C2H_PKT_HDL > 0) { + RTW_PRINT("%s: id=0x%02x, seq=%u, plen=%u, %s %s\n", __func__, id, seq, plen + , !bypass ? "handle" : "bypass" + , ret == _SUCCESS ? "ok" : "fail" + ); + if (DBG_C2H_PKT_HDL >= 2) + RTW_PRINT_DUMP("dump: ", buf, len); + } +#endif +} +#endif /* CONFIG_FW_C2H_PKT */ + +void c2h_iqk_offload(_adapter *adapter, u8 *data, u8 len) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct submit_ctx *iqk_sctx = &hal_data->iqk_sctx; + + RTW_INFO("IQK offload finish in %dms\n", rtw_get_passing_time_ms(iqk_sctx->submit_time)); + if (0) + RTW_INFO_DUMP("C2H_IQK_FINISH: ", data, len); + + rtw_sctx_done(&iqk_sctx); +} + +int c2h_iqk_offload_wait(_adapter *adapter, u32 timeout_ms) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct submit_ctx *iqk_sctx = &hal_data->iqk_sctx; + + iqk_sctx->submit_time = rtw_get_current_time(); + iqk_sctx->timeout_ms = timeout_ms; + iqk_sctx->status = RTW_SCTX_SUBMITTED; + + return rtw_sctx_wait(iqk_sctx, __func__); +} + +#ifdef CONFIG_FW_OFFLOAD_SET_TXPWR_IDX +void c2h_txpwr_idx_offload_done(_adapter *adapter, u8 *data, u8 len) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct submit_ctx *sctx = &hal_data->txpwr_idx_offload_sctx; + + if (0) + RTW_INFO("txpwr_idx offload finish in %dms\n", rtw_get_passing_time_ms(sctx->submit_time)); + rtw_sctx_done(&sctx); +} + +int c2h_txpwr_idx_offload_wait(_adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct submit_ctx *sctx = &hal_data->txpwr_idx_offload_sctx; + + return rtw_sctx_wait(sctx, __func__); +} +#endif + +#define GET_C2H_MAC_HIDDEN_RPT_UUID_X(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 0, 0, 8) +#define GET_C2H_MAC_HIDDEN_RPT_UUID_Y(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 1, 0, 8) +#define GET_C2H_MAC_HIDDEN_RPT_UUID_Z(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 2, 0, 5) +#define GET_C2H_MAC_HIDDEN_RPT_UUID_CRC(_data) LE_BITS_TO_2BYTE(((u8 *)(_data)) + 2, 5, 11) +#define GET_C2H_MAC_HIDDEN_RPT_HCI_TYPE(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 4, 0, 4) +#define GET_C2H_MAC_HIDDEN_RPT_PACKAGE_TYPE(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 4, 4, 3) +#define GET_C2H_MAC_HIDDEN_RPT_TR_SWITCH(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 4, 7, 1) +#define GET_C2H_MAC_HIDDEN_RPT_WL_FUNC(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 5, 0, 4) +#define GET_C2H_MAC_HIDDEN_RPT_HW_STYPE(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 5, 4, 4) +#define GET_C2H_MAC_HIDDEN_RPT_BW(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 6, 0, 3) +#define GET_C2H_MAC_HIDDEN_RPT_ANT_NUM(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 6, 5, 3) +#define GET_C2H_MAC_HIDDEN_RPT_80211_PROTOCOL(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 7, 2, 2) +#define GET_C2H_MAC_HIDDEN_RPT_NIC_ROUTER(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 7, 6, 2) + +#ifndef DBG_C2H_MAC_HIDDEN_RPT_HANDLE +#define DBG_C2H_MAC_HIDDEN_RPT_HANDLE 0 +#endif + +#ifdef CONFIG_RTW_MAC_HIDDEN_RPT +int c2h_mac_hidden_rpt_hdl(_adapter *adapter, u8 *data, u8 len) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + enum rf_type rf_type; + u8 tx_path_num, rx_path_num; + int ret = _FAIL; + + u8 uuid_x; + u8 uuid_y; + u8 uuid_z; + u16 uuid_crc; + + u8 hci_type; + u8 package_type; + u8 tr_switch; + u8 wl_func; + u8 hw_stype; + u8 bw; + u8 ss_num = 4; + u8 ant_num; + u8 protocol; + u8 nic; + + int i; + + if (len < MAC_HIDDEN_RPT_LEN) { + RTW_WARN("%s len(%u) < %d\n", __func__, len, MAC_HIDDEN_RPT_LEN); + goto exit; + } + + uuid_x = GET_C2H_MAC_HIDDEN_RPT_UUID_X(data); + uuid_y = GET_C2H_MAC_HIDDEN_RPT_UUID_Y(data); + uuid_z = GET_C2H_MAC_HIDDEN_RPT_UUID_Z(data); + uuid_crc = GET_C2H_MAC_HIDDEN_RPT_UUID_CRC(data); + + hci_type = GET_C2H_MAC_HIDDEN_RPT_HCI_TYPE(data); + package_type = GET_C2H_MAC_HIDDEN_RPT_PACKAGE_TYPE(data); + + tr_switch = GET_C2H_MAC_HIDDEN_RPT_TR_SWITCH(data); + + wl_func = GET_C2H_MAC_HIDDEN_RPT_WL_FUNC(data); + hw_stype = GET_C2H_MAC_HIDDEN_RPT_HW_STYPE(data); + + bw = GET_C2H_MAC_HIDDEN_RPT_BW(data); + ant_num = GET_C2H_MAC_HIDDEN_RPT_ANT_NUM(data); + + protocol = GET_C2H_MAC_HIDDEN_RPT_80211_PROTOCOL(data); + nic = GET_C2H_MAC_HIDDEN_RPT_NIC_ROUTER(data); + + if (DBG_C2H_MAC_HIDDEN_RPT_HANDLE) { + for (i = 0; i < len; i++) + RTW_PRINT("%s: 0x%02X\n", __func__, *(data + i)); + + RTW_PRINT("uuid x:0x%02x y:0x%02x z:0x%x crc:0x%x\n", uuid_x, uuid_y, uuid_z, uuid_crc); + RTW_PRINT("hci_type:0x%x\n", hci_type); + RTW_PRINT("package_type:0x%x\n", package_type); + RTW_PRINT("tr_switch:0x%x\n", tr_switch); + RTW_PRINT("wl_func:0x%x\n", wl_func); + RTW_PRINT("hw_stype:0x%x\n", hw_stype); + RTW_PRINT("bw:0x%x\n", bw); + RTW_PRINT("ant_num:0x%x\n", ant_num); + RTW_PRINT("protocol:0x%x\n", protocol); + RTW_PRINT("nic:0x%x\n", nic); + } + +#if defined(CONFIG_RTL8822C) || defined(CONFIG_RTL8814B) + if (IS_8822C_SERIES(hal_data->version_id) || IS_8814B_SERIES(hal_data->version_id)) { + #define GET_C2H_MAC_HIDDEN_RPT_SS_NUM(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 6, 3, 2) + ss_num = GET_C2H_MAC_HIDDEN_RPT_SS_NUM(data); + + if (DBG_C2H_MAC_HIDDEN_RPT_HANDLE) + RTW_PRINT("ss_num:0x%x\n", ss_num); + + if (ss_num == 0x03) + ss_num = 4; + } +#endif + +#if defined(CONFIG_RTL8822C) + if (IS_8822C_SERIES(hal_data->version_id)) { + if (ant_num == 1) + hal_spec->rf_reg_trx_path_bmp = 0x22; /* 1T1R pathB */ + if (hw_stype == 0xE) + hal_spec->max_tx_cnt = rtw_min(hal_spec->max_tx_cnt, 1); /* limit 1TX only */ + } +#endif + hal_data->PackageType = package_type; + hal_spec->hci_type = hci_type; + hal_spec->wl_func &= mac_hidden_wl_func_to_hal_wl_func(wl_func); + hal_spec->bw_cap &= mac_hidden_max_bw_to_hal_bw_cap(bw); + hal_spec->proto_cap &= mac_hidden_proto_to_hal_proto_cap(protocol); + + rf_type = rtw_chip_rftype_to_hal_rftype(adapter, 0); + if (!RF_TYPE_VALID(rf_type)) { + RTW_ERR("%s rtw_chip_rftype_to_hal_rftype failed\n", __func__); + goto exit; + } + hal_spec->rf_reg_path_avail_num = rtw_min(hal_spec->rf_reg_path_num, ant_num); + tx_path_num = rtw_min(rf_type_to_rf_tx_cnt(rf_type), hal_spec->rf_reg_path_avail_num); + rx_path_num = rtw_min(rf_type_to_rf_rx_cnt(rf_type), hal_spec->rf_reg_path_avail_num); + hal_spec->rf_reg_trx_path_bmp = rtw_restrict_trx_path_bmp_by_trx_num_lmt( + hal_spec->rf_reg_trx_path_bmp, tx_path_num, rx_path_num, &tx_path_num, &rx_path_num); + if (!hal_spec->rf_reg_trx_path_bmp) { + RTW_ERR("%s rtw_restrict_trx_path_bmp_by_trx_num_lmt(0x%x, %u, %u) failed\n" + , __func__, hal_spec->rf_reg_trx_path_bmp, tx_path_num, rx_path_num); + goto exit; + } + hal_spec->rf_reg_path_avail_num = rtw_max(tx_path_num, rx_path_num); + + /* + * RF TX path num >= max_tx_cnt >= tx_nss_num + * ex: RF TX path num(4) >= max_tx_cnt(2) >= tx_nss_num(1) + * Select at most 2 out of 4 TX RF path to do 1SS 2TX + */ + hal_spec->max_tx_cnt = rtw_min(hal_spec->max_tx_cnt, tx_path_num); + hal_spec->tx_nss_num = rtw_min(hal_spec->tx_nss_num, hal_spec->max_tx_cnt); + hal_spec->tx_nss_num = rtw_min(hal_spec->tx_nss_num, ss_num); + + hal_spec->rx_nss_num = rtw_min(hal_spec->rx_nss_num, rx_path_num); + hal_spec->rx_nss_num = rtw_min(hal_spec->rx_nss_num, ss_num); + + ret = _SUCCESS; + +exit: + return ret; +} + +int c2h_mac_hidden_rpt_2_hdl(_adapter *adapter, u8 *data, u8 len) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + int ret = _FAIL; + + int i; + + if (len < MAC_HIDDEN_RPT_2_LEN) { + RTW_WARN("%s len(%u) < %d\n", __func__, len, MAC_HIDDEN_RPT_2_LEN); + goto exit; + } + + if (DBG_C2H_MAC_HIDDEN_RPT_HANDLE) { + for (i = 0; i < len; i++) + RTW_PRINT("%s: 0x%02X\n", __func__, *(data + i)); + } + + #if defined(CONFIG_RTL8188F) || defined(CONFIG_RTL8188GTV) + if (IS_8188F(hal_data->version_id) || IS_8188GTV(hal_data->version_id)) { + #define GET_C2H_MAC_HIDDEN_RPT_IRV(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 0, 0, 4) + u8 irv = GET_C2H_MAC_HIDDEN_RPT_IRV(data); + + if (DBG_C2H_MAC_HIDDEN_RPT_HANDLE) + RTW_PRINT("irv:0x%x\n", irv); + + if(irv != 0xf) + hal_data->version_id.CUTVersion = irv; + } + #endif + + ret = _SUCCESS; + +exit: + return ret; +} + +int hal_read_mac_hidden_rpt(_adapter *adapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(adapter); + int ret = _FAIL; + int ret_fwdl; + u8 mac_hidden_rpt[MAC_HIDDEN_RPT_LEN + MAC_HIDDEN_RPT_2_LEN] = {0}; + systime start = rtw_get_current_time(); + u32 cnt = 0; + u32 timeout_ms = 800; + u32 min_cnt = 10; + u8 id = C2H_DEFEATURE_RSVD; + int i; + +#if defined(CONFIG_USB_HCI) || defined(CONFIG_PCI_HCI) + u8 hci_type = rtw_get_intf_type(adapter); + + if ((hci_type == RTW_USB || hci_type == RTW_PCIE) + && !rtw_is_hw_init_completed(adapter)) + rtw_hal_power_on(adapter); +#endif + + /* inform FW mac hidden rpt from reg is needed */ + rtw_write8(adapter, REG_C2HEVT_MSG_NORMAL, C2H_DEFEATURE_RSVD); + + /* download FW */ + pHalData->not_xmitframe_fw_dl = 1; + ret_fwdl = rtw_hal_fw_dl(adapter, _FALSE); + pHalData->not_xmitframe_fw_dl = 0; + if (ret_fwdl != _SUCCESS) + goto mac_hidden_rpt_hdl; + + /* polling for data ready */ + start = rtw_get_current_time(); + do { + cnt++; + id = rtw_read8(adapter, REG_C2HEVT_MSG_NORMAL); + if (id == C2H_MAC_HIDDEN_RPT || RTW_CANNOT_IO(adapter)) + break; + rtw_msleep_os(10); + } while (rtw_get_passing_time_ms(start) < timeout_ms || cnt < min_cnt); + + if (id == C2H_MAC_HIDDEN_RPT) { + /* read data */ + for (i = 0; i < MAC_HIDDEN_RPT_LEN + MAC_HIDDEN_RPT_2_LEN; i++) + mac_hidden_rpt[i] = rtw_read8(adapter, REG_C2HEVT_MSG_NORMAL + 2 + i); + } + + /* inform FW mac hidden rpt has read */ + rtw_write8(adapter, REG_C2HEVT_MSG_NORMAL, C2H_DBG); + +mac_hidden_rpt_hdl: + c2h_mac_hidden_rpt_hdl(adapter, mac_hidden_rpt, MAC_HIDDEN_RPT_LEN); + c2h_mac_hidden_rpt_2_hdl(adapter, mac_hidden_rpt + MAC_HIDDEN_RPT_LEN, MAC_HIDDEN_RPT_2_LEN); + + if (ret_fwdl == _SUCCESS && id == C2H_MAC_HIDDEN_RPT) + ret = _SUCCESS; + +#if defined(CONFIG_USB_HCI) || defined(CONFIG_PCI_HCI) + if ((hci_type == RTW_USB || hci_type == RTW_PCIE) + && !rtw_is_hw_init_completed(adapter)) + rtw_hal_power_off(adapter); +#endif + + RTW_INFO("%s %s! (%u, %dms), fwdl:%d, id:0x%02x\n", __func__ + , (ret == _SUCCESS) ? "OK" : "Fail", cnt, rtw_get_passing_time_ms(start), ret_fwdl, id); + + return ret; +} +#endif /* CONFIG_RTW_MAC_HIDDEN_RPT */ + +int c2h_defeature_dbg_hdl(_adapter *adapter, u8 *data, u8 len) +{ + int ret = _FAIL; + + int i; + + if (len < DEFEATURE_DBG_LEN) { + RTW_WARN("%s len(%u) < %d\n", __func__, len, DEFEATURE_DBG_LEN); + goto exit; + } + + for (i = 0; i < len; i++) + RTW_PRINT("%s: 0x%02X\n", __func__, *(data + i)); + + ret = _SUCCESS; + +exit: + return ret; +} + +#ifndef DBG_CUSTOMER_STR_RPT_HANDLE +#define DBG_CUSTOMER_STR_RPT_HANDLE 0 +#endif + +#ifdef CONFIG_RTW_CUSTOMER_STR +s32 rtw_hal_h2c_customer_str_req(_adapter *adapter) +{ + u8 h2c_data[H2C_CUSTOMER_STR_REQ_LEN] = {0}; + + SET_H2CCMD_CUSTOMER_STR_REQ_EN(h2c_data, 1); + return rtw_hal_fill_h2c_cmd(adapter, H2C_CUSTOMER_STR_REQ, H2C_CUSTOMER_STR_REQ_LEN, h2c_data); +} + +#define C2H_CUSTOMER_STR_RPT_BYTE0(_data) ((u8 *)(_data)) +#define C2H_CUSTOMER_STR_RPT_2_BYTE8(_data) ((u8 *)(_data)) + +int c2h_customer_str_rpt_hdl(_adapter *adapter, u8 *data, u8 len) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + int ret = _FAIL; + int i; + + if (len < CUSTOMER_STR_RPT_LEN) { + RTW_WARN("%s len(%u) < %d\n", __func__, len, CUSTOMER_STR_RPT_LEN); + goto exit; + } + + if (DBG_CUSTOMER_STR_RPT_HANDLE) + RTW_PRINT_DUMP("customer_str_rpt: ", data, CUSTOMER_STR_RPT_LEN); + + _enter_critical_mutex(&dvobj->customer_str_mutex, NULL); + + if (dvobj->customer_str_sctx != NULL) { + if (dvobj->customer_str_sctx->status != RTW_SCTX_SUBMITTED) + RTW_WARN("%s invalid sctx.status:%d\n", __func__, dvobj->customer_str_sctx->status); + _rtw_memcpy(dvobj->customer_str, C2H_CUSTOMER_STR_RPT_BYTE0(data), CUSTOMER_STR_RPT_LEN); + dvobj->customer_str_sctx->status = RTX_SCTX_CSTR_WAIT_RPT2; + } else + RTW_WARN("%s sctx not set\n", __func__); + + _exit_critical_mutex(&dvobj->customer_str_mutex, NULL); + + ret = _SUCCESS; + +exit: + return ret; +} + +int c2h_customer_str_rpt_2_hdl(_adapter *adapter, u8 *data, u8 len) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + int ret = _FAIL; + int i; + + if (len < CUSTOMER_STR_RPT_2_LEN) { + RTW_WARN("%s len(%u) < %d\n", __func__, len, CUSTOMER_STR_RPT_2_LEN); + goto exit; + } + + if (DBG_CUSTOMER_STR_RPT_HANDLE) + RTW_PRINT_DUMP("customer_str_rpt_2: ", data, CUSTOMER_STR_RPT_2_LEN); + + _enter_critical_mutex(&dvobj->customer_str_mutex, NULL); + + if (dvobj->customer_str_sctx != NULL) { + if (dvobj->customer_str_sctx->status != RTX_SCTX_CSTR_WAIT_RPT2) + RTW_WARN("%s rpt not ready\n", __func__); + _rtw_memcpy(dvobj->customer_str + CUSTOMER_STR_RPT_LEN, C2H_CUSTOMER_STR_RPT_2_BYTE8(data), CUSTOMER_STR_RPT_2_LEN); + rtw_sctx_done(&dvobj->customer_str_sctx); + } else + RTW_WARN("%s sctx not set\n", __func__); + + _exit_critical_mutex(&dvobj->customer_str_mutex, NULL); + + ret = _SUCCESS; + +exit: + return ret; +} + +/* read customer str */ +s32 rtw_hal_customer_str_read(_adapter *adapter, u8 *cs) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct submit_ctx sctx; + s32 ret = _SUCCESS; + + _enter_critical_mutex(&dvobj->customer_str_mutex, NULL); + if (dvobj->customer_str_sctx != NULL) + ret = _FAIL; + else { + rtw_sctx_init(&sctx, 2 * 1000); + dvobj->customer_str_sctx = &sctx; + } + _exit_critical_mutex(&dvobj->customer_str_mutex, NULL); + + if (ret == _FAIL) { + RTW_WARN("%s another handle ongoing\n", __func__); + goto exit; + } + + ret = rtw_customer_str_req_cmd(adapter); + if (ret != _SUCCESS) { + RTW_WARN("%s read cmd fail\n", __func__); + _enter_critical_mutex(&dvobj->customer_str_mutex, NULL); + dvobj->customer_str_sctx = NULL; + _exit_critical_mutex(&dvobj->customer_str_mutex, NULL); + goto exit; + } + + /* wait till rpt done or timeout */ + rtw_sctx_wait(&sctx, __func__); + + _enter_critical_mutex(&dvobj->customer_str_mutex, NULL); + dvobj->customer_str_sctx = NULL; + if (sctx.status == RTW_SCTX_DONE_SUCCESS) + _rtw_memcpy(cs, dvobj->customer_str, RTW_CUSTOMER_STR_LEN); + else + ret = _FAIL; + _exit_critical_mutex(&dvobj->customer_str_mutex, NULL); + +exit: + return ret; +} + +s32 rtw_hal_h2c_customer_str_write(_adapter *adapter, const u8 *cs) +{ + u8 h2c_data_w1[H2C_CUSTOMER_STR_W1_LEN] = {0}; + u8 h2c_data_w2[H2C_CUSTOMER_STR_W2_LEN] = {0}; + u8 h2c_data_w3[H2C_CUSTOMER_STR_W3_LEN] = {0}; + s32 ret; + + SET_H2CCMD_CUSTOMER_STR_W1_EN(h2c_data_w1, 1); + _rtw_memcpy(H2CCMD_CUSTOMER_STR_W1_BYTE0(h2c_data_w1), cs, 6); + + SET_H2CCMD_CUSTOMER_STR_W2_EN(h2c_data_w2, 1); + _rtw_memcpy(H2CCMD_CUSTOMER_STR_W2_BYTE6(h2c_data_w2), cs + 6, 6); + + SET_H2CCMD_CUSTOMER_STR_W3_EN(h2c_data_w3, 1); + _rtw_memcpy(H2CCMD_CUSTOMER_STR_W3_BYTE12(h2c_data_w3), cs + 6 + 6, 4); + + ret = rtw_hal_fill_h2c_cmd(adapter, H2C_CUSTOMER_STR_W1, H2C_CUSTOMER_STR_W1_LEN, h2c_data_w1); + if (ret != _SUCCESS) { + RTW_WARN("%s w1 fail\n", __func__); + goto exit; + } + + ret = rtw_hal_fill_h2c_cmd(adapter, H2C_CUSTOMER_STR_W2, H2C_CUSTOMER_STR_W2_LEN, h2c_data_w2); + if (ret != _SUCCESS) { + RTW_WARN("%s w2 fail\n", __func__); + goto exit; + } + + ret = rtw_hal_fill_h2c_cmd(adapter, H2C_CUSTOMER_STR_W3, H2C_CUSTOMER_STR_W3_LEN, h2c_data_w3); + if (ret != _SUCCESS) { + RTW_WARN("%s w3 fail\n", __func__); + goto exit; + } + +exit: + return ret; +} + +/* write customer str and check if value reported is the same as requested */ +s32 rtw_hal_customer_str_write(_adapter *adapter, const u8 *cs) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct submit_ctx sctx; + s32 ret = _SUCCESS; + + _enter_critical_mutex(&dvobj->customer_str_mutex, NULL); + if (dvobj->customer_str_sctx != NULL) + ret = _FAIL; + else { + rtw_sctx_init(&sctx, 2 * 1000); + dvobj->customer_str_sctx = &sctx; + } + _exit_critical_mutex(&dvobj->customer_str_mutex, NULL); + + if (ret == _FAIL) { + RTW_WARN("%s another handle ongoing\n", __func__); + goto exit; + } + + ret = rtw_customer_str_write_cmd(adapter, cs); + if (ret != _SUCCESS) { + RTW_WARN("%s write cmd fail\n", __func__); + _enter_critical_mutex(&dvobj->customer_str_mutex, NULL); + dvobj->customer_str_sctx = NULL; + _exit_critical_mutex(&dvobj->customer_str_mutex, NULL); + goto exit; + } + + ret = rtw_customer_str_req_cmd(adapter); + if (ret != _SUCCESS) { + RTW_WARN("%s read cmd fail\n", __func__); + _enter_critical_mutex(&dvobj->customer_str_mutex, NULL); + dvobj->customer_str_sctx = NULL; + _exit_critical_mutex(&dvobj->customer_str_mutex, NULL); + goto exit; + } + + /* wait till rpt done or timeout */ + rtw_sctx_wait(&sctx, __func__); + + _enter_critical_mutex(&dvobj->customer_str_mutex, NULL); + dvobj->customer_str_sctx = NULL; + if (sctx.status == RTW_SCTX_DONE_SUCCESS) { + if (_rtw_memcmp(cs, dvobj->customer_str, RTW_CUSTOMER_STR_LEN) != _TRUE) { + RTW_WARN("%s read back check fail\n", __func__); + RTW_INFO_DUMP("write req: ", cs, RTW_CUSTOMER_STR_LEN); + RTW_INFO_DUMP("read back: ", dvobj->customer_str, RTW_CUSTOMER_STR_LEN); + ret = _FAIL; + } + } else + ret = _FAIL; + _exit_critical_mutex(&dvobj->customer_str_mutex, NULL); + +exit: + return ret; +} +#endif /* CONFIG_RTW_CUSTOMER_STR */ + +#ifdef RTW_PER_CMD_SUPPORT_FW +#define H2C_REQ_PER_RPT_LEN 5 +#define SET_H2CCMD_REQ_PER_RPT_GROUP_MACID(__pH2CCmd, __Value) SET_BITS_TO_LE_1BYTE(__pH2CCmd, 0, 4, __Value) +#define SET_H2CCMD_REQ_PER_RPT_RPT_TYPE(__pH2CCmd, __Value) SET_BITS_TO_LE_1BYTE(__pH2CCmd, 4, 4, __Value) +#define SET_H2CCMD_REQ_PER_RPT_MACID_BMAP(__pH2CCmd, __Value) SET_BITS_TO_LE_4BYTE(__pH2CCmd + 1, 0, 32, __Value) + +u8 rtw_hal_set_req_per_rpt_cmd(_adapter *adapter, u8 group_macid, + u8 rpt_type, u32 macid_bitmap) +{ + u8 ret = _FAIL; + u8 cmd_buf[H2C_REQ_PER_RPT_LEN] = {0}; + + SET_H2CCMD_REQ_PER_RPT_GROUP_MACID(cmd_buf, group_macid); + SET_H2CCMD_REQ_PER_RPT_RPT_TYPE(cmd_buf, rpt_type); + SET_H2CCMD_REQ_PER_RPT_MACID_BMAP(cmd_buf, macid_bitmap); + + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_REQ_PER_RPT, + H2C_REQ_PER_RPT_LEN, + cmd_buf); + return ret; +} + +#define GET_C2H_PER_RATE_RPT_TYPE0_MACID0(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)), 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE0_PER0(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 1, 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE0_RATE0(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 2, 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE0_BW0(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 3, 0, 2) +#define GET_C2H_PER_RATE_RPT_TYPE0_TOTAL_PKT0(_data) LE_BITS_TO_2BYTE(((u8 *)(_data)) + 4, 0, 16) +#define GET_C2H_PER_RATE_RPT_TYPE0_MACID1(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 6, 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE0_PER1(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 7, 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE0_RATE1(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 8, 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE0_BW1(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 9, 0, 2) +#define GET_C2H_PER_RATE_RPT_TYPE0_TOTAL_PKT1(_data) LE_BITS_TO_2BYTE(((u8 *)(_data)) + 10, 0, 16) + +#define GET_C2H_PER_RATE_RPT_TYPE1_MACID0(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)), 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE1_PER0(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 1, 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE1_RATE0(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 2, 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE1_BW0(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 3, 0, 2) +#define GET_C2H_PER_RATE_RPT_TYPE1_MACID1(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 4, 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE1_PER1(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 5, 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE1_RATE1(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 6, 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE1_BW1(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 7, 0, 2) +#define GET_C2H_PER_RATE_RPT_TYPE1_MACID2(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 8, 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE1_PER2(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 9, 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE1_RATE2(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 10, 0, 8) +#define GET_C2H_PER_RATE_RPT_TYPE1_BW2(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 11, 0, 2) + +static void per_rate_rpt_update(_adapter *adapter, u8 mac_id, + u8 per, u8 rate, + u8 bw, u8 total_pkt) +{ +#ifdef CONFIG_RTW_MESH + rtw_ieee80211s_update_metric(adapter, mac_id, + per, rate, + bw, total_pkt); +#endif +} + +int c2h_per_rate_rpt_hdl(_adapter *adapter, u8 *data, u8 len) +{ + /* Now only consider type0, since it covers all params in type1 + * type0: mac_id, per, rate, bw, total_pkt + * type1: mac_id, per, rate, bw + */ + u8 mac_id[2] = {0}, per[2] = {0}, rate[2] = {0}, bw[2] = {0}; + u16 total_pkt[2] = {0}; + int ret = _FAIL, i, macid_cnt = 0; + + /* type0: + * 1 macid includes 6 bytes info + 1 byte 0xff + * 2 macid includes 2*6 bytes info + */ + if (!(len == 7 || len == 12)) { + RTW_WARN("%s len(%u) != 7 or 12\n", __FUNCTION__, len); + goto exit; + } + + macid_cnt++; + mac_id[0] = GET_C2H_PER_RATE_RPT_TYPE0_MACID0(data); + per[0] = GET_C2H_PER_RATE_RPT_TYPE0_PER0(data); + rate[0] = GET_C2H_PER_RATE_RPT_TYPE0_RATE0(data); + bw[0] = GET_C2H_PER_RATE_RPT_TYPE0_BW0(data); + total_pkt[0] = GET_C2H_PER_RATE_RPT_TYPE0_TOTAL_PKT0(data); + + mac_id[1] = GET_C2H_PER_RATE_RPT_TYPE0_MACID1(data); + /* 0xff means no report anymore */ + if (mac_id[1] == 0xff) + goto update_per; + if (len != 12) { + RTW_WARN("%s incorrect format\n", __FUNCTION__); + goto exit; + } + macid_cnt++; + per[1] = GET_C2H_PER_RATE_RPT_TYPE0_PER1(data); + rate[1] = GET_C2H_PER_RATE_RPT_TYPE0_RATE1(data); + bw[1] = GET_C2H_PER_RATE_RPT_TYPE0_BW1(data); + total_pkt[1] = GET_C2H_PER_RATE_RPT_TYPE0_TOTAL_PKT1(data); + +update_per: + for (i = 0; i < macid_cnt; i++) { + RTW_DBG("[%s] type0 rpt[%d]: macid = %u, per = %u, " + "rate = %u, bw = %u, total_pkt = %u\n", + __FUNCTION__, i, mac_id[i], per[i], + rate[i], bw[i], total_pkt[i]); + per_rate_rpt_update(adapter, mac_id[i], + per[i], rate[i], + bw[i], total_pkt[i]); + } + ret = _SUCCESS; +exit: + return ret; +} +#endif /* RTW_PER_CMD_SUPPORT_FW */ + +#ifdef CONFIG_LPS_ACK +#define GET_C2H_LPS_STATUS_RPT_GET_ACTION(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)), 0, 8) +#define GET_C2H_LPS_STATUS_RPT_GET_STATUS_CODE(_data) LE_BITS_TO_1BYTE(((u8 *)(_data)) + 1, 0, 8) +#define DBG_LPS_STATUS_RPT 0 + +int c2h_lps_status_rpt(PADAPTER adapter, u8 *data, u8 len) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + struct submit_ctx *lps_sctx = &pwrpriv->lps_ack_sctx; + u8 action = 0; + s8 status_code = 0; + int ret = _FAIL; + + if (len < LPS_STATUS_RPT_LEN) { + RTW_WARN("%s len(%u) < %d\n", __func__, len, LPS_STATUS_RPT_LEN); + goto exit; + } + + action = GET_C2H_LPS_STATUS_RPT_GET_ACTION(data); + status_code = GET_C2H_LPS_STATUS_RPT_GET_STATUS_CODE(data); + + /* action=0: report force leave null data status */ + /* action=1: report Rf on status when receiving a SetPwrMode H2C with PwrState = RFON */ + switch (action) { + case 0: + /* status code 0: success, 1: no ack, 2: timeout, 3: cancel */ + case 1: + /* status code 0: FW has already turn to RFON */ + pwrpriv->lps_ack_status = status_code; + + if (DBG_LPS_STATUS_RPT) + RTW_INFO("=== [C2H LPS Action(%d)] LPS Status Code:%d ===\n", action, status_code); + + break; + default: + RTW_INFO("UnKnown Action(%d) for C2H LPS RPT\n", action); + break; + } + + rtw_sctx_done(&lps_sctx); + ret = _SUCCESS; + +exit: + return ret; +} +#endif /* CONFIG_LPS_ACK */ + +void rtw_hal_update_sta_wset(_adapter *adapter, struct sta_info *psta) +{ + u8 w_set = 0; + + if (psta->wireless_mode & WIRELESS_11B) + w_set |= WIRELESS_CCK; + + if ((psta->wireless_mode & WIRELESS_11G) || (psta->wireless_mode & WIRELESS_11A)) + w_set |= WIRELESS_OFDM; + + if ((psta->wireless_mode & WIRELESS_11_24N) || (psta->wireless_mode & WIRELESS_11_5N)) + w_set |= WIRELESS_HT; + + if (psta->wireless_mode & WIRELESS_11AC) + w_set |= WIRELESS_VHT; + + psta->cmn.support_wireless_set = w_set; +} + +void rtw_hal_update_sta_mimo_type(_adapter *adapter, struct sta_info *psta) +{ + s8 tx_nss, rx_nss; + + tx_nss = rtw_get_sta_tx_nss(adapter, psta); + rx_nss = rtw_get_sta_rx_nss(adapter, psta); + if ((tx_nss == 1) && (rx_nss == 1)) + psta->cmn.mimo_type = RF_1T1R; + else if ((tx_nss == 1) && (rx_nss == 2)) + psta->cmn.mimo_type = RF_1T2R; + else if ((tx_nss == 2) && (rx_nss == 2)) + psta->cmn.mimo_type = RF_2T2R; + else if ((tx_nss == 2) && (rx_nss == 3)) + psta->cmn.mimo_type = RF_2T3R; + else if ((tx_nss == 2) && (rx_nss == 4)) + psta->cmn.mimo_type = RF_2T4R; + else if ((tx_nss == 3) && (rx_nss == 3)) + psta->cmn.mimo_type = RF_3T3R; + else if ((tx_nss == 3) && (rx_nss == 4)) + psta->cmn.mimo_type = RF_3T4R; + else if ((tx_nss == 4) && (rx_nss == 4)) + psta->cmn.mimo_type = RF_4T4R; + else + rtw_warn_on(1); + +#ifdef CONFIG_CTRL_TXSS_BY_TP + rtw_ctrl_txss_update_mimo_type(adapter, psta); +#endif + + RTW_INFO("STA - MAC_ID:%d, Tx - %d SS, Rx - %d SS\n", + psta->cmn.mac_id, tx_nss, rx_nss); +} + +void rtw_hal_update_sta_smps_cap(_adapter *adapter, struct sta_info *psta) +{ + /*Spatial Multiplexing Power Save*/ +#if 0 + if (check_fwstate(&adapter->mlmepriv, WIFI_AP_STATE) == _TRUE) { + #ifdef CONFIG_80211N_HT + if (psta->htpriv.ht_option) { + if (psta->htpriv.smps_cap == 0) + psta->cmn.sm_ps = SM_PS_STATIC; + else if (psta->htpriv.smps_cap == 1) + psta->cmn.sm_ps = SM_PS_DYNAMIC; + else + psta->cmn.sm_ps = SM_PS_DISABLE; + } + #endif /* CONFIG_80211N_HT */ + } else +#endif + psta->cmn.sm_ps = SM_PS_DISABLE; + + RTW_INFO("STA - MAC_ID:%d, SM_PS %d\n", + psta->cmn.mac_id, psta->cmn.sm_ps); +} + +u8 rtw_get_mgntframe_raid(_adapter *adapter, unsigned char network_type) +{ + + u8 raid; + if (IS_NEW_GENERATION_IC(adapter)) { + + raid = (network_type & WIRELESS_11B) ? RATEID_IDX_B + : RATEID_IDX_G; + } else { + raid = (network_type & WIRELESS_11B) ? RATR_INX_WIRELESS_B + : RATR_INX_WIRELESS_G; + } + return raid; +} + +void rtw_hal_update_sta_rate_mask(PADAPTER padapter, struct sta_info *psta) +{ + u8 i, tx_nss; + u64 tx_ra_bitmap = 0, tmp64=0; + + if (psta == NULL) + return; + + /* b/g mode ra_bitmap */ + for (i = 0; i < sizeof(psta->bssrateset); i++) { + if (psta->bssrateset[i]) + tx_ra_bitmap |= rtw_get_bit_value_from_ieee_value(psta->bssrateset[i] & 0x7f); + } + +#ifdef CONFIG_80211N_HT +if (padapter->registrypriv.ht_enable && is_supported_ht(padapter->registrypriv.wireless_mode)) { + tx_nss = GET_HAL_TX_NSS(padapter); +#ifdef CONFIG_80211AC_VHT + if (psta->vhtpriv.vht_option) { + /* AC mode ra_bitmap */ + tx_ra_bitmap |= (rtw_vht_mcs_map_to_bitmap(psta->vhtpriv.vht_mcs_map, tx_nss) << 12); + } else +#endif /* CONFIG_80211AC_VHT */ + if (psta->htpriv.ht_option) { + /* n mode ra_bitmap */ + + /* Handling SMPS mode for AP MODE only*/ + if (check_fwstate(&padapter->mlmepriv, WIFI_AP_STATE) == _TRUE) { + /*0:static SMPS, 1:dynamic SMPS, 3:SMPS disabled, 2:reserved*/ + if (psta->htpriv.smps_cap == 0 || psta->htpriv.smps_cap == 1) { + /*operate with only one active receive chain // 11n-MCS rate <= MSC7*/ + tx_nss = rtw_min(tx_nss, 1); + } + } + + tmp64 = rtw_ht_mcs_set_to_bitmap(psta->htpriv.ht_cap.supp_mcs_set, tx_nss); + tx_ra_bitmap |= (tmp64 << 12); + } +} +#endif /* CONFIG_80211N_HT */ + psta->cmn.ra_info.ramask = tx_ra_bitmap; + psta->init_rate = get_highest_rate_idx(tx_ra_bitmap) & 0x3f; +} + +void rtw_hal_update_sta_ra_info(PADAPTER padapter, struct sta_info *psta) +{ + rtw_hal_update_sta_mimo_type(padapter, psta); + rtw_hal_update_sta_smps_cap(padapter, psta); + rtw_hal_update_sta_rate_mask(padapter, psta); +} + +#ifndef CONFIG_HAS_HW_VAR_BCN_CTRL_ADDR +static u32 hw_bcn_ctrl_addr(_adapter *adapter, u8 hw_port) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + + if (hw_port >= hal_spec->port_num) { + RTW_ERR(FUNC_ADPT_FMT" HW Port(%d) invalid\n", FUNC_ADPT_ARG(adapter), hw_port); + rtw_warn_on(1); + return 0; + } + + switch (hw_port) { + case HW_PORT0: + return REG_BCN_CTRL; + case HW_PORT1: + return REG_BCN_CTRL_1; + } + + return 0; +} +#endif + +static void rtw_hal_get_msr(_adapter *adapter, u8 *net_type) +{ +#ifdef RTW_HALMAC + rtw_halmac_get_network_type(adapter_to_dvobj(adapter), + adapter->hw_port, net_type); +#else /* !RTW_HALMAC */ + switch (adapter->hw_port) { + case HW_PORT0: + /*REG_CR - BIT[17:16]-Network Type for port 1*/ + *net_type = rtw_read8(adapter, MSR) & 0x03; + break; + case HW_PORT1: + /*REG_CR - BIT[19:18]-Network Type for port 1*/ + *net_type = (rtw_read8(adapter, MSR) & 0x0C) >> 2; + break; +#if defined(CONFIG_RTL8814A) + case HW_PORT2: + /*REG_CR_EXT- BIT[1:0]-Network Type for port 2*/ + *net_type = rtw_read8(adapter, MSR1) & 0x03; + break; + case HW_PORT3: + /*REG_CR_EXT- BIT[3:2]-Network Type for port 3*/ + *net_type = (rtw_read8(adapter, MSR1) & 0x0C) >> 2; + break; + case HW_PORT4: + /*REG_CR_EXT- BIT[5:4]-Network Type for port 4*/ + *net_type = (rtw_read8(adapter, MSR1) & 0x30) >> 4; + break; +#endif /*#if defined(CONFIG_RTL8814A)*/ + default: + RTW_INFO("[WARN] "ADPT_FMT"- invalid hw port -%d\n", + ADPT_ARG(adapter), adapter->hw_port); + rtw_warn_on(1); + break; + } +#endif /* !RTW_HALMAC */ +} + +#if defined(CONFIG_MI_WITH_MBSSID_CAM) && defined(CONFIG_MBSSID_CAM) /*For 2 hw ports - 88E/92E/8812/8821/8723B*/ +static u8 rtw_hal_net_type_decision(_adapter *adapter, u8 net_type) +{ + if ((adapter->hw_port == HW_PORT0) && (rtw_get_mbid_cam_entry_num(adapter))) { + if (net_type != _HW_STATE_NOLINK_) + return _HW_STATE_AP_; + } + return net_type; +} +#endif +static void rtw_hal_set_msr(_adapter *adapter, u8 net_type) +{ +#ifdef RTW_HALMAC + #if defined(CONFIG_MI_WITH_MBSSID_CAM) && defined(CONFIG_MBSSID_CAM) + net_type = rtw_hal_net_type_decision(adapter, net_type); + #endif + rtw_halmac_set_network_type(adapter_to_dvobj(adapter), + adapter->hw_port, net_type); +#else /* !RTW_HALMAC */ + u8 val8 = 0; + + switch (adapter->hw_port) { + case HW_PORT0: + #if defined(CONFIG_MI_WITH_MBSSID_CAM) && defined(CONFIG_MBSSID_CAM) + net_type = rtw_hal_net_type_decision(adapter, net_type); + #endif + /*REG_CR - BIT[17:16]-Network Type for port 0*/ + val8 = rtw_read8(adapter, MSR) & 0x0C; + val8 |= net_type; + rtw_write8(adapter, MSR, val8); + break; + case HW_PORT1: + /*REG_CR - BIT[19:18]-Network Type for port 1*/ + val8 = rtw_read8(adapter, MSR) & 0x03; + val8 |= net_type << 2; + rtw_write8(adapter, MSR, val8); + break; +#if defined(CONFIG_RTL8814A) + case HW_PORT2: + /*REG_CR_EXT- BIT[1:0]-Network Type for port 2*/ + val8 = rtw_read8(adapter, MSR1) & 0xFC; + val8 |= net_type; + rtw_write8(adapter, MSR1, val8); + break; + case HW_PORT3: + /*REG_CR_EXT- BIT[3:2]-Network Type for port 3*/ + val8 = rtw_read8(adapter, MSR1) & 0xF3; + val8 |= net_type << 2; + rtw_write8(adapter, MSR1, val8); + break; + case HW_PORT4: + /*REG_CR_EXT- BIT[5:4]-Network Type for port 4*/ + val8 = rtw_read8(adapter, MSR1) & 0xCF; + val8 |= net_type << 4; + rtw_write8(adapter, MSR1, val8); + break; +#endif /* CONFIG_RTL8814A */ + default: + RTW_INFO("[WARN] "ADPT_FMT"- invalid hw port -%d\n", + ADPT_ARG(adapter), adapter->hw_port); + rtw_warn_on(1); + break; + } +#endif /* !RTW_HALMAC */ +} + +#ifndef SEC_CAM_ACCESS_TIMEOUT_MS + #define SEC_CAM_ACCESS_TIMEOUT_MS 200 +#endif + +#ifndef DBG_SEC_CAM_ACCESS + #define DBG_SEC_CAM_ACCESS 0 +#endif + +u32 rtw_sec_read_cam(_adapter *adapter, u8 addr) +{ + _mutex *mutex = &adapter_to_dvobj(adapter)->cam_ctl.sec_cam_access_mutex; + u32 rdata; + u32 cnt = 0; + systime start = 0, end = 0; + u8 timeout = 0; + u8 sr = 0; + + _enter_critical_mutex(mutex, NULL); + + rtw_write32(adapter, REG_CAMCMD, CAM_POLLINIG | addr); + + start = rtw_get_current_time(); + while (1) { + if (rtw_is_surprise_removed(adapter)) { + sr = 1; + break; + } + + cnt++; + if (0 == (rtw_read32(adapter, REG_CAMCMD) & CAM_POLLINIG)) + break; + + if (rtw_get_passing_time_ms(start) > SEC_CAM_ACCESS_TIMEOUT_MS) { + timeout = 1; + break; + } + } + end = rtw_get_current_time(); + + rdata = rtw_read32(adapter, REG_CAMREAD); + + _exit_critical_mutex(mutex, NULL); + + if (DBG_SEC_CAM_ACCESS || timeout) { + RTW_INFO(FUNC_ADPT_FMT" addr:0x%02x, rdata:0x%08x, to:%u, polling:%u, %d ms\n" + , FUNC_ADPT_ARG(adapter), addr, rdata, timeout, cnt, rtw_get_time_interval_ms(start, end)); + } + + return rdata; +} + +void rtw_sec_write_cam(_adapter *adapter, u8 addr, u32 wdata) +{ + _mutex *mutex = &adapter_to_dvobj(adapter)->cam_ctl.sec_cam_access_mutex; + u32 cnt = 0; + systime start = 0, end = 0; + u8 timeout = 0; + u8 sr = 0; + + _enter_critical_mutex(mutex, NULL); + + rtw_write32(adapter, REG_CAMWRITE, wdata); + rtw_write32(adapter, REG_CAMCMD, CAM_POLLINIG | CAM_WRITE | addr); + + start = rtw_get_current_time(); + while (1) { + if (rtw_is_surprise_removed(adapter)) { + sr = 1; + break; + } + + cnt++; + if (0 == (rtw_read32(adapter, REG_CAMCMD) & CAM_POLLINIG)) + break; + + if (rtw_get_passing_time_ms(start) > SEC_CAM_ACCESS_TIMEOUT_MS) { + timeout = 1; + break; + } + } + end = rtw_get_current_time(); + + _exit_critical_mutex(mutex, NULL); + + if (DBG_SEC_CAM_ACCESS || timeout) { + RTW_INFO(FUNC_ADPT_FMT" addr:0x%02x, wdata:0x%08x, to:%u, polling:%u, %d ms\n" + , FUNC_ADPT_ARG(adapter), addr, wdata, timeout, cnt, rtw_get_time_interval_ms(start, end)); + } +} + +void rtw_sec_read_cam_ent(_adapter *adapter, u8 id, u8 *ctrl, u8 *mac, u8 *key) +{ + u8 i; + u32 rdata; + u8 begin = 0; + u8 end = 5; /* TODO: consider other key length accordingly */ + + if (!ctrl && !mac && !key) { + rtw_warn_on(1); + goto exit; + } + + /* TODO: check id range */ + + if (!ctrl && !mac) + begin = 2; /* read from key */ + + if (!key && !mac) + end = 0; /* read to ctrl */ + else if (!key) + end = 2; /* read to mac */ + + for (i = begin; i <= end; i++) { + rdata = rtw_sec_read_cam(adapter, (id << 3) | i); + + switch (i) { + case 0: + if (ctrl) + _rtw_memcpy(ctrl, (u8 *)(&rdata), 2); + if (mac) + _rtw_memcpy(mac, ((u8 *)(&rdata)) + 2, 2); + break; + case 1: + if (mac) + _rtw_memcpy(mac + 2, (u8 *)(&rdata), 4); + break; + default: + if (key) + _rtw_memcpy(key + (i - 2) * 4, (u8 *)(&rdata), 4); + break; + } + } + +exit: + return; +} + + +void rtw_sec_write_cam_ent(_adapter *adapter, u8 id, u16 ctrl, u8 *mac, u8 *key) +{ + unsigned int i; + int j; + u8 addr, addr1 = 0; + u32 wdata, wdata1 = 0; + + /* TODO: consider other key length accordingly */ +#if 0 + switch ((ctrl & 0x1c) >> 2) { + case _WEP40_: + case _TKIP_: + case _AES_: + case _WEP104_: + + } +#else + j = 7; +#endif + + for (; j >= 0; j--) { + switch (j) { + case 0: + wdata = (ctrl | (mac[0] << 16) | (mac[1] << 24)); + break; + case 1: + wdata = (mac[2] | (mac[3] << 8) | (mac[4] << 16) | (mac[5] << 24)); + break; + case 6: + case 7: + wdata = 0; + break; + default: + i = (j - 2) << 2; + wdata = (key[i] | (key[i + 1] << 8) | (key[i + 2] << 16) | (key[i + 3] << 24)); + break; + } + + addr = (id << 3) + j; + +#if defined(CONFIG_RTL8192F) + if(j == 1) { + wdata1 = wdata; + addr1 = addr; + continue; + } +#endif + + rtw_sec_write_cam(adapter, addr, wdata); + } + +#if defined(CONFIG_RTL8192F) + rtw_sec_write_cam(adapter, addr1, wdata1); +#endif +} + +void rtw_sec_clr_cam_ent(_adapter *adapter, u8 id) +{ + u8 addr; + + addr = (id << 3); + rtw_sec_write_cam(adapter, addr, 0); +} + +bool rtw_sec_read_cam_is_gk(_adapter *adapter, u8 id) +{ + bool res; + u16 ctrl; + + rtw_sec_read_cam_ent(adapter, id, (u8 *)&ctrl, NULL, NULL); + + res = (ctrl & BIT6) ? _TRUE : _FALSE; + return res; +} +#ifdef CONFIG_MBSSID_CAM +void rtw_mbid_cam_init(struct dvobj_priv *dvobj) +{ + struct mbid_cam_ctl_t *mbid_cam_ctl = &dvobj->mbid_cam_ctl; + + _rtw_spinlock_init(&mbid_cam_ctl->lock); + mbid_cam_ctl->bitmap = 0; + ATOMIC_SET(&mbid_cam_ctl->mbid_entry_num, 0); + _rtw_memset(&dvobj->mbid_cam_cache, 0, sizeof(dvobj->mbid_cam_cache)); +} + +void rtw_mbid_cam_deinit(struct dvobj_priv *dvobj) +{ + struct mbid_cam_ctl_t *mbid_cam_ctl = &dvobj->mbid_cam_ctl; + + _rtw_spinlock_free(&mbid_cam_ctl->lock); +} + +void rtw_mbid_cam_reset(_adapter *adapter) +{ + _irqL irqL; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mbid_cam_ctl_t *mbid_cam_ctl = &dvobj->mbid_cam_ctl; + + _enter_critical_bh(&mbid_cam_ctl->lock, &irqL); + mbid_cam_ctl->bitmap = 0; + _rtw_memset(&dvobj->mbid_cam_cache, 0, sizeof(dvobj->mbid_cam_cache)); + _exit_critical_bh(&mbid_cam_ctl->lock, &irqL); + + ATOMIC_SET(&mbid_cam_ctl->mbid_entry_num, 0); +} +static u8 _rtw_mbid_cam_search_by_macaddr(_adapter *adapter, u8 *mac_addr) +{ + u8 i; + u8 cam_id = INVALID_CAM_ID; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + + for (i = 0; i < TOTAL_MBID_CAM_NUM; i++) { + if (mac_addr && _rtw_memcmp(dvobj->mbid_cam_cache[i].mac_addr, mac_addr, ETH_ALEN) == _TRUE) { + cam_id = i; + break; + } + } + + RTW_INFO("%s mac:"MAC_FMT" - cam_id:%d\n", __func__, MAC_ARG(mac_addr), cam_id); + return cam_id; +} + +u8 rtw_mbid_cam_search_by_macaddr(_adapter *adapter, u8 *mac_addr) +{ + _irqL irqL; + + u8 cam_id = INVALID_CAM_ID; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mbid_cam_ctl_t *mbid_cam_ctl = &dvobj->mbid_cam_ctl; + + _enter_critical_bh(&mbid_cam_ctl->lock, &irqL); + cam_id = _rtw_mbid_cam_search_by_macaddr(adapter, mac_addr); + _exit_critical_bh(&mbid_cam_ctl->lock, &irqL); + + return cam_id; +} +static u8 _rtw_mbid_cam_search_by_ifaceid(_adapter *adapter, u8 iface_id) +{ + u8 i; + u8 cam_id = INVALID_CAM_ID; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + + for (i = 0; i < TOTAL_MBID_CAM_NUM; i++) { + if (iface_id == dvobj->mbid_cam_cache[i].iface_id) { + cam_id = i; + break; + } + } + if (cam_id != INVALID_CAM_ID) + RTW_INFO("%s iface_id:%d mac:"MAC_FMT" - cam_id:%d\n", + __func__, iface_id, MAC_ARG(dvobj->mbid_cam_cache[cam_id].mac_addr), cam_id); + + return cam_id; +} + +u8 rtw_mbid_cam_search_by_ifaceid(_adapter *adapter, u8 iface_id) +{ + _irqL irqL; + u8 cam_id = INVALID_CAM_ID; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mbid_cam_ctl_t *mbid_cam_ctl = &dvobj->mbid_cam_ctl; + + _enter_critical_bh(&mbid_cam_ctl->lock, &irqL); + cam_id = _rtw_mbid_cam_search_by_ifaceid(adapter, iface_id); + _exit_critical_bh(&mbid_cam_ctl->lock, &irqL); + + return cam_id; +} +u8 rtw_get_max_mbid_cam_id(_adapter *adapter) +{ + _irqL irqL; + s8 i; + u8 cam_id = INVALID_CAM_ID; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mbid_cam_ctl_t *mbid_cam_ctl = &dvobj->mbid_cam_ctl; + + _enter_critical_bh(&mbid_cam_ctl->lock, &irqL); + for (i = (TOTAL_MBID_CAM_NUM - 1); i >= 0; i--) { + if (mbid_cam_ctl->bitmap & BIT(i)) { + cam_id = i; + break; + } + } + _exit_critical_bh(&mbid_cam_ctl->lock, &irqL); + /*RTW_INFO("%s max cam_id:%d\n", __func__, cam_id);*/ + return cam_id; +} + +inline u8 rtw_get_mbid_cam_entry_num(_adapter *adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mbid_cam_ctl_t *mbid_cam_ctl = &dvobj->mbid_cam_ctl; + + return ATOMIC_READ(&mbid_cam_ctl->mbid_entry_num); +} + +static inline void mbid_cam_cache_init(_adapter *adapter, struct mbid_cam_cache *pmbid_cam, u8 *mac_addr) +{ + if (adapter && pmbid_cam && mac_addr) { + _rtw_memcpy(pmbid_cam->mac_addr, mac_addr, ETH_ALEN); + pmbid_cam->iface_id = adapter->iface_id; + } +} +static inline void mbid_cam_cache_clr(struct mbid_cam_cache *pmbid_cam) +{ + if (pmbid_cam) { + _rtw_memset(pmbid_cam->mac_addr, 0, ETH_ALEN); + pmbid_cam->iface_id = CONFIG_IFACE_NUMBER; + } +} + +u8 rtw_mbid_camid_alloc(_adapter *adapter, u8 *mac_addr) +{ + _irqL irqL; + u8 cam_id = INVALID_CAM_ID, i; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mbid_cam_ctl_t *mbid_cam_ctl = &dvobj->mbid_cam_ctl; + u8 entry_num = ATOMIC_READ(&mbid_cam_ctl->mbid_entry_num); + + if (INVALID_CAM_ID != rtw_mbid_cam_search_by_macaddr(adapter, mac_addr)) + goto exit; + + if (entry_num >= TOTAL_MBID_CAM_NUM) { + RTW_INFO(FUNC_ADPT_FMT" failed !! MBSSID number :%d over TOTAL_CAM_ENTRY(8)\n", FUNC_ADPT_ARG(adapter), entry_num); + rtw_warn_on(1); + } + + _enter_critical_bh(&mbid_cam_ctl->lock, &irqL); + for (i = 0; i < TOTAL_MBID_CAM_NUM; i++) { + if (!(mbid_cam_ctl->bitmap & BIT(i))) { + mbid_cam_ctl->bitmap |= BIT(i); + cam_id = i; + break; + } + } + if ((cam_id != INVALID_CAM_ID) && (mac_addr)) + mbid_cam_cache_init(adapter, &dvobj->mbid_cam_cache[cam_id], mac_addr); + _exit_critical_bh(&mbid_cam_ctl->lock, &irqL); + + if (cam_id != INVALID_CAM_ID) { + ATOMIC_INC(&mbid_cam_ctl->mbid_entry_num); + RTW_INFO("%s mac:"MAC_FMT" - cam_id:%d\n", __func__, MAC_ARG(mac_addr), cam_id); +#ifdef DBG_MBID_CAM_DUMP + rtw_mbid_cam_cache_dump(RTW_DBGDUMP, __func__, adapter); +#endif + } else + RTW_INFO("%s [WARN] "MAC_FMT" - invalid cam_id:%d\n", __func__, MAC_ARG(mac_addr), cam_id); +exit: + return cam_id; +} + +u8 rtw_mbid_cam_info_change(_adapter *adapter, u8 *mac_addr) +{ + _irqL irqL; + u8 entry_id = INVALID_CAM_ID; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mbid_cam_ctl_t *mbid_cam_ctl = &dvobj->mbid_cam_ctl; + + _enter_critical_bh(&mbid_cam_ctl->lock, &irqL); + entry_id = _rtw_mbid_cam_search_by_ifaceid(adapter, adapter->iface_id); + if (entry_id != INVALID_CAM_ID) + mbid_cam_cache_init(adapter, &dvobj->mbid_cam_cache[entry_id], mac_addr); + + _exit_critical_bh(&mbid_cam_ctl->lock, &irqL); + + return entry_id; +} + +u8 rtw_mbid_cam_assign(_adapter *adapter, u8 *mac_addr, u8 camid) +{ + _irqL irqL; + u8 ret = _FALSE; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mbid_cam_ctl_t *mbid_cam_ctl = &dvobj->mbid_cam_ctl; + + if ((camid >= TOTAL_MBID_CAM_NUM) || (camid == INVALID_CAM_ID)) { + RTW_INFO(FUNC_ADPT_FMT" failed !! invlaid mbid_canid :%d\n", FUNC_ADPT_ARG(adapter), camid); + rtw_warn_on(1); + } + if (INVALID_CAM_ID != rtw_mbid_cam_search_by_macaddr(adapter, mac_addr)) + goto exit; + + _enter_critical_bh(&mbid_cam_ctl->lock, &irqL); + if (!(mbid_cam_ctl->bitmap & BIT(camid))) { + if (mac_addr) { + mbid_cam_ctl->bitmap |= BIT(camid); + mbid_cam_cache_init(adapter, &dvobj->mbid_cam_cache[camid], mac_addr); + ret = _TRUE; + } + } + _exit_critical_bh(&mbid_cam_ctl->lock, &irqL); + + if (ret == _TRUE) { + ATOMIC_INC(&mbid_cam_ctl->mbid_entry_num); + RTW_INFO("%s mac:"MAC_FMT" - cam_id:%d\n", __func__, MAC_ARG(mac_addr), camid); +#ifdef DBG_MBID_CAM_DUMP + rtw_mbid_cam_cache_dump(RTW_DBGDUMP, __func__, adapter); +#endif + } else + RTW_INFO("%s [WARN] mac:"MAC_FMT" - cam_id:%d assigned failed\n", __func__, MAC_ARG(mac_addr), camid); + +exit: + return ret; +} + +void rtw_mbid_camid_clean(_adapter *adapter, u8 mbss_canid) +{ + _irqL irqL; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mbid_cam_ctl_t *mbid_cam_ctl = &dvobj->mbid_cam_ctl; + + if ((mbss_canid >= TOTAL_MBID_CAM_NUM) || (mbss_canid == INVALID_CAM_ID)) { + RTW_INFO(FUNC_ADPT_FMT" failed !! invlaid mbid_canid :%d\n", FUNC_ADPT_ARG(adapter), mbss_canid); + rtw_warn_on(1); + } + _enter_critical_bh(&mbid_cam_ctl->lock, &irqL); + mbid_cam_cache_clr(&dvobj->mbid_cam_cache[mbss_canid]); + mbid_cam_ctl->bitmap &= (~BIT(mbss_canid)); + _exit_critical_bh(&mbid_cam_ctl->lock, &irqL); + ATOMIC_DEC(&mbid_cam_ctl->mbid_entry_num); + RTW_INFO("%s - cam_id:%d\n", __func__, mbss_canid); +} +int rtw_mbid_cam_cache_dump(void *sel, const char *fun_name, _adapter *adapter) +{ + _irqL irqL; + u8 i; + _adapter *iface; + u8 iface_id; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mbid_cam_ctl_t *mbid_cam_ctl = &dvobj->mbid_cam_ctl; + u8 entry_num = ATOMIC_READ(&mbid_cam_ctl->mbid_entry_num); + u8 max_cam_id = rtw_get_max_mbid_cam_id(adapter); + + RTW_PRINT_SEL(sel, "== MBSSID CAM DUMP (%s)==\n", fun_name); + + _enter_critical_bh(&mbid_cam_ctl->lock, &irqL); + RTW_PRINT_SEL(sel, "Entry numbers:%d, max_camid:%d, bitmap:0x%08x\n", entry_num, max_cam_id, mbid_cam_ctl->bitmap); + for (i = 0; i < TOTAL_MBID_CAM_NUM; i++) { + RTW_PRINT_SEL(sel, "CAM_ID = %d\t", i); + + if (mbid_cam_ctl->bitmap & BIT(i)) { + iface_id = dvobj->mbid_cam_cache[i].iface_id; + _RTW_PRINT_SEL(sel, "IF_ID:%d\t", iface_id); + _RTW_PRINT_SEL(sel, "MAC Addr:"MAC_FMT"\t", MAC_ARG(dvobj->mbid_cam_cache[i].mac_addr)); + + iface = dvobj->padapters[iface_id]; + if (iface) { + if (MLME_IS_STA(iface)) + _RTW_PRINT_SEL(sel, "ROLE:%s\n", "STA"); + else if (MLME_IS_AP(iface)) + _RTW_PRINT_SEL(sel, "ROLE:%s\n", "AP"); + else if (MLME_IS_MESH(iface)) + _RTW_PRINT_SEL(sel, "ROLE:%s\n", "MESH"); + else + _RTW_PRINT_SEL(sel, "ROLE:%s\n", "NONE"); + } + + } else + _RTW_PRINT_SEL(sel, "N/A\n"); + } + _exit_critical_bh(&mbid_cam_ctl->lock, &irqL); + return 0; +} + +static void read_mbssid_cam(_adapter *padapter, u8 cam_addr, u8 *mac) +{ + u8 poll = 1; + u8 cam_ready = _FALSE; + u32 cam_data1 = 0; + u16 cam_data2 = 0; + + if (RTW_CANNOT_RUN(padapter)) + return; + + rtw_write32(padapter, REG_MBIDCAMCFG_2, BIT_MBIDCAM_POLL | ((cam_addr & MBIDCAM_ADDR_MASK) << MBIDCAM_ADDR_SHIFT)); + + do { + if (0 == (rtw_read32(padapter, REG_MBIDCAMCFG_2) & BIT_MBIDCAM_POLL)) { + cam_ready = _TRUE; + break; + } + poll++; + } while ((poll % 10) != 0 && !RTW_CANNOT_RUN(padapter)); + + if (cam_ready) { + cam_data1 = rtw_read32(padapter, REG_MBIDCAMCFG_1); + mac[0] = cam_data1 & 0xFF; + mac[1] = (cam_data1 >> 8) & 0xFF; + mac[2] = (cam_data1 >> 16) & 0xFF; + mac[3] = (cam_data1 >> 24) & 0xFF; + + cam_data2 = rtw_read16(padapter, REG_MBIDCAMCFG_2); + mac[4] = cam_data2 & 0xFF; + mac[5] = (cam_data2 >> 8) & 0xFF; + } + +} +int rtw_mbid_cam_dump(void *sel, const char *fun_name, _adapter *adapter) +{ + /*_irqL irqL;*/ + u8 i; + u8 mac_addr[ETH_ALEN]; + + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mbid_cam_ctl_t *mbid_cam_ctl = &dvobj->mbid_cam_ctl; + + RTW_PRINT_SEL(sel, "\n== MBSSID HW-CAM DUMP (%s)==\n", fun_name); + + /*_enter_critical_bh(&mbid_cam_ctl->lock, &irqL);*/ + for (i = 0; i < TOTAL_MBID_CAM_NUM; i++) { + RTW_PRINT_SEL(sel, "CAM_ID = %d\t", i); + _rtw_memset(mac_addr, 0, ETH_ALEN); + read_mbssid_cam(adapter, i, mac_addr); + _RTW_PRINT_SEL(sel, "MAC Addr:"MAC_FMT"\n", MAC_ARG(mac_addr)); + } + /*_exit_critical_bh(&mbid_cam_ctl->lock, &irqL);*/ + return 0; +} + +static void write_mbssid_cam(_adapter *padapter, u8 cam_addr, u8 *mac) +{ + u32 cam_val[2] = {0}; + + cam_val[0] = (mac[3] << 24) | (mac[2] << 16) | (mac[1] << 8) | mac[0]; + cam_val[1] = ((cam_addr & MBIDCAM_ADDR_MASK) << MBIDCAM_ADDR_SHIFT) | (mac[5] << 8) | mac[4]; + + rtw_hal_set_hwreg(padapter, HW_VAR_MBSSID_CAM_WRITE, (u8 *)cam_val); +} + +/* +static void clear_mbssid_cam(_adapter *padapter, u8 cam_addr) +{ + rtw_hal_set_hwreg(padapter, HW_VAR_MBSSID_CAM_CLEAR, &cam_addr); +} +*/ + +void rtw_ap_set_mbid_num(_adapter *adapter, u8 ap_num) +{ + rtw_write8(adapter, REG_MBID_NUM, + ((rtw_read8(adapter, REG_MBID_NUM) & 0xF8) | ((ap_num -1) & 0x07))); + +} +void rtw_mbid_cam_enable(_adapter *adapter) +{ + /*enable MBSSID*/ + rtw_hal_rcr_add(adapter, RCR_ENMBID); +} +void rtw_mi_set_mbid_cam(_adapter *adapter) +{ + u8 i; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mbid_cam_ctl_t *mbid_cam_ctl = &dvobj->mbid_cam_ctl; + +#ifdef DBG_MBID_CAM_DUMP + rtw_mbid_cam_cache_dump(RTW_DBGDUMP, __func__, adapter); +#endif + + for (i = 0; i < TOTAL_MBID_CAM_NUM; i++) { + if (mbid_cam_ctl->bitmap & BIT(i)) { + write_mbssid_cam(adapter, i, dvobj->mbid_cam_cache[i].mac_addr); + RTW_INFO("%s - cam_id:%d => mac:"MAC_FMT"\n", __func__, i, MAC_ARG(dvobj->mbid_cam_cache[i].mac_addr)); + } + } + rtw_mbid_cam_enable(adapter); +} +#endif /*CONFIG_MBSSID_CAM*/ + +#ifdef CONFIG_FW_HANDLE_TXBCN +#define H2C_BCN_OFFLOAD_LEN 1 + +#define SET_H2CCMD_BCN_OFFLOAD_EN(__pH2CCmd, __Value) SET_BITS_TO_LE_1BYTE(__pH2CCmd, 0, 1, __Value) +#define SET_H2CCMD_BCN_ROOT_TBTT_RPT(__pH2CCmd, __Value) SET_BITS_TO_LE_1BYTE(__pH2CCmd, 1, 1, __Value) +#define SET_H2CCMD_BCN_VAP1_TBTT_RPT(__pH2CCmd, __Value) SET_BITS_TO_LE_1BYTE(__pH2CCmd, 2, 1, __Value) +#define SET_H2CCMD_BCN_VAP2_TBTT_RPT(__pH2CCmd, __Value) SET_BITS_TO_LE_1BYTE(__pH2CCmd, 3, 1, __Value) +#define SET_H2CCMD_BCN_VAP3_TBTT_RPT(__pH2CCmd, __Value) SET_BITS_TO_LE_1BYTE(__pH2CCmd, 4, 1, __Value) +#define SET_H2CCMD_BCN_VAP4_TBTT_RPT(__pH2CCmd, __Value) SET_BITS_TO_LE_1BYTE(__pH2CCmd, 5, 1, __Value) + +void rtw_hal_set_fw_ap_bcn_offload_cmd(_adapter *adapter, bool fw_bcn_en, u8 tbtt_rpt_map) +{ + u8 fw_bcn_offload[1] = {0}; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + + if (fw_bcn_en) + SET_H2CCMD_BCN_OFFLOAD_EN(fw_bcn_offload, 1); + + if (tbtt_rpt_map & BIT(0)) + SET_H2CCMD_BCN_ROOT_TBTT_RPT(fw_bcn_offload, 1); + if (tbtt_rpt_map & BIT(1)) + SET_H2CCMD_BCN_VAP1_TBTT_RPT(fw_bcn_offload, 1); + if (tbtt_rpt_map & BIT(2)) + SET_H2CCMD_BCN_VAP2_TBTT_RPT(fw_bcn_offload, 1); + if (tbtt_rpt_map & BIT(3)) + SET_H2CCMD_BCN_VAP3_TBTT_RPT(fw_bcn_offload, 1); + + dvobj->vap_tbtt_rpt_map = tbtt_rpt_map; + dvobj->fw_bcn_offload = fw_bcn_en; + RTW_INFO("[FW BCN] Offload : %s\n", (dvobj->fw_bcn_offload) ? "EN" : "DIS"); + RTW_INFO("[FW BCN] TBTT RPT map : 0x%02x\n", dvobj->vap_tbtt_rpt_map); + + rtw_hal_fill_h2c_cmd(adapter, H2C_FW_BCN_OFFLOAD, + H2C_BCN_OFFLOAD_LEN, fw_bcn_offload); +} + +void rtw_hal_set_bcn_rsvdpage_loc_cmd(_adapter *adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + u8 ret, vap_id; + u32 page_size = 0; + u8 bcn_rsvdpage[H2C_BCN_RSVDPAGE_LEN] = {0}; + + rtw_hal_get_def_var(adapter, HAL_DEF_TX_PAGE_SIZE, (u8 *)&page_size); + #if 1 + for (vap_id = 0; vap_id < CONFIG_LIMITED_AP_NUM; vap_id++) { + if (dvobj->vap_map & BIT(vap_id)) + bcn_rsvdpage[vap_id] = vap_id * (MAX_BEACON_LEN / page_size); + } + #else +#define SET_H2CCMD_BCN_RSVDPAGE_LOC_ROOT(__pH2CCmd, __Value) SET_BITS_TO_LE_1BYTE(__pH2CCmd, 0, 8, __Value) +#define SET_H2CCMD_BCN_RSVDPAGE_LOC_VAP1(__pH2CCmd, __Value) SET_BITS_TO_LE_1BYTE(__pH2CCmd, 1, 8, __Value) +#define SET_H2CCMD_BCN_RSVDPAGE_LOC_VAP2(__pH2CCmd, __Value) SET_BITS_TO_LE_1BYTE(__pH2CCmd, 2, 8, __Value) +#define SET_H2CCMD_BCN_RSVDPAGE_LOC_VAP3(__pH2CCmd, __Value) SET_BITS_TO_LE_1BYTE(__pH2CCmd, 3, 8, __Value) +#define SET_H2CCMD_BCN_RSVDPAGE_LOC_VAP4(__pH2CCmd, __Value) SET_BITS_TO_LE_1BYTE(__pH2CCmd, 4, 8, __Value) + + if (dvobj->vap_map & BIT(0)) + SET_H2CCMD_BCN_RSVDPAGE_LOC_ROOT(bcn_rsvdpage, 0); + if (dvobj->vap_map & BIT(1)) + SET_H2CCMD_BCN_RSVDPAGE_LOC_VAP1(bcn_rsvdpage, + 1 * (MAX_BEACON_LEN / page_size)); + if (dvobj->vap_map & BIT(2)) + SET_H2CCMD_BCN_RSVDPAGE_LOC_VAP2(bcn_rsvdpage, + 2 * (MAX_BEACON_LEN / page_size)); + if (dvobj->vap_map & BIT(3)) + SET_H2CCMD_BCN_RSVDPAGE_LOC_VAP3(bcn_rsvdpage, + 3 * (MAX_BEACON_LEN / page_size)); + if (dvobj->vap_map & BIT(4)) + SET_H2CCMD_BCN_RSVDPAGE_LOC_VAP4(bcn_rsvdpage, + 4 * (MAX_BEACON_LEN / page_size)); + #endif + if (1) { + RTW_INFO("[BCN_LOC] vap_map : 0x%02x\n", dvobj->vap_map); + RTW_INFO("[BCN_LOC] page_size :%d, @bcn_page_num :%d\n" + , page_size, (MAX_BEACON_LEN / page_size)); + RTW_INFO("[BCN_LOC] root ap : 0x%02x\n", *bcn_rsvdpage); + RTW_INFO("[BCN_LOC] vap_1 : 0x%02x\n", *(bcn_rsvdpage + 1)); + RTW_INFO("[BCN_LOC] vap_2 : 0x%02x\n", *(bcn_rsvdpage + 2)); + RTW_INFO("[BCN_LOC] vap_3 : 0x%02x\n", *(bcn_rsvdpage + 3)); + RTW_INFO("[BCN_LOC] vap_4 : 0x%02x\n", *(bcn_rsvdpage + 4)); + } + ret = rtw_hal_fill_h2c_cmd(adapter, H2C_BCN_RSVDPAGE, + H2C_BCN_RSVDPAGE_LEN, bcn_rsvdpage); +} + +void rtw_ap_multi_bcn_cfg(_adapter *adapter) +{ + u8 dft_bcn_space = DEFAULT_BCN_INTERVAL; + u8 sub_bcn_space = (DEFAULT_BCN_INTERVAL / CONFIG_LIMITED_AP_NUM); + + /*enable to rx data frame*/ + rtw_write16(adapter, REG_RXFLTMAP2, 0xFFFF); + + /*Disable Port0's beacon function*/ + rtw_write8(adapter, REG_BCN_CTRL, rtw_read8(adapter, REG_BCN_CTRL) & ~BIT_EN_BCN_FUNCTION); + /*Reset Port0's TSF*/ + rtw_write8(adapter, REG_DUAL_TSF_RST, BIT_TSFTR_RST); + + rtw_ap_set_mbid_num(adapter, CONFIG_LIMITED_AP_NUM); + + /*BCN space & BCN sub-space 0x554[15:0] = 0x64,0x5BC[23:16] = 0x21*/ + rtw_halmac_set_bcn_interval(adapter_to_dvobj(adapter), HW_PORT0, dft_bcn_space); + rtw_write8(adapter, REG_MBSSID_BCN_SPACE3 + 2, sub_bcn_space); + + #if 0 /*setting in hw_var_set_opmode_mbid - ResumeTxBeacon*/ + /*BCN hold time 0x540[19:8] = 0x80*/ + rtw_write8(adapter, REG_TBTT_PROHIBIT + 1, TBTT_PROHIBIT_HOLD_TIME & 0xFF); + rtw_write8(adapter, REG_TBTT_PROHIBIT + 2, + (rtw_read8(adapter, REG_TBTT_PROHIBIT + 2) & 0xF0) | (TBTT_PROHIBIT_HOLD_TIME >> 8)); + #endif + + /*ATIM window -0x55A = 0x32, reg 0x570 = 0x32, reg 0x5A0 = 0x32 */ + rtw_write8(adapter, REG_ATIMWND, 0x32); + rtw_write8(adapter, REG_ATIMWND1_V1, 0x32); + rtw_write8(adapter, REG_ATIMWND2, 0x32); + rtw_write8(adapter, REG_ATIMWND3, 0x32); + /* + rtw_write8(adapter, REG_ATIMWND4, 0x32); + rtw_write8(adapter, REG_ATIMWND5, 0x32); + rtw_write8(adapter, REG_ATIMWND6, 0x32); + rtw_write8(adapter, REG_ATIMWND7, 0x32);*/ + + /*no limit setting - 0x5A7 = 0xFF - Packet in Hi Queue Tx immediately*/ + rtw_write8(adapter, REG_HIQ_NO_LMT_EN, 0xFF); + + /*Mask all beacon*/ + rtw_write8(adapter, REG_MBSSID_CTRL, 0); + + /*BCN invalid bit setting 0x454[6] = 1*/ + /*rtw_write8(adapter, REG_CCK_CHECK, rtw_read8(adapter, REG_CCK_CHECK) | BIT_EN_BCN_PKT_REL);*/ + + /*Enable Port0's beacon function*/ + rtw_write8(adapter, REG_BCN_CTRL, + rtw_read8(adapter, REG_BCN_CTRL) | BIT_DIS_RX_BSSID_FIT | BIT_P0_EN_TXBCN_RPT | BIT_DIS_TSF_UDT | BIT_EN_BCN_FUNCTION); + + /* Enable HW seq for BCN + * 0x4FC[0]: EN_HWSEQ / 0x4FC[1]: EN_HWSEQEXT */ + #ifdef CONFIG_RTL8822B + if (IS_HARDWARE_TYPE_8822B(adapter)) + rtw_write8(adapter, REG_DUMMY_PAGE4_V1_8822B, 0x01); + #endif + + #ifdef CONFIG_RTL8822C + if (IS_HARDWARE_TYPE_8822C(adapter)) + rtw_write8(adapter, REG_DUMMY_PAGE4_V1_8822C, 0x01); + #endif +} +static void _rtw_mbid_bcn_cfg(_adapter *adapter, bool mbcnq_en, u8 mbcnq_id) +{ + if (mbcnq_id >= CONFIG_LIMITED_AP_NUM) { + RTW_ERR(FUNC_ADPT_FMT"- mbid bcnq_id(%d) invalid\n", FUNC_ADPT_ARG(adapter), mbcnq_id); + rtw_warn_on(1); + } + + if (mbcnq_en) { + rtw_write8(adapter, REG_MBSSID_CTRL, + rtw_read8(adapter, REG_MBSSID_CTRL) | BIT(mbcnq_id)); + RTW_INFO(FUNC_ADPT_FMT"- mbid bcnq_id(%d) enabled\n", FUNC_ADPT_ARG(adapter), mbcnq_id); + } else { + rtw_write8(adapter, REG_MBSSID_CTRL, + rtw_read8(adapter, REG_MBSSID_CTRL) & (~BIT(mbcnq_id))); + RTW_INFO(FUNC_ADPT_FMT"- mbid bcnq_id(%d) disabled\n", FUNC_ADPT_ARG(adapter), mbcnq_id); + } +} +/*#define CONFIG_FW_TBTT_RPT*/ +void rtw_ap_mbid_bcn_en(_adapter *adapter, u8 ap_id) +{ + RTW_INFO(FUNC_ADPT_FMT"- ap_id(%d)\n", FUNC_ADPT_ARG(adapter), ap_id); + + #ifdef CONFIG_FW_TBTT_RPT + if (rtw_ap_get_nums(adapter) >= 1) { + u8 tbtt_rpt_map = adapter_to_dvobj(adapter)->vap_tbtt_rpt_map; + + rtw_hal_set_fw_ap_bcn_offload_cmd(adapter, _TRUE, + tbtt_rpt_map | BIT(ap_id));/*H2C-0xBA*/ + } + #else + if (rtw_ap_get_nums(adapter) == 1) + rtw_hal_set_fw_ap_bcn_offload_cmd(adapter, _TRUE, 0);/*H2C-0xBA*/ + #endif + + rtw_hal_set_bcn_rsvdpage_loc_cmd(adapter);/*H2C-0x09*/ + + _rtw_mbid_bcn_cfg(adapter, _TRUE, ap_id); +} +void rtw_ap_mbid_bcn_dis(_adapter *adapter, u8 ap_id) +{ + RTW_INFO(FUNC_ADPT_FMT"- ap_id(%d)\n", FUNC_ADPT_ARG(adapter), ap_id); + _rtw_mbid_bcn_cfg(adapter, _FALSE, ap_id); + + if (rtw_ap_get_nums(adapter) == 0) + rtw_hal_set_fw_ap_bcn_offload_cmd(adapter, _FALSE, 0); + #ifdef CONFIG_FW_TBTT_RPT + else if (rtw_ap_get_nums(adapter) >= 1) { + u8 tbtt_rpt_map = adapter_to_dvobj(adapter)->vap_tbtt_rpt_map; + + rtw_hal_set_fw_ap_bcn_offload_cmd(adapter, _TRUE, + tbtt_rpt_map & ~BIT(ap_id));/*H2C-0xBA*/ + } + #endif +} +#endif +#ifdef CONFIG_SWTIMER_BASED_TXBCN +void rtw_ap_multi_bcn_cfg(_adapter *adapter) +{ + #if defined(CONFIG_RTL8822B) || defined(CONFIG_RTL8821C) || defined(CONFIG_RTL8822C) + rtw_write8(adapter, REG_BCN_CTRL, DIS_TSF_UDT); + #else + rtw_write8(adapter, REG_BCN_CTRL, DIS_TSF_UDT | DIS_BCNQ_SUB); + #endif + /*enable to rx data frame*/ + rtw_write16(adapter, REG_RXFLTMAP2, 0xFFFF); + + /*Beacon Control related register for first time*/ + rtw_write8(adapter, REG_BCNDMATIM, 0x02); /* 2ms */ + + /*rtw_write8(Adapter, REG_BCN_MAX_ERR, 0xFF);*/ + rtw_write8(adapter, REG_ATIMWND, 0x0c); /* 12ms */ + + #ifndef CONFIG_HW_P0_TSF_SYNC + #ifdef CONFIG_RTL8192F + rtw_write16(adapter, REG_TSFTR_SYN_OFFSET, 0x640);/*unit:32us*/ + #else/*not CONFIG_RTL8192F*/ + rtw_write16(adapter, REG_TSFTR_SYN_OFFSET, 0x7fff);/* +32767 (~32ms) */ + #endif + #endif + + /*reset TSF*/ + rtw_write8(adapter, REG_DUAL_TSF_RST, BIT(0)); + + /*enable BCN0 Function for if1*/ + /*don't enable update TSF0 for if1 (due to TSF update when beacon,probe rsp are received)*/ + #if defined(CONFIG_RTL8822B) || defined(CONFIG_RTL8821C) || defined(CONFIG_RTL8822C) + rtw_write8(adapter, REG_BCN_CTRL, BIT_DIS_RX_BSSID_FIT | BIT_P0_EN_TXBCN_RPT | BIT_DIS_TSF_UDT |BIT_EN_BCN_FUNCTION); + #else + rtw_write8(adapter, REG_BCN_CTRL, (DIS_TSF_UDT | EN_BCN_FUNCTION | EN_TXBCN_RPT | DIS_BCNQ_SUB)); + #endif + #ifdef CONFIG_BCN_XMIT_PROTECT + rtw_write8(adapter, REG_CCK_CHECK, rtw_read8(adapter, REG_CCK_CHECK) | BIT_EN_BCN_PKT_REL); + #endif + + if (IS_HARDWARE_TYPE_8821(adapter) || IS_HARDWARE_TYPE_8192E(adapter))/* select BCN on port 0 for DualBeacon*/ + rtw_write8(adapter, REG_CCK_CHECK, rtw_read8(adapter, REG_CCK_CHECK) & (~BIT_BCN_PORT_SEL)); + + /* Enable HW seq for BCN + * 0x4FC[0]: EN_HWSEQ / 0x4FC[1]: EN_HWSEQEXT */ + #ifdef CONFIG_RTL8822B + if (IS_HARDWARE_TYPE_8822B(adapter)) + rtw_write8(adapter, REG_DUMMY_PAGE4_V1_8822B, 0x01); + #endif + + #ifdef CONFIG_RTL8822C + if (IS_HARDWARE_TYPE_8822C(adapter)) + rtw_write8(adapter, REG_DUMMY_PAGE4_V1_8822C, 0x01); + #endif +} +#endif + +#ifdef CONFIG_MI_WITH_MBSSID_CAM +void rtw_hal_set_macaddr_mbid(_adapter *adapter, u8 *mac_addr) +{ + +#if 0 /*TODO - modify for more flexible*/ + u8 idx = 0; + + if ((check_fwstate(&adapter->mlmepriv, WIFI_STATION_STATE) == _TRUE) && + (DEV_STA_NUM(adapter_to_dvobj(adapter)) == 1)) { + for (idx = 0; idx < 6; idx++) + rtw_write8(GET_PRIMARY_ADAPTER(adapter), (REG_MACID + idx), val[idx]); + } else { + /*MBID entry_id = 0~7 ,0 for root AP, 1~7 for VAP*/ + u8 entry_id; + + if ((check_fwstate(&adapter->mlmepriv, WIFI_AP_STATE) == _TRUE) && + (DEV_AP_NUM(adapter_to_dvobj(adapter)) == 1)) { + entry_id = 0; + if (rtw_mbid_cam_assign(adapter, val, entry_id)) { + RTW_INFO(FUNC_ADPT_FMT" Root AP assigned success\n", FUNC_ADPT_ARG(adapter)); + write_mbssid_cam(adapter, entry_id, val); + } + } else { + entry_id = rtw_mbid_camid_alloc(adapter, val); + if (entry_id != INVALID_CAM_ID) + write_mbssid_cam(adapter, entry_id, val); + } + } +#else + { + /* + MBID entry_id = 0~7 ,for IFACE_ID0 ~ IFACE_IDx + */ + u8 entry_id = rtw_mbid_camid_alloc(adapter, mac_addr); + + + if (entry_id != INVALID_CAM_ID) { + write_mbssid_cam(adapter, entry_id, mac_addr); + RTW_INFO("%s "ADPT_FMT"- mbid(%d) mac_addr ="MAC_FMT"\n", __func__, + ADPT_ARG(adapter), entry_id, MAC_ARG(mac_addr)); + } + } +#endif +} + +void rtw_hal_change_macaddr_mbid(_adapter *adapter, u8 *mac_addr) +{ + u8 idx = 0; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + u8 entry_id; + + if (!mac_addr) { + rtw_warn_on(1); + return; + } + + + entry_id = rtw_mbid_cam_info_change(adapter, mac_addr); + + if (entry_id != INVALID_CAM_ID) + write_mbssid_cam(adapter, entry_id, mac_addr); +} + +#ifdef CONFIG_SWTIMER_BASED_TXBCN +u16 rtw_hal_bcn_interval_adjust(_adapter *adapter, u16 bcn_interval) +{ + if (adapter_to_dvobj(adapter)->inter_bcn_space != bcn_interval) + return adapter_to_dvobj(adapter)->inter_bcn_space; + else + return bcn_interval; +} +#endif/*CONFIG_SWTIMER_BASED_TXBCN*/ + +#else + +#ifndef RTW_HALMAC +static u32 _get_macaddr_reg(enum _hw_port hwport) +{ + u32 reg_macaddr = REG_MACID; + + #ifdef CONFIG_CONCURRENT_MODE + if (hwport == HW_PORT1) + reg_macaddr = REG_MACID1; + #if defined(CONFIG_RTL8814A) + else if (hwport == HW_PORT2) + reg_macaddr = REG_MACID2; + else if (hwport == HW_PORT3) + reg_macaddr = REG_MACID3; + else if (hwport == HW_PORT4) + reg_macaddr = REG_MACID4; + #endif /*CONFIG_RTL8814A*/ + #endif /*CONFIG_CONCURRENT_MODE*/ + + return reg_macaddr; +} +#endif /*!RTW_HALMAC*/ + +static void rtw_hal_set_macaddr_port(_adapter *adapter, u8 *mac_addr) +{ + enum _hw_port hwport; + + if (mac_addr == NULL) + return; + hwport = get_hw_port(adapter); + + RTW_INFO("%s "ADPT_FMT"- hw port(%d) mac_addr ="MAC_FMT"\n", __func__, + ADPT_ARG(adapter), hwport, MAC_ARG(mac_addr)); + +#ifdef RTW_HALMAC /*8822B ~ 8814B*/ + rtw_halmac_set_mac_address(adapter_to_dvobj(adapter), hwport, mac_addr); +#else /* !RTW_HALMAC */ + { + u8 idx = 0; + u32 reg_macaddr = _get_macaddr_reg(hwport); + + for (idx = 0; idx < ETH_ALEN; idx++) + rtw_write8(GET_PRIMARY_ADAPTER(adapter), (reg_macaddr + idx), mac_addr[idx]); + } +#endif /* !RTW_HALMAC */ +} + +static void rtw_hal_get_macaddr_port(_adapter *adapter, u8 *mac_addr) +{ + enum _hw_port hwport; + + if (mac_addr == NULL) + return; + hwport = get_hw_port(adapter); + + _rtw_memset(mac_addr, 0, ETH_ALEN); +#ifdef RTW_HALMAC /*8822B ~ 8814B*/ + rtw_halmac_get_mac_address(adapter_to_dvobj(adapter), hwport, mac_addr); +#else /* !RTW_HALMAC */ + { + u8 idx = 0; + u32 reg_macaddr = _get_macaddr_reg(hwport); + + for (idx = 0; idx < ETH_ALEN; idx++) + mac_addr[idx] = rtw_read8(GET_PRIMARY_ADAPTER(adapter), (reg_macaddr + idx)); + } +#endif /* !RTW_HALMAC */ + + RTW_DBG("%s "ADPT_FMT"- hw port(%d) mac_addr ="MAC_FMT"\n", __func__, + ADPT_ARG(adapter), hwport, MAC_ARG(mac_addr)); +} +#endif/*#ifdef CONFIG_MI_WITH_MBSSID_CAM*/ + +#ifndef RTW_HALMAC +static u32 _get_bssid_reg(enum _hw_port hw_port) +{ + u32 reg_bssid = REG_BSSID; + + #ifdef CONFIG_CONCURRENT_MODE + if (hw_port == HW_PORT1) + reg_bssid = REG_BSSID1; + #if defined(CONFIG_RTL8814A) + else if (hw_port == HW_PORT2) + reg_bssid = REG_BSSID2; + else if (hw_port == HW_PORT3) + reg_bssid = REG_BSSID3; + else if (hw_port == HW_PORT4) + reg_bssid = REG_BSSID4; + #endif /*CONFIG_RTL8814A*/ + #endif /*CONFIG_CONCURRENT_MODE*/ + + return reg_bssid; +} +#endif /*!RTW_HALMAC*/ +static void rtw_hal_set_bssid(_adapter *adapter, u8 *val) +{ + enum _hw_port hw_port = rtw_hal_get_port(adapter); +#ifdef RTW_HALMAC + + rtw_halmac_set_bssid(adapter_to_dvobj(adapter), hw_port, val); +#else /* !RTW_HALMAC */ + u8 idx = 0; + u32 reg_bssid = _get_bssid_reg(hw_port); + + for (idx = 0 ; idx < ETH_ALEN; idx++) + rtw_write8(adapter, (reg_bssid + idx), val[idx]); +#endif /* !RTW_HALMAC */ + + RTW_INFO("%s "ADPT_FMT"- hw port -%d BSSID: "MAC_FMT"\n", + __func__, ADPT_ARG(adapter), hw_port, MAC_ARG(val)); +} + +#ifndef CONFIG_MI_WITH_MBSSID_CAM +static void rtw_hal_set_tsf_update(_adapter *adapter, u8 en) +{ + u32 addr = 0; + u8 val8; + + rtw_hal_get_hwreg(adapter, HW_VAR_BCN_CTRL_ADDR, (u8 *)&addr); + if (addr) { + rtw_enter_protsel_port(adapter, get_hw_port(adapter)); + val8 = rtw_read8(adapter, addr); + if (en && (val8 & DIS_TSF_UDT)) { + rtw_write8(adapter, addr, val8 & ~DIS_TSF_UDT); + #ifdef DBG_TSF_UPDATE + RTW_INFO("port%u("ADPT_FMT") enable TSF update\n", adapter->hw_port, ADPT_ARG(adapter)); + #endif + } + if (!en && !(val8 & DIS_TSF_UDT)) { + rtw_write8(adapter, addr, val8 | DIS_TSF_UDT); + #ifdef DBG_TSF_UPDATE + RTW_INFO("port%u("ADPT_FMT") disable TSF update\n", adapter->hw_port, ADPT_ARG(adapter)); + #endif + } + rtw_leave_protsel_port(adapter); + } else { + RTW_WARN("unknown port%d("ADPT_FMT") %s TSF update\n" + , adapter->hw_port, ADPT_ARG(adapter), en ? "enable" : "disable"); + rtw_warn_on(1); + } +} +#endif /*CONFIG_MI_WITH_MBSSID_CAM*/ +static void rtw_hal_set_hw_update_tsf(PADAPTER padapter) +{ +#ifdef CONFIG_MI_WITH_MBSSID_CAM + +#else + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + + if (!pmlmeext->en_hw_update_tsf) + return; + + /* check RCR */ + if (!rtw_hal_rcr_check(padapter, RCR_CBSSID_BCN)) + return; + + if (pmlmeext->tsf_update_required) { + pmlmeext->tsf_update_pause_stime = 0; + rtw_hal_set_tsf_update(padapter, 1); + } + + pmlmeext->en_hw_update_tsf = 0; +#endif +} + +void rtw_iface_enable_tsf_update(_adapter *adapter) +{ + adapter->mlmeextpriv.tsf_update_pause_stime = 0; + adapter->mlmeextpriv.tsf_update_required = 1; +#ifdef CONFIG_MI_WITH_MBSSID_CAM + +#else + rtw_hal_set_tsf_update(adapter, 1); +#endif +} + +void rtw_iface_disable_tsf_update(_adapter *adapter) +{ + adapter->mlmeextpriv.tsf_update_required = 0; + adapter->mlmeextpriv.tsf_update_pause_stime = 0; + adapter->mlmeextpriv.en_hw_update_tsf = 0; +#ifdef CONFIG_MI_WITH_MBSSID_CAM + +#else + rtw_hal_set_tsf_update(adapter, 0); +#endif +} + +static void rtw_hal_tsf_update_pause(_adapter *adapter) +{ +#ifdef CONFIG_MI_WITH_MBSSID_CAM + +#else + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + _adapter *iface; + int i; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (!iface) + continue; + + rtw_hal_set_tsf_update(iface, 0); + if (iface->mlmeextpriv.tsf_update_required) { + iface->mlmeextpriv.tsf_update_pause_stime = rtw_get_current_time(); + if (!iface->mlmeextpriv.tsf_update_pause_stime) + iface->mlmeextpriv.tsf_update_pause_stime++; + } + iface->mlmeextpriv.en_hw_update_tsf = 0; + } +#endif +} + +static void rtw_hal_tsf_update_restore(_adapter *adapter) +{ +#ifdef CONFIG_MI_WITH_MBSSID_CAM + +#else + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + _adapter *iface; + int i; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (!iface) + continue; + + if (iface->mlmeextpriv.tsf_update_required) { + /* enable HW TSF update when recive beacon*/ + iface->mlmeextpriv.en_hw_update_tsf = 1; + #ifdef DBG_TSF_UPDATE + RTW_INFO("port%d("ADPT_FMT") enabling TSF update...\n" + , iface->hw_port, ADPT_ARG(iface)); + #endif + } + } +#endif +} + +void rtw_hal_periodic_tsf_update_chk(_adapter *adapter) +{ +#ifdef CONFIG_MI_WITH_MBSSID_CAM + +#else + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + _adapter *iface; + struct mlme_ext_priv *mlmeext; + int i; + u32 restore_ms = 0; + + if (dvobj->periodic_tsf_update_etime) { + if (rtw_time_after(rtw_get_current_time(), dvobj->periodic_tsf_update_etime)) { + /* end for restore status */ + dvobj->periodic_tsf_update_etime = 0; + rtw_hal_rcr_set_chk_bssid(adapter, MLME_ACTION_NONE); + } + return; + } + + if (dvobj->rf_ctl.offch_state != OFFCHS_NONE) + return; + + /* + * all required ifaces can switch to restore status together + * loop all pause iface to get largest restore time required + */ + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (!iface) + continue; + + mlmeext = &iface->mlmeextpriv; + + if (mlmeext->tsf_update_required + && mlmeext->tsf_update_pause_stime + && rtw_get_passing_time_ms(mlmeext->tsf_update_pause_stime) + > mlmeext->mlmext_info.bcn_interval * mlmeext->tsf_update_pause_factor + ) { + if (restore_ms < mlmeext->mlmext_info.bcn_interval * mlmeext->tsf_update_restore_factor) + restore_ms = mlmeext->mlmext_info.bcn_interval * mlmeext->tsf_update_restore_factor; + } + } + + if (!restore_ms) + return; + + dvobj->periodic_tsf_update_etime = rtw_get_current_time() + rtw_ms_to_systime(restore_ms); + if (!dvobj->periodic_tsf_update_etime) + dvobj->periodic_tsf_update_etime++; + + rtw_hal_rcr_set_chk_bssid(adapter, MLME_ACTION_NONE); + + /* set timer to end restore status */ + _set_timer(&dvobj->periodic_tsf_update_end_timer, restore_ms); +#endif +} + +void rtw_hal_periodic_tsf_update_end_timer_hdl(void *ctx) +{ + struct dvobj_priv *dvobj = (struct dvobj_priv *)ctx; + + if (dev_is_surprise_removed(dvobj) || dev_is_drv_stopped(dvobj)) + return; + + rtw_periodic_tsf_update_end_cmd(dvobj_get_primary_adapter(dvobj)); +} + +static inline u8 hw_var_rcr_config(_adapter *adapter, u32 rcr) +{ + int err; + +#ifdef CONFIG_CUSTOMER_ALIBABA_GENERAL + rcr = RCR_AAP | RCR_APM | RCR_AM | RCR_AB | RCR_APWRMGT | RCR_ADF | RCR_AMF | RCR_APP_PHYST_RXFF | RCR_APP_MIC | RCR_APP_ICV; +#endif + err = rtw_write32(adapter, REG_RCR, rcr); + if (err == _SUCCESS) + GET_HAL_DATA(adapter)->ReceiveConfig = rcr; + return err; +} + +static inline u8 hw_var_rcr_get(_adapter *adapter, u32 *rcr) +{ + u32 v32; + + v32 = rtw_read32(adapter, REG_RCR); + if (rcr) + *rcr = v32; + GET_HAL_DATA(adapter)->ReceiveConfig = v32; + return _SUCCESS; +} + +/* only check SW RCR variable */ +inline u8 rtw_hal_rcr_check(_adapter *adapter, u32 check_bit) +{ + PHAL_DATA_TYPE hal; + u32 rcr; + + hal = GET_HAL_DATA(adapter); + + rcr = hal->ReceiveConfig; + if ((rcr & check_bit) == check_bit) + return 1; + + return 0; +} + +inline u8 rtw_hal_rcr_add(_adapter *adapter, u32 add) +{ + PHAL_DATA_TYPE hal; + u32 rcr; + u8 ret = _SUCCESS; + + hal = GET_HAL_DATA(adapter); + + rtw_hal_get_hwreg(adapter, HW_VAR_RCR, (u8 *)&rcr); + rcr |= add; + if (rcr != hal->ReceiveConfig) + ret = rtw_hal_set_hwreg(adapter, HW_VAR_RCR, (u8 *)&rcr); + + return ret; +} + +inline u8 rtw_hal_rcr_clear(_adapter *adapter, u32 clear) +{ + PHAL_DATA_TYPE hal; + u32 rcr; + u8 ret = _SUCCESS; + + hal = GET_HAL_DATA(adapter); + + rtw_hal_get_hwreg(adapter, HW_VAR_RCR, (u8 *)&rcr); + rcr &= ~clear; + if (rcr != hal->ReceiveConfig) + ret = rtw_hal_set_hwreg(adapter, HW_VAR_RCR, (u8 *)&rcr); + + return ret; +} + +void rtw_hal_rcr_set_chk_bssid(_adapter *adapter, u8 self_action) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u32 rcr, rcr_new; + struct mi_state mstate, mstate_s; + + rtw_hal_get_hwreg(adapter, HW_VAR_RCR, (u8 *)&rcr); + rcr_new = rcr; + +#if defined(CONFIG_MI_WITH_MBSSID_CAM) && !defined(CONFIG_CLIENT_PORT_CFG) + rcr_new &= ~(RCR_CBSSID_BCN | RCR_CBSSID_DATA); +#else + rtw_mi_status_no_self(adapter, &mstate); + rtw_mi_status_no_others(adapter, &mstate_s); + + /* only adjust parameters interested */ + switch (self_action) { + case MLME_SCAN_ENTER: + mstate_s.scan_num = 1; + mstate_s.scan_enter_num = 1; + break; + case MLME_SCAN_DONE: + mstate_s.scan_enter_num = 0; + break; + case MLME_STA_CONNECTING: + mstate_s.lg_sta_num = 1; + mstate_s.ld_sta_num = 0; + break; + case MLME_STA_CONNECTED: + mstate_s.lg_sta_num = 0; + mstate_s.ld_sta_num = 1; + break; + case MLME_STA_DISCONNECTED: + mstate_s.lg_sta_num = 0; + mstate_s.ld_sta_num = 0; + break; +#ifdef CONFIG_TDLS + case MLME_TDLS_LINKED: + mstate_s.ld_tdls_num = 1; + break; + case MLME_TDLS_NOLINK: + mstate_s.ld_tdls_num = 0; + break; +#endif +#ifdef CONFIG_AP_MODE + case MLME_AP_STARTED: + mstate_s.ap_num = 1; + break; + case MLME_AP_STOPPED: + mstate_s.ap_num = 0; + mstate_s.ld_ap_num = 0; + break; +#endif +#ifdef CONFIG_RTW_MESH + case MLME_MESH_STARTED: + mstate_s.mesh_num = 1; + break; + case MLME_MESH_STOPPED: + mstate_s.mesh_num = 0; + mstate_s.ld_mesh_num = 0; + break; +#endif + case MLME_ACTION_NONE: + case MLME_ADHOC_STARTED: + /* caller without effect of decision */ + break; + default: + rtw_warn_on(1); + }; + + rtw_mi_status_merge(&mstate, &mstate_s); + + if (MSTATE_AP_NUM(&mstate) || MSTATE_MESH_NUM(&mstate) || MSTATE_TDLS_LD_NUM(&mstate) + #ifdef CONFIG_FIND_BEST_CHANNEL + || MSTATE_SCAN_ENTER_NUM(&mstate) + #endif + || hal_data->in_cta_test + ) + rcr_new &= ~RCR_CBSSID_DATA; + else + rcr_new |= RCR_CBSSID_DATA; + + if (MSTATE_SCAN_ENTER_NUM(&mstate) || hal_data->in_cta_test) + rcr_new &= ~RCR_CBSSID_BCN; + else if (MSTATE_STA_LG_NUM(&mstate) + || adapter_to_dvobj(adapter)->periodic_tsf_update_etime + ) + rcr_new |= RCR_CBSSID_BCN; + else if ((MSTATE_AP_NUM(&mstate) && adapter->registrypriv.wifi_spec) /* for 11n Logo 4.2.31/4.2.32 */ + || MSTATE_MESH_NUM(&mstate) + ) + rcr_new &= ~RCR_CBSSID_BCN; + else + rcr_new |= RCR_CBSSID_BCN; + + #ifdef CONFIG_CLIENT_PORT_CFG + if (get_clt_num(adapter) > MAX_CLIENT_PORT_NUM) + rcr_new &= ~RCR_CBSSID_BCN; + #endif +#endif /* CONFIG_MI_WITH_MBSSID_CAM */ + +#ifdef CONFIG_RTW_MULTI_AP + if (MSTATE_AP_NUM(&mstate) + && rtw_unassoc_sta_src_chk(adapter, UNASOC_STA_SRC_RX_NMY_UC) + ) { + rcr_new |= RCR_AAP; + } else + rcr_new &= ~RCR_AAP; +#endif + + if (rcr == rcr_new) + return; + + if (!hal_spec->rx_tsf_filter + && (rcr & RCR_CBSSID_BCN) && !(rcr_new & RCR_CBSSID_BCN)) + rtw_hal_tsf_update_pause(adapter); + + rtw_hal_set_hwreg(adapter, HW_VAR_RCR, (u8 *)&rcr_new); + + if (!hal_spec->rx_tsf_filter + && !(rcr & RCR_CBSSID_BCN) && (rcr_new & RCR_CBSSID_BCN) + && self_action != MLME_STA_CONNECTING) + rtw_hal_tsf_update_restore(adapter); +} + +void rtw_hal_rcr_set_chk_bssid_act_non(_adapter *adapter) +{ + rtw_hal_rcr_set_chk_bssid(adapter, MLME_ACTION_NONE); +} + +static void hw_var_set_rcr_am(_adapter *adapter, u8 enable) +{ + u32 rcr = RCR_AM; + + if (enable) + rtw_hal_rcr_add(adapter, rcr); + else + rtw_hal_rcr_clear(adapter, rcr); +} + +static void hw_var_set_bcn_interval(_adapter *adapter, u16 interval) +{ +#ifdef CONFIG_SWTIMER_BASED_TXBCN + interval = rtw_hal_bcn_interval_adjust(adapter, interval); +#endif + +#ifdef RTW_HALMAC + rtw_halmac_set_bcn_interval(adapter_to_dvobj(adapter), adapter->hw_port, interval); +#else + rtw_write16(adapter, REG_MBSSID_BCN_SPACE, interval); +#endif + +#ifdef CONFIG_INTERRUPT_BASED_TXBCN_EARLY_INT + { + struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + + if ((pmlmeinfo->state & 0x03) == WIFI_FW_AP_STATE) { + RTW_INFO("%s==> bcn_interval:%d, eraly_int:%d\n", __func__, interval, interval >> 1); + rtw_write8(adapter, REG_DRVERLYINT, interval >> 1); + } + } +#endif +} + +#if CONFIG_TX_AC_LIFETIME +const char *const _tx_aclt_conf_str[] = { + "DEFAULT", + "AP_M2U", + "MESH", + "INVALID", +}; + +void dump_tx_aclt_force_val(void *sel, struct dvobj_priv *dvobj) +{ +#define TX_ACLT_FORCE_MSG_LEN 64 + struct hal_spec_t *hal_spec = GET_HAL_SPEC(dvobj_get_primary_adapter(dvobj)); + struct tx_aclt_conf_t *conf = &dvobj->tx_aclt_force_val; + char buf[TX_ACLT_FORCE_MSG_LEN]; + int cnt = 0; + + RTW_PRINT_SEL(sel, "unit:%uus, maximum:%uus\n" + , hal_spec->tx_aclt_unit_factor * 32 + , 0xFFFF * hal_spec->tx_aclt_unit_factor * 32); + + RTW_PRINT_SEL(sel, "%-5s %-12s %-12s\n", "en", "vo_vi(us)", "be_bk(us)"); + RTW_PRINT_SEL(sel, " 0x%02x %12u %12u\n" + , conf->en + , conf->vo_vi * hal_spec->tx_aclt_unit_factor * 32 + , conf->be_bk * hal_spec->tx_aclt_unit_factor * 32 + ); + + cnt += snprintf(buf + cnt, TX_ACLT_FORCE_MSG_LEN - cnt - 1, "%5s", conf->en == 0xFF ? "AUTO" : "FORCE"); + if (cnt >= TX_ACLT_FORCE_MSG_LEN - 1) + goto exit; + + if (conf->vo_vi) + cnt += snprintf(buf + cnt, TX_ACLT_FORCE_MSG_LEN - cnt - 1, " FORCE:0x%04x", conf->vo_vi); + else + cnt += snprintf(buf + cnt, TX_ACLT_FORCE_MSG_LEN - cnt - 1, " AUTO"); + if (cnt >= TX_ACLT_FORCE_MSG_LEN - 1) + goto exit; + + + if (conf->be_bk) + cnt += snprintf(buf + cnt, TX_ACLT_FORCE_MSG_LEN - cnt - 1, " FORCE:0x%04x", conf->be_bk); + else + cnt += snprintf(buf + cnt, TX_ACLT_FORCE_MSG_LEN - cnt - 1, " AUTO"); + if (cnt >= TX_ACLT_FORCE_MSG_LEN - 1) + goto exit; + + RTW_PRINT_SEL(sel, "%s\n", buf); + +exit: + return; +} + +void rtw_hal_set_tx_aclt_force_val(_adapter *adapter, struct tx_aclt_conf_t *input, u8 arg_num) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct tx_aclt_conf_t *conf = &dvobj->tx_aclt_force_val; + + if (arg_num >= 1) { + if (input->en == 0xFF) + conf->en = input->en; + else + conf->en = input->en & 0xF; + } + if (arg_num >= 2) { + conf->vo_vi = input->vo_vi / (hal_spec->tx_aclt_unit_factor * 32); + if (conf->vo_vi > 0xFFFF) + conf->vo_vi = 0xFFFF; + } + if (arg_num >= 3) { + conf->be_bk = input->be_bk / (hal_spec->tx_aclt_unit_factor * 32); + if (conf->be_bk > 0xFFFF) + conf->be_bk = 0xFFFF; + } +} + +void dump_tx_aclt_confs(void *sel, struct dvobj_priv *dvobj) +{ +#define TX_ACLT_CONF_MSG_LEN 32 + struct hal_spec_t *hal_spec = GET_HAL_SPEC(dvobj_get_primary_adapter(dvobj)); + struct tx_aclt_conf_t *conf; + char buf[TX_ACLT_CONF_MSG_LEN]; + int cnt; + int i; + + RTW_PRINT_SEL(sel, "unit:%uus, maximum:%uus\n" + , hal_spec->tx_aclt_unit_factor * 32 + , 0xFFFF * hal_spec->tx_aclt_unit_factor * 32); + + RTW_PRINT_SEL(sel, "%-7s %-1s %-3s %-9s %-9s %-10s %-10s\n" + , "name", "#", "en", "vo_vi(us)", "be_bk(us)", "vo_vi(reg)", "be_bk(reg)"); + + for (i = 0; i < TX_ACLT_CONF_NUM; i++) { + conf = &dvobj->tx_aclt_confs[i]; + cnt = 0; + + if (conf->vo_vi) + cnt += snprintf(buf + cnt, TX_ACLT_CONF_MSG_LEN - cnt - 1, " 0x%04x", conf->vo_vi); + else + cnt += snprintf(buf + cnt, TX_ACLT_CONF_MSG_LEN - cnt - 1, " N/A"); + if (cnt >= TX_ACLT_CONF_MSG_LEN - 1) + continue; + + if (conf->be_bk) + cnt += snprintf(buf + cnt, TX_ACLT_CONF_MSG_LEN - cnt - 1, " 0x%04x", conf->be_bk); + else + cnt += snprintf(buf + cnt, TX_ACLT_CONF_MSG_LEN - cnt - 1, " N/A"); + if (cnt >= TX_ACLT_CONF_MSG_LEN - 1) + continue; + + RTW_PRINT_SEL(sel, "%7s %1u 0x%x %9u %9u%s\n" + , tx_aclt_conf_str(i), i + , conf->en + , conf->vo_vi * hal_spec->tx_aclt_unit_factor * 32 + , conf->be_bk * hal_spec->tx_aclt_unit_factor * 32 + , buf + ); + } +} + +void rtw_hal_set_tx_aclt_conf(_adapter *adapter, u8 conf_idx, struct tx_aclt_conf_t *input, u8 arg_num) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct tx_aclt_conf_t *conf; + + if (conf_idx >= TX_ACLT_CONF_NUM) + return; + + conf = &dvobj->tx_aclt_confs[conf_idx]; + + if (arg_num >= 1) { + if (input->en != 0xFF) + conf->en = input->en & 0xF; + } + if (arg_num >= 2) { + conf->vo_vi = input->vo_vi / (hal_spec->tx_aclt_unit_factor * 32); + if (conf->vo_vi > 0xFFFF) + conf->vo_vi = 0xFFFF; + } + if (arg_num >= 3) { + conf->be_bk = input->be_bk / (hal_spec->tx_aclt_unit_factor * 32); + if (conf->be_bk > 0xFFFF) + conf->be_bk = 0xFFFF; + } +} + +void rtw_hal_update_tx_aclt(_adapter *adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct macid_ctl_t *macid_ctl = adapter_to_macidctl(adapter); + u8 lt_en = 0, lt_en_ori; + u16 lt_vo_vi = 0xFFFF, lt_be_bk = 0xFFFF; + u32 lt, lt_ori; + struct tx_aclt_conf_t *conf; + int i; +#ifdef CONFIG_AP_MODE +#if CONFIG_RTW_AP_DATA_BMC_TO_UC + _adapter *iface; + u8 ap_m2u_num = 0; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (!iface) + continue; + + if (MLME_IS_AP(iface) + && ((iface->b2u_flags_ap_src & RTW_AP_B2U_IP_MCAST) + || (iface->b2u_flags_ap_fwd & RTW_AP_B2U_IP_MCAST)) + ) + ap_m2u_num++; + } +#endif +#endif /* CONFIG_AP_MODE */ + + lt_en_ori = rtw_read8(adapter, REG_LIFETIME_EN); + lt_ori = rtw_read32(adapter, REG_PKT_LIFE_TIME); + + for (i = 0; i < TX_ACLT_CONF_NUM; i++) { + if (!(dvobj->tx_aclt_flags & BIT(i))) + continue; + + conf = &dvobj->tx_aclt_confs[i]; + + if (i == TX_ACLT_CONF_DEFAULT) { + /* first and default status, assign directly */ + lt_en = conf->en; + if (conf->vo_vi) + lt_vo_vi = conf->vo_vi; + if (conf->be_bk) + lt_be_bk = conf->be_bk; + } + #ifdef CONFIG_AP_MODE + #if CONFIG_RTW_AP_DATA_BMC_TO_UC || defined(CONFIG_RTW_MESH) + else if (0 + #if CONFIG_RTW_AP_DATA_BMC_TO_UC + || (i == TX_ACLT_CONF_AP_M2U + && ap_m2u_num + && macid_ctl->op_num[H2C_MSR_ROLE_STA] /* having AP mode with STA connected */) + #endif + #ifdef CONFIG_RTW_MESH + || (i == TX_ACLT_CONF_MESH + && macid_ctl->op_num[H2C_MSR_ROLE_MESH] > 1 /* implies only 1 MESH mode supported */) + #endif + ) { + /* long term status, OR en and MIN lifetime */ + lt_en |= conf->en; + if (conf->vo_vi && lt_vo_vi > conf->vo_vi) + lt_vo_vi = conf->vo_vi; + if (conf->be_bk && lt_be_bk > conf->be_bk) + lt_be_bk = conf->be_bk; + } + #endif + #endif /* CONFIG_AP_MODE */ + } + + if (dvobj->tx_aclt_force_val.en != 0xFF) + lt_en = dvobj->tx_aclt_force_val.en; + if (dvobj->tx_aclt_force_val.vo_vi) + lt_vo_vi = dvobj->tx_aclt_force_val.vo_vi; + if (dvobj->tx_aclt_force_val.be_bk) + lt_be_bk = dvobj->tx_aclt_force_val.be_bk; + + lt_en = (lt_en_ori & 0xF0) | (lt_en & 0x0F); + lt = (lt_be_bk << 16) | lt_vo_vi; + + if (0) + RTW_INFO("lt_en:0x%x(0x%x), lt:0x%08x(0x%08x)\n", lt_en, lt_en_ori, lt, lt_ori); + + if (lt_en != lt_en_ori) + rtw_write8(adapter, REG_LIFETIME_EN, lt_en); + if (lt != lt_ori) + rtw_write32(adapter, REG_PKT_LIFE_TIME, lt); +} +#endif /* CONFIG_TX_AC_LIFETIME */ + +void hw_var_port_switch(_adapter *adapter) +{ +#ifdef CONFIG_CONCURRENT_MODE +#ifdef CONFIG_RUNTIME_PORT_SWITCH + /* + 0x102: MSR + 0x550: REG_BCN_CTRL + 0x551: REG_BCN_CTRL_1 + 0x55A: REG_ATIMWND + 0x560: REG_TSFTR + 0x568: REG_TSFTR1 + 0x570: REG_ATIMWND_1 + 0x610: REG_MACID + 0x618: REG_BSSID + 0x700: REG_MACID1 + 0x708: REG_BSSID1 + */ + + int i; + u8 msr; + u8 bcn_ctrl; + u8 bcn_ctrl_1; + u8 atimwnd[2]; + u8 atimwnd_1[2]; + u8 tsftr[8]; + u8 tsftr_1[8]; + u8 macid[6]; + u8 bssid[6]; + u8 macid_1[6]; + u8 bssid_1[6]; +#if defined(CONFIG_RTL8192F) + u16 wlan_act_mask_ctrl = 0; + u16 en_port_mask = EN_PORT_0_FUNCTION | EN_PORT_1_FUNCTION; +#endif + + u8 hw_port; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + _adapter *iface = NULL; + + msr = rtw_read8(adapter, MSR); + bcn_ctrl = rtw_read8(adapter, REG_BCN_CTRL); + bcn_ctrl_1 = rtw_read8(adapter, REG_BCN_CTRL_1); +#if defined(CONFIG_RTL8192F) + wlan_act_mask_ctrl = rtw_read16(adapter, REG_WLAN_ACT_MASK_CTRL_1); +#endif + + for (i = 0; i < 2; i++) + atimwnd[i] = rtw_read8(adapter, REG_ATIMWND + i); + for (i = 0; i < 2; i++) + atimwnd_1[i] = rtw_read8(adapter, REG_ATIMWND_1 + i); + + for (i = 0; i < 8; i++) + tsftr[i] = rtw_read8(adapter, REG_TSFTR + i); + for (i = 0; i < 8; i++) + tsftr_1[i] = rtw_read8(adapter, REG_TSFTR1 + i); + + for (i = 0; i < 6; i++) + macid[i] = rtw_read8(adapter, REG_MACID + i); + + for (i = 0; i < 6; i++) + bssid[i] = rtw_read8(adapter, REG_BSSID + i); + + for (i = 0; i < 6; i++) + macid_1[i] = rtw_read8(adapter, REG_MACID1 + i); + + for (i = 0; i < 6; i++) + bssid_1[i] = rtw_read8(adapter, REG_BSSID1 + i); + +#ifdef DBG_RUNTIME_PORT_SWITCH + RTW_INFO(FUNC_ADPT_FMT" before switch\n" + "msr:0x%02x\n" + "bcn_ctrl:0x%02x\n" + "bcn_ctrl_1:0x%02x\n" +#if defined(CONFIG_RTL8192F) + "wlan_act_mask_ctrl:0x%02x\n" +#endif + "atimwnd:0x%04x\n" + "atimwnd_1:0x%04x\n" + "tsftr:%llu\n" + "tsftr1:%llu\n" + "macid:"MAC_FMT"\n" + "bssid:"MAC_FMT"\n" + "macid_1:"MAC_FMT"\n" + "bssid_1:"MAC_FMT"\n" + , FUNC_ADPT_ARG(adapter) + , msr + , bcn_ctrl + , bcn_ctrl_1 +#if defined(CONFIG_RTL8192F) + , wlan_act_mask_ctrl +#endif + , *((u16 *)atimwnd) + , *((u16 *)atimwnd_1) + , *((u64 *)tsftr) + , *((u64 *)tsftr_1) + , MAC_ARG(macid) + , MAC_ARG(bssid) + , MAC_ARG(macid_1) + , MAC_ARG(bssid_1) + ); +#endif /* DBG_RUNTIME_PORT_SWITCH */ + + /* disable bcn function, disable update TSF */ + rtw_write8(adapter, REG_BCN_CTRL, (bcn_ctrl & (~EN_BCN_FUNCTION)) | DIS_TSF_UDT); + rtw_write8(adapter, REG_BCN_CTRL_1, (bcn_ctrl_1 & (~EN_BCN_FUNCTION)) | DIS_TSF_UDT); + +#if defined(CONFIG_RTL8192F) + rtw_write16(adapter, REG_WLAN_ACT_MASK_CTRL_1, wlan_act_mask_ctrl & ~en_port_mask); +#endif + + /* switch msr */ + msr = (msr & 0xf0) | ((msr & 0x03) << 2) | ((msr & 0x0c) >> 2); + rtw_write8(adapter, MSR, msr); + + /* write port0 */ + rtw_write8(adapter, REG_BCN_CTRL, bcn_ctrl_1 & ~EN_BCN_FUNCTION); + for (i = 0; i < 2; i++) + rtw_write8(adapter, REG_ATIMWND + i, atimwnd_1[i]); + for (i = 0; i < 8; i++) + rtw_write8(adapter, REG_TSFTR + i, tsftr_1[i]); + for (i = 0; i < 6; i++) + rtw_write8(adapter, REG_MACID + i, macid_1[i]); + for (i = 0; i < 6; i++) + rtw_write8(adapter, REG_BSSID + i, bssid_1[i]); + + /* write port1 */ + rtw_write8(adapter, REG_BCN_CTRL_1, bcn_ctrl & ~EN_BCN_FUNCTION); + for (i = 0; i < 2; i++) + rtw_write8(adapter, REG_ATIMWND_1 + i, atimwnd[i]); + for (i = 0; i < 8; i++) + rtw_write8(adapter, REG_TSFTR1 + i, tsftr[i]); + for (i = 0; i < 6; i++) + rtw_write8(adapter, REG_MACID1 + i, macid[i]); + for (i = 0; i < 6; i++) + rtw_write8(adapter, REG_BSSID1 + i, bssid[i]); + + /* write bcn ctl */ +#ifdef CONFIG_BT_COEXIST + /* always enable port0 beacon function for PSTDMA */ + if (IS_HARDWARE_TYPE_8723B(adapter) || IS_HARDWARE_TYPE_8703B(adapter) + || IS_HARDWARE_TYPE_8723D(adapter)) + bcn_ctrl_1 |= EN_BCN_FUNCTION; + /* always disable port1 beacon function for PSTDMA */ + if (IS_HARDWARE_TYPE_8723B(adapter) || IS_HARDWARE_TYPE_8703B(adapter)) + bcn_ctrl &= ~EN_BCN_FUNCTION; +#endif + rtw_write8(adapter, REG_BCN_CTRL, bcn_ctrl_1); + rtw_write8(adapter, REG_BCN_CTRL_1, bcn_ctrl); + +#if defined(CONFIG_RTL8192F) + /* if the setting of port0 and port1 are the same, it does not need to switch port setting*/ + if(((wlan_act_mask_ctrl & en_port_mask) != 0) && ((wlan_act_mask_ctrl & en_port_mask) + != (EN_PORT_0_FUNCTION | EN_PORT_1_FUNCTION))) + wlan_act_mask_ctrl ^= en_port_mask; + rtw_write16(adapter, REG_WLAN_ACT_MASK_CTRL_1, wlan_act_mask_ctrl); +#endif + + if (adapter->iface_id == IFACE_ID0) + iface = dvobj->padapters[IFACE_ID1]; + else if (adapter->iface_id == IFACE_ID1) + iface = dvobj->padapters[IFACE_ID0]; + + + if (adapter->hw_port == HW_PORT0) { + adapter->hw_port = HW_PORT1; + iface->hw_port = HW_PORT0; + RTW_PRINT("port switch - port0("ADPT_FMT"), port1("ADPT_FMT")\n", + ADPT_ARG(iface), ADPT_ARG(adapter)); + } else { + adapter->hw_port = HW_PORT0; + iface->hw_port = HW_PORT1; + RTW_PRINT("port switch - port0("ADPT_FMT"), port1("ADPT_FMT")\n", + ADPT_ARG(adapter), ADPT_ARG(iface)); + } + +#ifdef DBG_RUNTIME_PORT_SWITCH + msr = rtw_read8(adapter, MSR); + bcn_ctrl = rtw_read8(adapter, REG_BCN_CTRL); + bcn_ctrl_1 = rtw_read8(adapter, REG_BCN_CTRL_1); +#if defined(CONFIG_RTL8192F) + wlan_act_mask_ctrl = rtw_read16(adapter, REG_WLAN_ACT_MASK_CTRL_1); +#endif + + for (i = 0; i < 2; i++) + atimwnd[i] = rtw_read8(adapter, REG_ATIMWND + i); + for (i = 0; i < 2; i++) + atimwnd_1[i] = rtw_read8(adapter, REG_ATIMWND_1 + i); + + for (i = 0; i < 8; i++) + tsftr[i] = rtw_read8(adapter, REG_TSFTR + i); + for (i = 0; i < 8; i++) + tsftr_1[i] = rtw_read8(adapter, REG_TSFTR1 + i); + + for (i = 0; i < 6; i++) + macid[i] = rtw_read8(adapter, REG_MACID + i); + + for (i = 0; i < 6; i++) + bssid[i] = rtw_read8(adapter, REG_BSSID + i); + + for (i = 0; i < 6; i++) + macid_1[i] = rtw_read8(adapter, REG_MACID1 + i); + + for (i = 0; i < 6; i++) + bssid_1[i] = rtw_read8(adapter, REG_BSSID1 + i); + + RTW_INFO(FUNC_ADPT_FMT" after switch\n" + "msr:0x%02x\n" + "bcn_ctrl:0x%02x\n" + "bcn_ctrl_1:0x%02x\n" +#if defined(CONFIG_RTL8192F) + "wlan_act_mask_ctrl:0x%02x\n" +#endif + "atimwnd:%u\n" + "atimwnd_1:%u\n" + "tsftr:%llu\n" + "tsftr1:%llu\n" + "macid:"MAC_FMT"\n" + "bssid:"MAC_FMT"\n" + "macid_1:"MAC_FMT"\n" + "bssid_1:"MAC_FMT"\n" + , FUNC_ADPT_ARG(adapter) + , msr + , bcn_ctrl + , bcn_ctrl_1 +#if defined(CONFIG_RTL8192F) + , wlan_act_mask_ctrl +#endif + , *((u16 *)atimwnd) + , *((u16 *)atimwnd_1) + , *((u64 *)tsftr) + , *((u64 *)tsftr_1) + , MAC_ARG(macid) + , MAC_ARG(bssid) + , MAC_ARG(macid_1) + , MAC_ARG(bssid_1) + ); +#endif /* DBG_RUNTIME_PORT_SWITCH */ + +#endif /* CONFIG_RUNTIME_PORT_SWITCH */ +#endif /* CONFIG_CONCURRENT_MODE */ +} + +const char *const _h2c_msr_role_str[] = { + "RSVD", + "STA", + "AP", + "GC", + "GO", + "TDLS", + "ADHOC", + "MESH", + "INVALID", +}; + +#ifdef CONFIG_FW_MULTI_PORT_SUPPORT +s32 rtw_hal_set_default_port_id_cmd(_adapter *adapter, u8 mac_id) +{ + s32 ret = _SUCCESS; + u8 parm[H2C_DEFAULT_PORT_ID_LEN] = {0}; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + u8 port_id = rtw_hal_get_port(adapter); + + if ((dvobj->dft.port_id == port_id) && (dvobj->dft.mac_id == mac_id)) + return ret; + + SET_H2CCMD_DFTPID_PORT_ID(parm, port_id); + SET_H2CCMD_DFTPID_MAC_ID(parm, mac_id); + + RTW_DBG_DUMP("DFT port id parm:", parm, H2C_DEFAULT_PORT_ID_LEN); + RTW_INFO("%s ("ADPT_FMT") port_id :%d, mad_id:%d\n", + __func__, ADPT_ARG(adapter), port_id, mac_id); + + ret = rtw_hal_fill_h2c_cmd(adapter, H2C_DEFAULT_PORT_ID, H2C_DEFAULT_PORT_ID_LEN, parm); + dvobj->dft.port_id = port_id; + dvobj->dft.mac_id = mac_id; + + return ret; +} +s32 rtw_set_default_port_id(_adapter *adapter) +{ + s32 ret = _SUCCESS; + struct sta_info *psta; + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + + if (is_client_associated_to_ap(adapter)) { + psta = rtw_get_stainfo(&adapter->stapriv, get_bssid(pmlmepriv)); + if (psta) + ret = rtw_hal_set_default_port_id_cmd(adapter, psta->cmn.mac_id); + } else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == _TRUE) { + + } else { + } + + return ret; +} +s32 rtw_set_ps_rsvd_page(_adapter *adapter) +{ + s32 ret = _SUCCESS; + u16 media_status_rpt = RT_MEDIA_CONNECT; + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + + if (adapter->iface_id == pwrctl->fw_psmode_iface_id) + return ret; + + rtw_hal_set_hwreg(adapter, HW_VAR_H2C_FW_JOINBSSRPT, + (u8 *)&media_status_rpt); + + return ret; +} + +#if 0 +_adapter * _rtw_search_dp_iface(_adapter *adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + _adapter *iface; + _adapter *target_iface = NULL; + int i; + u8 sta_num = 0, tdls_num = 0, ap_num = 0, mesh_num = 0, adhoc_num = 0; + u8 p2p_go_num = 0, p2p_gc_num = 0; + _adapter *sta_ifs[8]; + _adapter *ap_ifs[8]; + _adapter *mesh_ifs[8]; + _adapter *gc_ifs[8]; + _adapter *go_ifs[8]; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + + if (check_fwstate(&iface->mlmepriv, WIFI_STATION_STATE) == _TRUE) { + if (check_fwstate(&iface->mlmepriv, WIFI_ASOC_STATE) == _TRUE) { + sta_ifs[sta_num++] = iface; + + #ifdef CONFIG_TDLS + if (iface->tdlsinfo.link_established == _TRUE) + tdls_num++; + #endif + #ifdef CONFIG_P2P + if (MLME_IS_GC(iface)) + gc_ifs[p2p_gc_num++] = iface; + #endif + } +#ifdef CONFIG_AP_MODE + } else if (check_fwstate(&iface->mlmepriv, WIFI_AP_STATE) == _TRUE ) { + if (check_fwstate(&iface->mlmepriv, WIFI_ASOC_STATE) == _TRUE) { + ap_ifs[ap_num++] = iface; + #ifdef CONFIG_P2P + if (MLME_IS_GO(iface)) + go_ifs[p2p_go_num++] = iface; + #endif + } +#endif + } else if (check_fwstate(&iface->mlmepriv, WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE) == _TRUE + && check_fwstate(&iface->mlmepriv, WIFI_ASOC_STATE) == _TRUE + ) { + adhoc_num++; + +#ifdef CONFIG_RTW_MESH + } else if (check_fwstate(&iface->mlmepriv, WIFI_MESH_STATE) == _TRUE + && check_fwstate(&iface->mlmepriv, WIFI_ASOC_STATE) == _TRUE + ) { + mesh_ifs[mesh_num++] = iface; +#endif + } + } + + if (p2p_gc_num) { + target_iface = gc_ifs[0]; + } + else if (sta_num) { + if(sta_num == 1) { + target_iface = sta_ifs[0]; + } else if (sta_num >= 2) { + /*TODO get target_iface by timestamp*/ + target_iface = sta_ifs[0]; + } + } else if (ap_num) { + target_iface = ap_ifs[0]; + } + + RTW_INFO("[IFS_ASSOC_STATUS] - STA :%d", sta_num); + RTW_INFO("[IFS_ASSOC_STATUS] - TDLS :%d", tdls_num); + RTW_INFO("[IFS_ASSOC_STATUS] - AP:%d", ap_num); + RTW_INFO("[IFS_ASSOC_STATUS] - MESH :%d", mesh_num); + RTW_INFO("[IFS_ASSOC_STATUS] - ADHOC :%d", adhoc_num); + RTW_INFO("[IFS_ASSOC_STATUS] - P2P-GC :%d", p2p_gc_num); + RTW_INFO("[IFS_ASSOC_STATUS] - P2P-GO :%d", p2p_go_num); + + if (target_iface) + RTW_INFO("%s => target_iface ("ADPT_FMT")\n", + __func__, ADPT_ARG(target_iface)); + else + RTW_INFO("%s => target_iface NULL\n", __func__); + + return target_iface; +} + +void rtw_search_default_port(_adapter *adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + _adapter *adp_iface = NULL; +#ifdef CONFIG_WOWLAN + struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj); + + if (pwrpriv->wowlan_mode == _TRUE) { + adp_iface = adapter; + goto exit; + } +#endif + adp_iface = _rtw_search_dp_iface(adapter); + +exit : + if ((adp_iface != NULL) && (MLME_IS_STA(adp_iface))) + rtw_set_default_port_id(adp_iface); + else + rtw_hal_set_default_port_id_cmd(adapter, 0); + + if (1) { + _adapter *tmp_adp; + + tmp_adp = (adp_iface) ? adp_iface : adapter; + + RTW_INFO("%s ("ADPT_FMT")=> hw_port :%d, default_port(%d)\n", + __func__, ADPT_ARG(adapter), get_hw_port(tmp_adp), get_dft_portid(tmp_adp)); + } +} +#endif +#endif /*CONFIG_FW_MULTI_PORT_SUPPORT*/ + +#ifdef CONFIG_P2P_PS +#ifdef RTW_HALMAC +void rtw_set_p2p_ps_offload_cmd(_adapter *adapter, u8 p2p_ps_state) +{ + PHAL_DATA_TYPE hal = GET_HAL_DATA(adapter); + struct wifidirect_info *pwdinfo = &adapter->wdinfo; + struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + WLAN_BSSID_EX *cur_network = &(pmlmeinfo->network); + struct sta_priv *pstapriv = &adapter->stapriv; + struct sta_info *psta; + HAL_P2P_PS_PARA p2p_ps_para; + int status = -1; + u8 i; + u8 hw_port = rtw_hal_get_port(adapter); + + _rtw_memset(&p2p_ps_para, 0, sizeof(HAL_P2P_PS_PARA)); + _rtw_memcpy((&p2p_ps_para) , &hal->p2p_ps_offload , sizeof(hal->p2p_ps_offload)); + + (&p2p_ps_para)->p2p_port_id = hw_port; + (&p2p_ps_para)->p2p_group = 0; + psta = rtw_get_stainfo(pstapriv, cur_network->MacAddress); + if (psta) { + (&p2p_ps_para)->p2p_macid = psta->cmn.mac_id; + } else { + if (p2p_ps_state != P2P_PS_DISABLE) { + RTW_ERR("%s , psta was NULL\n", __func__); + return; + } + } + + + switch (p2p_ps_state) { + case P2P_PS_DISABLE: + RTW_INFO("P2P_PS_DISABLE\n"); + _rtw_memset(&p2p_ps_para , 0, sizeof(HAL_P2P_PS_PARA)); + break; + + case P2P_PS_ENABLE: + RTW_INFO("P2P_PS_ENABLE\n"); + /* update CTWindow value. */ + if (pwdinfo->ctwindow > 0) { + (&p2p_ps_para)->ctwindow_en = 1; + (&p2p_ps_para)->ctwindow_length = pwdinfo->ctwindow; + /*RTW_INFO("%s , ctwindow_length = %d\n" , __func__ , (&p2p_ps_para)->ctwindow_length);*/ + } + + + if ((pwdinfo->opp_ps == 1) || (pwdinfo->noa_num > 0)) { + (&p2p_ps_para)->offload_en = 1; + if (pwdinfo->role == P2P_ROLE_GO) { + (&p2p_ps_para)->role = 1; + (&p2p_ps_para)->all_sta_sleep = 0; + } else + (&p2p_ps_para)->role = 0; + + (&p2p_ps_para)->discovery = 0; + } + /* hw only support 2 set of NoA */ + for (i = 0; i < pwdinfo->noa_num; i++) { + /* To control the register setting for which NOA */ + (&p2p_ps_para)->noa_sel = i; + (&p2p_ps_para)->noa_en = 1; + (&p2p_ps_para)->disable_close_rf = 0; +#ifdef CONFIG_P2P_PS_NOA_USE_MACID_SLEEP +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_buddy_check_fwstate(adapter, WIFI_ASOC_STATE)) +#endif /* CONFIG_CONCURRENT_MODE */ + (&p2p_ps_para)->disable_close_rf = 1; +#endif /* CONFIG_P2P_PS_NOA_USE_MACID_SLEEP */ + /* config P2P NoA Descriptor Register */ + /* config NOA duration */ + (&p2p_ps_para)->noa_duration_para = pwdinfo->noa_duration[i]; + /* config NOA interval */ + (&p2p_ps_para)->noa_interval_para = pwdinfo->noa_interval[i]; + /* config NOA start time */ + (&p2p_ps_para)->noa_start_time_para = pwdinfo->noa_start_time[i]; + /* config NOA count */ + (&p2p_ps_para)->noa_count_para = pwdinfo->noa_count[i]; + /*RTW_INFO("%s , noa_duration_para = %d , noa_interval_para = %d , noa_start_time_para = %d , noa_count_para = %d\n" , __func__ , + (&p2p_ps_para)->noa_duration_para , (&p2p_ps_para)->noa_interval_para , + (&p2p_ps_para)->noa_start_time_para , (&p2p_ps_para)->noa_count_para);*/ + status = rtw_halmac_p2pps(adapter_to_dvobj(adapter) , (&p2p_ps_para)); + if (status == -1) + RTW_ERR("%s , rtw_halmac_p2pps fail\n", __func__); + } + + break; + + case P2P_PS_SCAN: + /*This feature FW not ready 20161116 YiWei*/ + return; + /* + RTW_INFO("P2P_PS_SCAN\n"); + (&p2p_ps_para)->discovery = 1; + (&p2p_ps_para)->ctwindow_length = pwdinfo->ctwindow; + (&p2p_ps_para)->noa_duration_para = pwdinfo->noa_duration[0]; + (&p2p_ps_para)->noa_interval_para = pwdinfo->noa_interval[0]; + (&p2p_ps_para)->noa_start_time_para = pwdinfo->noa_start_time[0]; + (&p2p_ps_para)->noa_count_para = pwdinfo->noa_count[0]; + */ + break; + + case P2P_PS_SCAN_DONE: + /*This feature FW not ready 20161116 YiWei*/ + return; + /* + RTW_INFO("P2P_PS_SCAN_DONE\n"); + (&p2p_ps_para)->discovery = 0; + pwdinfo->p2p_ps_state = P2P_PS_ENABLE; + (&p2p_ps_para)->ctwindow_length = pwdinfo->ctwindow; + (&p2p_ps_para)->noa_duration_para = pwdinfo->noa_duration[0]; + (&p2p_ps_para)->noa_interval_para = pwdinfo->noa_interval[0]; + (&p2p_ps_para)->noa_start_time_para = pwdinfo->noa_start_time[0]; + (&p2p_ps_para)->noa_count_para = pwdinfo->noa_count[0]; + */ + break; + + default: + break; + } + + if (p2p_ps_state != P2P_PS_ENABLE || (&p2p_ps_para)->noa_en == 0) { + status = rtw_halmac_p2pps(adapter_to_dvobj(adapter) , (&p2p_ps_para)); + if (status == -1) + RTW_ERR("%s , rtw_halmac_p2pps fail\n", __func__); + } + _rtw_memcpy(&hal->p2p_ps_offload , (&p2p_ps_para) , sizeof(hal->p2p_ps_offload)); + +} +#endif /* RTW_HALMAC */ +#endif /* CONFIG_P2P */ + +#if defined(CONFIG_RTL8822C) && defined(CONFIG_SUPPORT_DYNAMIC_TXPWR) +static void _rtw_hal_dtp_macid_set( + _adapter *padapter, u8 opmode, u8 mac_id) +{ + struct macid_ctl_t *macid_ctl = &(padapter->dvobj->macid_ctl); + struct sta_info *psta; + u8 h2c_cmd[H2C_FW_CRC5_SEARCH_LEN] = {0}; + u8 mac_addr[ETH_ALEN] = {0}; + + if (opmode) { + psta = macid_ctl->sta[mac_id]; + if (psta) + _rtw_memcpy(mac_addr, psta->cmn.mac_addr, ETH_ALEN); + + if (rtw_check_invalid_mac_address(mac_addr, _FALSE)) + return; + } + /* else DON'T CARE H2C_FW_CRC5_SEARCH mac addr in disconnected case */ + + if (rtw_get_chip_type(padapter) == RTL8822C) { + SET_H2CCMD_FW_CRC5_SEARCH_EN(h2c_cmd, opmode); + SET_H2CCMD_FW_CRC5_SEARCH_MACID(h2c_cmd, mac_id); + SET_H2CCMD_FW_CRC5_SEARCH_MAC(&h2c_cmd[1], mac_addr); + if (rtw_hal_fill_h2c_cmd(padapter, H2C_FW_CRC5_SEARCH, + H2C_FW_CRC5_SEARCH_LEN, h2c_cmd) != _SUCCESS) + RTW_WARN("%s : set h2c - 0x%02x fail!\n", __func__, H2C_FW_CRC5_SEARCH); + } +} + +static void rtw_hal_dtp_macid_set(_adapter *padapter, u8 opmode, + bool macid_ind, u8 mac_id, u8 macid_end) +{ + int i; + + if (macid_ind == 0) { + _rtw_hal_dtp_macid_set(padapter, opmode, mac_id); + } else { + for (i = mac_id; i <= macid_end; i++) + _rtw_hal_dtp_macid_set(padapter, opmode, i); + } +} +#endif + +/* +* rtw_hal_set_FwMediaStatusRpt_cmd - +* +* @adapter: +* @opmode: 0:disconnect, 1:connect +* @miracast: 0:it's not in miracast scenario. 1:it's in miracast scenario +* @miracast_sink: 0:source. 1:sink +* @role: The role of this macid. 0:rsvd. 1:STA. 2:AP. 3:GC. 4:GO. 5:TDLS +* @macid: +* @macid_ind: 0:update Media Status to macid. 1:update Media Status from macid to macid_end +* @macid_end: +*/ +s32 rtw_hal_set_FwMediaStatusRpt_cmd(_adapter *adapter, bool opmode, bool miracast, bool miracast_sink, u8 role, u8 macid, bool macid_ind, u8 macid_end) +{ + struct macid_ctl_t *macid_ctl = &adapter->dvobj->macid_ctl; + u8 parm[H2C_MEDIA_STATUS_RPT_LEN] = {0}; + int i; + s32 ret; +#ifdef CONFIG_FW_MULTI_PORT_SUPPORT + u8 hw_port = rtw_hal_get_port(adapter); +#endif + u8 op_num_change_bmp = 0; + +#if defined(CONFIG_RTL8822C) && defined(CONFIG_SUPPORT_DYNAMIC_TXPWR) + rtw_hal_dtp_macid_set(adapter, opmode, macid_ind, macid, macid_end); +#endif + + SET_H2CCMD_MSRRPT_PARM_OPMODE(parm, opmode); + SET_H2CCMD_MSRRPT_PARM_MACID_IND(parm, macid_ind); + SET_H2CCMD_MSRRPT_PARM_MIRACAST(parm, miracast); + SET_H2CCMD_MSRRPT_PARM_MIRACAST_SINK(parm, miracast_sink); + SET_H2CCMD_MSRRPT_PARM_ROLE(parm, role); + SET_H2CCMD_MSRRPT_PARM_MACID(parm, macid); + SET_H2CCMD_MSRRPT_PARM_MACID_END(parm, macid_end); +#ifdef CONFIG_FW_MULTI_PORT_SUPPORT + SET_H2CCMD_MSRRPT_PARM_PORT_NUM(parm, hw_port); +#endif + RTW_DBG_DUMP("MediaStatusRpt parm:", parm, H2C_MEDIA_STATUS_RPT_LEN); + + ret = rtw_hal_fill_h2c_cmd(adapter, H2C_MEDIA_STATUS_RPT, H2C_MEDIA_STATUS_RPT_LEN, parm); + if (ret != _SUCCESS) + goto exit; + +#if defined(CONFIG_RTL8188E) + if (rtw_get_chip_type(adapter) == RTL8188E) { + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + + /* 8188E FW doesn't set macid no link, driver does it by self */ + if (opmode) + rtw_hal_set_hwreg(adapter, HW_VAR_MACID_LINK, &macid); + else + rtw_hal_set_hwreg(adapter, HW_VAR_MACID_NOLINK, &macid); + + /* for 8188E RA */ +#if (RATE_ADAPTIVE_SUPPORT == 1) + if (hal_data->fw_ractrl == _FALSE) { + u8 max_macid; + + max_macid = rtw_search_max_mac_id(adapter); + rtw_hal_set_hwreg(adapter, HW_VAR_TX_RPT_MAX_MACID, &max_macid); + } +#endif + } +#endif + +#if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A) + /* TODO: this should move to IOT issue area */ + if (rtw_get_chip_type(adapter) == RTL8812 + || rtw_get_chip_type(adapter) == RTL8821 + ) { + if (MLME_IS_STA(adapter)) + Hal_PatchwithJaguar_8812(adapter, opmode); + } +#endif + + SET_H2CCMD_MSRRPT_PARM_MACID_IND(parm, 0); + if (macid_ind == 0) + macid_end = macid; + + for (i = macid; macid <= macid_end; macid++) { + op_num_change_bmp |= rtw_macid_ctl_set_h2c_msr(macid_ctl, macid, parm[0]); + if (!opmode) { + rtw_macid_ctl_set_bw(macid_ctl, macid, CHANNEL_WIDTH_20); + rtw_macid_ctl_set_vht_en(macid_ctl, macid, 0); + rtw_macid_ctl_set_rate_bmp0(macid_ctl, macid, 0); + rtw_macid_ctl_set_rate_bmp1(macid_ctl, macid, 0); + } + } + +#if CONFIG_TX_AC_LIFETIME + if (op_num_change_bmp) + rtw_hal_update_tx_aclt(adapter); +#endif + + if (!opmode) + rtw_update_tx_rate_bmp(adapter_to_dvobj(adapter)); + +exit: + return ret; +} + +inline s32 rtw_hal_set_FwMediaStatusRpt_single_cmd(_adapter *adapter, bool opmode, bool miracast, bool miracast_sink, u8 role, u8 macid) +{ + return rtw_hal_set_FwMediaStatusRpt_cmd(adapter, opmode, miracast, miracast_sink, role, macid, 0, 0); +} + +inline s32 rtw_hal_set_FwMediaStatusRpt_range_cmd(_adapter *adapter, bool opmode, bool miracast, bool miracast_sink, u8 role, u8 macid, u8 macid_end) +{ + return rtw_hal_set_FwMediaStatusRpt_cmd(adapter, opmode, miracast, miracast_sink, role, macid, 1, macid_end); +} + +void rtw_hal_set_FwRsvdPage_cmd(PADAPTER padapter, PRSVDPAGE_LOC rsvdpageloc) +{ + u8 u1H2CRsvdPageParm[H2C_RSVDPAGE_LOC_LEN] = {0}; + u8 ret = 0; + + RTW_INFO("RsvdPageLoc: ProbeRsp=%d PsPoll=%d Null=%d QoSNull=%d BTNull=%d\n", + rsvdpageloc->LocProbeRsp, rsvdpageloc->LocPsPoll, + rsvdpageloc->LocNullData, rsvdpageloc->LocQosNull, + rsvdpageloc->LocBTQosNull); + + SET_H2CCMD_RSVDPAGE_LOC_PROBE_RSP(u1H2CRsvdPageParm, rsvdpageloc->LocProbeRsp); + SET_H2CCMD_RSVDPAGE_LOC_PSPOLL(u1H2CRsvdPageParm, rsvdpageloc->LocPsPoll); + SET_H2CCMD_RSVDPAGE_LOC_NULL_DATA(u1H2CRsvdPageParm, rsvdpageloc->LocNullData); + SET_H2CCMD_RSVDPAGE_LOC_QOS_NULL_DATA(u1H2CRsvdPageParm, rsvdpageloc->LocQosNull); + SET_H2CCMD_RSVDPAGE_LOC_BT_QOS_NULL_DATA(u1H2CRsvdPageParm, rsvdpageloc->LocBTQosNull); + + ret = rtw_hal_fill_h2c_cmd(padapter, + H2C_RSVD_PAGE, + H2C_RSVDPAGE_LOC_LEN, + u1H2CRsvdPageParm); + +} + +#ifdef CONFIG_GPIO_WAKEUP +void rtw_hal_switch_gpio_wl_ctrl(_adapter *padapter, u8 index, u8 enable) +{ + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); + + if (IS_8723D_SERIES(pHalData->version_id) || IS_8192F_SERIES(pHalData->version_id) + || IS_8822B_SERIES(pHalData->version_id) || IS_8821C_SERIES(pHalData->version_id) + || IS_8822C_SERIES(pHalData->version_id)) + rtw_hal_set_hwreg(padapter, HW_SET_GPIO_WL_CTRL, (u8 *)(&enable)); + /* + * Switch GPIO_13, GPIO_14 to wlan control, or pull GPIO_13,14 MUST fail. + * It happended at 8723B/8192E/8821A. New IC will check multi function GPIO, + * and implement HAL function. + * TODO: GPIO_8 multi function? + */ + + if ((index == 13 || index == 14) + #if defined(CONFIG_RTL8821A) && defined(CONFIG_SDIO_HCI) + /* 8821A's LED2 circuit(used by HW_LED strategy) needs enable WL GPIO control of GPIO[14:13], can't disable */ + && (!IS_HW_LED_STRATEGY(rtw_led_get_strategy(padapter)) || enable) + #endif + ) + rtw_hal_set_hwreg(padapter, HW_SET_GPIO_WL_CTRL, (u8 *)(&enable)); +} + +void rtw_hal_set_output_gpio(_adapter *padapter, u8 index, u8 outputval) +{ +#if defined(CONFIG_RTL8192F) + rtw_hal_set_hwreg(padapter, HW_VAR_WOW_OUTPUT_GPIO, (u8 *)(&outputval)); +#else + if (index <= 7) { + /* config GPIO mode */ + rtw_write8(padapter, REG_GPIO_PIN_CTRL + 3, + rtw_read8(padapter, REG_GPIO_PIN_CTRL + 3) & ~BIT(index)); + + /* config GPIO Sel */ + /* 0: input */ + /* 1: output */ + rtw_write8(padapter, REG_GPIO_PIN_CTRL + 2, + rtw_read8(padapter, REG_GPIO_PIN_CTRL + 2) | BIT(index)); + + /* set output value */ + if (outputval) { + rtw_write8(padapter, REG_GPIO_PIN_CTRL + 1, + rtw_read8(padapter, REG_GPIO_PIN_CTRL + 1) | BIT(index)); + } else { + rtw_write8(padapter, REG_GPIO_PIN_CTRL + 1, + rtw_read8(padapter, REG_GPIO_PIN_CTRL + 1) & ~BIT(index)); + } + } else if (index <= 15) { + /* 88C Series: */ + /* index: 11~8 transform to 3~0 */ + /* 8723 Series: */ + /* index: 12~8 transform to 4~0 */ + + index -= 8; + + /* config GPIO mode */ + rtw_write8(padapter, REG_GPIO_PIN_CTRL_2 + 3, + rtw_read8(padapter, REG_GPIO_PIN_CTRL_2 + 3) & ~BIT(index)); + + /* config GPIO Sel */ + /* 0: input */ + /* 1: output */ + rtw_write8(padapter, REG_GPIO_PIN_CTRL_2 + 2, + rtw_read8(padapter, REG_GPIO_PIN_CTRL_2 + 2) | BIT(index)); + + /* set output value */ + if (outputval) { + rtw_write8(padapter, REG_GPIO_PIN_CTRL_2 + 1, + rtw_read8(padapter, REG_GPIO_PIN_CTRL_2 + 1) | BIT(index)); + } else { + rtw_write8(padapter, REG_GPIO_PIN_CTRL_2 + 1, + rtw_read8(padapter, REG_GPIO_PIN_CTRL_2 + 1) & ~BIT(index)); + } + } else { + RTW_INFO("%s: invalid GPIO%d=%d\n", + __FUNCTION__, index, outputval); + } +#endif +} +void rtw_hal_set_input_gpio(_adapter *padapter, u8 index) +{ +#if defined(CONFIG_RTL8192F) + rtw_hal_set_hwreg(padapter, HW_VAR_WOW_INPUT_GPIO, (u8 *)(&index)); +#else + if (index <= 7) { + /* config GPIO mode */ + rtw_write8(padapter, REG_GPIO_PIN_CTRL + 3, + rtw_read8(padapter, REG_GPIO_PIN_CTRL + 3) & ~BIT(index)); + + /* config GPIO Sel */ + /* 0: input */ + /* 1: output */ + rtw_write8(padapter, REG_GPIO_PIN_CTRL + 2, + rtw_read8(padapter, REG_GPIO_PIN_CTRL + 2) & ~BIT(index)); + + } else if (index <= 15) { + /* 88C Series: */ + /* index: 11~8 transform to 3~0 */ + /* 8723 Series: */ + /* index: 12~8 transform to 4~0 */ + + index -= 8; + + /* config GPIO mode */ + rtw_write8(padapter, REG_GPIO_PIN_CTRL_2 + 3, + rtw_read8(padapter, REG_GPIO_PIN_CTRL_2 + 3) & ~BIT(index)); + + /* config GPIO Sel */ + /* 0: input */ + /* 1: output */ + rtw_write8(padapter, REG_GPIO_PIN_CTRL_2 + 2, + rtw_read8(padapter, REG_GPIO_PIN_CTRL_2 + 2) & ~BIT(index)); + } else + RTW_INFO("%s: invalid GPIO%d\n", __func__, index); +#endif +} + +#endif + +void rtw_hal_set_FwAoacRsvdPage_cmd(PADAPTER padapter, PRSVDPAGE_LOC rsvdpageloc) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + u8 ret = 0; +#ifdef CONFIG_WOWLAN + u8 u1H2CAoacRsvdPageParm[H2C_AOAC_RSVDPAGE_LOC_LEN] = {0}; + + RTW_INFO("%s: RWC: %d ArpRsp: %d NbrAdv: %d LocNDPInfo: %d\n", + __func__, rsvdpageloc->LocRemoteCtrlInfo, + rsvdpageloc->LocArpRsp, rsvdpageloc->LocNbrAdv, + rsvdpageloc->LocNDPInfo); + RTW_INFO("%s:GtkRsp: %d GtkInfo: %d ProbeReq: %d NetworkList: %d\n", + __func__, rsvdpageloc->LocGTKRsp, rsvdpageloc->LocGTKInfo, + rsvdpageloc->LocProbeReq, rsvdpageloc->LocNetList); + + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) { + SET_H2CCMD_AOAC_RSVDPAGE_LOC_REMOTE_WAKE_CTRL_INFO(u1H2CAoacRsvdPageParm, rsvdpageloc->LocRemoteCtrlInfo); + SET_H2CCMD_AOAC_RSVDPAGE_LOC_ARP_RSP(u1H2CAoacRsvdPageParm, rsvdpageloc->LocArpRsp); + SET_H2CCMD_AOAC_RSVDPAGE_LOC_NEIGHBOR_ADV(u1H2CAoacRsvdPageParm, + rsvdpageloc->LocNbrAdv); + SET_H2CCMD_AOAC_RSVDPAGE_LOC_NDP_INFO(u1H2CAoacRsvdPageParm, + rsvdpageloc->LocNDPInfo); +#ifdef CONFIG_GTK_OL + SET_H2CCMD_AOAC_RSVDPAGE_LOC_GTK_RSP(u1H2CAoacRsvdPageParm, rsvdpageloc->LocGTKRsp); + SET_H2CCMD_AOAC_RSVDPAGE_LOC_GTK_INFO(u1H2CAoacRsvdPageParm, rsvdpageloc->LocGTKInfo); + SET_H2CCMD_AOAC_RSVDPAGE_LOC_GTK_EXT_MEM(u1H2CAoacRsvdPageParm, rsvdpageloc->LocGTKEXTMEM); +#endif /* CONFIG_GTK_OL */ + ret = rtw_hal_fill_h2c_cmd(padapter, + H2C_AOAC_RSVD_PAGE, + H2C_AOAC_RSVDPAGE_LOC_LEN, + u1H2CAoacRsvdPageParm); + + RTW_INFO("AOAC Report=%d\n", rsvdpageloc->LocAOACReport); + _rtw_memset(&u1H2CAoacRsvdPageParm, 0, sizeof(u1H2CAoacRsvdPageParm)); + SET_H2CCMD_AOAC_RSVDPAGE_LOC_AOAC_REPORT(u1H2CAoacRsvdPageParm, + rsvdpageloc->LocAOACReport); + ret = rtw_hal_fill_h2c_cmd(padapter, + H2C_AOAC_RSVDPAGE3, + H2C_AOAC_RSVDPAGE_LOC_LEN, + u1H2CAoacRsvdPageParm); + pwrpriv->wowlan_aoac_rpt_loc = rsvdpageloc->LocAOACReport; + } +#ifdef CONFIG_PNO_SUPPORT + else { + + if (!pwrpriv->wowlan_in_resume) { + RTW_INFO("NLO_INFO=%d\n", rsvdpageloc->LocPNOInfo); + _rtw_memset(&u1H2CAoacRsvdPageParm, 0, + sizeof(u1H2CAoacRsvdPageParm)); + SET_H2CCMD_AOAC_RSVDPAGE_LOC_NLO_INFO(u1H2CAoacRsvdPageParm, + rsvdpageloc->LocPNOInfo); + ret = rtw_hal_fill_h2c_cmd(padapter, + H2C_AOAC_RSVDPAGE3, + H2C_AOAC_RSVDPAGE_LOC_LEN, + u1H2CAoacRsvdPageParm); + } + } +#endif /* CONFIG_PNO_SUPPORT */ +#endif /* CONFIG_WOWLAN */ +} + +#ifdef DBG_FW_DEBUG_MSG_PKT +void rtw_hal_set_fw_dbg_msg_pkt_rsvd_page_cmd(PADAPTER padapter, PRSVDPAGE_LOC rsvdpageloc) +{ + struct hal_ops *pHalFunc = &padapter->hal_func; + u8 u1H2C_fw_dbg_msg_pkt_parm[H2C_FW_DBG_MSG_PKT_LEN] = {0}; + u8 ret = 0; + + + RTW_INFO("RsvdPageLoc: loc_fw_dbg_msg_pkt =%d\n", rsvdpageloc->loc_fw_dbg_msg_pkt); + + SET_H2CCMD_FW_DBG_MSG_PKT_EN(u1H2C_fw_dbg_msg_pkt_parm, 1); + SET_H2CCMD_RSVDPAGE_LOC_FW_DBG_MSG_PKT(u1H2C_fw_dbg_msg_pkt_parm, rsvdpageloc->loc_fw_dbg_msg_pkt); + ret = rtw_hal_fill_h2c_cmd(padapter, + H2C_FW_DBG_MSG_PKT, + H2C_FW_DBG_MSG_PKT_LEN, + u1H2C_fw_dbg_msg_pkt_parm); + +} +#endif /*DBG_FW_DEBUG_MSG_PKT*/ + +/*#define DBG_GET_RSVD_PAGE*/ +int rtw_hal_get_rsvd_page(_adapter *adapter, u32 page_offset, + u32 page_num, u8 *buffer, u32 buffer_size) +{ + u32 addr = 0, size = 0, count = 0; + u32 page_size = 0, data_low = 0, data_high = 0; + u16 txbndy = 0, offset = 0; + u8 i = 0; + bool rst = _FALSE; + +#ifdef DBG_LA_MODE + struct registry_priv *registry_par = &adapter->registrypriv; + + if(registry_par->la_mode_en == 1) { + RTW_INFO("%s LA debug mode can't dump rsvd pg \n", __func__); + return rst; + } +#endif + rtw_hal_get_def_var(adapter, HAL_DEF_TX_PAGE_SIZE, &page_size); + + addr = page_offset * page_size; + size = page_num * page_size; + + if (buffer_size < size) { + RTW_ERR("%s buffer_size(%d) < get page total size(%d)\n", + __func__, buffer_size, size); + return rst; + } +#ifdef RTW_HALMAC + if (rtw_halmac_dump_fifo(adapter_to_dvobj(adapter), 2, addr, size, buffer) < 0) + rst = _FALSE; + else + rst = _TRUE; +#else + txbndy = rtw_read8(adapter, REG_TDECTRL + 1); + + offset = (txbndy + page_offset) * page_size / 8; + count = (buffer_size / 8) + 1; + + rtw_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, 0x69); + + for (i = 0 ; i < count ; i++) { + rtw_write32(adapter, REG_PKTBUF_DBG_CTRL, offset + i); + data_low = rtw_read32(adapter, REG_PKTBUF_DBG_DATA_L); + data_high = rtw_read32(adapter, REG_PKTBUF_DBG_DATA_H); + _rtw_memcpy(buffer + (i * 8), + &data_low, sizeof(data_low)); + _rtw_memcpy(buffer + ((i * 8) + 4), + &data_high, sizeof(data_high)); + } + rtw_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, 0x0); + rst = _TRUE; +#endif /*RTW_HALMAC*/ + +#ifdef DBG_GET_RSVD_PAGE + RTW_INFO("%s [page_offset:%d , page_num:%d][start_addr:0x%04x , size:%d]\n", + __func__, page_offset, page_num, addr, size); + RTW_INFO_DUMP("\n", buffer, size); + RTW_INFO(" ==================================================\n"); +#endif + return rst; +} + +void rtw_dump_rsvd_page(void *sel, _adapter *adapter, u8 page_offset, u8 page_num) +{ +#if defined(CONFIG_RTW_DEBUG) || defined(CONFIG_PROC_DEBUG) + u32 page_size = 0; + u8 *buffer = NULL; + u32 buf_size = 0; + + if (page_num == 0) + return; + + RTW_PRINT_SEL(sel, "======= RSVD PAGE DUMP =======\n"); + RTW_PRINT_SEL(sel, "page_offset:%d, page_num:%d\n", page_offset, page_num); + + rtw_hal_get_def_var(adapter, HAL_DEF_TX_PAGE_SIZE, &page_size); + if (page_size) { + buf_size = page_size * page_num; + buffer = rtw_zvmalloc(buf_size); + + if (buffer) { + rtw_hal_get_rsvd_page(adapter, page_offset, page_num, buffer, buf_size); + RTW_DUMP_SEL(sel, buffer, buf_size); + rtw_vmfree(buffer, buf_size); + } else + RTW_PRINT_SEL(sel, "ERROR - rsvd_buf mem allocate failed\n"); + } else + RTW_PRINT_SEL(sel, "ERROR - Tx page size is zero ??\n"); + + RTW_PRINT_SEL(sel, "==========================\n"); +#endif +} + +#ifdef CONFIG_SUPPORT_FIFO_DUMP +void rtw_dump_fifo(void *sel, _adapter *adapter, u8 fifo_sel, u32 fifo_addr, u32 fifo_size) +{ + u8 *buffer = NULL; + u32 buff_size = 0; + static const char * const fifo_sel_str[] = { + "TX", "RX", "RSVD_PAGE", "REPORT", "LLT", "RXBUF_FW" + }; + + if (fifo_sel > 5) { + RTW_ERR("fifo_sel:%d invalid\n", fifo_sel); + return; + } + + RTW_PRINT_SEL(sel, "========= FIFO DUMP =========\n"); + RTW_PRINT_SEL(sel, "%s FIFO DUMP [start_addr:0x%04x , size:%d]\n", fifo_sel_str[fifo_sel], fifo_addr, fifo_size); + + if (fifo_size) { + buff_size = RND4(fifo_size); + buffer = rtw_zvmalloc(buff_size); + if (buffer == NULL) + buff_size = 0; + } + + rtw_halmac_dump_fifo(adapter_to_dvobj(adapter), fifo_sel, fifo_addr, buff_size, buffer); + + if (buffer) { + RTW_DUMP_SEL(sel, buffer, fifo_size); + rtw_vmfree(buffer, buff_size); + } + + RTW_PRINT_SEL(sel, "==========================\n"); +} +#endif + +#if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN) +static void rtw_hal_force_enable_rxdma(_adapter *adapter) +{ + RTW_INFO("%s: Set 0x690=0x00\n", __func__); + rtw_write8(adapter, REG_WOW_CTRL, + (rtw_read8(adapter, REG_WOW_CTRL) & 0xf0)); + RTW_PRINT("%s: Release RXDMA\n", __func__); + rtw_write32(adapter, REG_RXPKT_NUM, + (rtw_read32(adapter, REG_RXPKT_NUM) & (~RW_RELEASE_EN))); +} +#if defined(CONFIG_RTL8188E) +static void rtw_hal_disable_tx_report(_adapter *adapter) +{ + rtw_write8(adapter, REG_TX_RPT_CTRL, + ((rtw_read8(adapter, REG_TX_RPT_CTRL) & ~BIT(1))) & ~BIT(5)); + RTW_INFO("disable TXRPT:0x%02x\n", rtw_read8(adapter, REG_TX_RPT_CTRL)); +} + +static void rtw_hal_enable_tx_report(_adapter *adapter) +{ + rtw_write8(adapter, REG_TX_RPT_CTRL, + ((rtw_read8(adapter, REG_TX_RPT_CTRL) | BIT(1))) | BIT(5)); + RTW_INFO("enable TX_RPT:0x%02x\n", rtw_read8(adapter, REG_TX_RPT_CTRL)); +} +#endif +static void rtw_hal_release_rx_dma(_adapter *adapter) +{ + u32 val32 = 0; + + val32 = rtw_read32(adapter, REG_RXPKT_NUM); + + rtw_write32(adapter, REG_RXPKT_NUM, (val32 & (~RW_RELEASE_EN))); + + RTW_INFO("%s, [0x%04x]: 0x%08x\n", + __func__, REG_RXPKT_NUM, (u32)(val32 & (~RW_RELEASE_EN))); +} + +static u8 rtw_hal_pause_rx_dma(_adapter *adapter) +{ + PHAL_DATA_TYPE hal = GET_HAL_DATA(adapter); + u8 ret = 0; + s8 trycnt = 100; + u32 tmp = 0; + int res = 0; + /* RX DMA stop */ + RTW_PRINT("Pause DMA\n"); + rtw_write32(adapter, REG_RXPKT_NUM, + (rtw_read32(adapter, REG_RXPKT_NUM) | RW_RELEASE_EN)); + do { + if ((rtw_read32(adapter, REG_RXPKT_NUM) & RXDMA_IDLE)) { +#ifdef CONFIG_USB_HCI + /* stop interface before leave */ + if (_TRUE == hal->usb_intf_start) { + rtw_intf_stop(adapter); + RTW_ENABLE_FUNC(adapter, DF_RX_BIT); + RTW_ENABLE_FUNC(adapter, DF_TX_BIT); + } +#endif /* CONFIG_USB_HCI */ + + RTW_PRINT("RX_DMA_IDLE is true\n"); + ret = _SUCCESS; + break; + } +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) + else { + res = RecvOnePkt(adapter); + RTW_PRINT("RecvOnePkt Result: %d\n", res); + } +#endif /* CONFIG_SDIO_HCI || CONFIG_GSPI_HCI */ + +#ifdef CONFIG_USB_HCI + else { + /* to avoid interface start repeatedly */ + if (_FALSE == hal->usb_intf_start) + rtw_intf_start(adapter); + } +#endif /* CONFIG_USB_HCI */ + } while (trycnt--); + + if (trycnt < 0) { + tmp = rtw_read16(adapter, REG_RXPKT_NUM + 2); + + RTW_PRINT("Stop RX DMA failed......\n"); +#if defined(CONFIG_RTL8822C) || defined(CONFIG_RTL8814B) + RTW_PRINT("%s, RXPKT_NUM: 0x%04x\n", + __func__, rtw_read16(adapter, REG_RXPKTNUM)); +#else + RTW_PRINT("%s, RXPKT_NUM: 0x%02x\n", + __func__, ((tmp & 0xFF00) >> 8)); +#endif + if (tmp & BIT(3)) + RTW_PRINT("%s, RX DMA has req\n", + __func__); + else + RTW_PRINT("%s, RX DMA no req\n", + __func__); + ret = _FAIL; + } + + return ret; +} + +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) +#ifndef RTW_HALMAC +static u8 rtw_hal_enable_cpwm2(_adapter *adapter) +{ + u8 ret = 0; + int res = 0; + u32 tmp = 0; +#ifdef CONFIG_GPIO_WAKEUP + return _SUCCESS; +#else + RTW_PRINT("%s\n", __func__); + + res = sdio_local_read(adapter, SDIO_REG_HIMR, 4, (u8 *)&tmp); + if (!res) + RTW_INFO("read SDIO_REG_HIMR: 0x%08x\n", tmp); + else + RTW_INFO("sdio_local_read fail\n"); + + tmp = SDIO_HIMR_CPWM2_MSK; + + res = sdio_local_write(adapter, SDIO_REG_HIMR, 4, (u8 *)&tmp); + + if (!res) { + res = sdio_local_read(adapter, SDIO_REG_HIMR, 4, (u8 *)&tmp); + RTW_INFO("read again SDIO_REG_HIMR: 0x%08x\n", tmp); + ret = _SUCCESS; + } else { + RTW_INFO("sdio_local_write fail\n"); + ret = _FAIL; + } + return ret; +#endif /* CONFIG_CPIO_WAKEUP */ +} +#endif +#endif /* CONFIG_SDIO_HCI, CONFIG_GSPI_HCI */ +#endif /* CONFIG_WOWLAN || CONFIG_AP_WOWLAN */ + +#ifdef CONFIG_WOWLAN +/* + * rtw_hal_check_wow_ctrl + * chk_type: _TRUE means to check enable, if 0x690 & bit1 (for 8051), WOW enable successful. + * If 0x1C7 == 0 (for 3081), WOW enable successful. + * _FALSE means to check disable, if 0x690 & bit1 (for 8051), WOW disable fail. + * If 0x120 & bit16 || 0x284 & bit18 (for 3081), WOW disable fail. + */ +static u8 rtw_hal_check_wow_ctrl(_adapter *adapter, u8 chk_type) +{ + u32 fe1_imr = 0xFF, rxpkt_num = 0xFF; + u8 mstatus = 0; + u8 reason = 0xFF; + u8 trycnt = 25; + u8 res = _FALSE; + + if (IS_HARDWARE_TYPE_JAGUAR2(adapter) || IS_HARDWARE_TYPE_JAGUAR3(adapter)) { + if (chk_type) { + reason = rtw_read8(adapter, REG_WOWLAN_WAKE_REASON); + RTW_INFO("%s reason:0x%02x\n", __func__, reason); + + while (reason && trycnt > 1) { + reason = rtw_read8(adapter, REG_WOWLAN_WAKE_REASON); + RTW_PRINT("Loop index: %d :0x%02x\n", + trycnt, reason); + trycnt--; + rtw_msleep_os(20); + } + if (!reason) + res = _TRUE; + else + res = _FALSE; + } else { + /* Wait FW to cleare 0x120 bit16, 0x284 bit18 to 0 */ + fe1_imr = rtw_read32(adapter, REG_FE1IMR); /* RxDone IMR for 3081 */ + rxpkt_num = rtw_read32(adapter, REG_RXPKT_NUM); /* Release RXDMA */ + RTW_PRINT("%s REG_FE1IMR (reg120): 0x%x, REG_RXPKT_NUM(reg284): 0x%x\n", __func__, fe1_imr, rxpkt_num); + + while (((fe1_imr & BIT_FS_RXDONE_INT_EN) || (rxpkt_num & BIT_RW_RELEASE_EN)) && trycnt > 1) { + rtw_msleep_os(20); + fe1_imr = rtw_read32(adapter, REG_FE1IMR); + rxpkt_num = rtw_read32(adapter, REG_RXPKT_NUM); + RTW_PRINT("Loop index: %d :0x%x, 0x%x\n", + trycnt, fe1_imr, rxpkt_num); + trycnt--; + } + + if ((fe1_imr & BIT_FS_RXDONE_INT_EN) || (rxpkt_num & BIT_RW_RELEASE_EN)) + res = _FALSE; + else + res = _TRUE; + } + } else { + mstatus = rtw_read8(adapter, REG_WOW_CTRL); + RTW_INFO("%s mstatus:0x%02x\n", __func__, mstatus); + + + if (chk_type) { + while (!(mstatus & BIT1) && trycnt > 1) { + mstatus = rtw_read8(adapter, REG_WOW_CTRL); + RTW_PRINT("Loop index: %d :0x%02x\n", + trycnt, mstatus); + trycnt--; + rtw_msleep_os(20); + } + if (mstatus & BIT1) + res = _TRUE; + else + res = _FALSE; + } else { + while (mstatus & BIT1 && trycnt > 1) { + mstatus = rtw_read8(adapter, REG_WOW_CTRL); + RTW_PRINT("Loop index: %d :0x%02x\n", + trycnt, mstatus); + trycnt--; + rtw_msleep_os(20); + } + + if (mstatus & BIT1) + res = _FALSE; + else + res = _TRUE; + } + } + + RTW_PRINT("%s check_type: %d res: %d trycnt: %d\n", + __func__, chk_type, res, (25 - trycnt)); + return res; +} + +#ifdef CONFIG_PNO_SUPPORT +static u8 rtw_hal_check_pno_enabled(_adapter *adapter) +{ + struct pwrctrl_priv *ppwrpriv = adapter_to_pwrctl(adapter); + u8 res = 0, count = 0; + u8 ret = _FALSE; + + if (ppwrpriv->wowlan_pno_enable && ppwrpriv->wowlan_in_resume == _FALSE) { + res = rtw_read8(adapter, REG_PNO_STATUS); + while (!(res & BIT(7)) && count < 25) { + RTW_INFO("[%d] cmd: 0x81 REG_PNO_STATUS: 0x%02x\n", + count, res); + res = rtw_read8(adapter, REG_PNO_STATUS); + count++; + rtw_msleep_os(2); + } + if (res & BIT(7)) + ret = _TRUE; + else + ret = _FALSE; + RTW_INFO("cmd: 0x81 REG_PNO_STATUS: ret(%d)\n", ret); + } + return ret; +} +#endif + +static void rtw_hal_backup_rate(_adapter *adapter) +{ + RTW_INFO("%s\n", __func__); + /* backup data rate to register 0x8b for wowlan FW */ + rtw_write8(adapter, 0x8d, 1); + rtw_write8(adapter, 0x8c, 0); + rtw_write8(adapter, 0x8f, 0x40); + rtw_write8(adapter, 0x8b, rtw_read8(adapter, 0x2f0)); +} + +#ifdef CONFIG_GTK_OL +static void rtw_hal_fw_sync_cam_id(_adapter *adapter) +{ + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + int cam_id, index = 0; + u8 *addr = NULL; + + if (!MLME_IS_STA(adapter)) + return; + + addr = get_bssid(pmlmepriv); + + if (addr == NULL) { + RTW_INFO("%s: get bssid MAC addr fail!!\n", __func__); + return; + } + + rtw_clean_dk_section(adapter); + + do { + cam_id = rtw_camid_search(adapter, addr, index, 1); + + if (cam_id == -1) + RTW_INFO("%s: cam_id: %d, key_id:%d\n", __func__, cam_id, index); + else + rtw_sec_cam_swap(adapter, cam_id, index); + + index++; + } while (index < 4); + + rtw_write8(adapter, REG_SECCFG, 0xcc); +} + +static void rtw_hal_update_gtk_offload_info(_adapter *adapter) +{ + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + struct aoac_report *paoac_rpt = &pwrctl->wowlan_aoac_rpt; + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + struct security_priv *psecuritypriv = &adapter->securitypriv; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct cam_ctl_t *cam_ctl = &dvobj->cam_ctl; + _irqL irqL; + u8 get_key[16]; + u8 gtk_id = 0, offset = 0, i = 0, sz = 0, aoac_rpt_ver = 0, has_rekey = _FALSE; + u64 replay_count = 0, tmp_iv_hdr = 0, pkt_pn = 0; + + if (!MLME_IS_STA(adapter)) + return; + + _rtw_memset(get_key, 0, sizeof(get_key)); + _rtw_memcpy(&replay_count, + paoac_rpt->replay_counter_eapol_key, 8); + + /*read gtk key index*/ + gtk_id = paoac_rpt->key_index; + aoac_rpt_ver = paoac_rpt->version_info; + + if (aoac_rpt_ver == 0) { + /* initial verison */ + if (gtk_id == 5) + has_rekey = _FALSE; + else + has_rekey = _TRUE; + } else if (aoac_rpt_ver >= 1) { + /* Add krack patch */ + if (gtk_id == 5) + RTW_WARN("%s FW check iv fail\n", __func__); + + if (aoac_rpt_ver == 1) + RTW_WARN("%s aoac report version should be update to v2\n", __func__); + + /* Fix key id mismatch */ + if (aoac_rpt_ver == 2) + has_rekey = paoac_rpt->rekey_ok == 1 ? _TRUE : _FALSE; + } + + if (has_rekey == _FALSE) { + RTW_INFO("%s no rekey event happened.\n", __func__); + } else if (has_rekey == _TRUE) { + RTW_INFO("%s update security key.\n", __func__); + /*read key from sec-cam,for DK ,keyindex is equal to cam-id*/ + rtw_sec_read_cam_ent(adapter, gtk_id, + NULL, NULL, get_key); + rtw_clean_hw_dk_cam(adapter); + + if (_rtw_camid_is_gk(adapter, gtk_id)) { + _enter_critical_bh(&cam_ctl->lock, &irqL); + _rtw_memcpy(&dvobj->cam_cache[gtk_id].key, + get_key, 16); + _exit_critical_bh(&cam_ctl->lock, &irqL); + } else { + struct setkey_parm parm_gtk; + + parm_gtk.algorithm = paoac_rpt->security_type; + parm_gtk.keyid = gtk_id; + _rtw_memcpy(parm_gtk.key, get_key, 16); + setkey_hdl(adapter, (u8 *)&parm_gtk); + } + + /*update key into related sw variable and sec-cam cache*/ + psecuritypriv->dot118021XGrpKeyid = gtk_id; + _rtw_memcpy(&psecuritypriv->dot118021XGrpKey[gtk_id], + get_key, 16); + /* update SW TKIP TX/RX MIC value */ + if (psecuritypriv->dot118021XGrpPrivacy == _TKIP_) { + offset = RTW_KEK_LEN + RTW_TKIP_MIC_LEN; + _rtw_memcpy( + &psecuritypriv->dot118021XGrptxmickey[gtk_id], + &(paoac_rpt->group_key[offset]), + RTW_TKIP_MIC_LEN); + + offset = RTW_KEK_LEN; + _rtw_memcpy( + &psecuritypriv->dot118021XGrprxmickey[gtk_id], + &(paoac_rpt->group_key[offset]), + RTW_TKIP_MIC_LEN); + } + RTW_PRINT("GTK (%d) "KEY_FMT"\n", gtk_id, + KEY_ARG(psecuritypriv->dot118021XGrpKey[gtk_id].skey)); + } + + /* Update broadcast RX IV */ + if (psecuritypriv->dot118021XGrpPrivacy == _AES_) { + sz = sizeof(psecuritypriv->iv_seq[0]); + for (i = 0 ; i < 4 ; i++) { + _rtw_memcpy(&tmp_iv_hdr, paoac_rpt->rxgtk_iv[i], sz); + tmp_iv_hdr = le64_to_cpu(tmp_iv_hdr); + pkt_pn = CCMPH_2_PN(tmp_iv_hdr); + _rtw_memcpy(psecuritypriv->iv_seq[i], &pkt_pn, sz); + } + } + + rtw_clean_dk_section(adapter); + + rtw_write8(adapter, REG_SECCFG, 0x0c); + + #ifdef CONFIG_GTK_OL_DBG + /* if (gtk_keyindex != 5) */ + dump_sec_cam(RTW_DBGDUMP, adapter); + dump_sec_cam_cache(RTW_DBGDUMP, adapter); + #endif +} +#endif /*CONFIG_GTK_OL*/ + +static void rtw_dump_aoac_rpt(_adapter *adapter) +{ + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + struct aoac_report *paoac_rpt = &pwrctl->wowlan_aoac_rpt; + int i = 0; + + RTW_INFO_DUMP("[AOAC-RPT] IV -", paoac_rpt->iv, 8); + RTW_INFO_DUMP("[AOAC-RPT] Replay counter of EAPOL key - ", + paoac_rpt->replay_counter_eapol_key, 8); + RTW_INFO_DUMP("[AOAC-RPT] Group key - ", paoac_rpt->group_key, 32); + RTW_INFO("[AOAC-RPT] Key Index - %d\n", paoac_rpt->key_index); + RTW_INFO("[AOAC-RPT] Security Type - %d\n", paoac_rpt->security_type); + RTW_INFO("[AOAC-RPT] wow_pattern_idx - %d\n", + paoac_rpt->wow_pattern_idx); + RTW_INFO("[AOAC-RPT] version_info - %d\n", paoac_rpt->version_info); + RTW_INFO("[AOAC-RPT] rekey_ok - %d\n", paoac_rpt->rekey_ok); + RTW_INFO_DUMP("[AOAC-RPT] RX PTK IV-", paoac_rpt->rxptk_iv, 8); + RTW_INFO_DUMP("[AOAC-RPT] RX GTK[0] IV-", paoac_rpt->rxgtk_iv[0], 8); + RTW_INFO_DUMP("[AOAC-RPT] RX GTK[1] IV-", paoac_rpt->rxgtk_iv[1], 8); + RTW_INFO_DUMP("[AOAC-RPT] RX GTK[2] IV-", paoac_rpt->rxgtk_iv[2], 8); + RTW_INFO_DUMP("[AOAC-RPT] RX GTK[3] IV-", paoac_rpt->rxgtk_iv[3], 8); +} + +static void rtw_hal_get_aoac_rpt(_adapter *adapter) +{ + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + struct aoac_report *paoac_rpt = &pwrctl->wowlan_aoac_rpt; + u32 page_offset = 0, page_number = 0; + u32 page_size = 0, buf_size = 0; + u8 *buffer = NULL; + u8 i = 0, tmp = 0; + int ret = -1; + + /* read aoac report from rsvd page */ + page_offset = pwrctl->wowlan_aoac_rpt_loc; + page_number = 1; + + rtw_hal_get_def_var(adapter, HAL_DEF_TX_PAGE_SIZE, &page_size); + buf_size = page_size * page_number; + + buffer = rtw_zvmalloc(buf_size); + + if (buffer == NULL) { + RTW_ERR("%s buffer allocate failed size(%d)\n", + __func__, buf_size); + return; + } + + RTW_INFO("Get AOAC Report from rsvd page_offset:%d\n", page_offset); + + ret = rtw_hal_get_rsvd_page(adapter, page_offset, + page_number, buffer, buf_size); + + if (ret == _FALSE) { + RTW_ERR("%s get aoac report failed\n", __func__); + rtw_warn_on(1); + goto _exit; + } + + _rtw_memset(paoac_rpt, 0, sizeof(struct aoac_report)); + _rtw_memcpy(paoac_rpt, buffer, sizeof(struct aoac_report)); + + for (i = 0 ; i < 4 ; i++) { + tmp = paoac_rpt->replay_counter_eapol_key[i]; + paoac_rpt->replay_counter_eapol_key[i] = + paoac_rpt->replay_counter_eapol_key[7 - i]; + paoac_rpt->replay_counter_eapol_key[7 - i] = tmp; + } + + rtw_dump_aoac_rpt(adapter); + +_exit: + if (buffer) + rtw_vmfree(buffer, buf_size); +} + +static void rtw_hal_update_tx_iv(_adapter *adapter) +{ + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + struct aoac_report *paoac_rpt = &pwrctl->wowlan_aoac_rpt; + struct sta_info *psta; + struct mlme_ext_priv *pmlmeext = &(adapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct security_priv *psecpriv = &adapter->securitypriv; + + u16 val16 = 0; + u32 val32 = 0; + u64 txiv = 0; + u8 *pval = NULL; + + psta = rtw_get_stainfo(&adapter->stapriv, + get_my_bssid(&pmlmeinfo->network)); + + /* Update TX iv data. */ + pval = (u8 *)&paoac_rpt->iv; + + if (psecpriv->dot11PrivacyAlgrthm == _TKIP_) { + val16 = ((u16)(paoac_rpt->iv[2]) << 0) + + ((u16)(paoac_rpt->iv[0]) << 8); + val32 = ((u32)(paoac_rpt->iv[4]) << 0) + + ((u32)(paoac_rpt->iv[5]) << 8) + + ((u32)(paoac_rpt->iv[6]) << 16) + + ((u32)(paoac_rpt->iv[7]) << 24); + } else if (psecpriv->dot11PrivacyAlgrthm == _AES_) { + val16 = ((u16)(paoac_rpt->iv[0]) << 0) + + ((u16)(paoac_rpt->iv[1]) << 8); + val32 = ((u32)(paoac_rpt->iv[4]) << 0) + + ((u32)(paoac_rpt->iv[5]) << 8) + + ((u32)(paoac_rpt->iv[6]) << 16) + + ((u32)(paoac_rpt->iv[7]) << 24); + } + + if (psta) { + txiv = val16 + ((u64)val32 << 16); + if (txiv != 0) + psta->dot11txpn.val = txiv; + } +} + +static void rtw_hal_update_sw_security_info(_adapter *adapter) +{ + struct security_priv *psecpriv = &adapter->securitypriv; + u8 sz = sizeof (psecpriv->iv_seq); + + rtw_hal_update_tx_iv(adapter); +#ifdef CONFIG_GTK_OL + if (psecpriv->binstallKCK_KEK == _TRUE && + (psecpriv->ndisauthtype == Ndis802_11AuthModeWPA2PSK || _rtw_wow_chk_cap(adapter, WOW_CAP_TKIP_OL))) + rtw_hal_update_gtk_offload_info(adapter); +#else + _rtw_memset(psecpriv->iv_seq, 0, sz); +#endif +} +#ifdef CONFIG_CUSTOM_PULSE +static u8 rtw_hal_set_gpio_custom_cmd(_adapter *adapter, u8 enable) +{ + u8 H2CGpioCustomParm[H2C_GPIO_CUSTOM_LEN] = {0}; + u8 customid = 0x2, special_wakeup_reason = RX_MAGIC_PKT, custom_for_wakeup_reason=0x1; + u8 ret = _FAIL; + + RTW_INFO("%s(): enable = %d\n", __func__, enable); + + if(enable) { + SET_H2CCMD_CUSTOMERID(H2CGpioCustomParm, customid); + SET_H2CCMD_SPECIAL_WAKE_REASON(H2CGpioCustomParm, special_wakeup_reason); + SET_H2CCMD_CUSTOM_WAKE_REASON(H2CGpioCustomParm, custom_for_wakeup_reason); + + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_GPIO_CUSTOM, + H2C_GPIO_CUSTOM_LEN, + H2CGpioCustomParm); + RTW_DBG("%s(): H2C_cmd=%x, cmd=%02x, %02x, %02x\n", __func__, H2C_GPIO_CUSTOM, + H2CGpioCustomParm[0], H2CGpioCustomParm[1], H2CGpioCustomParm[2]); + } + + return ret; +} +#endif /* CONFIG_CUSTOM_PULSE */ +static u8 rtw_hal_set_keep_alive_cmd(_adapter *adapter, u8 enable, u8 pkt_type) +{ + u8 u1H2CKeepAliveParm[H2C_KEEP_ALIVE_CTRL_LEN] = {0}; + u8 adopt = 1, check_period = 5; + u8 ret = _FAIL; + u8 hw_port = rtw_hal_get_port(adapter); + + SET_H2CCMD_KEEPALIVE_PARM_ENABLE(u1H2CKeepAliveParm, enable); + SET_H2CCMD_KEEPALIVE_PARM_ADOPT(u1H2CKeepAliveParm, adopt); + SET_H2CCMD_KEEPALIVE_PARM_PKT_TYPE(u1H2CKeepAliveParm, pkt_type); + SET_H2CCMD_KEEPALIVE_PARM_CHECK_PERIOD(u1H2CKeepAliveParm, check_period); +#ifdef CONFIG_FW_MULTI_PORT_SUPPORT + SET_H2CCMD_KEEPALIVE_PARM_PORT_NUM(u1H2CKeepAliveParm, hw_port); + RTW_INFO("%s(): enable = %d, port = %d\n", __func__, enable, hw_port); +#else + RTW_INFO("%s(): enable = %d\n", __func__, enable); +#endif + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_KEEP_ALIVE, + H2C_KEEP_ALIVE_CTRL_LEN, + u1H2CKeepAliveParm); + + return ret; +} + +#ifdef CONFIG_WOW_KEEP_ALIVE_PATTERN +static u8 rtw_hal_set_keep_alive_pattern_cmd(PADAPTER adapter, u8 enable) +{ + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + u8 u1H2CKeepAliveParm[H2C_KEEP_ALIVE_PATTERN_LEN] = {0}; + u8 ret = _FAIL; + int i; + + /* If keep alive pattern is set, FW will use pattern for keep alive action */ + if(enable == 0 || (pwrctl->wowlan_keep_alive_mode == wow_keep_alive_pattern_disable)) { + SET_H2CCMD_UDP_KEEP_ALIVE_EN(u1H2CKeepAliveParm, _FALSE); + SET_H2CCMD_UDP_KEEP_ALIVE_ACK_PATTERN_EN(u1H2CKeepAliveParm, _FALSE); + SET_H2CCMD_UDP_KEEP_ALIVE_WAKE_EN(u1H2CKeepAliveParm, _FALSE); + return ret; + } + /*step1:set keep alive period*/ + SET_H2CCMD_UDP_KEEP_ALIVE_PERIOD_LOW_BIT(u1H2CKeepAliveParm, pwrctl->wowlan_keep_alive_period & 0x00FF); + SET_H2CCMD_UDP_KEEP_ALIVE_PERIOD_HI_BIT(u1H2CKeepAliveParm, ((pwrctl->wowlan_keep_alive_period & 0xFF00)>> 8)); + + if (pwrctl->wowlan_keep_alive_mode == wow_keep_alive_pattern_tx) { + SET_H2CCMD_UDP_KEEP_ALIVE_EN(u1H2CKeepAliveParm, _TRUE); + SET_H2CCMD_UDP_KEEP_ALIVE_ACK_PATTERN_EN(u1H2CKeepAliveParm, _FALSE); + SET_H2CCMD_UDP_KEEP_ALIVE_WAKE_EN(u1H2CKeepAliveParm, _FALSE); + SET_H2CCMD_UDP_KEEP_ALIVE_PACKET_LOC(u1H2CKeepAliveParm, pwrctl->keep_alive_pattern_loc); + goto exit; + } + if (pwrctl->wowlan_keep_alive_mode == wow_keep_alive_pattern_trx) { + SET_H2CCMD_UDP_KEEP_ALIVE_EN(u1H2CKeepAliveParm, _TRUE); + SET_H2CCMD_UDP_KEEP_ALIVE_ACK_PATTERN_EN(u1H2CKeepAliveParm, _TRUE); + SET_H2CCMD_UDP_KEEP_ALIVE_WAKE_EN(u1H2CKeepAliveParm, _FALSE); + SET_H2CCMD_UDP_KEEP_ALIVE_PACKET_LOC(u1H2CKeepAliveParm, pwrctl->keep_alive_pattern_loc); + SET_H2CCMD_UDP_KEEP_ALIVE_ACK_PATTERN_idx(u1H2CKeepAliveParm, pwrctl->wowlan_keep_alive_ack_index); + SET_H2CCMD_UDP_KEEP_ALIVE_RETRY_INTERVAL(u1H2CKeepAliveParm, pwrctl->wowlan_keep_alive_retry_interval); + SET_H2CCMD_UDP_KEEP_ALIVE_RETRY_LIMIT(u1H2CKeepAliveParm, pwrctl->wowlan_keep_alive_retry_counter); + goto exit; + } + if (pwrctl->wowlan_keep_alive_mode == wow_keep_alive_pattern_trx_with_ack) { + SET_H2CCMD_UDP_KEEP_ALIVE_EN(u1H2CKeepAliveParm, _TRUE); + SET_H2CCMD_UDP_KEEP_ALIVE_ACK_PATTERN_EN(u1H2CKeepAliveParm, _TRUE); + SET_H2CCMD_UDP_KEEP_ALIVE_WAKE_EN(u1H2CKeepAliveParm, _TRUE); + SET_H2CCMD_UDP_KEEP_ALIVE_PACKET_LOC(u1H2CKeepAliveParm, pwrctl->keep_alive_pattern_loc); + SET_H2CCMD_UDP_KEEP_ALIVE_ACK_PATTERN_idx(u1H2CKeepAliveParm, pwrctl->wowlan_keep_alive_ack_index); + SET_H2CCMD_UDP_KEEP_ALIVE_WAKE_PATTERN_idx(u1H2CKeepAliveParm, pwrctl->wowlan_wake_pattern_index); + SET_H2CCMD_UDP_KEEP_ALIVE_RETRY_INTERVAL(u1H2CKeepAliveParm, pwrctl->wowlan_keep_alive_retry_interval); + SET_H2CCMD_UDP_KEEP_ALIVE_RETRY_LIMIT(u1H2CKeepAliveParm, pwrctl->wowlan_keep_alive_retry_counter); + goto exit; + } +exit: + for(i=0; iregistrypriv; + u8 hw_port = rtw_hal_get_port(adapter); + + SET_H2CCMD_DISCONDECISION_PARM_ENABLE(u1H2CDisconDecisionParm, enable); + SET_H2CCMD_DISCONDECISION_PARM_ADOPT(u1H2CDisconDecisionParm, adopt); + if (!(pregistry->wakeup_event & BIT(2))) + SET_H2CCMD_DISCONDECISION_PARM_DISCONNECT_EN(u1H2CDisconDecisionParm, adopt); + SET_H2CCMD_DISCONDECISION_PARM_CHECK_PERIOD(u1H2CDisconDecisionParm, check_period); + SET_H2CCMD_DISCONDECISION_PARM_TRY_PKT_NUM(u1H2CDisconDecisionParm, trypkt_num); +#ifdef CONFIG_FW_MULTI_PORT_SUPPORT + SET_H2CCMD_DISCONDECISION_PORT_NUM(u1H2CDisconDecisionParm, hw_port); + RTW_INFO("%s(): enable = %d, port = %d\n", __func__, enable, hw_port); +#else + RTW_INFO("%s(): enable = %d\n", __func__, enable); +#endif + + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_DISCON_DECISION, + H2C_DISCON_DECISION_LEN, + u1H2CDisconDecisionParm); + return ret; +} + +static u8 rtw_hal_set_wowlan_ctrl_cmd(_adapter *adapter, u8 enable, u8 change_unit) +{ + struct registry_priv *registry_par = &adapter->registrypriv; + struct security_priv *psecpriv = &adapter->securitypriv; + struct pwrctrl_priv *ppwrpriv = adapter_to_pwrctl(adapter); + struct mlme_priv *pmlmepriv = &(adapter->mlmepriv); + + u8 u1H2CWoWlanCtrlParm[H2C_WOWLAN_LEN] = {0}; + u8 discont_wake = 0, gpionum = 0, gpio_dur = 0, no_wake = 0; + u8 hw_unicast = 0, gpio_pulse_cnt = 0, gpio_pulse_en = 0; + u8 sdio_wakeup_enable = 1; + u8 gpio_high_active = 0; + u8 magic_pkt = 0; + u8 gpio_unit = 0; /*0: 64ns, 1: 8ms*/ + u8 ret = _FAIL; +#ifdef CONFIG_DIS_UPHY + u8 dis_uphy = 0, dis_uphy_unit = 0, dis_uphy_time = 0; +#endif /* CONFIG_DIS_UPHY */ + +#ifdef CONFIG_GPIO_WAKEUP + gpio_high_active = ppwrpriv->is_high_active; + gpionum = ppwrpriv->wowlan_gpio_index; + sdio_wakeup_enable = 0; +#endif /* CONFIG_GPIO_WAKEUP */ + + if(registry_par->suspend_type == FW_IPS_DISABLE_BBRF && + !check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) + no_wake = 1; + + if (!ppwrpriv->wowlan_pno_enable && + registry_par->wakeup_event & BIT(0) && !no_wake) + magic_pkt = enable; + + if (registry_par->wakeup_event & BIT(2) && !no_wake) + discont_wake = enable; + + RTW_INFO("%s(): enable=%d change_unit=%d\n", __func__, + enable, change_unit); + + /* time = (gpio_dur/2) * gpio_unit, default:256 ms */ + if (enable && change_unit) { + gpio_dur = 0x40; + gpio_unit = 1; + gpio_pulse_en = 1; + } + +#ifdef CONFIG_PLATFORM_ARM_RK3188 + if (enable) { + gpio_pulse_en = 1; + gpio_pulse_cnt = 0x04; + } +#endif + + SET_H2CCMD_WOWLAN_FUNC_ENABLE(u1H2CWoWlanCtrlParm, enable); + if(!no_wake) + SET_H2CCMD_WOWLAN_PATTERN_MATCH_ENABLE(u1H2CWoWlanCtrlParm, enable); + SET_H2CCMD_WOWLAN_MAGIC_PKT_ENABLE(u1H2CWoWlanCtrlParm, magic_pkt); + SET_H2CCMD_WOWLAN_UNICAST_PKT_ENABLE(u1H2CWoWlanCtrlParm, hw_unicast); + SET_H2CCMD_WOWLAN_ALL_PKT_DROP(u1H2CWoWlanCtrlParm, 0); + SET_H2CCMD_WOWLAN_GPIO_ACTIVE(u1H2CWoWlanCtrlParm, gpio_high_active); + +#ifdef CONFIG_GTK_OL + if (psecpriv->binstallKCK_KEK == _TRUE && + (psecpriv->ndisauthtype == Ndis802_11AuthModeWPA2PSK || _rtw_wow_chk_cap(adapter, WOW_CAP_TKIP_OL))) + SET_H2CCMD_WOWLAN_REKEY_WAKE_UP(u1H2CWoWlanCtrlParm, 0); + else + SET_H2CCMD_WOWLAN_REKEY_WAKE_UP(u1H2CWoWlanCtrlParm, 1); +#else + SET_H2CCMD_WOWLAN_REKEY_WAKE_UP(u1H2CWoWlanCtrlParm, enable); +#endif + SET_H2CCMD_WOWLAN_DISCONNECT_WAKE_UP(u1H2CWoWlanCtrlParm, discont_wake); + SET_H2CCMD_WOWLAN_GPIONUM(u1H2CWoWlanCtrlParm, gpionum); + SET_H2CCMD_WOWLAN_DATAPIN_WAKE_UP(u1H2CWoWlanCtrlParm, sdio_wakeup_enable); + + SET_H2CCMD_WOWLAN_GPIO_DURATION(u1H2CWoWlanCtrlParm, gpio_dur); + SET_H2CCMD_WOWLAN_CHANGE_UNIT(u1H2CWoWlanCtrlParm, gpio_unit); + + SET_H2CCMD_WOWLAN_GPIO_PULSE_EN(u1H2CWoWlanCtrlParm, gpio_pulse_en); + SET_H2CCMD_WOWLAN_GPIO_PULSE_COUNT(u1H2CWoWlanCtrlParm, gpio_pulse_cnt); + +#ifdef CONFIG_WAKEUP_GPIO_INPUT_MODE + if (enable) + SET_H2CCMD_WOWLAN_GPIO_INPUT_EN(u1H2CWoWlanCtrlParm, 1); +#endif + +#ifdef CONFIG_DIS_UPHY + if (enable) { + dis_uphy = 1; + /* time unit: 0 -> ms, 1 -> 256 ms*/ + dis_uphy_unit = 1; + dis_uphy_time = 0x4; + } + + SET_H2CCMD_WOWLAN_DISABLE_UPHY(u1H2CWoWlanCtrlParm, dis_uphy); + SET_H2CCMD_WOWLAN_UNIT_FOR_UPHY_DISABLE(u1H2CWoWlanCtrlParm, dis_uphy_unit); + SET_H2CCMD_WOWLAN_TIME_FOR_UPHY_DISABLE(u1H2CWoWlanCtrlParm, dis_uphy_time); + if (ppwrpriv->hst2dev_high_active == 1) + SET_H2CCMD_WOWLAN_RISE_HST2DEV(u1H2CWoWlanCtrlParm, 1); +#ifdef CONFIG_RTW_ONE_PIN_GPIO + SET_H2CCMD_WOWLAN_GPIO_INPUT_EN(u1H2CWoWlanCtrlParm, 1); + SET_H2CCMD_WOWLAN_DEV2HST_EN(u1H2CWoWlanCtrlParm, 1); + SET_H2CCMD_WOWLAN_HST2DEV_EN(u1H2CWoWlanCtrlParm, 0); +#else + SET_H2CCMD_WOWLAN_HST2DEV_EN(u1H2CWoWlanCtrlParm, 1); +#endif /* CONFIG_RTW_ONE_PIN_GPIO */ +#endif /* CONFIG_DIS_UPHY */ + + + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_WOWLAN, + H2C_WOWLAN_LEN, + u1H2CWoWlanCtrlParm); + return ret; +} + +static u8 rtw_hal_set_remote_wake_ctrl_cmd(_adapter *adapter, u8 enable) +{ + struct security_priv *psecuritypriv = &(adapter->securitypriv); + struct pwrctrl_priv *ppwrpriv = adapter_to_pwrctl(adapter); + struct registry_priv *pregistrypriv = &adapter->registrypriv; + u8 arp_en = pregistrypriv->wakeup_event & BIT(3); + u8 u1H2CRemoteWakeCtrlParm[H2C_REMOTE_WAKE_CTRL_LEN] = {0}; + u8 ret = _FAIL, count = 0, no_wake = 0; + struct mlme_priv *pmlmepriv = &(adapter->mlmepriv); + + RTW_INFO("%s(): enable=%d\n", __func__, enable); + + if(pregistrypriv->suspend_type == FW_IPS_DISABLE_BBRF && + !check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) + no_wake = 1; + if(no_wake) { + SET_H2CCMD_REMOTE_WAKECTRL_ENABLE( + u1H2CRemoteWakeCtrlParm, enable); + } else { + if (!ppwrpriv->wowlan_pno_enable) { + SET_H2CCMD_REMOTE_WAKECTRL_ENABLE( + u1H2CRemoteWakeCtrlParm, enable); + if (arp_en) + SET_H2CCMD_REMOTE_WAKE_CTRL_ARP_OFFLOAD_EN( + u1H2CRemoteWakeCtrlParm, 1); + else + SET_H2CCMD_REMOTE_WAKE_CTRL_ARP_OFFLOAD_EN( + u1H2CRemoteWakeCtrlParm, 0); + #ifdef CONFIG_GTK_OL + if (psecuritypriv->binstallKCK_KEK == _TRUE && + (psecuritypriv->ndisauthtype == Ndis802_11AuthModeWPA2PSK || _rtw_wow_chk_cap(adapter, WOW_CAP_TKIP_OL))) { + SET_H2CCMD_REMOTE_WAKE_CTRL_GTK_OFFLOAD_EN( + u1H2CRemoteWakeCtrlParm, 1); + } else { + RTW_INFO("no kck kek\n"); + SET_H2CCMD_REMOTE_WAKE_CTRL_GTK_OFFLOAD_EN( + u1H2CRemoteWakeCtrlParm, 0); + } + #endif /* CONFIG_GTK_OL */ + + #ifdef CONFIG_IPV6 + if (ppwrpriv->wowlan_ns_offload_en == _TRUE) { + RTW_INFO("enable NS offload\n"); + SET_H2CCMD_REMOTE_WAKE_CTRL_NDP_OFFLOAD_EN( + u1H2CRemoteWakeCtrlParm, enable); + } + + /* + * filter NetBios name service pkt to avoid being waked-up + * by this kind of unicast pkt this exceptional modification + * is used for match competitor's behavior + */ + + SET_H2CCMD_REMOTE_WAKE_CTRL_NBNS_FILTER_EN( + u1H2CRemoteWakeCtrlParm, enable); + #endif /*CONFIG_IPV6*/ +#if 0 /* replaced by WOWLAN pattern match */ + #ifdef CONFIG_RTL8192F + if (IS_HARDWARE_TYPE_8192F(adapter)){ + SET_H2CCMD_REMOTE_WAKE_CTRL_FW_UNICAST_EN( + u1H2CRemoteWakeCtrlParm, enable); + } + #endif /* CONFIG_RTL8192F */ +#endif + if ((psecuritypriv->dot11PrivacyAlgrthm == _AES_) || + (psecuritypriv->dot11PrivacyAlgrthm == _TKIP_) || + (psecuritypriv->dot11PrivacyAlgrthm == _NO_PRIVACY_)) { + SET_H2CCMD_REMOTE_WAKE_CTRL_ARP_ACTION( + u1H2CRemoteWakeCtrlParm, 0); + } else { /* WEP etc. */ + if (arp_en) + SET_H2CCMD_REMOTE_WAKE_CTRL_ARP_ACTION( + u1H2CRemoteWakeCtrlParm, 1); + } + + if (psecuritypriv->dot11PrivacyAlgrthm == _TKIP_) { +#ifdef CONFIG_GTK_OL + if(_rtw_wow_chk_cap(adapter, WOW_CAP_TKIP_OL)) + SET_H2CCMD_REMOTE_WAKE_CTRL_TKIP_OFFLOAD_EN( + u1H2CRemoteWakeCtrlParm, enable); +#endif /* CONFIG_GTK_OL */ + if (IS_HARDWARE_TYPE_8188E(adapter) || + IS_HARDWARE_TYPE_8812(adapter)) { + SET_H2CCMD_REMOTE_WAKE_CTRL_TKIP_OFFLOAD_EN( + u1H2CRemoteWakeCtrlParm, 0); + if (arp_en) + SET_H2CCMD_REMOTE_WAKE_CTRL_ARP_ACTION( + u1H2CRemoteWakeCtrlParm, 1); + } + } + + SET_H2CCMD_REMOTE_WAKE_CTRL_FW_PARSING_UNTIL_WAKEUP( + u1H2CRemoteWakeCtrlParm, 1); + } + #ifdef CONFIG_PNO_SUPPORT + else { + SET_H2CCMD_REMOTE_WAKECTRL_ENABLE( + u1H2CRemoteWakeCtrlParm, enable); + SET_H2CCMD_REMOTE_WAKE_CTRL_NLO_OFFLOAD_EN( + u1H2CRemoteWakeCtrlParm, enable); + } + #endif + + #ifdef CONFIG_P2P_WOWLAN + if (_TRUE == ppwrpriv->wowlan_p2p_mode) { + RTW_INFO("P2P OFFLOAD ENABLE\n"); + SET_H2CCMD_REMOTE_WAKE_CTRL_P2P_OFFLAD_EN(u1H2CRemoteWakeCtrlParm, 1); + } else { + RTW_INFO("P2P OFFLOAD DISABLE\n"); + SET_H2CCMD_REMOTE_WAKE_CTRL_P2P_OFFLAD_EN(u1H2CRemoteWakeCtrlParm, 0); + } + #endif /* CONFIG_P2P_WOWLAN */ + } + + + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_REMOTE_WAKE_CTRL, + H2C_REMOTE_WAKE_CTRL_LEN, + u1H2CRemoteWakeCtrlParm); + return ret; +} + +#ifdef CONFIG_WAR_OFFLOAD +static u8 rtw_hal_set_war_offload_ctrl_cmd(_adapter *adapter, u8 enable) +{ + struct pwrctrl_priv *ppwrpriv = adapter_to_pwrctl(adapter); + u8 u1H2CWarOffloadParm[H2C_WAR_OFFLOAD_LEN] = {0}; + u8 ret = _FAIL; + + RTW_INFO("%s(): enable=%d\n", __func__, enable); + + if (_TRUE == ppwrpriv->wowlan_war_offload_mode) { + SET_H2CCMD_WAR_CFG_EN(u1H2CWarOffloadParm, enable); + SET_H2CCMD_WAR_CFG_ARP_RSP_EN(u1H2CWarOffloadParm, 1); + +#ifdef CONFIG_OFFLOAD_MDNS_V4 + if (WAR_MDNS_V4_RSP_EN & ppwrpriv->wowlan_war_offload_ctrl) { + SET_H2CCMD_WAR_CFG_MDNSV4_RSP_EN(u1H2CWarOffloadParm, 1); + } + if (WAR_MDNS_V4_WAKEUP_EN& ppwrpriv->wowlan_war_offload_ctrl) { + SET_H2CCMD_WAR_CFG_MDNSV4_WAKE_EN(u1H2CWarOffloadParm, 1); + } +#endif /* CONFIG_OFFLOAD_MDNS_V4 */ + +#ifdef CONFIG_OFFLOAD_MDNS_V6 + if (WAR_MDNS_V6_RSP_EN & ppwrpriv->wowlan_war_offload_ctrl) { + SET_H2CCMD_WAR_CFG_MDNSV6_RSP_EN(u1H2CWarOffloadParm, 1); + } + if (WAR_MDNS_V6_WAKEUP_EN & ppwrpriv->wowlan_war_offload_ctrl) { + SET_H2CCMD_WAR_CFG_MDNSV6_WAKE_EN(u1H2CWarOffloadParm, 1); + } +#endif /* CONFIG_OFFLOAD_MDNS_V6 */ + + } + + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_WAR_OFFLOAD, + H2C_WAR_OFFLOAD_LEN, + u1H2CWarOffloadParm); + return ret; +} + +static u8 rtw_hal_set_war_offload_parm(_adapter *adapter, PRSVDPAGE_LOC rsvdpageloc) +{ + struct pwrctrl_priv *ppwrpriv = adapter_to_pwrctl(adapter); + u8 u1H2CWarOfldParm[H2C_WAROFLD_RSVDPAGE1_LEN] = {0}; + u8 ret = _FAIL; + + SET_H2CCMD_WAROFLD_RSVDPAGE1_LOC_PARM(u1H2CWarOfldParm, rsvdpageloc->LocIpParm); + RTW_INFO("%s(): LocIpParm = %d\n", __func__, rsvdpageloc->LocIpParm); + + + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_WAROFLD_RSVDPAGE1, + H2C_WAROFLD_RSVDPAGE1_LEN, + u1H2CWarOfldParm); + + return ret; +} +#endif /* CONFIG_WAR_OFFLOAD */ + + + +static u8 rtw_hal_set_global_info_cmd(_adapter *adapter, u8 group_alg, u8 pairwise_alg) +{ + u8 ret = _FAIL; + u8 u1H2CAOACGlobalInfoParm[H2C_AOAC_GLOBAL_INFO_LEN] = {0}; + + RTW_INFO("%s(): group_alg=%d pairwise_alg=%d\n", + __func__, group_alg, pairwise_alg); + SET_H2CCMD_AOAC_GLOBAL_INFO_PAIRWISE_ENC_ALG(u1H2CAOACGlobalInfoParm, + pairwise_alg); + SET_H2CCMD_AOAC_GLOBAL_INFO_GROUP_ENC_ALG(u1H2CAOACGlobalInfoParm, + group_alg); + + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_AOAC_GLOBAL_INFO, + H2C_AOAC_GLOBAL_INFO_LEN, + u1H2CAOACGlobalInfoParm); + + return ret; +} + +#ifdef CONFIG_PNO_SUPPORT +static u8 rtw_hal_set_scan_offload_info_cmd(_adapter *adapter, + PRSVDPAGE_LOC rsvdpageloc, u8 enable) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + u8 u1H2CScanOffloadInfoParm[H2C_SCAN_OFFLOAD_CTRL_LEN] = {0}; + u8 res = 0, count = 0, ret = _FAIL; + + RTW_INFO("%s: loc_probe_packet:%d, loc_scan_info: %d loc_ssid_info:%d\n", + __func__, rsvdpageloc->LocProbePacket, + rsvdpageloc->LocScanInfo, rsvdpageloc->LocSSIDInfo); + + SET_H2CCMD_AOAC_NLO_FUN_EN(u1H2CScanOffloadInfoParm, enable); + SET_H2CCMD_AOAC_NLO_IPS_EN(u1H2CScanOffloadInfoParm, enable); + SET_H2CCMD_AOAC_RSVDPAGE_LOC_SCAN_INFO(u1H2CScanOffloadInfoParm, + rsvdpageloc->LocScanInfo); + SET_H2CCMD_AOAC_RSVDPAGE_LOC_PROBE_PACKET(u1H2CScanOffloadInfoParm, + rsvdpageloc->LocProbePacket); + /* + SET_H2CCMD_AOAC_RSVDPAGE_LOC_SSID_INFO(u1H2CScanOffloadInfoParm, + rsvdpageloc->LocSSIDInfo); + */ + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_D0_SCAN_OFFLOAD_INFO, + H2C_SCAN_OFFLOAD_CTRL_LEN, + u1H2CScanOffloadInfoParm); + return ret; +} +#endif /* CONFIG_PNO_SUPPORT */ + +void rtw_hal_set_fw_wow_related_cmd(_adapter *padapter, u8 enable) +{ + struct security_priv *psecpriv = &padapter->securitypriv; + struct pwrctrl_priv *ppwrpriv = adapter_to_pwrctl(padapter); + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct registry_priv *pregistry = &padapter->registrypriv; + u8 pkt_type = 0, no_wake = 0; + + if(pregistry->suspend_type == FW_IPS_DISABLE_BBRF && + !check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) + no_wake = 1; + + RTW_PRINT("+%s()+: enable=%d\n", __func__, enable); + + rtw_hal_set_wowlan_ctrl_cmd(padapter, enable, _FALSE); + + if (enable) { + if(!no_wake) + rtw_hal_set_global_info_cmd(padapter, + psecpriv->dot118021XGrpPrivacy, + psecpriv->dot11PrivacyAlgrthm); + + if (!(ppwrpriv->wowlan_pno_enable)) { + if (!no_wake) + rtw_hal_set_disconnect_decision_cmd(padapter, + enable); +#ifdef CONFIG_ARP_KEEP_ALIVE + if ((psecpriv->dot11PrivacyAlgrthm == _WEP40_) || + (psecpriv->dot11PrivacyAlgrthm == _WEP104_)) + pkt_type = 0; + else + pkt_type = 1; +#else + pkt_type = 0; +#endif /* CONFIG_ARP_KEEP_ALIVE */ + if(!no_wake) { + #ifdef CONFIG_WOW_KEEP_ALIVE_PATTERN + rtw_hal_set_keep_alive_pattern_cmd(padapter,enable); + #endif /*CONFIG_WOW_KEEP_ALIVE_PATTERN*/ + rtw_hal_set_keep_alive_cmd(padapter, enable, pkt_type); + } + } +#ifdef CONFIG_PNO_SUPPORT + rtw_hal_check_pno_enabled(padapter); +#endif /* CONFIG_PNO_SUPPORT */ +#ifdef CONFIG_WAR_OFFLOAD + rtw_hal_set_war_offload_ctrl_cmd(padapter, enable); +#endif /* CONFIG_WAR_OFFLOAD */ + + } else { +#if 0 + { + u32 PageSize = 0; + rtw_hal_get_def_var(padapter, HAL_DEF_TX_PAGE_SIZE, (u8 *)&PageSize); + dump_TX_FIFO(padapter, 4, PageSize); + } +#endif + } +#ifdef CONFIG_CUSTOM_PULSE + rtw_hal_set_gpio_custom_cmd(padapter, enable); +#endif /* CONFIG_CUSTOM_PULSE */ + rtw_hal_set_remote_wake_ctrl_cmd(padapter, enable); + RTW_PRINT("-%s()-\n", __func__); +} +#endif /* CONFIG_WOWLAN */ + +#ifdef CONFIG_AP_WOWLAN +static u8 rtw_hal_set_ap_wowlan_ctrl_cmd(_adapter *adapter, u8 enable) +{ + struct pwrctrl_priv *ppwrpriv = adapter_to_pwrctl(adapter); + + u8 u1H2CAPWoWlanCtrlParm[H2C_AP_WOW_GPIO_CTRL_LEN] = {0}; + u8 gpionum = 0, gpio_dur = 0; + u8 gpio_pulse = enable; + u8 sdio_wakeup_enable = 1; + u8 gpio_high_active = 0; + u8 ret = _FAIL; + +#ifdef CONFIG_GPIO_WAKEUP + gpio_high_active = ppwrpriv->is_high_active; + gpionum = ppwrpriv->wowlan_gpio_index; + sdio_wakeup_enable = 0; +#endif /*CONFIG_GPIO_WAKEUP*/ + + RTW_INFO("%s(): enable=%d\n", __func__, enable); + + SET_H2CCMD_AP_WOW_GPIO_CTRL_INDEX(u1H2CAPWoWlanCtrlParm, + gpionum); + SET_H2CCMD_AP_WOW_GPIO_CTRL_PLUS(u1H2CAPWoWlanCtrlParm, + gpio_pulse); + SET_H2CCMD_AP_WOW_GPIO_CTRL_HIGH_ACTIVE(u1H2CAPWoWlanCtrlParm, + gpio_high_active); + SET_H2CCMD_AP_WOW_GPIO_CTRL_EN(u1H2CAPWoWlanCtrlParm, + enable); + SET_H2CCMD_AP_WOW_GPIO_CTRL_DURATION(u1H2CAPWoWlanCtrlParm, + gpio_dur); + + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_AP_WOW_GPIO_CTRL, + H2C_AP_WOW_GPIO_CTRL_LEN, + u1H2CAPWoWlanCtrlParm); + + return ret; +} + +static u8 rtw_hal_set_ap_offload_ctrl_cmd(_adapter *adapter, u8 enable) +{ + u8 u1H2CAPOffloadCtrlParm[H2C_WOWLAN_LEN] = {0}; + u8 ret = _FAIL; + + RTW_INFO("%s(): bFuncEn=%d\n", __func__, enable); + + SET_H2CCMD_AP_WOWLAN_EN(u1H2CAPOffloadCtrlParm, enable); + + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_AP_OFFLOAD, + H2C_AP_OFFLOAD_LEN, + u1H2CAPOffloadCtrlParm); + + return ret; +} + +static u8 rtw_hal_set_ap_ps_cmd(_adapter *adapter, u8 enable) +{ + u8 ap_ps_parm[H2C_AP_PS_LEN] = {0}; + u8 ret = _FAIL; + + RTW_INFO("%s(): enable=%d\n" , __func__ , enable); + + SET_H2CCMD_AP_WOW_PS_EN(ap_ps_parm, enable); +#ifndef CONFIG_USB_HCI + SET_H2CCMD_AP_WOW_PS_32K_EN(ap_ps_parm, enable); +#endif /*CONFIG_USB_HCI*/ + SET_H2CCMD_AP_WOW_PS_RF(ap_ps_parm, enable); + + if (enable) + SET_H2CCMD_AP_WOW_PS_DURATION(ap_ps_parm, 0x32); + else + SET_H2CCMD_AP_WOW_PS_DURATION(ap_ps_parm, 0x0); + + ret = rtw_hal_fill_h2c_cmd(adapter, H2C_SAP_PS_, + H2C_AP_PS_LEN, ap_ps_parm); + + return ret; +} + +static void rtw_hal_set_ap_rsvdpage_loc_cmd(PADAPTER padapter, + PRSVDPAGE_LOC rsvdpageloc) +{ + struct hal_ops *pHalFunc = &padapter->hal_func; + u8 rsvdparm[H2C_AOAC_RSVDPAGE_LOC_LEN] = {0}; + u8 ret = _FAIL, header = 0; + + if (pHalFunc->fill_h2c_cmd == NULL) { + RTW_INFO("%s: Please hook fill_h2c_cmd first!\n", __func__); + return; + } + + header = rtw_read8(padapter, REG_BCNQ_BDNY); + + RTW_INFO("%s: beacon: %d, probeRsp: %d, header:0x%02x\n", __func__, + rsvdpageloc->LocApOffloadBCN, + rsvdpageloc->LocProbeRsp, + header); + + SET_H2CCMD_AP_WOWLAN_RSVDPAGE_LOC_BCN(rsvdparm, + rsvdpageloc->LocApOffloadBCN + header); + + ret = rtw_hal_fill_h2c_cmd(padapter, H2C_BCN_RSVDPAGE, + H2C_BCN_RSVDPAGE_LEN, rsvdparm); + + if (ret == _FAIL) + RTW_INFO("%s: H2C_BCN_RSVDPAGE cmd fail\n", __func__); + + rtw_msleep_os(10); + + _rtw_memset(&rsvdparm, 0, sizeof(rsvdparm)); + + SET_H2CCMD_AP_WOWLAN_RSVDPAGE_LOC_ProbeRsp(rsvdparm, + rsvdpageloc->LocProbeRsp + header); + + ret = rtw_hal_fill_h2c_cmd(padapter, H2C_PROBERSP_RSVDPAGE, + H2C_PROBERSP_RSVDPAGE_LEN, rsvdparm); + + if (ret == _FAIL) + RTW_INFO("%s: H2C_PROBERSP_RSVDPAGE cmd fail\n", __func__); + + rtw_msleep_os(10); +} + +static void rtw_hal_set_fw_ap_wow_related_cmd(_adapter *padapter, u8 enable) +{ + rtw_hal_set_ap_offload_ctrl_cmd(padapter, enable); + rtw_hal_set_ap_wowlan_ctrl_cmd(padapter, enable); + rtw_hal_set_ap_ps_cmd(padapter, enable); +} + +static void rtw_hal_ap_wow_enable(_adapter *padapter) +{ + struct security_priv *psecuritypriv = &padapter->securitypriv; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + struct sta_info *psta = NULL; + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); +#ifdef DBG_CHECK_FW_PS_STATE + struct dvobj_priv *psdpriv = padapter->dvobj; + struct debug_priv *pdbgpriv = &psdpriv->drv_dbg; +#endif /*DBG_CHECK_FW_PS_STATE*/ + int res; + u16 media_status_rpt; +#ifdef CONFIG_GPIO_WAKEUP + u8 val8 = 0; +#endif + + RTW_INFO("%s, WOWLAN_AP_ENABLE\n", __func__); +#ifdef DBG_CHECK_FW_PS_STATE + if (rtw_fw_ps_state(padapter) == _FAIL) { + pdbgpriv->dbg_enwow_dload_fw_fail_cnt++; + RTW_PRINT("wowlan enable no leave 32k\n"); + } +#endif /*DBG_CHECK_FW_PS_STATE*/ + + /* 1. Download WOWLAN FW*/ + rtw_hal_fw_dl(padapter, _TRUE); + + media_status_rpt = RT_MEDIA_CONNECT; + rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_JOINBSSRPT, + (u8 *)&media_status_rpt); + + issue_beacon(padapter, 0); + + rtw_msleep_os(2); + #if defined(CONFIG_RTL8188E) + if (IS_HARDWARE_TYPE_8188E(padapter)) + rtw_hal_disable_tx_report(padapter); + #endif + /* RX DMA stop */ + res = rtw_hal_pause_rx_dma(padapter); + if (res == _FAIL) + RTW_PRINT("[WARNING] pause RX DMA fail\n"); + +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) + /* Enable CPWM2 only. */ + res = rtw_hal_enable_cpwm2(padapter); + if (res == _FAIL) + RTW_PRINT("[WARNING] enable cpwm2 fail\n"); +#endif + +#ifdef CONFIG_GPIO_WAKEUP +#ifdef CONFIG_RTW_ONE_PIN_GPIO + rtw_hal_switch_gpio_wl_ctrl(padapter, pwrpriv->wowlan_gpio_index, _TRUE); + rtw_hal_set_input_gpio(padapter, pwrpriv->wowlan_gpio_index); +#else +#ifdef CONFIG_WAKEUP_GPIO_INPUT_MODE + if (pwrctrlpriv->is_high_active == 0) + rtw_hal_set_input_gpio(padapter, pwrpriv->wowlan_gpio_index); + else + rtw_hal_set_output_gpio(padapter, pwrpriv->wowlan_gpio_index, + GPIO_OUTPUT_LOW); +#else + val8 = (pwrpriv->is_high_active == 0) ? 1 : 0; + rtw_hal_set_output_gpio(padapter, pwrpriv->wowlan_gpio_index, val8); + rtw_hal_switch_gpio_wl_ctrl(padapter, pwrpriv->wowlan_gpio_index, _TRUE); + RTW_INFO("%s: set GPIO_%d to OUTPUT %s state in ap wow suspend and %s_ACTIVE.\n", + __func__, pwrpriv->wowlan_gpio_index, + pwrpriv->wowlan_gpio_output_state ? "HIGH" : "LOW", + pwrpriv->is_high_active ? "HIGI" : "LOW"); +#endif /* CONFIG_WAKEUP_GPIO_INPUT_MODE */ +#endif /* CONFIG_RTW_ONE_PIN_GPIO */ +#endif /* CONFIG_GPIO_WAKEUP */ + + /* 5. Set Enable WOWLAN H2C command. */ + RTW_PRINT("Set Enable AP WOWLan cmd\n"); + rtw_hal_set_fw_ap_wow_related_cmd(padapter, 1); + + rtw_write8(padapter, REG_MCUTST_WOWLAN, 0); +#ifdef CONFIG_USB_HCI + rtw_mi_intf_stop(padapter); +#endif +#if defined(CONFIG_USB_HCI) || defined(CONFIG_PCI_HCI) + /* Invoid SE0 reset signal during suspending*/ + rtw_write8(padapter, REG_RSV_CTRL, 0x20); + if (IS_8188F(pHalData->version_id) == FALSE + && IS_8188GTV(pHalData->version_id) == FALSE) + rtw_write8(padapter, REG_RSV_CTRL, 0x60); +#endif +} + +static void rtw_hal_ap_wow_disable(_adapter *padapter) +{ + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter); +#ifdef DBG_CHECK_FW_PS_STATE + struct dvobj_priv *psdpriv = padapter->dvobj; + struct debug_priv *pdbgpriv = &psdpriv->drv_dbg; +#endif /*DBG_CHECK_FW_PS_STATE*/ + u16 media_status_rpt; + + RTW_INFO("%s, WOWLAN_AP_DISABLE\n", __func__); + /* 1. Read wakeup reason*/ + pwrctl->wowlan_wake_reason = rtw_read8(padapter, REG_MCUTST_WOWLAN); + + RTW_PRINT("wakeup_reason: 0x%02x\n", + pwrctl->wowlan_wake_reason); + + rtw_hal_set_fw_ap_wow_related_cmd(padapter, 0); + + rtw_msleep_os(2); +#ifdef DBG_CHECK_FW_PS_STATE + if (rtw_fw_ps_state(padapter) == _FAIL) { + pdbgpriv->dbg_diswow_dload_fw_fail_cnt++; + RTW_PRINT("wowlan enable no leave 32k\n"); + } +#endif /*DBG_CHECK_FW_PS_STATE*/ + + #if defined(CONFIG_RTL8188E) + if (IS_HARDWARE_TYPE_8188E(padapter)) + rtw_hal_enable_tx_report(padapter); + #endif + + rtw_hal_force_enable_rxdma(padapter); + + rtw_hal_fw_dl(padapter, _FALSE); + +#ifdef CONFIG_GPIO_WAKEUP +#ifdef CONFIG_RTW_ONE_PIN_GPIO + rtw_hal_set_input_gpio(padapter, pwrctl->wowlan_gpio_index); +#else +#ifdef CONFIG_WAKEUP_GPIO_INPUT_MODE + if (pwrctl->is_high_active == 0) + rtw_hal_set_input_gpio(padapter, pwrctl->wowlan_gpio_index); + else + rtw_hal_set_output_gpio(padapter, pwrctl->wowlan_gpio_index + , GPIO_OUTPUT_LOW); +#else + rtw_hal_set_output_gpio(padapter, pwrctl->wowlan_gpio_index, + pwrctl->wowlan_gpio_output_state); + RTW_INFO("%s: set GPIO_%d to OUTPUT %s state in ap wow resume and %s_ACTIVE.\n", + __func__, pwrctl->wowlan_gpio_index, + pwrctl->wowlan_gpio_output_state ? "HIGH" : "LOW", + pwrctl->is_high_active ? "HIGI" : "LOW"); +#endif /*CONFIG_WAKEUP_GPIO_INPUT_MODE*/ +#endif /* CONFIG_RTW_ONE_PIN_GPIO */ +#endif /* CONFIG_GPIO_WAKEUP */ + media_status_rpt = RT_MEDIA_CONNECT; + + rtw_hal_set_hwreg(padapter, HW_VAR_H2C_FW_JOINBSSRPT, + (u8 *)&media_status_rpt); + + issue_beacon(padapter, 0); +} +#endif /*CONFIG_AP_WOWLAN*/ + +#ifdef CONFIG_P2P_WOWLAN +static int update_hidden_ssid(u8 *ies, u32 ies_len, u8 hidden_ssid_mode) +{ + u8 *ssid_ie; + sint ssid_len_ori; + int len_diff = 0; + + ssid_ie = rtw_get_ie(ies, WLAN_EID_SSID, &ssid_len_ori, ies_len); + + /* RTW_INFO("%s hidden_ssid_mode:%u, ssid_ie:%p, ssid_len_ori:%d\n", __FUNCTION__, hidden_ssid_mode, ssid_ie, ssid_len_ori); */ + + if (ssid_ie && ssid_len_ori > 0) { + switch (hidden_ssid_mode) { + case 1: { + u8 *next_ie = ssid_ie + 2 + ssid_len_ori; + u32 remain_len = 0; + + remain_len = ies_len - (next_ie - ies); + + ssid_ie[1] = 0; + _rtw_memcpy(ssid_ie + 2, next_ie, remain_len); + len_diff -= ssid_len_ori; + + break; + } + case 2: + _rtw_memset(&ssid_ie[2], 0, ssid_len_ori); + break; + default: + break; + } + } + + return len_diff; +} + +static void rtw_hal_construct_P2PBeacon(_adapter *padapter, u8 *pframe, u32 *pLength) +{ + /* struct xmit_frame *pmgntframe; */ + /* struct pkt_attrib *pattrib; */ + /* unsigned char *pframe; */ + struct rtw_ieee80211_hdr *pwlanhdr; + unsigned short *fctrl; + unsigned int rate_len; + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); + u32 pktlen; + /* #if defined (CONFIG_AP_MODE) && defined (CONFIG_NATIVEAP_MLME) */ + /* _irqL irqL; + * struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + * #endif */ /* #if defined (CONFIG_AP_MODE) && defined (CONFIG_NATIVEAP_MLME) */ + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + WLAN_BSSID_EX *cur_network = &(pmlmeinfo->network); + u8 bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; +#ifdef CONFIG_P2P + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); +#endif /* CONFIG_P2P */ + + /* for debug */ + u8 *dbgbuf = pframe; + u8 dbgbufLen = 0, index = 0; + + RTW_INFO("%s\n", __FUNCTION__); + /* #if defined (CONFIG_AP_MODE) && defined (CONFIG_NATIVEAP_MLME) */ + /* _enter_critical_bh(&pmlmepriv->bcn_update_lock, &irqL); + * #endif */ /* #if defined (CONFIG_AP_MODE) && defined (CONFIG_NATIVEAP_MLME) */ + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + + fctrl = &(pwlanhdr->frame_ctl); + *(fctrl) = 0; + + _rtw_memcpy(pwlanhdr->addr1, bc_addr, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, get_my_bssid(cur_network), ETH_ALEN); + + SetSeqNum(pwlanhdr, 0/*pmlmeext->mgnt_seq*/); + /* pmlmeext->mgnt_seq++; */ + set_frame_sub_type(pframe, WIFI_BEACON); + + pframe += sizeof(struct rtw_ieee80211_hdr_3addr); + pktlen = sizeof(struct rtw_ieee80211_hdr_3addr); + + if ((pmlmeinfo->state & 0x03) == WIFI_FW_AP_STATE) { + /* RTW_INFO("ie len=%d\n", cur_network->IELength); */ +#ifdef CONFIG_P2P + /* for P2P : Primary Device Type & Device Name */ + u32 wpsielen = 0, insert_len = 0; + u8 *wpsie = NULL; + wpsie = rtw_get_wps_ie(cur_network->IEs + _FIXED_IE_LENGTH_, cur_network->IELength - _FIXED_IE_LENGTH_, NULL, &wpsielen); + + if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO) && wpsie && wpsielen > 0) { + uint wps_offset, remainder_ielen; + u8 *premainder_ie, *pframe_wscie; + + wps_offset = (uint)(wpsie - cur_network->IEs); + + premainder_ie = wpsie + wpsielen; + + remainder_ielen = cur_network->IELength - wps_offset - wpsielen; + +#ifdef CONFIG_IOCTL_CFG80211 + if (pwdinfo->driver_interface == DRIVER_CFG80211) { + if (pmlmepriv->wps_beacon_ie && pmlmepriv->wps_beacon_ie_len > 0) { + _rtw_memcpy(pframe, cur_network->IEs, wps_offset); + pframe += wps_offset; + pktlen += wps_offset; + + _rtw_memcpy(pframe, pmlmepriv->wps_beacon_ie, pmlmepriv->wps_beacon_ie_len); + pframe += pmlmepriv->wps_beacon_ie_len; + pktlen += pmlmepriv->wps_beacon_ie_len; + + /* copy remainder_ie to pframe */ + _rtw_memcpy(pframe, premainder_ie, remainder_ielen); + pframe += remainder_ielen; + pktlen += remainder_ielen; + } else { + _rtw_memcpy(pframe, cur_network->IEs, cur_network->IELength); + pframe += cur_network->IELength; + pktlen += cur_network->IELength; + } + } else +#endif /* CONFIG_IOCTL_CFG80211 */ + { + pframe_wscie = pframe + wps_offset; + _rtw_memcpy(pframe, cur_network->IEs, wps_offset + wpsielen); + pframe += (wps_offset + wpsielen); + pktlen += (wps_offset + wpsielen); + + /* now pframe is end of wsc ie, insert Primary Device Type & Device Name */ + /* Primary Device Type */ + /* Type: */ + *(u16 *)(pframe + insert_len) = cpu_to_be16(WPS_ATTR_PRIMARY_DEV_TYPE); + insert_len += 2; + + /* Length: */ + *(u16 *)(pframe + insert_len) = cpu_to_be16(0x0008); + insert_len += 2; + + /* Value: */ + /* Category ID */ + *(u16 *)(pframe + insert_len) = cpu_to_be16(WPS_PDT_CID_MULIT_MEDIA); + insert_len += 2; + + /* OUI */ + *(u32 *)(pframe + insert_len) = cpu_to_be32(WPSOUI); + insert_len += 4; + + /* Sub Category ID */ + *(u16 *)(pframe + insert_len) = cpu_to_be16(WPS_PDT_SCID_MEDIA_SERVER); + insert_len += 2; + + + /* Device Name */ + /* Type: */ + *(u16 *)(pframe + insert_len) = cpu_to_be16(WPS_ATTR_DEVICE_NAME); + insert_len += 2; + + /* Length: */ + *(u16 *)(pframe + insert_len) = cpu_to_be16(pwdinfo->device_name_len); + insert_len += 2; + + /* Value: */ + _rtw_memcpy(pframe + insert_len, pwdinfo->device_name, pwdinfo->device_name_len); + insert_len += pwdinfo->device_name_len; + + + /* update wsc ie length */ + *(pframe_wscie + 1) = (wpsielen - 2) + insert_len; + + /* pframe move to end */ + pframe += insert_len; + pktlen += insert_len; + + /* copy remainder_ie to pframe */ + _rtw_memcpy(pframe, premainder_ie, remainder_ielen); + pframe += remainder_ielen; + pktlen += remainder_ielen; + } + } else +#endif /* CONFIG_P2P */ + { + int len_diff; + _rtw_memcpy(pframe, cur_network->IEs, cur_network->IELength); + len_diff = update_hidden_ssid( + pframe + _BEACON_IE_OFFSET_ + , cur_network->IELength - _BEACON_IE_OFFSET_ + , pmlmeinfo->hidden_ssid_mode + ); + pframe += (cur_network->IELength + len_diff); + pktlen += (cur_network->IELength + len_diff); + } +#if 0 + { + u8 *wps_ie; + uint wps_ielen; + u8 sr = 0; + wps_ie = rtw_get_wps_ie(pmgntframe->buf_addr + TXDESC_OFFSET + sizeof(struct rtw_ieee80211_hdr_3addr) + _BEACON_IE_OFFSET_, + pattrib->pktlen - sizeof(struct rtw_ieee80211_hdr_3addr) - _BEACON_IE_OFFSET_, NULL, &wps_ielen); + if (wps_ie && wps_ielen > 0) + rtw_get_wps_attr_content(wps_ie, wps_ielen, WPS_ATTR_SELECTED_REGISTRAR, (u8 *)(&sr), NULL); + if (sr != 0) + set_fwstate(pmlmepriv, WIFI_UNDER_WPS); + else + _clr_fwstate_(pmlmepriv, WIFI_UNDER_WPS); + } +#endif +#ifdef CONFIG_P2P + if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO)) { + u32 len; +#ifdef CONFIG_IOCTL_CFG80211 + if (pwdinfo->driver_interface == DRIVER_CFG80211) { + len = pmlmepriv->p2p_beacon_ie_len; + if (pmlmepriv->p2p_beacon_ie && len > 0) + _rtw_memcpy(pframe, pmlmepriv->p2p_beacon_ie, len); + } else +#endif /* CONFIG_IOCTL_CFG80211 */ + { + len = build_beacon_p2p_ie(pwdinfo, pframe); + } + + pframe += len; + pktlen += len; + + #ifdef CONFIG_WFD + len = rtw_append_beacon_wfd_ie(padapter, pframe); + pframe += len; + pktlen += len; + #endif + + } +#endif /* CONFIG_P2P */ + + goto _issue_bcn; + + } + + /* below for ad-hoc mode */ + + /* timestamp will be inserted by hardware */ + pframe += 8; + pktlen += 8; + + /* beacon interval: 2 bytes */ + + _rtw_memcpy(pframe, (unsigned char *)(rtw_get_beacon_interval_from_ie(cur_network->IEs)), 2); + + pframe += 2; + pktlen += 2; + + /* capability info: 2 bytes */ + + _rtw_memcpy(pframe, (unsigned char *)(rtw_get_capability_from_ie(cur_network->IEs)), 2); + + pframe += 2; + pktlen += 2; + + /* SSID */ + pframe = rtw_set_ie(pframe, _SSID_IE_, cur_network->Ssid.SsidLength, cur_network->Ssid.Ssid, &pktlen); + + /* supported rates... */ + rate_len = rtw_get_rateset_len(cur_network->SupportedRates); + pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, ((rate_len > 8) ? 8 : rate_len), cur_network->SupportedRates, &pktlen); + + /* DS parameter set */ + pframe = rtw_set_ie(pframe, _DSSET_IE_, 1, (unsigned char *)&(cur_network->Configuration.DSConfig), &pktlen); + + /* if( (pmlmeinfo->state&0x03) == WIFI_FW_ADHOC_STATE) */ + { + u8 erpinfo = 0; + u32 ATIMWindow; + /* IBSS Parameter Set... */ + /* ATIMWindow = cur->Configuration.ATIMWindow; */ + ATIMWindow = 0; + pframe = rtw_set_ie(pframe, _IBSS_PARA_IE_, 2, (unsigned char *)(&ATIMWindow), &pktlen); + + /* ERP IE */ + pframe = rtw_set_ie(pframe, _ERPINFO_IE_, 1, &erpinfo, &pktlen); + } + + + /* EXTERNDED SUPPORTED RATE */ + if (rate_len > 8) + pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (rate_len - 8), (cur_network->SupportedRates + 8), &pktlen); + + + /* todo:HT for adhoc */ + +_issue_bcn: + + /* #if defined (CONFIG_AP_MODE) && defined (CONFIG_NATIVEAP_MLME) */ + /* pmlmepriv->update_bcn = _FALSE; + * + * _exit_critical_bh(&pmlmepriv->bcn_update_lock, &irqL); + * #endif */ /* #if defined (CONFIG_AP_MODE) && defined (CONFIG_NATIVEAP_MLME) */ + + *pLength = pktlen; +#if 0 + /* printf dbg msg */ + dbgbufLen = pktlen; + RTW_INFO("======> DBG MSG FOR CONSTRAUCT P2P BEACON\n"); + + for (index = 0; index < dbgbufLen; index++) + printk("%x ", *(dbgbuf + index)); + + printk("\n"); + RTW_INFO("<====== DBG MSG FOR CONSTRAUCT P2P BEACON\n"); + +#endif +} + +static void rtw_hal_construct_P2PProbeRsp(_adapter *padapter, u8 *pframe, u32 *pLength) +{ + /* struct xmit_frame *pmgntframe; */ + /* struct pkt_attrib *pattrib; */ + /* unsigned char *pframe; */ + struct rtw_ieee80211_hdr *pwlanhdr; + unsigned short *fctrl; + unsigned char *mac; + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + /* WLAN_BSSID_EX *cur_network = &(pmlmeinfo->network); */ + u16 beacon_interval = 100; + u16 capInfo = 0; + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + u8 wpsie[255] = { 0x00 }; + u32 wpsielen = 0, p2pielen = 0; + u32 pktlen; +#ifdef CONFIG_WFD + u32 wfdielen = 0; +#endif + + /* for debug */ + u8 *dbgbuf = pframe; + u8 dbgbufLen = 0, index = 0; + + RTW_INFO("%s\n", __FUNCTION__); + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + mac = adapter_mac_addr(padapter); + + fctrl = &(pwlanhdr->frame_ctl); + *(fctrl) = 0; + + /* DA filled by FW */ + _rtw_memset(pwlanhdr->addr1, 0, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, mac, ETH_ALEN); + + /* Use the device address for BSSID field. */ + _rtw_memcpy(pwlanhdr->addr3, mac, ETH_ALEN); + + SetSeqNum(pwlanhdr, 0); + set_frame_sub_type(fctrl, WIFI_PROBERSP); + + pktlen = sizeof(struct rtw_ieee80211_hdr_3addr); + pframe += pktlen; + + + /* timestamp will be inserted by hardware */ + pframe += 8; + pktlen += 8; + + /* beacon interval: 2 bytes */ + _rtw_memcpy(pframe, (unsigned char *) &beacon_interval, 2); + pframe += 2; + pktlen += 2; + + /* capability info: 2 bytes */ + /* ESS and IBSS bits must be 0 (defined in the 3.1.2.1.1 of WiFi Direct Spec) */ + capInfo |= cap_ShortPremble; + capInfo |= cap_ShortSlot; + + _rtw_memcpy(pframe, (unsigned char *) &capInfo, 2); + pframe += 2; + pktlen += 2; + + + /* SSID */ + pframe = rtw_set_ie(pframe, _SSID_IE_, 7, pwdinfo->p2p_wildcard_ssid, &pktlen); + + /* supported rates... */ + /* Use the OFDM rate in the P2P probe response frame. ( 6(B), 9(B), 12, 18, 24, 36, 48, 54 ) */ + pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, 8, pwdinfo->support_rate, &pktlen); + + /* DS parameter set */ + pframe = rtw_set_ie(pframe, _DSSET_IE_, 1, (unsigned char *)&pwdinfo->listen_channel, &pktlen); + +#ifdef CONFIG_IOCTL_CFG80211 + if (pwdinfo->driver_interface == DRIVER_CFG80211) { + if (pmlmepriv->wps_probe_resp_ie != NULL && pmlmepriv->p2p_probe_resp_ie != NULL) { + /* WPS IE */ + _rtw_memcpy(pframe, pmlmepriv->wps_probe_resp_ie, pmlmepriv->wps_probe_resp_ie_len); + pktlen += pmlmepriv->wps_probe_resp_ie_len; + pframe += pmlmepriv->wps_probe_resp_ie_len; + + /* P2P IE */ + _rtw_memcpy(pframe, pmlmepriv->p2p_probe_resp_ie, pmlmepriv->p2p_probe_resp_ie_len); + pktlen += pmlmepriv->p2p_probe_resp_ie_len; + pframe += pmlmepriv->p2p_probe_resp_ie_len; + } + } else +#endif /* CONFIG_IOCTL_CFG80211 */ + { + + /* Todo: WPS IE */ + /* Noted by Albert 20100907 */ + /* According to the WPS specification, all the WPS attribute is presented by Big Endian. */ + + wpsielen = 0; + /* WPS OUI */ + *(u32 *)(wpsie) = cpu_to_be32(WPSOUI); + wpsielen += 4; + + /* WPS version */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_VER1); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001); + wpsielen += 2; + + /* Value: */ + wpsie[wpsielen++] = WPS_VERSION_1; /* Version 1.0 */ + + /* WiFi Simple Config State */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_SIMPLE_CONF_STATE); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001); + wpsielen += 2; + + /* Value: */ + wpsie[wpsielen++] = WPS_WSC_STATE_NOT_CONFIG; /* Not Configured. */ + + /* Response Type */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_RESP_TYPE); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001); + wpsielen += 2; + + /* Value: */ + wpsie[wpsielen++] = WPS_RESPONSE_TYPE_8021X; + + /* UUID-E */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_UUID_E); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(0x0010); + wpsielen += 2; + + /* Value: */ + if (pwdinfo->external_uuid == 0) { + _rtw_memset(wpsie + wpsielen, 0x0, 16); + _rtw_memcpy(wpsie + wpsielen, mac, ETH_ALEN); + } else + _rtw_memcpy(wpsie + wpsielen, pwdinfo->uuid, 0x10); + wpsielen += 0x10; + + /* Manufacturer */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_MANUFACTURER); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(0x0007); + wpsielen += 2; + + /* Value: */ + _rtw_memcpy(wpsie + wpsielen, "Realtek", 7); + wpsielen += 7; + + /* Model Name */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_MODEL_NAME); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(0x0006); + wpsielen += 2; + + /* Value: */ + _rtw_memcpy(wpsie + wpsielen, "8192CU", 6); + wpsielen += 6; + + /* Model Number */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_MODEL_NUMBER); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001); + wpsielen += 2; + + /* Value: */ + wpsie[wpsielen++] = 0x31; /* character 1 */ + + /* Serial Number */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_SERIAL_NUMBER); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(ETH_ALEN); + wpsielen += 2; + + /* Value: */ + _rtw_memcpy(wpsie + wpsielen, "123456" , ETH_ALEN); + wpsielen += ETH_ALEN; + + /* Primary Device Type */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_PRIMARY_DEV_TYPE); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(0x0008); + wpsielen += 2; + + /* Value: */ + /* Category ID */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_PDT_CID_MULIT_MEDIA); + wpsielen += 2; + + /* OUI */ + *(u32 *)(wpsie + wpsielen) = cpu_to_be32(WPSOUI); + wpsielen += 4; + + /* Sub Category ID */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_PDT_SCID_MEDIA_SERVER); + wpsielen += 2; + + /* Device Name */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_DEVICE_NAME); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(pwdinfo->device_name_len); + wpsielen += 2; + + /* Value: */ + _rtw_memcpy(wpsie + wpsielen, pwdinfo->device_name, pwdinfo->device_name_len); + wpsielen += pwdinfo->device_name_len; + + /* Config Method */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_CONF_METHOD); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(0x0002); + wpsielen += 2; + + /* Value: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(pwdinfo->supported_wps_cm); + wpsielen += 2; + + + pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, wpsielen, (unsigned char *) wpsie, &pktlen); + + + p2pielen = build_probe_resp_p2p_ie(pwdinfo, pframe); + pframe += p2pielen; + pktlen += p2pielen; + } + +#ifdef CONFIG_WFD + wfdielen = rtw_append_probe_resp_wfd_ie(padapter, pframe); + pframe += wfdielen; + pktlen += wfdielen; +#endif + + *pLength = pktlen; + +#if 0 + /* printf dbg msg */ + dbgbufLen = pktlen; + RTW_INFO("======> DBG MSG FOR CONSTRAUCT P2P Probe Rsp\n"); + + for (index = 0; index < dbgbufLen; index++) + printk("%x ", *(dbgbuf + index)); + + printk("\n"); + RTW_INFO("<====== DBG MSG FOR CONSTRAUCT P2P Probe Rsp\n"); +#endif +} +static void rtw_hal_construct_P2PNegoRsp(_adapter *padapter, u8 *pframe, u32 *pLength) +{ + struct p2p_channels *ch_list = &(adapter_to_rfctl(padapter)->channel_list); + unsigned char category = RTW_WLAN_CATEGORY_PUBLIC; + u8 action = P2P_PUB_ACTION_ACTION; + u32 p2poui = cpu_to_be32(P2POUI); + u8 oui_subtype = P2P_GO_NEGO_RESP; + u8 wpsie[255] = { 0x00 }, p2pie[255] = { 0x00 }; + u8 p2pielen = 0, i; + uint wpsielen = 0; + u16 wps_devicepassword_id = 0x0000; + uint wps_devicepassword_id_len = 0; + u8 channel_cnt_24g = 0, channel_cnt_5gl = 0, channel_cnt_5gh; + u16 len_channellist_attr = 0; + u32 pktlen; + u8 dialogToken = 0; + + /* struct xmit_frame *pmgntframe; */ + /* struct pkt_attrib *pattrib; */ + /* unsigned char *pframe; */ + struct rtw_ieee80211_hdr *pwlanhdr; + unsigned short *fctrl; + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + /* WLAN_BSSID_EX *cur_network = &(pmlmeinfo->network); */ + +#ifdef CONFIG_WFD + u32 wfdielen = 0; +#endif + + /* for debug */ + u8 *dbgbuf = pframe; + u8 dbgbufLen = 0, index = 0; + + RTW_INFO("%s\n", __FUNCTION__); + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + fctrl = &(pwlanhdr->frame_ctl); + *(fctrl) = 0; + + /* RA, filled by FW */ + _rtw_memset(pwlanhdr->addr1, 0, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, adapter_mac_addr(padapter), ETH_ALEN); + + SetSeqNum(pwlanhdr, 0); + set_frame_sub_type(pframe, WIFI_ACTION); + + pktlen = sizeof(struct rtw_ieee80211_hdr_3addr); + pframe += pktlen; + + pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pktlen)); + pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pktlen)); + pframe = rtw_set_fixed_ie(pframe, 4, (unsigned char *) &(p2poui), &(pktlen)); + pframe = rtw_set_fixed_ie(pframe, 1, &(oui_subtype), &(pktlen)); + + /* dialog token, filled by FW */ + pframe = rtw_set_fixed_ie(pframe, 1, &(dialogToken), &(pktlen)); + + _rtw_memset(wpsie, 0x00, 255); + wpsielen = 0; + + /* WPS Section */ + wpsielen = 0; + /* WPS OUI */ + *(u32 *)(wpsie) = cpu_to_be32(WPSOUI); + wpsielen += 4; + + /* WPS version */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_VER1); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001); + wpsielen += 2; + + /* Value: */ + wpsie[wpsielen++] = WPS_VERSION_1; /* Version 1.0 */ + + /* Device Password ID */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_DEVICE_PWID); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(0x0002); + wpsielen += 2; + + /* Value: */ + if (wps_devicepassword_id == WPS_DPID_USER_SPEC) + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_DPID_REGISTRAR_SPEC); + else if (wps_devicepassword_id == WPS_DPID_REGISTRAR_SPEC) + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_DPID_USER_SPEC); + else + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_DPID_PBC); + wpsielen += 2; + + pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, wpsielen, (unsigned char *) wpsie, &pktlen); + + + /* P2P IE Section. */ + + /* P2P OUI */ + p2pielen = 0; + p2pie[p2pielen++] = 0x50; + p2pie[p2pielen++] = 0x6F; + p2pie[p2pielen++] = 0x9A; + p2pie[p2pielen++] = 0x09; /* WFA P2P v1.0 */ + + /* Commented by Albert 20100908 */ + /* According to the P2P Specification, the group negoitation response frame should contain 9 P2P attributes */ + /* 1. Status */ + /* 2. P2P Capability */ + /* 3. Group Owner Intent */ + /* 4. Configuration Timeout */ + /* 5. Operating Channel */ + /* 6. Intended P2P Interface Address */ + /* 7. Channel List */ + /* 8. Device Info */ + /* 9. Group ID ( Only GO ) */ + + + /* ToDo: */ + + /* P2P Status */ + /* Type: */ + p2pie[p2pielen++] = P2P_ATTR_STATUS; + + /* Length: */ + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(0x0001); + p2pielen += 2; + + /* Value, filled by FW */ + p2pie[p2pielen++] = 1; + + /* P2P Capability */ + /* Type: */ + p2pie[p2pielen++] = P2P_ATTR_CAPABILITY; + + /* Length: */ + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(0x0002); + p2pielen += 2; + + /* Value: */ + /* Device Capability Bitmap, 1 byte */ + + if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_CLIENT)) { + /* Commented by Albert 2011/03/08 */ + /* According to the P2P specification */ + /* if the sending device will be client, the P2P Capability should be reserved of group negotation response frame */ + p2pie[p2pielen++] = 0; + } else { + /* Be group owner or meet the error case */ + p2pie[p2pielen++] = DMP_P2P_DEVCAP_SUPPORT; + } + + /* Group Capability Bitmap, 1 byte */ + if (pwdinfo->persistent_supported) + p2pie[p2pielen++] = P2P_GRPCAP_CROSS_CONN | P2P_GRPCAP_PERSISTENT_GROUP; + else + p2pie[p2pielen++] = P2P_GRPCAP_CROSS_CONN; + + /* Group Owner Intent */ + /* Type: */ + p2pie[p2pielen++] = P2P_ATTR_GO_INTENT; + + /* Length: */ + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(0x0001); + p2pielen += 2; + + /* Value: */ + if (pwdinfo->peer_intent & 0x01) { + /* Peer's tie breaker bit is 1, our tie breaker bit should be 0 */ + p2pie[p2pielen++] = (pwdinfo->intent << 1); + } else { + /* Peer's tie breaker bit is 0, our tie breaker bit should be 1 */ + p2pie[p2pielen++] = ((pwdinfo->intent << 1) | BIT(0)); + } + + + /* Configuration Timeout */ + /* Type: */ + p2pie[p2pielen++] = P2P_ATTR_CONF_TIMEOUT; + + /* Length: */ + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(0x0002); + p2pielen += 2; + + /* Value: */ + p2pie[p2pielen++] = 200; /* 2 seconds needed to be the P2P GO */ + p2pie[p2pielen++] = 200; /* 2 seconds needed to be the P2P Client */ + + /* Operating Channel */ + /* Type: */ + p2pie[p2pielen++] = P2P_ATTR_OPERATING_CH; + + /* Length: */ + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(0x0005); + p2pielen += 2; + + /* Value: */ + /* Country String */ + p2pie[p2pielen++] = 'X'; + p2pie[p2pielen++] = 'X'; + + /* The third byte should be set to 0x04. */ + /* Described in the "Operating Channel Attribute" section. */ + p2pie[p2pielen++] = 0x04; + + /* Operating Class */ + if (pwdinfo->operating_channel <= 14) { + /* Operating Class */ + p2pie[p2pielen++] = 0x51; + } else if ((pwdinfo->operating_channel >= 36) && (pwdinfo->operating_channel <= 48)) { + /* Operating Class */ + p2pie[p2pielen++] = 0x73; + } else { + /* Operating Class */ + p2pie[p2pielen++] = 0x7c; + } + + /* Channel Number */ + p2pie[p2pielen++] = pwdinfo->operating_channel; /* operating channel number */ + + /* Intended P2P Interface Address */ + /* Type: */ + p2pie[p2pielen++] = P2P_ATTR_INTENDED_IF_ADDR; + + /* Length: */ + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(ETH_ALEN); + p2pielen += 2; + + /* Value: */ + _rtw_memcpy(p2pie + p2pielen, adapter_mac_addr(padapter), ETH_ALEN); + p2pielen += ETH_ALEN; + + /* Channel List */ + /* Type: */ + p2pie[p2pielen++] = P2P_ATTR_CH_LIST; + + /* Country String(3) */ + /* + ( Operating Class (1) + Number of Channels(1) ) * Operation Classes (?) */ + /* + number of channels in all classes */ + len_channellist_attr = 3 + + (1 + 1) * (u16)ch_list->reg_classes + + get_reg_classes_full_count(ch_list); + +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_buddy_check_fwstate(padapter, WIFI_ASOC_STATE)) + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(5 + 1); + else + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(len_channellist_attr); + +#else + + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(len_channellist_attr); + +#endif + p2pielen += 2; + + /* Value: */ + /* Country String */ + p2pie[p2pielen++] = 'X'; + p2pie[p2pielen++] = 'X'; + + /* The third byte should be set to 0x04. */ + /* Described in the "Operating Channel Attribute" section. */ + p2pie[p2pielen++] = 0x04; + + /* Channel Entry List */ + +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_check_status(padapter, MI_LINKED)) { + u8 union_ch = rtw_mi_get_union_chan(padapter); + + /* Operating Class */ + if (union_ch > 14) { + if (union_ch >= 149) + p2pie[p2pielen++] = 0x7c; + else + p2pie[p2pielen++] = 0x73; + } else + p2pie[p2pielen++] = 0x51; + + + /* Number of Channels */ + /* Just support 1 channel and this channel is AP's channel */ + p2pie[p2pielen++] = 1; + + /* Channel List */ + p2pie[p2pielen++] = union_ch; + } else +#endif /* CONFIG_CONCURRENT_MODE */ + { + int i, j; + for (j = 0; j < ch_list->reg_classes; j++) { + /* Operating Class */ + p2pie[p2pielen++] = ch_list->reg_class[j].reg_class; + + /* Number of Channels */ + p2pie[p2pielen++] = ch_list->reg_class[j].channels; + + /* Channel List */ + for (i = 0; i < ch_list->reg_class[j].channels; i++) + p2pie[p2pielen++] = ch_list->reg_class[j].channel[i]; + } + } + + /* Device Info */ + /* Type: */ + p2pie[p2pielen++] = P2P_ATTR_DEVICE_INFO; + + /* Length: */ + /* 21->P2P Device Address (6bytes) + Config Methods (2bytes) + Primary Device Type (8bytes) */ + /* + NumofSecondDevType (1byte) + WPS Device Name ID field (2bytes) + WPS Device Name Len field (2bytes) */ + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(21 + pwdinfo->device_name_len); + p2pielen += 2; + + /* Value: */ + /* P2P Device Address */ + _rtw_memcpy(p2pie + p2pielen, adapter_mac_addr(padapter), ETH_ALEN); + p2pielen += ETH_ALEN; + + /* Config Method */ + /* This field should be big endian. Noted by P2P specification. */ + + *(u16 *)(p2pie + p2pielen) = cpu_to_be16(pwdinfo->supported_wps_cm); + + p2pielen += 2; + + /* Primary Device Type */ + /* Category ID */ + *(u16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_PDT_CID_MULIT_MEDIA); + p2pielen += 2; + + /* OUI */ + *(u32 *)(p2pie + p2pielen) = cpu_to_be32(WPSOUI); + p2pielen += 4; + + /* Sub Category ID */ + *(u16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_PDT_SCID_MEDIA_SERVER); + p2pielen += 2; + + /* Number of Secondary Device Types */ + p2pie[p2pielen++] = 0x00; /* No Secondary Device Type List */ + + /* Device Name */ + /* Type: */ + *(u16 *)(p2pie + p2pielen) = cpu_to_be16(WPS_ATTR_DEVICE_NAME); + p2pielen += 2; + + /* Length: */ + *(u16 *)(p2pie + p2pielen) = cpu_to_be16(pwdinfo->device_name_len); + p2pielen += 2; + + /* Value: */ + _rtw_memcpy(p2pie + p2pielen, pwdinfo->device_name , pwdinfo->device_name_len); + p2pielen += pwdinfo->device_name_len; + + if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO)) { + /* Group ID Attribute */ + /* Type: */ + p2pie[p2pielen++] = P2P_ATTR_GROUP_ID; + + /* Length: */ + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(ETH_ALEN + pwdinfo->nego_ssidlen); + p2pielen += 2; + + /* Value: */ + /* p2P Device Address */ + _rtw_memcpy(p2pie + p2pielen , pwdinfo->device_addr, ETH_ALEN); + p2pielen += ETH_ALEN; + + /* SSID */ + _rtw_memcpy(p2pie + p2pielen, pwdinfo->nego_ssid, pwdinfo->nego_ssidlen); + p2pielen += pwdinfo->nego_ssidlen; + + } + + pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, p2pielen, (unsigned char *) p2pie, &pktlen); + +#ifdef CONFIG_WFD + wfdielen = build_nego_resp_wfd_ie(pwdinfo, pframe); + pframe += wfdielen; + pktlen += wfdielen; +#endif + + *pLength = pktlen; +#if 0 + /* printf dbg msg */ + dbgbufLen = pktlen; + RTW_INFO("======> DBG MSG FOR CONSTRAUCT Nego Rsp\n"); + + for (index = 0; index < dbgbufLen; index++) + printk("%x ", *(dbgbuf + index)); + + printk("\n"); + RTW_INFO("<====== DBG MSG FOR CONSTRAUCT Nego Rsp\n"); +#endif +} + +static void rtw_hal_construct_P2PInviteRsp(_adapter *padapter, u8 *pframe, u32 *pLength) +{ + unsigned char category = RTW_WLAN_CATEGORY_PUBLIC; + u8 action = P2P_PUB_ACTION_ACTION; + u32 p2poui = cpu_to_be32(P2POUI); + u8 oui_subtype = P2P_INVIT_RESP; + u8 p2pie[255] = { 0x00 }; + u8 p2pielen = 0, i; + u8 channel_cnt_24g = 0, channel_cnt_5gl = 0, channel_cnt_5gh = 0; + u16 len_channellist_attr = 0; + u32 pktlen; + u8 dialogToken = 0; +#ifdef CONFIG_WFD + u32 wfdielen = 0; +#endif + + /* struct xmit_frame *pmgntframe; */ + /* struct pkt_attrib *pattrib; */ + /* unsigned char *pframe; */ + struct rtw_ieee80211_hdr *pwlanhdr; + unsigned short *fctrl; + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + /* for debug */ + u8 *dbgbuf = pframe; + u8 dbgbufLen = 0, index = 0; + + + RTW_INFO("%s\n", __FUNCTION__); + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + fctrl = &(pwlanhdr->frame_ctl); + *(fctrl) = 0; + + /* RA fill by FW */ + _rtw_memset(pwlanhdr->addr1, 0, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, adapter_mac_addr(padapter), ETH_ALEN); + + /* BSSID fill by FW */ + _rtw_memset(pwlanhdr->addr3, 0, ETH_ALEN); + + SetSeqNum(pwlanhdr, 0); + set_frame_sub_type(pframe, WIFI_ACTION); + + pframe += sizeof(struct rtw_ieee80211_hdr_3addr); + pktlen = sizeof(struct rtw_ieee80211_hdr_3addr); + + pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pktlen)); + pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pktlen)); + pframe = rtw_set_fixed_ie(pframe, 4, (unsigned char *) &(p2poui), &(pktlen)); + pframe = rtw_set_fixed_ie(pframe, 1, &(oui_subtype), &(pktlen)); + + /* dialog token, filled by FW */ + pframe = rtw_set_fixed_ie(pframe, 1, &(dialogToken), &(pktlen)); + + /* P2P IE Section. */ + + /* P2P OUI */ + p2pielen = 0; + p2pie[p2pielen++] = 0x50; + p2pie[p2pielen++] = 0x6F; + p2pie[p2pielen++] = 0x9A; + p2pie[p2pielen++] = 0x09; /* WFA P2P v1.0 */ + + /* Commented by Albert 20101005 */ + /* According to the P2P Specification, the P2P Invitation response frame should contain 5 P2P attributes */ + /* 1. Status */ + /* 2. Configuration Timeout */ + /* 3. Operating Channel ( Only GO ) */ + /* 4. P2P Group BSSID ( Only GO ) */ + /* 5. Channel List */ + + /* P2P Status */ + /* Type: */ + p2pie[p2pielen++] = P2P_ATTR_STATUS; + + /* Length: */ + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(0x0001); + p2pielen += 2; + + /* Value: filled by FW, defult value is FAIL INFO UNAVAILABLE */ + p2pie[p2pielen++] = P2P_STATUS_FAIL_INFO_UNAVAILABLE; + + /* Configuration Timeout */ + /* Type: */ + p2pie[p2pielen++] = P2P_ATTR_CONF_TIMEOUT; + + /* Length: */ + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(0x0002); + p2pielen += 2; + + /* Value: */ + p2pie[p2pielen++] = 200; /* 2 seconds needed to be the P2P GO */ + p2pie[p2pielen++] = 200; /* 2 seconds needed to be the P2P Client */ + + /* due to defult value is FAIL INFO UNAVAILABLE, so the following IE is not needed */ +#if 0 + if (status_code == P2P_STATUS_SUCCESS) { + struct p2p_channels *ch_list = &(adapter_to_rfctl(padapter)->channel_list); + + if (rtw_p2p_chk_role(pwdinfo, P2P_ROLE_GO)) { + /* The P2P Invitation request frame asks this Wi-Fi device to be the P2P GO */ + /* In this case, the P2P Invitation response frame should carry the two more P2P attributes. */ + /* First one is operating channel attribute. */ + /* Second one is P2P Group BSSID attribute. */ + + /* Operating Channel */ + /* Type: */ + p2pie[p2pielen++] = P2P_ATTR_OPERATING_CH; + + /* Length: */ + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(0x0005); + p2pielen += 2; + + /* Value: */ + /* Country String */ + p2pie[p2pielen++] = 'X'; + p2pie[p2pielen++] = 'X'; + + /* The third byte should be set to 0x04. */ + /* Described in the "Operating Channel Attribute" section. */ + p2pie[p2pielen++] = 0x04; + + /* Operating Class */ + p2pie[p2pielen++] = 0x51; /* Copy from SD7 */ + + /* Channel Number */ + p2pie[p2pielen++] = pwdinfo->operating_channel; /* operating channel number */ + + + /* P2P Group BSSID */ + /* Type: */ + p2pie[p2pielen++] = P2P_ATTR_GROUP_BSSID; + + /* Length: */ + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(ETH_ALEN); + p2pielen += 2; + + /* Value: */ + /* P2P Device Address for GO */ + _rtw_memcpy(p2pie + p2pielen, adapter_mac_addr(padapter), ETH_ALEN); + p2pielen += ETH_ALEN; + + } + + /* Channel List */ + /* Type: */ + p2pie[p2pielen++] = P2P_ATTR_CH_LIST; + + /* Length: */ + /* Country String(3) */ + /* + ( Operating Class (1) + Number of Channels(1) ) * Operation Classes (?) */ + /* + number of channels in all classes */ + len_channellist_attr = 3 + + (1 + 1) * (u16)ch_list->reg_classes + + get_reg_classes_full_count(ch_list); + +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_check_status(padapter, MI_LINKED)) + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(5 + 1); + else + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(len_channellist_attr); + +#else + + *(u16 *)(p2pie + p2pielen) = cpu_to_le16(len_channellist_attr); + +#endif + p2pielen += 2; + + /* Value: */ + /* Country String */ + p2pie[p2pielen++] = 'X'; + p2pie[p2pielen++] = 'X'; + + /* The third byte should be set to 0x04. */ + /* Described in the "Operating Channel Attribute" section. */ + p2pie[p2pielen++] = 0x04; + + /* Channel Entry List */ +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_check_status(padapter, MI_LINKED)) { + u8 union_ch = rtw_mi_get_union_chan(padapter); + + /* Operating Class */ + if (union_ch > 14) { + if (union_ch >= 149) + p2pie[p2pielen++] = 0x7c; + else + p2pie[p2pielen++] = 0x73; + + } else + p2pie[p2pielen++] = 0x51; + + + /* Number of Channels */ + /* Just support 1 channel and this channel is AP's channel */ + p2pie[p2pielen++] = 1; + + /* Channel List */ + p2pie[p2pielen++] = union_ch; + } else +#endif /* CONFIG_CONCURRENT_MODE */ + { + int i, j; + for (j = 0; j < ch_list->reg_classes; j++) { + /* Operating Class */ + p2pie[p2pielen++] = ch_list->reg_class[j].reg_class; + + /* Number of Channels */ + p2pie[p2pielen++] = ch_list->reg_class[j].channels; + + /* Channel List */ + for (i = 0; i < ch_list->reg_class[j].channels; i++) + p2pie[p2pielen++] = ch_list->reg_class[j].channel[i]; + } + } + } +#endif + + pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, p2pielen, (unsigned char *) p2pie, &pktlen); + +#ifdef CONFIG_WFD + wfdielen = build_invitation_resp_wfd_ie(pwdinfo, pframe); + pframe += wfdielen; + pktlen += wfdielen; +#endif + + *pLength = pktlen; + +#if 0 + /* printf dbg msg */ + dbgbufLen = pktlen; + RTW_INFO("======> DBG MSG FOR CONSTRAUCT Invite Rsp\n"); + + for (index = 0; index < dbgbufLen; index++) + printk("%x ", *(dbgbuf + index)); + + printk("\n"); + RTW_INFO("<====== DBG MSG FOR CONSTRAUCT Invite Rsp\n"); +#endif +} + + +static void rtw_hal_construct_P2PProvisionDisRsp(_adapter *padapter, u8 *pframe, u32 *pLength) +{ + unsigned char category = RTW_WLAN_CATEGORY_PUBLIC; + u8 action = P2P_PUB_ACTION_ACTION; + u8 dialogToken = 0; + u32 p2poui = cpu_to_be32(P2POUI); + u8 oui_subtype = P2P_PROVISION_DISC_RESP; + u8 wpsie[100] = { 0x00 }; + u8 wpsielen = 0; + u32 pktlen; +#ifdef CONFIG_WFD + u32 wfdielen = 0; +#endif + + /* struct xmit_frame *pmgntframe; */ + /* struct pkt_attrib *pattrib; */ + /* unsigned char *pframe; */ + struct rtw_ieee80211_hdr *pwlanhdr; + unsigned short *fctrl; + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + + /* for debug */ + u8 *dbgbuf = pframe; + u8 dbgbufLen = 0, index = 0; + + RTW_INFO("%s\n", __FUNCTION__); + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + fctrl = &(pwlanhdr->frame_ctl); + *(fctrl) = 0; + + /* RA filled by FW */ + _rtw_memset(pwlanhdr->addr1, 0, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, adapter_mac_addr(padapter), ETH_ALEN); + + SetSeqNum(pwlanhdr, 0); + set_frame_sub_type(pframe, WIFI_ACTION); + + pframe += sizeof(struct rtw_ieee80211_hdr_3addr); + pktlen = sizeof(struct rtw_ieee80211_hdr_3addr); + + pframe = rtw_set_fixed_ie(pframe, 1, &(category), &(pktlen)); + pframe = rtw_set_fixed_ie(pframe, 1, &(action), &(pktlen)); + pframe = rtw_set_fixed_ie(pframe, 4, (unsigned char *) &(p2poui), &(pktlen)); + pframe = rtw_set_fixed_ie(pframe, 1, &(oui_subtype), &(pktlen)); + /* dialog token, filled by FW */ + pframe = rtw_set_fixed_ie(pframe, 1, &(dialogToken), &(pktlen)); + + wpsielen = 0; + /* WPS OUI */ + /* *(u32*) ( wpsie ) = cpu_to_be32( WPSOUI ); */ + RTW_PUT_BE32(wpsie, WPSOUI); + wpsielen += 4; + +#if 0 + /* WPS version */ + /* Type: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(WPS_ATTR_VER1); + wpsielen += 2; + + /* Length: */ + *(u16 *)(wpsie + wpsielen) = cpu_to_be16(0x0001); + wpsielen += 2; + + /* Value: */ + wpsie[wpsielen++] = WPS_VERSION_1; /* Version 1.0 */ +#endif + + /* Config Method */ + /* Type: */ + /* *(u16*) ( wpsie + wpsielen ) = cpu_to_be16( WPS_ATTR_CONF_METHOD ); */ + RTW_PUT_BE16(wpsie + wpsielen, WPS_ATTR_CONF_METHOD); + wpsielen += 2; + + /* Length: */ + /* *(u16*) ( wpsie + wpsielen ) = cpu_to_be16( 0x0002 ); */ + RTW_PUT_BE16(wpsie + wpsielen, 0x0002); + wpsielen += 2; + + /* Value: filled by FW, default value is PBC */ + /* *(u16*) ( wpsie + wpsielen ) = cpu_to_be16( config_method ); */ + RTW_PUT_BE16(wpsie + wpsielen, WPS_CM_PUSH_BUTTON); + wpsielen += 2; + + pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_, wpsielen, (unsigned char *) wpsie, &pktlen); + +#ifdef CONFIG_WFD + wfdielen = build_provdisc_resp_wfd_ie(pwdinfo, pframe); + pframe += wfdielen; + pktlen += wfdielen; +#endif + + *pLength = pktlen; + + /* printf dbg msg */ +#if 0 + dbgbufLen = pktlen; + RTW_INFO("======> DBG MSG FOR CONSTRAUCT ProvisionDis Rsp\n"); + + for (index = 0; index < dbgbufLen; index++) + printk("%x ", *(dbgbuf + index)); + + printk("\n"); + RTW_INFO("<====== DBG MSG FOR CONSTRAUCT ProvisionDis Rsp\n"); +#endif +} + +u8 rtw_hal_set_FwP2PRsvdPage_cmd(_adapter *adapter, PRSVDPAGE_LOC rsvdpageloc) +{ + u8 u1H2CP2PRsvdPageParm[H2C_P2PRSVDPAGE_LOC_LEN] = {0}; + struct hal_ops *pHalFunc = &adapter->hal_func; + u8 ret = _FAIL; + + RTW_INFO("P2PRsvdPageLoc: P2PBeacon=%d P2PProbeRsp=%d NegoRsp=%d InviteRsp=%d PDRsp=%d\n", + rsvdpageloc->LocP2PBeacon, rsvdpageloc->LocP2PProbeRsp, + rsvdpageloc->LocNegoRsp, rsvdpageloc->LocInviteRsp, + rsvdpageloc->LocPDRsp); + + SET_H2CCMD_RSVDPAGE_LOC_P2P_BCN(u1H2CP2PRsvdPageParm, rsvdpageloc->LocProbeRsp); + SET_H2CCMD_RSVDPAGE_LOC_P2P_PROBE_RSP(u1H2CP2PRsvdPageParm, rsvdpageloc->LocPsPoll); + SET_H2CCMD_RSVDPAGE_LOC_P2P_NEGO_RSP(u1H2CP2PRsvdPageParm, rsvdpageloc->LocNullData); + SET_H2CCMD_RSVDPAGE_LOC_P2P_INVITE_RSP(u1H2CP2PRsvdPageParm, rsvdpageloc->LocQosNull); + SET_H2CCMD_RSVDPAGE_LOC_P2P_PD_RSP(u1H2CP2PRsvdPageParm, rsvdpageloc->LocBTQosNull); + + /* FillH2CCmd8723B(padapter, H2C_8723B_P2P_OFFLOAD_RSVD_PAGE, H2C_P2PRSVDPAGE_LOC_LEN, u1H2CP2PRsvdPageParm); */ + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_P2P_OFFLOAD_RSVD_PAGE, + H2C_P2PRSVDPAGE_LOC_LEN, + u1H2CP2PRsvdPageParm); + + return ret; +} + +u8 rtw_hal_set_p2p_wowlan_offload_cmd(_adapter *adapter) +{ + + u8 offload_cmd[H2C_P2P_OFFLOAD_LEN] = {0}; + struct wifidirect_info *pwdinfo = &(adapter->wdinfo); + struct P2P_WoWlan_Offload_t *p2p_wowlan_offload = (struct P2P_WoWlan_Offload_t *)offload_cmd; + struct hal_ops *pHalFunc = &adapter->hal_func; + u8 ret = _FAIL; + + _rtw_memset(p2p_wowlan_offload, 0 , sizeof(struct P2P_WoWlan_Offload_t)); + RTW_INFO("%s\n", __func__); + switch (pwdinfo->role) { + case P2P_ROLE_DEVICE: + RTW_INFO("P2P_ROLE_DEVICE\n"); + p2p_wowlan_offload->role = 0; + break; + case P2P_ROLE_CLIENT: + RTW_INFO("P2P_ROLE_CLIENT\n"); + p2p_wowlan_offload->role = 1; + break; + case P2P_ROLE_GO: + RTW_INFO("P2P_ROLE_GO\n"); + p2p_wowlan_offload->role = 2; + break; + default: + RTW_INFO("P2P_ROLE_DISABLE\n"); + break; + } + p2p_wowlan_offload->Wps_Config[0] = pwdinfo->supported_wps_cm >> 8; + p2p_wowlan_offload->Wps_Config[1] = pwdinfo->supported_wps_cm; + offload_cmd = (u8 *)p2p_wowlan_offload; + RTW_INFO("p2p_wowlan_offload: %x:%x:%x\n", offload_cmd[0], offload_cmd[1], offload_cmd[2]); + + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_P2P_OFFLOAD, + H2C_P2P_OFFLOAD_LEN, + offload_cmd); + return ret; + + /* FillH2CCmd8723B(adapter, H2C_8723B_P2P_OFFLOAD, sizeof(struct P2P_WoWlan_Offload_t), (u8 *)p2p_wowlan_offload); */ +} +#endif /* CONFIG_P2P_WOWLAN */ + +void rtw_hal_construct_beacon(_adapter *padapter, + u8 *pframe, u32 *pLength) +{ + struct rtw_ieee80211_hdr *pwlanhdr; + u16 *fctrl; + u32 pktlen; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + WLAN_BSSID_EX *cur_network = &(pmlmeinfo->network); + u8 bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + + + /* RTW_INFO("%s\n", __FUNCTION__); */ + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + fctrl = &(pwlanhdr->frame_ctl); + *(fctrl) = 0; + + _rtw_memcpy(pwlanhdr->addr1, bc_addr, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, get_my_bssid(cur_network), ETH_ALEN); + + SetSeqNum(pwlanhdr, 0/*pmlmeext->mgnt_seq*/); + /* pmlmeext->mgnt_seq++; */ + set_frame_sub_type(pframe, WIFI_BEACON); + + pframe += sizeof(struct rtw_ieee80211_hdr_3addr); + pktlen = sizeof(struct rtw_ieee80211_hdr_3addr); + + /* timestamp will be inserted by hardware */ + pframe += 8; + pktlen += 8; + + /* beacon interval: 2 bytes */ + _rtw_memcpy(pframe, (unsigned char *)(rtw_get_beacon_interval_from_ie(cur_network->IEs)), 2); + + pframe += 2; + pktlen += 2; + +#if 0 + /* capability info: 2 bytes */ + _rtw_memcpy(pframe, (unsigned char *)(rtw_get_capability_from_ie(cur_network->IEs)), 2); + + pframe += 2; + pktlen += 2; + + if ((pmlmeinfo->state & 0x03) == WIFI_FW_AP_STATE) { + /* RTW_INFO("ie len=%d\n", cur_network->IELength); */ + pktlen += cur_network->IELength - sizeof(NDIS_802_11_FIXED_IEs); + _rtw_memcpy(pframe, cur_network->IEs + sizeof(NDIS_802_11_FIXED_IEs), pktlen); + + goto _ConstructBeacon; + } + + /* below for ad-hoc mode */ + + /* SSID */ + pframe = rtw_set_ie(pframe, _SSID_IE_, cur_network->Ssid.SsidLength, cur_network->Ssid.Ssid, &pktlen); + + /* supported rates... */ + rate_len = rtw_get_rateset_len(cur_network->SupportedRates); + pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_, ((rate_len > 8) ? 8 : rate_len), cur_network->SupportedRates, &pktlen); + + /* DS parameter set */ + pframe = rtw_set_ie(pframe, _DSSET_IE_, 1, (unsigned char *)&(cur_network->Configuration.DSConfig), &pktlen); + + if ((pmlmeinfo->state & 0x03) == WIFI_FW_ADHOC_STATE) { + u32 ATIMWindow; + /* IBSS Parameter Set... */ + /* ATIMWindow = cur->Configuration.ATIMWindow; */ + ATIMWindow = 0; + pframe = rtw_set_ie(pframe, _IBSS_PARA_IE_, 2, (unsigned char *)(&ATIMWindow), &pktlen); + } + + + /* todo: ERP IE */ + + + /* EXTERNDED SUPPORTED RATE */ + if (rate_len > 8) + pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_, (rate_len - 8), (cur_network->SupportedRates + 8), &pktlen); + + /* todo:HT for adhoc */ + +_ConstructBeacon: +#endif + + if ((pktlen + TXDESC_SIZE) > MAX_BEACON_LEN) { + RTW_ERR("beacon frame too large ,len(%d,%d)\n", + (pktlen + TXDESC_SIZE), MAX_BEACON_LEN); + rtw_warn_on(1); + return; + } + + *pLength = pktlen; + + /* RTW_INFO("%s bcn_sz=%d\n", __FUNCTION__, pktlen); */ + +} + +static void rtw_hal_construct_PSPoll(_adapter *padapter, + u8 *pframe, u32 *pLength) +{ + struct rtw_ieee80211_hdr *pwlanhdr; + u16 *fctrl; + u32 pktlen; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + + /* RTW_INFO("%s\n", __FUNCTION__); */ + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + /* Frame control. */ + fctrl = &(pwlanhdr->frame_ctl); + *(fctrl) = 0; + SetPwrMgt(fctrl); + set_frame_sub_type(pframe, WIFI_PSPOLL); + + /* AID. */ + set_duration(pframe, (pmlmeinfo->aid | 0xc000)); + + /* BSSID. */ + _rtw_memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + + /* TA. */ + _rtw_memcpy(pwlanhdr->addr2, adapter_mac_addr(padapter), ETH_ALEN); + + *pLength = 16; +} + + +#ifdef DBG_FW_DEBUG_MSG_PKT +void rtw_hal_construct_fw_dbg_msg_pkt( + PADAPTER padapter, + u8 *pframe, + u32 *plength) +{ + struct rtw_ieee80211_hdr *pwlanhdr; + u16 *fctrl; + u32 pktlen; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct wlan_network *cur_network = &pmlmepriv->cur_network; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + u8 bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + + + /* RTW_INFO("%s:%d\n", __FUNCTION__, bForcePowerSave); */ + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + fctrl = &pwlanhdr->frame_ctl; + *(fctrl) = 0; + + _rtw_memcpy(pwlanhdr->addr1, bc_addr, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + + SetSeqNum(pwlanhdr, 0); + + set_frame_sub_type(pframe, WIFI_DATA); + + pktlen = sizeof(struct rtw_ieee80211_hdr_3addr); + + *plength = pktlen; +} +#endif /*DBG_FW_DEBUG_MSG_PKT*/ + +void rtw_hal_construct_NullFunctionData( + PADAPTER padapter, + u8 *pframe, + u32 *pLength, + u8 bQoS, + u8 AC, + u8 bEosp, + u8 bForcePowerSave) +{ + struct rtw_ieee80211_hdr *pwlanhdr; + u16 *fctrl; + u32 pktlen; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct wlan_network *cur_network = &pmlmepriv->cur_network; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + u8 *sta_addr = NULL; + u8 bssid[ETH_ALEN] = {0}; + + /* RTW_INFO("%s:%d\n", __FUNCTION__, bForcePowerSave); */ + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + fctrl = &pwlanhdr->frame_ctl; + *(fctrl) = 0; + if (bForcePowerSave) + SetPwrMgt(fctrl); + + sta_addr = get_my_bssid(&pmlmeinfo->network); + if (NULL == sta_addr) { + _rtw_memcpy(bssid, adapter_mac_addr(padapter), ETH_ALEN); + sta_addr = bssid; + } + + switch (cur_network->network.InfrastructureMode) { + case Ndis802_11Infrastructure: + SetToDs(fctrl); + _rtw_memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, sta_addr, ETH_ALEN); + break; + case Ndis802_11APMode: + SetFrDs(fctrl); + _rtw_memcpy(pwlanhdr->addr1, sta_addr, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, adapter_mac_addr(padapter), ETH_ALEN); + break; + case Ndis802_11IBSS: + default: + _rtw_memcpy(pwlanhdr->addr1, sta_addr, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + break; + } + + SetSeqNum(pwlanhdr, 0); + set_duration(pwlanhdr, 0); + + if (bQoS == _TRUE) { + struct rtw_ieee80211_hdr_3addr_qos *pwlanqoshdr; + + set_frame_sub_type(pframe, WIFI_QOS_DATA_NULL); + + pwlanqoshdr = (struct rtw_ieee80211_hdr_3addr_qos *)pframe; + SetPriority(&pwlanqoshdr->qc, AC); + SetEOSP(&pwlanqoshdr->qc, bEosp); + + pktlen = sizeof(struct rtw_ieee80211_hdr_3addr_qos); + } else { + set_frame_sub_type(pframe, WIFI_DATA_NULL); + + pktlen = sizeof(struct rtw_ieee80211_hdr_3addr); + } + + *pLength = pktlen; +} + +void rtw_hal_construct_ProbeRsp(_adapter *padapter, u8 *pframe, u32 *pLength, + BOOLEAN bHideSSID) +{ + struct rtw_ieee80211_hdr *pwlanhdr; + u16 *fctrl; + u8 *mac, *bssid, *sta_addr; + u32 pktlen; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + WLAN_BSSID_EX *cur_network = &(pmlmeinfo->network); + + /*RTW_INFO("%s\n", __FUNCTION__);*/ + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + mac = adapter_mac_addr(padapter); + bssid = cur_network->MacAddress; + sta_addr = get_my_bssid(&pmlmeinfo->network); + + fctrl = &(pwlanhdr->frame_ctl); + *(fctrl) = 0; + _rtw_memcpy(pwlanhdr->addr1, sta_addr, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, mac, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, bssid, ETH_ALEN); + + SetSeqNum(pwlanhdr, 0); + set_frame_sub_type(fctrl, WIFI_PROBERSP); + + pktlen = sizeof(struct rtw_ieee80211_hdr_3addr); + pframe += pktlen; + + if (cur_network->IELength > MAX_IE_SZ) + return; + + _rtw_memcpy(pframe, cur_network->IEs, cur_network->IELength); + pframe += cur_network->IELength; + pktlen += cur_network->IELength; + + *pLength = pktlen; +} + +#ifdef CONFIG_WOWLAN +static void rtw_hal_append_tkip_mic(PADAPTER padapter, + u8 *pframe, u32 offset) +{ + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct rtw_ieee80211_hdr *pwlanhdr; + struct mic_data micdata; + struct sta_info *psta = NULL; + int res = 0; + + u8 *payload = (u8 *)(pframe + offset); + + u8 mic[8]; + u8 priority[4] = {0x0}; + u8 null_key[16] = {0x0}; + + RTW_INFO("%s(): Add MIC, offset: %d\n", __func__, offset); + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + psta = rtw_get_stainfo(&padapter->stapriv, + get_my_bssid(&(pmlmeinfo->network))); + if (psta != NULL) { + res = _rtw_memcmp(&psta->dot11tkiptxmickey.skey[0], + null_key, 16); + if (res == _TRUE) + RTW_INFO("%s(): STA dot11tkiptxmickey==0\n", __func__); + rtw_secmicsetkey(&micdata, &psta->dot11tkiptxmickey.skey[0]); + } + + rtw_secmicappend(&micdata, pwlanhdr->addr3, 6); /* DA */ + + rtw_secmicappend(&micdata, pwlanhdr->addr2, 6); /* SA */ + + priority[0] = 0; + + rtw_secmicappend(&micdata, &priority[0], 4); + + rtw_secmicappend(&micdata, payload, 36); /* payload length = 8 + 28 */ + + rtw_secgetmic(&micdata, &(mic[0])); + + payload += 36; + + _rtw_memcpy(payload, &(mic[0]), 8); +} +/* + * Description: + * Construct the ARP response packet to support ARP offload. + * */ +static void rtw_hal_construct_ARPRsp( + PADAPTER padapter, + u8 *pframe, + u32 *pLength, + u8 *pIPAddress +) +{ + struct rtw_ieee80211_hdr *pwlanhdr; + u16 *fctrl; + u32 pktlen; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct security_priv *psecuritypriv = &padapter->securitypriv; + static u8 ARPLLCHeader[8] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00, 0x08, 0x06}; + u8 *pARPRspPkt = pframe; + /* for TKIP Cal MIC */ + u8 *payload = pframe; + u8 EncryptionHeadOverhead = 0, arp_offset = 0; + /* RTW_INFO("%s:%d\n", __FUNCTION__, bForcePowerSave); */ + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + fctrl = &pwlanhdr->frame_ctl; + *(fctrl) = 0; + + /* ------------------------------------------------------------------------- */ + /* MAC Header. */ + /* ------------------------------------------------------------------------- */ + SetFrameType(fctrl, WIFI_DATA); + /* set_frame_sub_type(fctrl, 0); */ + SetToDs(fctrl); + _rtw_memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + + SetSeqNum(pwlanhdr, 0); + set_duration(pwlanhdr, 0); + /* SET_80211_HDR_FRAME_CONTROL(pARPRspPkt, 0); */ + /* SET_80211_HDR_TYPE_AND_SUBTYPE(pARPRspPkt, Type_Data); */ + /* SET_80211_HDR_TO_DS(pARPRspPkt, 1); */ + /* SET_80211_HDR_ADDRESS1(pARPRspPkt, pMgntInfo->Bssid); */ + /* SET_80211_HDR_ADDRESS2(pARPRspPkt, Adapter->CurrentAddress); */ + /* SET_80211_HDR_ADDRESS3(pARPRspPkt, pMgntInfo->Bssid); */ + + /* SET_80211_HDR_DURATION(pARPRspPkt, 0); */ + /* SET_80211_HDR_FRAGMENT_SEQUENCE(pARPRspPkt, 0); */ +#ifdef CONFIG_WAPI_SUPPORT + *pLength = sMacHdrLng; +#else + *pLength = 24; +#endif + switch (psecuritypriv->dot11PrivacyAlgrthm) { + case _WEP40_: + case _WEP104_: + EncryptionHeadOverhead = 4; + break; + case _TKIP_: + EncryptionHeadOverhead = 8; + break; + case _AES_: + EncryptionHeadOverhead = 8; + break; +#ifdef CONFIG_WAPI_SUPPORT + case _SMS4_: + EncryptionHeadOverhead = 18; + break; +#endif + default: + EncryptionHeadOverhead = 0; + } + + if (EncryptionHeadOverhead > 0) { + _rtw_memset(&(pframe[*pLength]), 0, EncryptionHeadOverhead); + *pLength += EncryptionHeadOverhead; + /* SET_80211_HDR_WEP(pARPRspPkt, 1); */ /* Suggested by CCW. */ + SetPrivacy(fctrl); + } + + /* ------------------------------------------------------------------------- */ + /* Frame Body. */ + /* ------------------------------------------------------------------------- */ + arp_offset = *pLength; + pARPRspPkt = (u8 *)(pframe + arp_offset); + payload = pARPRspPkt; /* Get Payload pointer */ + /* LLC header */ + _rtw_memcpy(pARPRspPkt, ARPLLCHeader, 8); + *pLength += 8; + + /* ARP element */ + pARPRspPkt += 8; + SET_ARP_HTYPE(pARPRspPkt, 1); + SET_ARP_PTYPE(pARPRspPkt, ETH_P_IP); /* IP protocol */ + SET_ARP_HLEN(pARPRspPkt, ETH_ALEN); + SET_ARP_PLEN(pARPRspPkt, RTW_IP_ADDR_LEN); + SET_ARP_OPER(pARPRspPkt, 2); /* ARP response */ + SET_ARP_SENDER_MAC_ADDR(pARPRspPkt, adapter_mac_addr(padapter)); + SET_ARP_SENDER_IP_ADDR(pARPRspPkt, pIPAddress); +#ifdef CONFIG_ARP_KEEP_ALIVE + if (!is_zero_mac_addr(pmlmepriv->gw_mac_addr)) { + SET_ARP_TARGET_MAC_ADDR(pARPRspPkt, pmlmepriv->gw_mac_addr); + SET_ARP_TARGET_IP_ADDR(pARPRspPkt, pmlmepriv->gw_ip); + } else +#endif + { + SET_ARP_TARGET_MAC_ADDR(pARPRspPkt, + get_my_bssid(&(pmlmeinfo->network))); + SET_ARP_TARGET_IP_ADDR(pARPRspPkt, + pIPAddress); + RTW_INFO("%s Target Mac Addr:" MAC_FMT "\n", __FUNCTION__, + MAC_ARG(get_my_bssid(&(pmlmeinfo->network)))); + RTW_INFO("%s Target IP Addr" IP_FMT "\n", __FUNCTION__, + IP_ARG(pIPAddress)); + } + + *pLength += 28; + + if (psecuritypriv->dot11PrivacyAlgrthm == _TKIP_) { + if (IS_HARDWARE_TYPE_8188E(padapter) || + IS_HARDWARE_TYPE_8812(padapter)) { + rtw_hal_append_tkip_mic(padapter, pframe, arp_offset); + } + *pLength += 8; + } +} +#ifdef CONFIG_WOW_KEEP_ALIVE_PATTERN +/* + * Description: + * Construct the Keep Alive packet to support specific Keep Alive packet. + * */ +static void rtw_hal_construct_keepalive( PADAPTER padapter, + u8 *pframe, + u32 *pLength +){ + struct rtw_ieee80211_hdr *pwlanhdr; + u16 *fctrl; + u32 pktlen; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct security_priv *psecuritypriv = &padapter->securitypriv; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + static u8 LLCHeader[6] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00}; + u8 *pKeepAlivePkt = pframe; + /* for TKIP Cal MIC */ + u8 *payload = pframe; + u8 EncryptionHeadOverhead = 0, frame_offset = 0; + int i; + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + fctrl = &pwlanhdr->frame_ctl; + *(fctrl) = 0; + + RTW_INFO("%s======>\n", __func__); + + + /* ------------------------------------------------------------------------- */ + /* MAC Header. */ + /* ------------------------------------------------------------------------- */ + SetFrameType(fctrl, WIFI_DATA); + /* set_frame_sub_type(fctrl, 0); */ + SetToDs(fctrl); + _rtw_memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, pwrpriv->keep_alive_pattern+6, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3,pwrpriv->keep_alive_pattern, ETH_ALEN); + + SetSeqNum(pwlanhdr, 0); + set_duration(pwlanhdr, 0); + +#ifdef CONFIG_WAPI_SUPPORT + *pLength = sMacHdrLng; +#else + *pLength = 24; +#endif + switch (psecuritypriv->dot11PrivacyAlgrthm) { + case _WEP40_: + case _WEP104_: + EncryptionHeadOverhead = 4; + break; + case _TKIP_: + EncryptionHeadOverhead = 8; + break; + case _AES_: + EncryptionHeadOverhead = 8; + break; +#ifdef CONFIG_WAPI_SUPPORT + case _SMS4_: + EncryptionHeadOverhead = 18; + break; +#endif + default: + EncryptionHeadOverhead = 0; + } + + if (EncryptionHeadOverhead > 0) { + _rtw_memset(&(pframe[*pLength]), 0, EncryptionHeadOverhead); + *pLength += EncryptionHeadOverhead; + /* SET_80211_HDR_WEP(pARPRspPkt, 1); */ /* Suggested by CCW. */ + SetPrivacy(fctrl); + } + + /* ------------------------------------------------------------------------- */ + /* Frame Body. */ + /* ------------------------------------------------------------------------- */ + frame_offset = *pLength; + pKeepAlivePkt = (u8 *)(pframe + frame_offset); + payload = pKeepAlivePkt; /* Get Payload pointer */ + /* LLC header */ + _rtw_memcpy(pKeepAlivePkt, LLCHeader, 6); + *pLength += 6; + + /*From protocol type*/ + pKeepAlivePkt+=6; + + _rtw_memcpy(pKeepAlivePkt,pwrpriv->keep_alive_pattern+12,pwrpriv->keep_alive_pattern_len-12); + + *pLength+=pwrpriv->keep_alive_pattern_len-12; + + if (psecuritypriv->dot11PrivacyAlgrthm == _TKIP_) { + *pLength += 8; + } + + /* for debug + for (i=0; i< (*pLength) ;i++) { + RTW_INFO("KA_Pkt[0x%x]=x%0x", i,pKeepAlivePkt[i]); + if((i%8) == 7) + RTW_INFO("\n"); + } + */ + + RTW_INFO("%s <======\n", __func__); +} + +#endif/*CONFIG_WOW_KEEP_ALIVE_PATTERN*/ + +#ifdef CONFIG_IPV6 +/* + * Description: Neighbor Discovery Offload. + */ +static void rtw_hal_construct_na_message(_adapter *padapter, + u8 *pframe, u32 *pLength) +{ + struct rtw_ieee80211_hdr *pwlanhdr = NULL; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info; + struct security_priv *psecuritypriv = &padapter->securitypriv; + + u32 pktlen = 0; + u16 *fctrl = NULL; + + u8 ns_hdr[8] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00, 0x86, 0xDD}; + u8 ipv6_info[4] = {0x60, 0x00, 0x00, 0x00}; + u8 ipv6_contx[4] = {0x00, 0x20, 0x3a, 0xff}; + u8 icmpv6_hdr[8] = {0x88, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00}; + u8 val8 = 0; + + u8 *p_na_msg = pframe; + /* for TKIP Cal MIC */ + u8 *payload = pframe; + u8 EncryptionHeadOverhead = 0, na_msg_offset = 0; + /* RTW_INFO("%s:%d\n", __FUNCTION__, bForcePowerSave); */ + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + fctrl = &pwlanhdr->frame_ctl; + *(fctrl) = 0; + + /* ------------------------------------------------------------------------- */ + /* MAC Header. */ + /* ------------------------------------------------------------------------- */ + SetFrameType(fctrl, WIFI_DATA); + SetToDs(fctrl); + _rtw_memcpy(pwlanhdr->addr1, + get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, + adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, + get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + + SetSeqNum(pwlanhdr, 0); + set_duration(pwlanhdr, 0); + +#ifdef CONFIG_WAPI_SUPPORT + *pLength = sMacHdrLng; +#else + *pLength = 24; +#endif + switch (psecuritypriv->dot11PrivacyAlgrthm) { + case _WEP40_: + case _WEP104_: + EncryptionHeadOverhead = 4; + break; + case _TKIP_: + EncryptionHeadOverhead = 8; + break; + case _AES_: + EncryptionHeadOverhead = 8; + break; +#ifdef CONFIG_WAPI_SUPPORT + case _SMS4_: + EncryptionHeadOverhead = 18; + break; +#endif + default: + EncryptionHeadOverhead = 0; + } + + if (EncryptionHeadOverhead > 0) { + _rtw_memset(&(pframe[*pLength]), 0, EncryptionHeadOverhead); + *pLength += EncryptionHeadOverhead; + /* SET_80211_HDR_WEP(pARPRspPkt, 1); */ /* Suggested by CCW. */ + SetPrivacy(fctrl); + } + + /* ------------------------------------------------------------------------- */ + /* Frame Body. */ + /* ------------------------------------------------------------------------- */ + na_msg_offset = *pLength; + p_na_msg = (u8 *)(pframe + na_msg_offset); + payload = p_na_msg; /* Get Payload pointer */ + + /* LLC header */ + val8 = sizeof(ns_hdr); + _rtw_memcpy(p_na_msg, ns_hdr, val8); + *pLength += val8; + p_na_msg += val8; + + /* IPv6 Header */ + /* 1 . Information (4 bytes): 0x60 0x00 0x00 0x00 */ + val8 = sizeof(ipv6_info); + _rtw_memcpy(p_na_msg, ipv6_info, val8); + *pLength += val8; + p_na_msg += val8; + + /* 2 . playload : 0x00 0x20 , NextProt : 0x3a (ICMPv6) HopLim : 0xff */ + val8 = sizeof(ipv6_contx); + _rtw_memcpy(p_na_msg, ipv6_contx, val8); + *pLength += val8; + p_na_msg += val8; + + /* 3 . SA : 16 bytes , DA : 16 bytes ( Fw will filled ) */ + _rtw_memset(&(p_na_msg[*pLength]), 0, 32); + *pLength += 32; + p_na_msg += 32; + + /* ICMPv6 */ + /* 1. Type : 0x88 (NA) + * 2. Code : 0x00 + * 3. ChechSum : 0x00 0x00 (RSvd) + * 4. NAFlag: 0x60 0x00 0x00 0x00 ( Solicited , Override) + */ + val8 = sizeof(icmpv6_hdr); + _rtw_memcpy(p_na_msg, icmpv6_hdr, val8); + *pLength += val8; + p_na_msg += val8; + + /* TA: 16 bytes*/ + _rtw_memset(&(p_na_msg[*pLength]), 0, 16); + *pLength += 16; + p_na_msg += 16; + + /* ICMPv6 Target Link Layer Address */ + p_na_msg[0] = 0x02; /* type */ + p_na_msg[1] = 0x01; /* len 1 unit of 8 octes */ + *pLength += 2; + p_na_msg += 2; + + _rtw_memset(&(p_na_msg[*pLength]), 0, 6); + *pLength += 6; + p_na_msg += 6; + + if (psecuritypriv->dot11PrivacyAlgrthm == _TKIP_) { + if (IS_HARDWARE_TYPE_8188E(padapter) || + IS_HARDWARE_TYPE_8812(padapter)) { + rtw_hal_append_tkip_mic(padapter, pframe, + na_msg_offset); + } + *pLength += 8; + } +} +/* + * Description: Neighbor Discovery Protocol Information. + */ +static void rtw_hal_construct_ndp_info(_adapter *padapter, + u8 *pframe, u32 *pLength) +{ + struct mlme_ext_priv *pmlmeext = NULL; + struct mlme_ext_info *pmlmeinfo = NULL; + struct rtw_ndp_info ndp_info; + u8 *pndp_info = pframe; + u8 len = sizeof(struct rtw_ndp_info); + + RTW_INFO("%s: len: %d\n", __func__, len); + + pmlmeext = &padapter->mlmeextpriv; + pmlmeinfo = &pmlmeext->mlmext_info; + + _rtw_memset(pframe, 0, len); + _rtw_memset(&ndp_info, 0, len); + + ndp_info.enable = 1; + ndp_info.check_remote_ip = 0; + ndp_info.num_of_target_ip = 1; + + _rtw_memcpy(&ndp_info.target_link_addr, adapter_mac_addr(padapter), + ETH_ALEN); + _rtw_memcpy(&ndp_info.target_ipv6_addr, pmlmeinfo->ip6_addr, + RTW_IPv6_ADDR_LEN); + + _rtw_memcpy(pndp_info, &ndp_info, len); +} +#endif /* CONFIG_IPV6 */ + +#ifdef CONFIG_PNO_SUPPORT +static void rtw_hal_construct_ProbeReq(_adapter *padapter, u8 *pframe, + u32 *pLength, pno_ssid_t *ssid) +{ + struct rtw_ieee80211_hdr *pwlanhdr; + u16 *fctrl; + u32 pktlen; + unsigned char *mac; + unsigned char bssrate[NumRates]; + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + int bssrate_len = 0; + u8 bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + mac = adapter_mac_addr(padapter); + + fctrl = &(pwlanhdr->frame_ctl); + *(fctrl) = 0; + + _rtw_memcpy(pwlanhdr->addr1, bc_addr, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, bc_addr, ETH_ALEN); + + _rtw_memcpy(pwlanhdr->addr2, mac, ETH_ALEN); + + SetSeqNum(pwlanhdr, 0); + set_frame_sub_type(pframe, WIFI_PROBEREQ); + + pktlen = sizeof(struct rtw_ieee80211_hdr_3addr); + pframe += pktlen; + + if (ssid == NULL) + pframe = rtw_set_ie(pframe, _SSID_IE_, 0, NULL, &pktlen); + else { + /* RTW_INFO("%s len:%d\n", ssid->SSID, ssid->SSID_len); */ + pframe = rtw_set_ie(pframe, _SSID_IE_, ssid->SSID_len, ssid->SSID, &pktlen); + } + + get_rate_set(padapter, bssrate, &bssrate_len); + + if (bssrate_len > 8) { + pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_ , 8, bssrate, &pktlen); + pframe = rtw_set_ie(pframe, _EXT_SUPPORTEDRATES_IE_ , (bssrate_len - 8), (bssrate + 8), &pktlen); + } else + pframe = rtw_set_ie(pframe, _SUPPORTEDRATES_IE_ , bssrate_len , bssrate, &pktlen); + + *pLength = pktlen; +} + +static void rtw_hal_construct_PNO_info(_adapter *padapter, + u8 *pframe, u32 *pLength) +{ + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter); + int i; + + u8 *pPnoInfoPkt = pframe; + pPnoInfoPkt = (u8 *)(pframe + *pLength); + _rtw_memcpy(pPnoInfoPkt, &pwrctl->pnlo_info->ssid_num, 1); + + pPnoInfoPkt += 1; + _rtw_memcpy(pPnoInfoPkt, &pwrctl->pnlo_info->hidden_ssid_num, 1); + + pPnoInfoPkt += 3; + _rtw_memcpy(pPnoInfoPkt, &pwrctl->pnlo_info->fast_scan_period, 1); + + pPnoInfoPkt += 4; + _rtw_memcpy(pPnoInfoPkt, &pwrctl->pnlo_info->fast_scan_iterations, 4); + + pPnoInfoPkt += 4; + _rtw_memcpy(pPnoInfoPkt, &pwrctl->pnlo_info->slow_scan_period, 4); + + pPnoInfoPkt += 4; + _rtw_memcpy(pPnoInfoPkt, &pwrctl->pnlo_info->ssid_length, MAX_PNO_LIST_COUNT); + + pPnoInfoPkt += MAX_PNO_LIST_COUNT; + _rtw_memcpy(pPnoInfoPkt, &pwrctl->pnlo_info->ssid_cipher_info, MAX_PNO_LIST_COUNT); + + pPnoInfoPkt += MAX_PNO_LIST_COUNT; + _rtw_memcpy(pPnoInfoPkt, &pwrctl->pnlo_info->ssid_channel_info, MAX_PNO_LIST_COUNT); + + pPnoInfoPkt += MAX_PNO_LIST_COUNT; + _rtw_memcpy(pPnoInfoPkt, &pwrctl->pnlo_info->loc_probe_req, MAX_HIDDEN_AP); + + pPnoInfoPkt += MAX_HIDDEN_AP; + + /* + SSID is located at 128th Byte in NLO info Page + */ + + *pLength += 128; + pPnoInfoPkt = pframe + 128; + + for (i = 0; i < pwrctl->pnlo_info->ssid_num ; i++) { + _rtw_memcpy(pPnoInfoPkt, &pwrctl->pno_ssid_list->node[i].SSID, + pwrctl->pnlo_info->ssid_length[i]); + *pLength += WLAN_SSID_MAXLEN; + pPnoInfoPkt += WLAN_SSID_MAXLEN; + } +} + +static void rtw_hal_construct_ssid_list(_adapter *padapter, + u8 *pframe, u32 *pLength) +{ + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter); + u8 *pSSIDListPkt = pframe; + int i; + + pSSIDListPkt = (u8 *)(pframe + *pLength); + + for (i = 0; i < pwrctl->pnlo_info->ssid_num ; i++) { + _rtw_memcpy(pSSIDListPkt, &pwrctl->pno_ssid_list->node[i].SSID, + pwrctl->pnlo_info->ssid_length[i]); + + *pLength += WLAN_SSID_MAXLEN; + pSSIDListPkt += WLAN_SSID_MAXLEN; + } +} + +static void rtw_hal_construct_scan_info(_adapter *padapter, + u8 *pframe, u32 *pLength) +{ + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(padapter); + u8 *pScanInfoPkt = pframe; + int i; + + pScanInfoPkt = (u8 *)(pframe + *pLength); + + _rtw_memcpy(pScanInfoPkt, &pwrctl->pscan_info->channel_num, 1); + + *pLength += 1; + pScanInfoPkt += 1; + _rtw_memcpy(pScanInfoPkt, &pwrctl->pscan_info->orig_ch, 1); + + + *pLength += 1; + pScanInfoPkt += 1; + _rtw_memcpy(pScanInfoPkt, &pwrctl->pscan_info->orig_bw, 1); + + + *pLength += 1; + pScanInfoPkt += 1; + _rtw_memcpy(pScanInfoPkt, &pwrctl->pscan_info->orig_40_offset, 1); + + *pLength += 1; + pScanInfoPkt += 1; + _rtw_memcpy(pScanInfoPkt, &pwrctl->pscan_info->orig_80_offset, 1); + + *pLength += 1; + pScanInfoPkt += 1; + _rtw_memcpy(pScanInfoPkt, &pwrctl->pscan_info->periodScan, 1); + + *pLength += 1; + pScanInfoPkt += 1; + _rtw_memcpy(pScanInfoPkt, &pwrctl->pscan_info->period_scan_time, 1); + + *pLength += 1; + pScanInfoPkt += 1; + _rtw_memcpy(pScanInfoPkt, &pwrctl->pscan_info->enableRFE, 1); + + *pLength += 1; + pScanInfoPkt += 1; + _rtw_memcpy(pScanInfoPkt, &pwrctl->pscan_info->rfe_type, 8); + + *pLength += 8; + pScanInfoPkt += 8; + + for (i = 0 ; i < MAX_SCAN_LIST_COUNT ; i++) { + _rtw_memcpy(pScanInfoPkt, + &pwrctl->pscan_info->ssid_channel_info[i], 4); + *pLength += 4; + pScanInfoPkt += 4; + } +} +#endif /* CONFIG_PNO_SUPPORT */ + +#ifdef CONFIG_WAR_OFFLOAD +#ifdef CONFIG_OFFLOAD_MDNS_V4 + +/* + * Description: + * Construct the MDNS V4 response packet to support MDNS offload. + * + */ +static void rtw_hal_construct_mdns_rsp_v4( + PADAPTER padapter, + u8 *pframe, + u32 *pLength, + u8 *pIPAddress +) +{ + struct rtw_ieee80211_hdr *pwlanhdr; + u16 *fctrl; + u32 pktlen; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct wlan_network *cur_network = &pmlmepriv->cur_network; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct security_priv *psecuritypriv = &padapter->securitypriv; + static u8 ICMPLLCHeader[8] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00, 0x08, 0x00}; + u8 mulicast_ipv4_addr[4] = {0xe0, 0x00, 0x00, 0xfb}; + u8 mulicast_mac_addr_for_mdns[6] = {0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb}; + u8 *pMdnsRspPkt = pframe; + /* for TKIP Cal MIC */ + u8 EncryptionHeadOverhead = 0, mdns_offset = 0; + + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj); + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + fctrl = &pwlanhdr->frame_ctl; + *(fctrl) = 0; + + /* ------------------------------------------------------------------------- */ + /* MAC Header. */ + /* ------------------------------------------------------------------------- */ + SetFrameType(fctrl, WIFI_DATA); + /* set_frame_sub_type(fctrl, 0); */ + SetToDs(fctrl); + //_rtw_memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, mulicast_mac_addr_for_mdns, ETH_ALEN ); + + SetSeqNum(pwlanhdr, 0); + set_duration(pwlanhdr, 0); + +#ifdef CONFIG_WAPI_SUPPORT + *pLength = sMacHdrLng; +#else + *pLength = 24; +#endif + switch (psecuritypriv->dot11PrivacyAlgrthm) { + case _WEP40_: + case _WEP104_: + EncryptionHeadOverhead = 4; + break; + case _TKIP_: + EncryptionHeadOverhead = 8; + break; + case _AES_: + EncryptionHeadOverhead = 8; + break; +#ifdef CONFIG_WAPI_SUPPORT + case _SMS4_: + EncryptionHeadOverhead = 18; + break; +#endif + default: + EncryptionHeadOverhead = 0; + } + + if (EncryptionHeadOverhead > 0) { + _rtw_memset(&(pframe[*pLength]), 0, EncryptionHeadOverhead); + *pLength += EncryptionHeadOverhead; + /* SET_80211_HDR_WEP(pARPRspPkt, 1); */ /* Suggested by CCW. */ + SetPrivacy(fctrl); + } + + /* ------------------------------------------------------------------------- */ + /* Frame Body. */ + /* ------------------------------------------------------------------------- */ + mdns_offset = *pLength; + pMdnsRspPkt = (u8 *)(pframe + mdns_offset); + /* LLC header */ + _rtw_memcpy(pMdnsRspPkt, ICMPLLCHeader, 8); + *pLength += 8; + + /* IP element */ + pMdnsRspPkt += 8; + SET_IPHDR_VERSION(pMdnsRspPkt, 0x45); + SET_IPHDR_DSCP(pMdnsRspPkt, 0); + SET_IPHDR_TOTAL_LEN(pMdnsRspPkt, 0); // filled by fw + SET_IPHDR_IDENTIFIER(pMdnsRspPkt, 0); // filled by fw + SET_IPHDR_FLAGS(pMdnsRspPkt, 0x40); + SET_IPHDR_FRAG_OFFSET(pMdnsRspPkt, 0); + SET_IPHDR_TTL(pMdnsRspPkt, 0x40); + SET_IPHDR_PROTOCOL(pMdnsRspPkt, 0x11); // ICMP-UDP + SET_IPHDR_HDR_CHECKSUM(pMdnsRspPkt, 0); // filled by fw + SET_IPHDR_SRC_IP_ADDR(pMdnsRspPkt, pIPAddress); + SET_IPHDR_DST_IP_ADDR(pMdnsRspPkt, mulicast_ipv4_addr); // filled by fw + + *pLength += 20; + + if (psecuritypriv->dot11PrivacyAlgrthm == _TKIP_) { + if (IS_HARDWARE_TYPE_8188E(padapter) || + IS_HARDWARE_TYPE_8812(padapter)) { + rtw_hal_append_tkip_mic(padapter, pframe, mdns_offset); + } + *pLength += 8; + } + + /* UDP element */ + pMdnsRspPkt += 20; + SET_UDP_SRC_PORT(pMdnsRspPkt, 0xe914); // MDNS + SET_UDP_DST_PORT(pMdnsRspPkt, 0xe914); // MDNS + SET_UDP_LEN(pMdnsRspPkt, 0); // filled by fw + SET_UDP_CHECKSUM(pMdnsRspPkt, 0); // filled by fw + *pLength += 8; + + /* MDNS Header */ + pMdnsRspPkt += 8; + SET_MDNS_HDR_FLAG(pMdnsRspPkt, 0x84); + *pLength += 12; + +} + +#endif /* CONFIG_OFFLOAD_MDNS_V4 */ + +#ifdef CONFIG_OFFLOAD_MDNS_V6 + +/* + * Description: + * Construct the MDNS response V6 packet to support MDNS offload. + * + */ +static void rtw_hal_construct_mdns_rsp_v6( + PADAPTER padapter, + u8 *pframe, + u32 *pLength, + u8 *pIPAddress +) +{ + struct rtw_ieee80211_hdr *pwlanhdr; + u16 *fctrl; + u32 pktlen; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct wlan_network *cur_network = &pmlmepriv->cur_network; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct security_priv *psecuritypriv = &padapter->securitypriv; + static u8 ICMPLLCHeader[8] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00, 0x86, 0xDD}; + u8 mulicast_ipv6_addr[16] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb}; + u8 mulicast_mac_addr_for_mdns[6] = {0x33, 0x33, 0x00, 0x00, 0x00, 0xfb}; /* could be revise by fw */ + u8 *pMdnsRspPkt = pframe; + /* for TKIP Cal MIC */ + u8 EncryptionHeadOverhead = 0, mdns_offset = 0; + /* RTW_INFO("%s:%d\n", __FUNCTION__, bForcePowerSave); */ + + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj); + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + fctrl = &pwlanhdr->frame_ctl; + *(fctrl) = 0; + + /* ------------------------------------------------------------------------- */ + /* MAC Header. */ + /* ------------------------------------------------------------------------- */ + SetFrameType(fctrl, WIFI_DATA); + /* set_frame_sub_type(fctrl, 0); */ + SetToDs(fctrl); + //_rtw_memcpy(pwlanhdr->addr1, mulicast_mac_addr_for_mdns, ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr1, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr2, adapter_mac_addr(padapter), ETH_ALEN); + //_rtw_memcpy(pwlanhdr->addr3, get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + _rtw_memcpy(pwlanhdr->addr3, mulicast_mac_addr_for_mdns, ETH_ALEN); + + SetSeqNum(pwlanhdr, 0); + set_duration(pwlanhdr, 0); + +#ifdef CONFIG_WAPI_SUPPORT + *pLength = sMacHdrLng; +#else + *pLength = 24; +#endif + switch (psecuritypriv->dot11PrivacyAlgrthm) { + case _WEP40_: + case _WEP104_: + EncryptionHeadOverhead = 4; + break; + case _TKIP_: + EncryptionHeadOverhead = 8; + break; + case _AES_: + EncryptionHeadOverhead = 8; + break; +#ifdef CONFIG_WAPI_SUPPORT + case _SMS4_: + EncryptionHeadOverhead = 18; + break; +#endif + default: + EncryptionHeadOverhead = 0; + } + + if (EncryptionHeadOverhead > 0) { + _rtw_memset(&(pframe[*pLength]), 0, EncryptionHeadOverhead); + *pLength += EncryptionHeadOverhead; + SetPrivacy(fctrl); + } + + /* ------------------------------------------------------------------------- */ + /* Frame Body. */ + /* ------------------------------------------------------------------------- */ + mdns_offset = *pLength; + pMdnsRspPkt = (u8 *)(pframe + mdns_offset); + /* LLC header */ + _rtw_memcpy(pMdnsRspPkt, ICMPLLCHeader, 8); + *pLength += 8; + + /* ICMP element */ + pMdnsRspPkt += 8; + SET_IPHDRV6_VERSION(pMdnsRspPkt, 0x06); + SET_IPHDRV6_PAYLOAD_LENGTH(pMdnsRspPkt, 0); // filled by fw + SET_IPHDRV6_NEXT_HEADER(pMdnsRspPkt, 0x3A); + SET_IPHDRV6_HOP_LIMIT(pMdnsRspPkt, 0xFF); + SET_IPHDRV6_SRC_IP_ADDR(pMdnsRspPkt, pIPAddress); // filled by fw + SET_IPHDRV6_DST_IP_ADDR(pMdnsRspPkt, mulicast_ipv6_addr); // filled by fw + + *pLength += 40; + + if (psecuritypriv->dot11PrivacyAlgrthm == _TKIP_) { + if (IS_HARDWARE_TYPE_8188E(padapter) || + IS_HARDWARE_TYPE_8812(padapter)) { + rtw_hal_append_tkip_mic(padapter, pframe, mdns_offset); + } + *pLength += 8; + } + + /* UDP element */ + pMdnsRspPkt += 40; + SET_UDP_SRC_PORT(pMdnsRspPkt, 0xe914); // SNMP + SET_UDP_DST_PORT(pMdnsRspPkt, 0xe914); // SNMP + SET_UDP_LEN(pMdnsRspPkt, 0); // filled by fw + SET_UDP_CHECKSUM(pMdnsRspPkt, 0); // filled by fw + *pLength += 8; + + /* MDNS Header */ + pMdnsRspPkt += 8; + SET_MDNS_HDR_FLAG(pMdnsRspPkt, 0x84); + *pLength += 12; + +} + +#endif /* CONFIG_OFFLOAD_MDNS_V6 */ +#endif + +#ifdef CONFIG_GTK_OL +static void rtw_hal_construct_GTKRsp( + PADAPTER padapter, + u8 *pframe, + u32 *pLength +) +{ + struct rtw_ieee80211_hdr *pwlanhdr; + u16 *fctrl; + u32 pktlen; + struct mlme_priv *pmlmepriv = &padapter->mlmepriv; + struct wlan_network *cur_network = &pmlmepriv->cur_network; + struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct security_priv *psecuritypriv = &padapter->securitypriv; + static u8 LLCHeader[8] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8E}; + static u8 GTKbody_a[11] = {0x01, 0x03, 0x00, 0x5F, 0x02, 0x03, 0x12, 0x00, 0x10, 0x42, 0x0B}; + u8 *pGTKRspPkt = pframe; + u8 EncryptionHeadOverhead = 0; + /* RTW_INFO("%s:%d\n", __FUNCTION__, bForcePowerSave); */ + + pwlanhdr = (struct rtw_ieee80211_hdr *)pframe; + + fctrl = &pwlanhdr->frame_ctl; + *(fctrl) = 0; + + /* ------------------------------------------------------------------------- */ + /* MAC Header. */ + /* ------------------------------------------------------------------------- */ + SetFrameType(fctrl, WIFI_DATA); + /* set_frame_sub_type(fctrl, 0); */ + SetToDs(fctrl); + + _rtw_memcpy(pwlanhdr->addr1, + get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + + _rtw_memcpy(pwlanhdr->addr2, + adapter_mac_addr(padapter), ETH_ALEN); + + _rtw_memcpy(pwlanhdr->addr3, + get_my_bssid(&(pmlmeinfo->network)), ETH_ALEN); + + SetSeqNum(pwlanhdr, 0); + set_duration(pwlanhdr, 0); + +#ifdef CONFIG_WAPI_SUPPORT + *pLength = sMacHdrLng; +#else + *pLength = 24; +#endif /* CONFIG_WAPI_SUPPORT */ + + /* ------------------------------------------------------------------------- */ + /* Security Header: leave space for it if necessary. */ + /* ------------------------------------------------------------------------- */ + switch (psecuritypriv->dot11PrivacyAlgrthm) { + case _WEP40_: + case _WEP104_: + EncryptionHeadOverhead = 4; + break; + case _TKIP_: + EncryptionHeadOverhead = 8; + break; + case _AES_: + EncryptionHeadOverhead = 8; + break; +#ifdef CONFIG_WAPI_SUPPORT + case _SMS4_: + EncryptionHeadOverhead = 18; + break; +#endif /* CONFIG_WAPI_SUPPORT */ + default: + EncryptionHeadOverhead = 0; + } + + if (EncryptionHeadOverhead > 0) { + _rtw_memset(&(pframe[*pLength]), 0, EncryptionHeadOverhead); + *pLength += EncryptionHeadOverhead; + /* SET_80211_HDR_WEP(pGTKRspPkt, 1); */ /* Suggested by CCW. */ + /* GTK's privacy bit is done by FW */ + /* SetPrivacy(fctrl); */ + } + /* ------------------------------------------------------------------------- */ + /* Frame Body. */ + /* ------------------------------------------------------------------------- */ + pGTKRspPkt = (u8 *)(pframe + *pLength); + /* LLC header */ + _rtw_memcpy(pGTKRspPkt, LLCHeader, 8); + *pLength += 8; + + /* GTK element */ + pGTKRspPkt += 8; + + /* GTK frame body after LLC, part 1 */ + /* TKIP key_length = 32, AES key_length = 16 */ + if (psecuritypriv->dot118021XGrpPrivacy == _TKIP_) + GTKbody_a[8] = 0x20; + + /* GTK frame body after LLC, part 1 */ + _rtw_memcpy(pGTKRspPkt, GTKbody_a, 11); + *pLength += 11; + pGTKRspPkt += 11; + /* GTK frame body after LLC, part 2 */ + _rtw_memset(&(pframe[*pLength]), 0, 88); + *pLength += 88; + pGTKRspPkt += 88; + + if (psecuritypriv->dot118021XGrpPrivacy == _TKIP_) + *pLength += 8; +} +#endif /* CONFIG_GTK_OL */ + +#define PN_2_CCMPH(ch,key_id) ((ch) & 0x000000000000ffff) \ + | (((ch) & 0x0000ffffffff0000) << 16) \ + | (((key_id) << 30)) \ + | BIT(29) +static void rtw_hal_construct_remote_control_info(_adapter *adapter, + u8 *pframe, u32 *pLength) +{ + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + struct sta_priv *pstapriv = &adapter->stapriv; + struct security_priv *psecuritypriv = &adapter->securitypriv; + struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info; + struct sta_info *psta; + struct stainfo_rxcache *prxcache; + u8 cur_dot11rxiv[8], id = 0, tid_id = 0, i = 0; + size_t sz = 0, total = 0; + u64 ccmp_hdr = 0, tmp_key = 0; + + psta = rtw_get_stainfo(pstapriv, get_bssid(pmlmepriv)); + + if (psta == NULL) { + rtw_warn_on(1); + return; + } + + prxcache = &psta->sta_recvpriv.rxcache; + sz = sizeof(cur_dot11rxiv); + + /* 3 SEC IV * 1 page */ + rtw_get_sec_iv(adapter, cur_dot11rxiv, + get_my_bssid(&pmlmeinfo->network)); + + _rtw_memcpy(pframe, cur_dot11rxiv, sz); + *pLength += sz; + pframe += sz; + + _rtw_memset(&cur_dot11rxiv, 0, sz); + + if (psecuritypriv->ndisauthtype == Ndis802_11AuthModeWPA2PSK) { + id = psecuritypriv->dot118021XGrpKeyid; + tid_id = prxcache->last_tid; + REMOTE_INFO_CTRL_SET_VALD_EN(cur_dot11rxiv, 0xdd); + REMOTE_INFO_CTRL_SET_PTK_EN(cur_dot11rxiv, 1); + REMOTE_INFO_CTRL_SET_GTK_EN(cur_dot11rxiv, 1); + REMOTE_INFO_CTRL_SET_GTK_IDX(cur_dot11rxiv, id); + _rtw_memcpy(pframe, cur_dot11rxiv, sz); + *pLength += sz; + pframe += sz; + + _rtw_memcpy(pframe, prxcache->iv[tid_id], sz); + *pLength += sz; + pframe += sz; + + total = sizeof(psecuritypriv->iv_seq); + total /= sizeof(psecuritypriv->iv_seq[0]); + + for (i = 0 ; i < total ; i ++) { + ccmp_hdr = + le64_to_cpu(*(u64*)psecuritypriv->iv_seq[i]); + _rtw_memset(&cur_dot11rxiv, 0, sz); + if (ccmp_hdr != 0) { + tmp_key = i; + ccmp_hdr = PN_2_CCMPH(ccmp_hdr, tmp_key); + *(u64*)cur_dot11rxiv = cpu_to_le64(ccmp_hdr); + _rtw_memcpy(pframe, cur_dot11rxiv, sz); + } + *pLength += sz; + pframe += sz; + } + } +} + +static void rtw_hal_gate_bb(_adapter *adapter, bool stop) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + u8 i = 0, val8 = 0, empty = _FAIL; + + if (stop) { + /* checking TX queue status */ + for (i = 0 ; i < 5 ; i++) { + rtw_hal_get_hwreg(adapter, HW_VAR_CHK_MGQ_CPU_EMPTY, &empty); + if (empty) { + break; + } else { + RTW_WARN("%s: MGQ_CPU is busy(%d)!\n", + __func__, i); + rtw_mdelay_os(10); + } + } + + if (val8 == 5) + RTW_ERR("%s: Polling MGQ_CPU empty fail!\n", __func__); + + /* Pause TX*/ + pwrpriv->wowlan_txpause_status = rtw_read8(adapter, REG_TXPAUSE); + rtw_write8(adapter, REG_TXPAUSE, 0xff); + val8 = rtw_read8(adapter, REG_SYS_FUNC_EN); + val8 &= ~BIT(0); + rtw_write8(adapter, REG_SYS_FUNC_EN, val8); + RTW_INFO("%s: BB gated: 0x%02x, store TXPAUSE: %02x\n", + __func__, + rtw_read8(adapter, REG_SYS_FUNC_EN), + pwrpriv->wowlan_txpause_status); + } else { + val8 = rtw_read8(adapter, REG_SYS_FUNC_EN); + val8 |= BIT(0); + rtw_write8(adapter, REG_SYS_FUNC_EN, val8); + RTW_INFO("%s: BB release: 0x%02x, recover TXPAUSE:%02x\n", + __func__, rtw_read8(adapter, REG_SYS_FUNC_EN), + pwrpriv->wowlan_txpause_status); + /* release TX*/ + rtw_write8(adapter, REG_TXPAUSE, pwrpriv->wowlan_txpause_status); + } +} + +static u8 rtw_hal_wow_pattern_generate(_adapter *adapter, u8 idx, struct rtl_wow_pattern *pwow_pattern) +{ + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + u8 *pattern; + u8 len = 0; + u8 *mask; + + u8 mask_hw[MAX_WKFM_SIZE] = {0}; + u8 content[MAX_WKFM_PATTERN_SIZE] = {0}; + u8 broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + u8 multicast_addr1[2] = {0x33, 0x33}; + u8 multicast_addr2[3] = {0x01, 0x00, 0x5e}; + u8 mask_len = 0; + u8 mac_addr[ETH_ALEN] = {0}; + u16 count = 0; + int i; + + if (pwrctl->wowlan_pattern_idx > MAX_WKFM_CAM_NUM) { + RTW_INFO("%s pattern_idx is more than MAX_FMC_NUM: %d\n", + __func__, MAX_WKFM_CAM_NUM); + return _FAIL; + } + + pattern = pwrctl->patterns[idx].content; + len = pwrctl->patterns[idx].len; + mask = pwrctl->patterns[idx].mask; + + _rtw_memcpy(mac_addr, adapter_mac_addr(adapter), ETH_ALEN); + _rtw_memset(pwow_pattern, 0, sizeof(struct rtl_wow_pattern)); + + mask_len = DIV_ROUND_UP(len, 8); + + /* 1. setup A1 table */ + if (memcmp(pattern, broadcast_addr, ETH_ALEN) == 0) + pwow_pattern->type = PATTERN_BROADCAST; + else if (memcmp(pattern, multicast_addr1, 2) == 0) + pwow_pattern->type = PATTERN_MULTICAST; + else if (memcmp(pattern, multicast_addr2, 3) == 0) + pwow_pattern->type = PATTERN_MULTICAST; + else if (memcmp(pattern, mac_addr, ETH_ALEN) == 0) + pwow_pattern->type = PATTERN_UNICAST; + else + pwow_pattern->type = PATTERN_INVALID; + + /* translate mask from os to mask for hw */ + + /****************************************************************************** + * pattern from OS uses 'ethenet frame', like this: + + | 6 | 6 | 2 | 20 | Variable | 4 | + |--------+--------+------+-----------+------------+-----| + | 802.3 Mac Header | IP Header | TCP Packet | FCS | + | DA | SA | Type | + + * BUT, packet catched by our HW is in '802.11 frame', begin from LLC, + + | 24 or 30 | 6 | 2 | 20 | Variable | 4 | + |-------------------+--------+------+-----------+------------+-----| + | 802.11 MAC Header | LLC | IP Header | TCP Packet | FCS | + | Others | Tpye | + + * Therefore, we need translate mask_from_OS to mask_to_hw. + * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0, + * because new mask[0~5] means 'SA', but our HW packet begins from LLC, + * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match. + ******************************************************************************/ + /* Shift 6 bits */ + for (i = 0; i < mask_len - 1; i++) { + mask_hw[i] = mask[i] >> 6; + mask_hw[i] |= (mask[i + 1] & 0x3F) << 2; + } + + mask_hw[i] = (mask[i] >> 6) & 0x3F; + /* Set bit 0-5 to zero */ + mask_hw[0] &= 0xC0; + + for (i = 0; i < (MAX_WKFM_SIZE / 4); i++) { + pwow_pattern->mask[i] = mask_hw[i * 4]; + pwow_pattern->mask[i] |= (mask_hw[i * 4 + 1] << 8); + pwow_pattern->mask[i] |= (mask_hw[i * 4 + 2] << 16); + pwow_pattern->mask[i] |= (mask_hw[i * 4 + 3] << 24); + } + + /* To get the wake up pattern from the mask. + * We do not count first 12 bits which means + * DA[6] and SA[6] in the pattern to match HW design. */ + count = 0; + for (i = 12; i < len; i++) { + if ((mask[i / 8] >> (i % 8)) & 0x01) { + content[count] = pattern[i]; + count++; + } + } + + pwow_pattern->crc = rtw_calc_crc(content, count); + + if (pwow_pattern->crc != 0) { + if (pwow_pattern->type == PATTERN_INVALID) + pwow_pattern->type = PATTERN_VALID; + } + + return _SUCCESS; +} + +void rtw_dump_wow_pattern(void *sel, struct rtl_wow_pattern *pwow_pattern, u8 idx) +{ + int j; + + RTW_PRINT_SEL(sel, "=======WOW CAM-ID[%d]=======\n", idx); + RTW_PRINT_SEL(sel, "[WOW CAM] type:%d\n", pwow_pattern->type); + RTW_PRINT_SEL(sel, "[WOW CAM] crc:0x%04x\n", pwow_pattern->crc); + for (j = 0; j < 4; j++) + RTW_PRINT_SEL(sel, "[WOW CAM] Mask:0x%08x\n", pwow_pattern->mask[j]); +} +/*bit definition of pattern match format*/ +#define WOW_VALID_BIT BIT31 +#ifndef CONFIG_WOW_PATTERN_IN_TXFIFO +#define WOW_BC_BIT BIT26 +#define WOW_MC_BIT BIT25 +#define WOW_UC_BIT BIT24 +#else +#define WOW_BC_BIT BIT18 +#define WOW_UC_BIT BIT17 +#define WOW_MC_BIT BIT16 +#endif /*CONFIG_WOW_PATTERN_IN_TXFIFO*/ + +#ifndef CONFIG_WOW_PATTERN_HW_CAM +#ifndef CONFIG_WOW_PATTERN_IN_TXFIFO +static void rtw_hal_reset_mac_rx(_adapter *adapter) +{ + u8 val8 = 0; + /* Set REG_CR bit1, bit3, bit7 to 0*/ + val8 = rtw_read8(adapter, REG_CR); + val8 &= 0x75; + rtw_write8(adapter, REG_CR, val8); + val8 = rtw_read8(adapter, REG_CR); + /* Set REG_CR bit1, bit3, bit7 to 1*/ + val8 |= 0x8a; + rtw_write8(adapter, REG_CR, val8); + RTW_INFO("0x%04x: %02x\n", REG_CR, rtw_read8(adapter, REG_CR)); +} +static void rtw_hal_set_wow_rxff_boundary(_adapter *adapter, bool wow_mode) +{ + u8 val8 = 0; + u16 rxff_bndy = 0; + u32 rx_dma_buff_sz = 0; + + val8 = rtw_read8(adapter, REG_FIFOPAGE + 3); + if (val8 != 0) + RTW_INFO("%s:[%04x]some PKTs in TXPKTBUF\n", + __func__, (REG_FIFOPAGE + 3)); + + rtw_hal_reset_mac_rx(adapter); + + if (wow_mode) { + rtw_hal_get_def_var(adapter, HAL_DEF_RX_DMA_SZ_WOW, + (u8 *)&rx_dma_buff_sz); + rxff_bndy = rx_dma_buff_sz - 1; + + rtw_write16(adapter, (REG_TRXFF_BNDY + 2), rxff_bndy); + RTW_INFO("%s: wow mode, 0x%04x: 0x%04x\n", __func__, + REG_TRXFF_BNDY + 2, + rtw_read16(adapter, (REG_TRXFF_BNDY + 2))); + } else { + rtw_hal_get_def_var(adapter, HAL_DEF_RX_DMA_SZ, + (u8 *)&rx_dma_buff_sz); + rxff_bndy = rx_dma_buff_sz - 1; + rtw_write16(adapter, (REG_TRXFF_BNDY + 2), rxff_bndy); + RTW_INFO("%s: normal mode, 0x%04x: 0x%04x\n", __func__, + REG_TRXFF_BNDY + 2, + rtw_read16(adapter, (REG_TRXFF_BNDY + 2))); + } +} +#endif /* CONFIG_WOW_PATTERN_IN_TXFIFO*/ +#ifndef CONFIG_WOW_PATTERN_IN_TXFIFO +bool rtw_read_from_frame_mask(_adapter *adapter, u8 idx) +{ + u32 data_l = 0, data_h = 0, rx_dma_buff_sz = 0, page_sz = 0; + u16 offset, rx_buf_ptr = 0; + u16 cam_start_offset = 0; + u16 ctrl_l = 0, ctrl_h = 0; + u8 count = 0, tmp = 0; + int i = 0; + bool res = _TRUE; + + if (idx > MAX_WKFM_CAM_NUM) { + RTW_INFO("[Error]: %s, pattern index is out of range\n", + __func__); + return _FALSE; + } + + rtw_hal_get_def_var(adapter, HAL_DEF_RX_DMA_SZ_WOW, + (u8 *)&rx_dma_buff_sz); + + if (rx_dma_buff_sz == 0) { + RTW_INFO("[Error]: %s, rx_dma_buff_sz is 0!!\n", __func__); + return _FALSE; + } + + rtw_hal_get_def_var(adapter, HAL_DEF_RX_PAGE_SIZE, (u8 *)&page_sz); + + if (page_sz == 0) { + RTW_INFO("[Error]: %s, page_sz is 0!!\n", __func__); + return _FALSE; + } + + offset = (u16)PageNum(rx_dma_buff_sz, page_sz); + cam_start_offset = offset * page_sz; + + ctrl_l = 0x0; + ctrl_h = 0x0; + + /* Enable RX packet buffer access */ + rtw_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, RXPKT_BUF_SELECT); + + /* Read the WKFM CAM */ + for (i = 0; i < (WKFMCAM_ADDR_NUM / 2); i++) { + /* + * Set Rx packet buffer offset. + * RxBufer pointer increases 1, we can access 8 bytes in Rx packet buffer. + * CAM start offset (unit: 1 byte) = Index*WKFMCAM_SIZE + * RxBufer pointer addr = (CAM start offset + per entry offset of a WKFMCAM)/8 + * * Index: The index of the wake up frame mask + * * WKFMCAM_SIZE: the total size of one WKFM CAM + * * per entry offset of a WKFM CAM: Addr i * 4 bytes + */ + rx_buf_ptr = + (cam_start_offset + idx * WKFMCAM_SIZE + i * 8) >> 3; + rtw_write16(adapter, REG_PKTBUF_DBG_CTRL, rx_buf_ptr); + + rtw_write16(adapter, REG_RXPKTBUF_CTRL, ctrl_l); + data_l = rtw_read32(adapter, REG_PKTBUF_DBG_DATA_L); + data_h = rtw_read32(adapter, REG_PKTBUF_DBG_DATA_H); + + RTW_INFO("[%d]: %08x %08x\n", i, data_h, data_l); + + count = 0; + + do { + tmp = rtw_read8(adapter, REG_RXPKTBUF_CTRL); + rtw_udelay_os(2); + count++; + } while (!tmp && count < 100); + + if (count >= 100) { + RTW_INFO("%s count:%d\n", __func__, count); + res = _FALSE; + } + } + + /* Disable RX packet buffer access */ + rtw_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, + DISABLE_TRXPKT_BUF_ACCESS); + return res; +} + +bool rtw_write_to_frame_mask(_adapter *adapter, u8 idx, + struct rtl_wow_pattern *context) +{ + u32 data = 0, rx_dma_buff_sz = 0, page_sz = 0; + u16 offset, rx_buf_ptr = 0; + u16 cam_start_offset = 0; + u16 ctrl_l = 0, ctrl_h = 0; + u8 count = 0, tmp = 0; + int res = 0, i = 0; + + if (idx > MAX_WKFM_CAM_NUM) { + RTW_INFO("[Error]: %s, pattern index is out of range\n", + __func__); + return _FALSE; + } + + rtw_hal_get_def_var(adapter, HAL_DEF_RX_DMA_SZ_WOW, + (u8 *)&rx_dma_buff_sz); + + if (rx_dma_buff_sz == 0) { + RTW_INFO("[Error]: %s, rx_dma_buff_sz is 0!!\n", __func__); + return _FALSE; + } + + rtw_hal_get_def_var(adapter, HAL_DEF_RX_PAGE_SIZE, (u8 *)&page_sz); + + if (page_sz == 0) { + RTW_INFO("[Error]: %s, page_sz is 0!!\n", __func__); + return _FALSE; + } + + offset = (u16)PageNum(rx_dma_buff_sz, page_sz); + + cam_start_offset = offset * page_sz; + + if (IS_HARDWARE_TYPE_8188E(adapter)) { + ctrl_l = 0x0001; + ctrl_h = 0x0001; + } else { + ctrl_l = 0x0f01; + ctrl_h = 0xf001; + } + + /* Enable RX packet buffer access */ + rtw_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, RXPKT_BUF_SELECT); + + /* Write the WKFM CAM */ + for (i = 0; i < WKFMCAM_ADDR_NUM; i++) { + /* + * Set Rx packet buffer offset. + * RxBufer pointer increases 1, we can access 8 bytes in Rx packet buffer. + * CAM start offset (unit: 1 byte) = Index*WKFMCAM_SIZE + * RxBufer pointer addr = (CAM start offset + per entry offset of a WKFMCAM)/8 + * * Index: The index of the wake up frame mask + * * WKFMCAM_SIZE: the total size of one WKFM CAM + * * per entry offset of a WKFM CAM: Addr i * 4 bytes + */ + rx_buf_ptr = + (cam_start_offset + idx * WKFMCAM_SIZE + i * 4) >> 3; + rtw_write16(adapter, REG_PKTBUF_DBG_CTRL, rx_buf_ptr); + + if (i == 0) { + if (context->type == PATTERN_VALID) + data = WOW_VALID_BIT; + else if (context->type == PATTERN_BROADCAST) + data = WOW_VALID_BIT | WOW_BC_BIT; + else if (context->type == PATTERN_MULTICAST) + data = WOW_VALID_BIT | WOW_MC_BIT; + else if (context->type == PATTERN_UNICAST) + data = WOW_VALID_BIT | WOW_UC_BIT; + + if (context->crc != 0) + data |= context->crc; + + rtw_write32(adapter, REG_PKTBUF_DBG_DATA_L, data); + rtw_write16(adapter, REG_RXPKTBUF_CTRL, ctrl_l); + } else if (i == 1) { + data = 0; + rtw_write32(adapter, REG_PKTBUF_DBG_DATA_H, data); + rtw_write16(adapter, REG_RXPKTBUF_CTRL, ctrl_h); + } else if (i == 2 || i == 4) { + data = context->mask[i - 2]; + rtw_write32(adapter, REG_PKTBUF_DBG_DATA_L, data); + /* write to RX packet buffer*/ + rtw_write16(adapter, REG_RXPKTBUF_CTRL, ctrl_l); + } else if (i == 3 || i == 5) { + data = context->mask[i - 2]; + rtw_write32(adapter, REG_PKTBUF_DBG_DATA_H, data); + /* write to RX packet buffer*/ + rtw_write16(adapter, REG_RXPKTBUF_CTRL, ctrl_h); + } + + count = 0; + do { + tmp = rtw_read8(adapter, REG_RXPKTBUF_CTRL); + rtw_udelay_os(2); + count++; + } while (tmp && count < 100); + + if (count >= 100) + res = _FALSE; + else + res = _TRUE; + } + + /* Disable RX packet buffer access */ + rtw_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, + DISABLE_TRXPKT_BUF_ACCESS); + + return res; +} +void rtw_fill_pattern(_adapter *adapter) +{ + int i = 0, total = 0, index; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + struct rtl_wow_pattern wow_pattern; + + total = pwrpriv->wowlan_pattern_idx; + + if (total > MAX_WKFM_CAM_NUM) + total = MAX_WKFM_CAM_NUM; + + for (i = 0 ; i < total ; i++) { + if (_SUCCESS == rtw_hal_wow_pattern_generate(adapter, i, &wow_pattern)) { + + index = i; + if (!pwrpriv->bInSuspend) + index += 2; + rtw_dump_wow_pattern(RTW_DBGDUMP, &wow_pattern, i); + if (rtw_write_to_frame_mask(adapter, index, &wow_pattern) == _FALSE) + RTW_INFO("%s: ERROR!! idx: %d write_to_frame_mask_cam fail\n", __func__, i); + } + + } + rtw_write8(adapter, REG_WKFMCAM_NUM, total); + +} +#else /* CONFIG_WOW_PATTERN_IN_TXFIFO */ +bool rtw_read_from_frame_mask(_adapter *adapter, u8 idx) +{ + u32 data_l = 0, data_h = 0, page_sz = 0; + u16 tx_page_start, tx_buf_ptr = 0; + u16 cam_start_offset = 0; + u16 ctrl_l = 0, ctrl_h = 0; + u8 count = 0, tmp = 0, last_entry = 0; + int i = 0; + bool res = _TRUE; + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + u32 buffer[WKFMCAM_ADDR_NUM]; + + + if (idx > MAX_WKFM_CAM_NUM) { + RTW_INFO("[Error]: %s, pattern index is out of range\n", + __func__); + return _FALSE; + } + + rtw_hal_get_def_var(adapter, HAL_DEF_TX_PAGE_SIZE, (u8 *)&page_sz); + if (page_sz == 0) { + RTW_INFO("[Error]: %s, page_sz is 0!!\n", __func__); + return _FALSE; + } + + rtw_hal_get_def_var(adapter, HAL_DEF_TX_BUFFER_LAST_ENTRY, (u8 *)&last_entry); + if (last_entry == 0) { + RTW_INFO("[Error]: %s, last entry of tx buffer is 0!!\n", __func__); + return _FALSE; + } + + + if(_rtw_wow_chk_cap(adapter, WOW_CAP_HALMAC_ACCESS_PATTERN_IN_TXFIFO)) { + /* 8723F cannot indirect access tx fifo + * rtw_halmac_dump_fifo(dvobj, fifo_sel, fifo_addr, buf_sz, buf) + */ + #ifdef RTW_HALMAC + rtw_halmac_dump_fifo(adapter_to_dvobj(adapter), + 2, + (pwrctl->pattern_rsvd_page_loc * page_sz) + (idx * WKFMCAM_ADDR_NUM * 4), + WKFMCAM_ADDR_NUM*4, (u8*)buffer); + #endif + + for (i = 0; i < (WKFMCAM_ADDR_NUM / 2); i++) { + RTW_INFO("[%d]: %08x %08x\n", i, *(buffer + i*2), *(buffer + i*2 + 1)); + } + } else { + /* use the last 2 pages for wow pattern e.g. 0xfe and 0xff */ + tx_page_start = last_entry - 1; + cam_start_offset = tx_page_start * page_sz / 8; + ctrl_l = 0x0; + ctrl_h = 0x0; + + /* Enable TX packet buffer access */ + rtw_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, TXPKT_BUF_SELECT); + + /* Read the WKFM CAM */ + for (i = 0; i < (WKFMCAM_ADDR_NUM / 2); i++) { + /* + * Set Tx packet buffer offset. + * TxBufer pointer increases 1, we can access 8 bytes in Tx packet buffer. + * CAM start offset (unit: 1 byte) = Index*WKFMCAM_SIZE + * TxBufer pointer addr = (CAM start offset + per entry offset of a WKFMCAM)/8 + * * Index: The index of the wake up frame mask + * * WKFMCAM_SIZE: the total size of one WKFM CAM + * * per entry offset of a WKFM CAM: Addr i * 4 bytes + */ + tx_buf_ptr = + (cam_start_offset + idx * WKFMCAM_SIZE + i * 8) >> 3; + rtw_write16(adapter, REG_PKTBUF_DBG_CTRL, tx_buf_ptr); + rtw_write16(adapter, REG_RXPKTBUF_CTRL, ctrl_l); + data_l = rtw_read32(adapter, REG_PKTBUF_DBG_DATA_L); + data_h = rtw_read32(adapter, REG_PKTBUF_DBG_DATA_H); + + RTW_INFO("[%d]: %08x %08x\n", i, data_h, data_l); + + count = 0; + + do { + tmp = rtw_read32(adapter, REG_PKTBUF_DBG_CTRL) & BIT23; + rtw_udelay_os(2); + count++; + } while (!tmp && count < 100); + + if (count >= 100) { + RTW_INFO("%s count:%d\n", __func__, count); + res = _FALSE; + } + } + + /* Disable RX packet buffer access */ + rtw_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, + DISABLE_TRXPKT_BUF_ACCESS); + } + + return res; +} + +bool rtw_write_to_frame_mask(_adapter *adapter, u8 idx, + struct rtl_wow_pattern *context) +{ + u32 tx_page_start = 0, page_sz = 0; + u16 tx_buf_ptr = 0; + u16 cam_start_offset = 0; + u32 data_l = 0, data_h = 0; + u8 count = 0, tmp = 0, last_entry = 0; + int res = 0, i = 0; + + if (idx > MAX_WKFM_CAM_NUM) { + RTW_INFO("[Error]: %s, pattern index is out of range\n", + __func__); + return _FALSE; + } + + rtw_hal_get_def_var(adapter, HAL_DEF_TX_PAGE_SIZE, (u8 *)&page_sz); + if (page_sz == 0) { + RTW_INFO("[Error]: %s, page_sz is 0!!\n", __func__); + return _FALSE; + } + + rtw_hal_get_def_var(adapter, HAL_DEF_TX_BUFFER_LAST_ENTRY, (u8 *)&last_entry); + if (last_entry == 0) { + RTW_INFO("[Error]: %s, last entry of tx buffer is 0!!\n", __func__); + return _FALSE; + } + + /* use the last 2 pages for wow pattern e.g. 0xfe and 0xff */ + tx_page_start = last_entry - 1; + cam_start_offset = tx_page_start * page_sz / 8; + + /* Write the PATTERN location to BIT_TXBUF_WKCAM_OFFSET */ + rtw_write8(adapter, REG_TXBUF_WKCAM_OFFSET, cam_start_offset & 0xFF); + rtw_write8(adapter, REG_TXBUF_WKCAM_OFFSET + 1, (cam_start_offset >> 8) & 0xFF); + /* Enable TX packet buffer access */ + rtw_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, TXPKT_BUF_SELECT); + + /* Write the WKFM CAM */ + for (i = 0; i < WKFMCAM_ADDR_NUM / 2; i++) { + /* + * Set Tx packet buffer offset. + * TxBufer pointer increases 1, we can access 8 bytes in Rx packet buffer. + * CAM start offset (unit: 1 byte) = Index*WKFMCAM_SIZE + * TxBufer pointer addr = (CAM start offset + per entry offset of a WKFMCAM)/8 + * * Index: The index of the wake up frame mask + * * WKFMCAM_SIZE: the total size of one WKFM CAM + * * per entry offset of a WKFM CAM: Addr i * 4 bytes + */ + tx_buf_ptr = cam_start_offset + ((idx * WKFMCAM_SIZE + i * 8) >> 3); + + if (i == 0) { + if (context->type == PATTERN_VALID) + data_l = WOW_VALID_BIT; + else if (context->type == PATTERN_BROADCAST) + data_l = WOW_VALID_BIT | WOW_BC_BIT; + else if (context->type == PATTERN_MULTICAST) + data_l = WOW_VALID_BIT | WOW_MC_BIT; + else if (context->type == PATTERN_UNICAST) + data_l = WOW_VALID_BIT | WOW_UC_BIT; + + if (context->crc != 0) + data_l |= context->crc; + + rtw_write32(adapter, REG_PKTBUF_DBG_DATA_L, data_l); + } else { + data_l = context->mask[i * 2 - 2]; + data_h = context->mask[i * 2 - 1]; + rtw_write32(adapter, REG_PKTBUF_DBG_DATA_L, data_l); + rtw_write32(adapter, REG_PKTBUF_DBG_DATA_H, data_h); + } + + rtw_write32(adapter, REG_PKTBUF_DBG_CTRL, (tx_buf_ptr & 0x1FFF) | BIT23 | (0xff <<24)); + count = 0; + do { + tmp = rtw_read32(adapter, REG_PKTBUF_DBG_CTRL) & BIT23; + rtw_udelay_os(2); + count++; + } while (tmp && count < 100); + + if (count >= 100) { + res = _FALSE; + RTW_INFO("%s write failed\n", __func__); + } else { + res = _TRUE; + RTW_INFO("%s write OK\n", __func__); + } + } + + /* Disable TX packet buffer access */ + rtw_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, DISABLE_TRXPKT_BUF_ACCESS); + return res; +} +bool rtw_write_to_frame_mask_buf(_adapter *adapter, u8 idx, + struct rtl_wow_pattern *context, char *pattern_info, u32 *ppattern_info_len) +{ + + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + u32 page_sz = 0; + int res = _FALSE, i = 0; + u32 tmp_pattern_buf[6] = {0}; + + if (pattern_info == NULL) { + RTW_ERR("[Error]: %s, pattern info is NULL\n", __func__); + } + if (idx > MAX_WKFM_CAM_NUM) { + RTW_ERR("[Error]: %s, pattern index is out of range\n", + __func__); + return _FALSE; + } + + rtw_hal_get_def_var(adapter, HAL_DEF_TX_PAGE_SIZE, (u8 *)&page_sz); + if (page_sz == 0) { + RTW_ERR("[Error]: %s, page_sz is 0!!\n", __func__); + return _FALSE; + } + + /* Fill WKFM */ + for (i = 0; i < WKFMCAM_ADDR_NUM / 2; i++) { + if (i == 0) { + if (context->type == PATTERN_VALID) + tmp_pattern_buf[0] = WOW_VALID_BIT; + else if (context->type == PATTERN_BROADCAST) + tmp_pattern_buf[0] = WOW_VALID_BIT | WOW_BC_BIT; + else if (context->type == PATTERN_MULTICAST) + tmp_pattern_buf[0] = WOW_VALID_BIT | WOW_MC_BIT; + else if (context->type == PATTERN_UNICAST) + tmp_pattern_buf[0] = WOW_VALID_BIT | WOW_UC_BIT; + + if (context->crc != 0) + tmp_pattern_buf[0] |= context->crc; + /* pattern[1] is reserved in pattern format, dont care. */ + + } else { + tmp_pattern_buf[i * 2] = context->mask[i * 2 - 2]; + tmp_pattern_buf[i * 2 + 1] = context->mask[i * 2 - 1]; + } + } + + + /* put pattern to pattern_buf */ + _rtw_memcpy((pattern_info + idx * WKFMCAM_SIZE) , tmp_pattern_buf, WKFMCAM_SIZE); + *ppattern_info_len += WKFMCAM_SIZE; + #ifdef CONFIG_WOW_PATTERN_IN_TXFIFO_DBG + RTW_INFO("\nidx : %u pattern_info_len : %u\n", idx, *ppattern_info_len); + RTW_INFO_DUMP("", (pattern_info + idx * WKFMCAM_SIZE), WKFMCAM_SIZE); + #endif + res = _TRUE; + + + return res; +} +#endif /* CONFIG_WOW_PATTERN_IN_TXFIFO */ + +void rtw_clean_pattern(_adapter *adapter) +{ + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + struct rtl_wow_pattern zero_pattern; + int i = 0; + + _rtw_memset(&zero_pattern, 0, sizeof(struct rtl_wow_pattern)); + + zero_pattern.type = PATTERN_INVALID; + /* pattern in tx fifo do not need clear to zero*/ + + rtw_write8(adapter, REG_WKFMCAM_NUM, 0); +} +#if 0 +static int rtw_hal_set_pattern(_adapter *adapter, u8 *pattern, + u8 len, u8 *mask, u8 idx) +{ + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + struct mlme_ext_priv *pmlmeext = NULL; + struct mlme_ext_info *pmlmeinfo = NULL; + struct rtl_wow_pattern wow_pattern; + u8 mask_hw[MAX_WKFM_SIZE] = {0}; + u8 content[MAX_WKFM_PATTERN_SIZE] = {0}; + u8 broadcast_addr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + u8 multicast_addr1[2] = {0x33, 0x33}; + u8 multicast_addr2[3] = {0x01, 0x00, 0x5e}; + u8 res = _FALSE, index = 0, mask_len = 0; + u8 mac_addr[ETH_ALEN] = {0}; + u16 count = 0; + int i, j; + + if (pwrctl->wowlan_pattern_idx > MAX_WKFM_CAM_NUM) { + RTW_INFO("%s pattern_idx is more than MAX_FMC_NUM: %d\n", + __func__, MAX_WKFM_CAM_NUM); + return _FALSE; + } + + pmlmeext = &adapter->mlmeextpriv; + pmlmeinfo = &pmlmeext->mlmext_info; + _rtw_memcpy(mac_addr, adapter_mac_addr(adapter), ETH_ALEN); + _rtw_memset(&wow_pattern, 0, sizeof(struct rtl_wow_pattern)); + + mask_len = DIV_ROUND_UP(len, 8); + + /* 1. setup A1 table */ + if (memcmp(pattern, broadcast_addr, ETH_ALEN) == 0) + wow_pattern.type = PATTERN_BROADCAST; + else if (memcmp(pattern, multicast_addr1, 2) == 0) + wow_pattern.type = PATTERN_MULTICAST; + else if (memcmp(pattern, multicast_addr2, 3) == 0) + wow_pattern.type = PATTERN_MULTICAST; + else if (memcmp(pattern, mac_addr, ETH_ALEN) == 0) + wow_pattern.type = PATTERN_UNICAST; + else + wow_pattern.type = PATTERN_INVALID; + + /* translate mask from os to mask for hw */ + +/****************************************************************************** + * pattern from OS uses 'ethenet frame', like this: + + | 6 | 6 | 2 | 20 | Variable | 4 | + |--------+--------+------+-----------+------------+-----| + | 802.3 Mac Header | IP Header | TCP Packet | FCS | + | DA | SA | Type | + + * BUT, packet catched by our HW is in '802.11 frame', begin from LLC, + + | 24 or 30 | 6 | 2 | 20 | Variable | 4 | + |-------------------+--------+------+-----------+------------+-----| + | 802.11 MAC Header | LLC | IP Header | TCP Packet | FCS | + | Others | Tpye | + + * Therefore, we need translate mask_from_OS to mask_to_hw. + * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0, + * because new mask[0~5] means 'SA', but our HW packet begins from LLC, + * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match. + ******************************************************************************/ + /* Shift 6 bits */ + for (i = 0; i < mask_len - 1; i++) { + mask_hw[i] = mask[i] >> 6; + mask_hw[i] |= (mask[i + 1] & 0x3F) << 2; + } + + mask_hw[i] = (mask[i] >> 6) & 0x3F; + /* Set bit 0-5 to zero */ + mask_hw[0] &= 0xC0; + + for (i = 0; i < (MAX_WKFM_SIZE / 4); i++) { + wow_pattern.mask[i] = mask_hw[i * 4]; + wow_pattern.mask[i] |= (mask_hw[i * 4 + 1] << 8); + wow_pattern.mask[i] |= (mask_hw[i * 4 + 2] << 16); + wow_pattern.mask[i] |= (mask_hw[i * 4 + 3] << 24); + } + + /* To get the wake up pattern from the mask. + * We do not count first 12 bits which means + * DA[6] and SA[6] in the pattern to match HW design. */ + count = 0; + for (i = 12; i < len; i++) { + if ((mask[i / 8] >> (i % 8)) & 0x01) { + content[count] = pattern[i]; + count++; + } + } + + wow_pattern.crc = rtw_calc_crc(content, count); + + if (wow_pattern.crc != 0) { + if (wow_pattern.type == PATTERN_INVALID) + wow_pattern.type = PATTERN_VALID; + } + + index = idx; + + if (!pwrctl->bInSuspend) + index += 2; + + /* write pattern */ + res = rtw_write_to_frame_mask(adapter, index, &wow_pattern); + + if (res == _FALSE) + RTW_INFO("%s: ERROR!! idx: %d write_to_frame_mask_cam fail\n", + __func__, idx); + + return res; +} +#endif +#else /*CONFIG_WOW_PATTERN_HW_CAM*/ + +#define WOW_CAM_ACCESS_TIMEOUT_MS 200 +static u32 _rtw_wow_pattern_read_cam(_adapter *adapter, u8 addr) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + _mutex *mutex = &pwrpriv->wowlan_pattern_cam_mutex; + + u32 rdata = 0; + u32 cnt = 0; + systime start = 0; + u8 timeout = 0; + u8 rst = _FALSE; + + _enter_critical_mutex(mutex, NULL); + + rtw_write32(adapter, REG_WKFMCAM_CMD, BIT_WKFCAM_POLLING_V1 | BIT_WKFCAM_ADDR_V2(addr)); + + start = rtw_get_current_time(); + while (1) { + if (rtw_is_surprise_removed(adapter)) + break; + + cnt++; + if (0 == (rtw_read32(adapter, REG_WKFMCAM_CMD) & BIT_WKFCAM_POLLING_V1)) { + rst = _SUCCESS; + break; + } + if (rtw_get_passing_time_ms(start) > WOW_CAM_ACCESS_TIMEOUT_MS) { + timeout = 1; + break; + } + } + + rdata = rtw_read32(adapter, REG_WKFMCAM_RWD); + + _exit_critical_mutex(mutex, NULL); + + /*RTW_INFO("%s ==> addr:0x%02x , rdata:0x%08x\n", __func__, addr, rdata);*/ + + if (timeout) + RTW_ERR(FUNC_ADPT_FMT" failed due to polling timeout\n", FUNC_ADPT_ARG(adapter)); + + return rdata; +} +void rtw_wow_pattern_read_cam_ent(_adapter *adapter, u8 id, struct rtl_wow_pattern *context) +{ + int i; + u32 rdata; + + _rtw_memset(context, 0, sizeof(struct rtl_wow_pattern)); + + for (i = 4; i >= 0; i--) { + rdata = _rtw_wow_pattern_read_cam(adapter, (id << 3) | i); + + switch (i) { + case 4: + if (rdata & WOW_BC_BIT) + context->type = PATTERN_BROADCAST; + else if (rdata & WOW_MC_BIT) + context->type = PATTERN_MULTICAST; + else if (rdata & WOW_UC_BIT) + context->type = PATTERN_UNICAST; + else + context->type = PATTERN_INVALID; + + context->crc = rdata & 0xFFFF; + break; + default: + _rtw_memcpy(&context->mask[i], (u8 *)(&rdata), 4); + break; + } + } +} + +static void _rtw_wow_pattern_write_cam(_adapter *adapter, u8 addr, u32 wdata) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + _mutex *mutex = &pwrpriv->wowlan_pattern_cam_mutex; + u32 cnt = 0; + systime start = 0, end = 0; + u8 timeout = 0; + + /*RTW_INFO("%s ==> addr:0x%02x , wdata:0x%08x\n", __func__, addr, wdata);*/ + _enter_critical_mutex(mutex, NULL); + + rtw_write32(adapter, REG_WKFMCAM_RWD, wdata); + rtw_write32(adapter, REG_WKFMCAM_CMD, BIT_WKFCAM_POLLING_V1 | BIT_WKFCAM_WE | BIT_WKFCAM_ADDR_V2(addr)); + + start = rtw_get_current_time(); + while (1) { + if (rtw_is_surprise_removed(adapter)) + break; + + cnt++; + if (0 == (rtw_read32(adapter, REG_WKFMCAM_CMD) & BIT_WKFCAM_POLLING_V1)) + break; + + if (rtw_get_passing_time_ms(start) > WOW_CAM_ACCESS_TIMEOUT_MS) { + timeout = 1; + break; + } + } + end = rtw_get_current_time(); + + _exit_critical_mutex(mutex, NULL); + + if (timeout) { + RTW_ERR(FUNC_ADPT_FMT" addr:0x%02x, wdata:0x%08x, to:%u, polling:%u, %d ms\n" + , FUNC_ADPT_ARG(adapter), addr, wdata, timeout, cnt, rtw_get_time_interval_ms(start, end)); + } +} + +void rtw_wow_pattern_write_cam_ent(_adapter *adapter, u8 id, struct rtl_wow_pattern *context) +{ + int j; + u8 addr; + u32 wdata = 0; + + for (j = 4; j >= 0; j--) { + switch (j) { + case 4: + wdata = context->crc; + + if (PATTERN_BROADCAST == context->type) + wdata |= WOW_BC_BIT; + if (PATTERN_MULTICAST == context->type) + wdata |= WOW_MC_BIT; + if (PATTERN_UNICAST == context->type) + wdata |= WOW_UC_BIT; + if (PATTERN_INVALID != context->type) + wdata |= WOW_VALID_BIT; + break; + default: + wdata = context->mask[j]; + break; + } + + addr = (id << 3) + j; + + _rtw_wow_pattern_write_cam(adapter, addr, wdata); + } +} + +static u8 _rtw_wow_pattern_clean_cam(_adapter *adapter) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + _mutex *mutex = &pwrpriv->wowlan_pattern_cam_mutex; + u32 cnt = 0; + systime start = 0; + u8 timeout = 0; + u8 rst = _FAIL; + + _enter_critical_mutex(mutex, NULL); + rtw_write32(adapter, REG_WKFMCAM_CMD, BIT_WKFCAM_POLLING_V1 | BIT_WKFCAM_CLR_V1); + + start = rtw_get_current_time(); + while (1) { + if (rtw_is_surprise_removed(adapter)) + break; + + cnt++; + if (0 == (rtw_read32(adapter, REG_WKFMCAM_CMD) & BIT_WKFCAM_POLLING_V1)) { + rst = _SUCCESS; + break; + } + if (rtw_get_passing_time_ms(start) > WOW_CAM_ACCESS_TIMEOUT_MS) { + timeout = 1; + break; + } + } + _exit_critical_mutex(mutex, NULL); + + if (timeout) + RTW_ERR(FUNC_ADPT_FMT" falied ,polling timeout\n", FUNC_ADPT_ARG(adapter)); + + return rst; +} + +void rtw_clean_pattern(_adapter *adapter) +{ + if (_FAIL == _rtw_wow_pattern_clean_cam(adapter)) + RTW_ERR("rtw_clean_pattern failed\n"); +} +void rtw_fill_pattern(_adapter *adapter) +{ + int i = 0, total = 0; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + struct rtl_wow_pattern wow_pattern; + + total = pwrpriv->wowlan_pattern_idx; + + if (total > MAX_WKFM_CAM_NUM) + total = MAX_WKFM_CAM_NUM; + + for (i = 0 ; i < total ; i++) { + if (_SUCCESS == rtw_hal_wow_pattern_generate(adapter, i, &wow_pattern)) { + rtw_dump_wow_pattern(RTW_DBGDUMP, &wow_pattern, i); + rtw_wow_pattern_write_cam_ent(adapter, i, &wow_pattern); + } + } +} + +#endif +void rtw_wow_pattern_cam_dump(_adapter *adapter) +{ + +#ifndef CONFIG_WOW_PATTERN_HW_CAM + int i = 0, total = 0; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + total = pwrpriv->wowlan_pattern_idx; + + if (total > MAX_WKFM_CAM_NUM) + total = MAX_WKFM_CAM_NUM; + + for (i = 0 ; i < total; i++) { + RTW_INFO("=======[%d]=======\n", i); + rtw_read_from_frame_mask(adapter, i); + } +#else + struct rtl_wow_pattern context; + int i; + + for (i = 0 ; i < MAX_WKFM_CAM_NUM; i++) { + rtw_wow_pattern_read_cam_ent(adapter, i, &context); + rtw_dump_wow_pattern(RTW_DBGDUMP, &context, i); + } + +#endif +} + + +static void rtw_hal_dl_pattern(_adapter *adapter, u8 mode) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + + switch (mode) { + case 0: + rtw_clean_pattern(adapter); + RTW_INFO("%s: total patterns: %d\n", __func__, pwrpriv->wowlan_pattern_idx); + break; + case 1: + #if defined(CONFIG_WOW_PATTERN_IN_TXFIFO) + RTW_INFO("%s Patterns have been downloaded in rsvd pages\n", __func__); + #else + rtw_set_default_pattern(adapter); + rtw_fill_pattern(adapter); + RTW_INFO("%s: pattern total: %d downloaded\n", __func__, pwrpriv->wowlan_pattern_idx); + #endif + break; + case 2: + rtw_clean_pattern(adapter); + rtw_wow_pattern_sw_reset(adapter); + RTW_INFO("%s: clean patterns\n", __func__); + break; + default: + RTW_INFO("%s: unknown mode\n", __func__); + break; + } +} + +static void rtw_hal_wow_enable(_adapter *adapter) +{ + struct registry_priv *registry_par = &adapter->registrypriv; + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + struct security_priv *psecuritypriv = &adapter->securitypriv; + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + struct sta_info *psta = NULL; + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(adapter); + int res; + u16 media_status_rpt; + u8 no_wake = 0, i; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + _adapter *iface; +#ifdef CONFIG_GPIO_WAKEUP + u8 val8 = 0; +#endif + +#ifdef CONFIG_LPS_PG + u8 lps_pg_hdl_id = 0; +#endif + + + + if(registry_par->suspend_type == FW_IPS_DISABLE_BBRF && + !check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) + no_wake = 1; + + RTW_PRINT(FUNC_ADPT_FMT " WOWLAN_ENABLE\n", FUNC_ADPT_ARG(adapter)); + rtw_hal_gate_bb(adapter, _TRUE); + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + /* Start Usb TxDMA */ + if(iface) { + RTW_INFO(ADPT_FMT "enable TX\n", ADPT_ARG(iface)); + RTW_ENABLE_FUNC(iface, DF_TX_BIT); + } + } + +#ifdef CONFIG_GTK_OL + if (psecuritypriv->binstallKCK_KEK == _TRUE) + rtw_hal_fw_sync_cam_id(adapter); +#endif + if (IS_HARDWARE_TYPE_8723B(adapter)) + rtw_hal_backup_rate(adapter); + + rtw_hal_fw_dl(adapter, _TRUE); + if(no_wake) + media_status_rpt = RT_MEDIA_DISCONNECT; + else + media_status_rpt = RT_MEDIA_CONNECT; + rtw_hal_set_hwreg(adapter, HW_VAR_H2C_FW_JOINBSSRPT, + (u8 *)&media_status_rpt); + + /* RX DMA stop */ + #if defined(CONFIG_RTL8188E) + if (IS_HARDWARE_TYPE_8188E(adapter)) + rtw_hal_disable_tx_report(adapter); + #endif + + res = rtw_hal_pause_rx_dma(adapter); + if (res == _FAIL) + RTW_PRINT("[WARNING] pause RX DMA fail\n"); + + #ifndef CONFIG_WOW_PATTERN_HW_CAM + /* Reconfig RX_FF Boundary */ + #ifndef CONFIG_WOW_PATTERN_IN_TXFIFO + rtw_hal_set_wow_rxff_boundary(adapter, _TRUE); + #endif /*CONFIG_WOW_PATTERN_IN_TXFIFO*/ + #endif + + /* redownload wow pattern */ + if(!no_wake) + rtw_hal_dl_pattern(adapter, 1); + + if (!pwrctl->wowlan_pno_enable) { + psta = rtw_get_stainfo(&adapter->stapriv, get_bssid(pmlmepriv)); + + if (psta != NULL) { + #ifdef CONFIG_FW_MULTI_PORT_SUPPORT + adapter_to_dvobj(adapter)->dft.port_id = 0xFF; + adapter_to_dvobj(adapter)->dft.mac_id = 0xFF; + rtw_hal_set_default_port_id_cmd(adapter, psta->cmn.mac_id); + #endif + if(!no_wake) + rtw_sta_media_status_rpt(adapter, psta, 1); + } +#ifdef CONFIG_FW_MULTI_PORT_SUPPORT + else { + if(registry_par->suspend_type == FW_IPS_WRC) { + adapter_to_dvobj(adapter)->dft.port_id = 0xFF; + adapter_to_dvobj(adapter)->dft.mac_id = 0xFF; + rtw_hal_set_default_port_id_cmd(adapter, 0); + } + } +#endif /* CONFIG_FW_MULTI_PORT_SUPPORT */ + } + +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) + /* Enable CPWM2 only. */ + res = rtw_hal_enable_cpwm2(adapter); + if (res == _FAIL) + RTW_PRINT("[WARNING] enable cpwm2 fail\n"); +#endif +#ifdef CONFIG_GPIO_WAKEUP +#ifdef CONFIG_RTW_ONE_PIN_GPIO + rtw_hal_switch_gpio_wl_ctrl(adapter, pwrctl->wowlan_gpio_index, _TRUE); + rtw_hal_set_input_gpio(adapter, pwrctl->wowlan_gpio_index); +#else +#ifdef CONFIG_WAKEUP_GPIO_INPUT_MODE + if (pwrctl->is_high_active == 0) + rtw_hal_set_input_gpio(adapter, pwrctl->wowlan_gpio_index); + else + rtw_hal_set_output_gpio(adapter, pwrctl->wowlan_gpio_index, + GPIO_OUTPUT_LOW); +#else + val8 = (pwrctl->is_high_active == 0) ? 1 : 0; + rtw_hal_set_output_gpio(adapter, pwrctl->wowlan_gpio_index, val8); + rtw_hal_switch_gpio_wl_ctrl(adapter, pwrctl->wowlan_gpio_index, _TRUE); + RTW_INFO("%s: set GPIO_%d to OUTPUT %s state in wow suspend and %s_ACTIVE.\n", + __func__, pwrctl->wowlan_gpio_index, + pwrctl->wowlan_gpio_output_state ? "HIGH" : "LOW", + pwrctl->is_high_active ? "HIGI" : "LOW"); +#endif /* CONFIG_WAKEUP_GPIO_INPUT_MODE */ +#endif /* CONFIG_RTW_ONE_PIN_GPIO */ +#endif /* CONFIG_GPIO_WAKEUP */ + /* Set WOWLAN H2C command. */ + RTW_PRINT("Set WOWLan cmd\n"); + rtw_hal_set_fw_wow_related_cmd(adapter, 1); + + res = rtw_hal_check_wow_ctrl(adapter, _TRUE); + + if (res == _FALSE) + RTW_INFO("[Error]%s: set wowlan CMD fail!!\n", __func__); + + pwrctl->wowlan_wake_reason = + rtw_read8(adapter, REG_WOWLAN_WAKE_REASON); + + RTW_PRINT("wowlan_wake_reason: 0x%02x\n", + pwrctl->wowlan_wake_reason); +#ifdef CONFIG_GTK_OL_DBG + dump_sec_cam(RTW_DBGDUMP, adapter); + dump_sec_cam_cache(RTW_DBGDUMP, adapter); +#endif + +#ifdef CONFIG_LPS_PG + if (pwrctl->lps_level == LPS_PG) { + lps_pg_hdl_id = LPS_PG_INFO_CFG; + rtw_hal_set_hwreg(adapter, HW_VAR_LPS_PG_HANDLE, (u8 *)(&lps_pg_hdl_id)); + } +#endif + +#ifdef CONFIG_USB_HCI + /* free adapter's resource */ + rtw_mi_intf_stop(adapter); + +#endif +#if defined(CONFIG_USB_HCI) || defined(CONFIG_PCI_HCI) + /* Invoid SE0 reset signal during suspending*/ + rtw_write8(adapter, REG_RSV_CTRL, 0x20); + if (IS_8188F(pHalData->version_id) == FALSE + && IS_8188GTV(pHalData->version_id) == FALSE) + rtw_write8(adapter, REG_RSV_CTRL, 0x60); +#endif + + rtw_hal_gate_bb(adapter, _FALSE); +} + +#define DBG_WAKEUP_REASON +#ifdef DBG_WAKEUP_REASON +void _dbg_wake_up_reason_string(_adapter *adapter, const char *srt_res) +{ + RTW_INFO(ADPT_FMT "- wake up reason - %s\n", ADPT_ARG(adapter), srt_res); +} +void _dbg_rtw_wake_up_reason(_adapter *adapter, u8 reason) +{ + if (RX_PAIRWISEKEY == reason) + _dbg_wake_up_reason_string(adapter, "Rx pairwise key"); + else if (RX_GTK == reason) + _dbg_wake_up_reason_string(adapter, "Rx GTK"); + else if (RX_FOURWAY_HANDSHAKE == reason) + _dbg_wake_up_reason_string(adapter, "Rx four way handshake"); + else if (RX_DISASSOC == reason) + _dbg_wake_up_reason_string(adapter, "Rx disassoc"); + else if (RX_DEAUTH == reason) + _dbg_wake_up_reason_string(adapter, "Rx deauth"); + else if (RX_ARP_REQUEST == reason) + _dbg_wake_up_reason_string(adapter, "Rx ARP request"); + else if (FW_DECISION_DISCONNECT == reason) + _dbg_wake_up_reason_string(adapter, "FW detect disconnect"); + else if (RX_MAGIC_PKT == reason) + _dbg_wake_up_reason_string(adapter, "Rx magic packet"); + else if (RX_UNICAST_PKT == reason) + _dbg_wake_up_reason_string(adapter, "Rx unicast packet"); + else if (RX_PATTERN_PKT == reason) + _dbg_wake_up_reason_string(adapter, "Rx pattern packet"); + else if (RTD3_SSID_MATCH == reason) + _dbg_wake_up_reason_string(adapter, "RTD3 SSID match"); + else if (RX_REALWOW_V2_WAKEUP_PKT == reason) + _dbg_wake_up_reason_string(adapter, "Rx real WOW V2 wakeup packet"); + else if (RX_REALWOW_V2_ACK_LOST == reason) + _dbg_wake_up_reason_string(adapter, "Rx real WOW V2 ack lost"); + else if (ENABLE_FAIL_DMA_IDLE == reason) + _dbg_wake_up_reason_string(adapter, "enable fail DMA idle"); + else if (ENABLE_FAIL_DMA_PAUSE == reason) + _dbg_wake_up_reason_string(adapter, "enable fail DMA pause"); + else if (AP_OFFLOAD_WAKEUP == reason) + _dbg_wake_up_reason_string(adapter, "AP offload wakeup"); + else if (CLK_32K_UNLOCK == reason) + _dbg_wake_up_reason_string(adapter, "clk 32k unlock"); + else if (RTIME_FAIL_DMA_IDLE == reason) + _dbg_wake_up_reason_string(adapter, "RTIME fail DMA idle"); + else if (CLK_32K_LOCK == reason) + _dbg_wake_up_reason_string(adapter, "clk 32k lock"); + #ifdef CONFIG_WOW_KEEP_ALIVE_PATTERN + else if (WOW_KEEPALIVE_ACK_TIMEOUT == reason) + _dbg_wake_up_reason_string(adapter, "rx keep alive ack timeout"); + else if (WOW_KEEPALIVE_WAKE == reason) + _dbg_wake_up_reason_string(adapter, "rx keep alive wake pattern"); + #endif /*CONFIG_WOW_KEEP_ALIVE_PATTERN*/ + else + _dbg_wake_up_reason_string(adapter, "unknown reasoen"); +} +#endif + +static void rtw_hal_wow_disable(_adapter *adapter) +{ + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + struct security_priv *psecuritypriv = &adapter->securitypriv; + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + struct sta_info *psta = NULL; + struct registry_priv *registry_par = &adapter->registrypriv; + int res; + u16 media_status_rpt; + + RTW_PRINT("%s, WOWLAN_DISABLE\n", __func__); + + if(registry_par->suspend_type == FW_IPS_DISABLE_BBRF && !check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) { + RTW_INFO("FW_IPS_DISABLE_BBRF resume\n"); + return; + } + + if (!pwrctl->wowlan_pno_enable) { + psta = rtw_get_stainfo(&adapter->stapriv, get_bssid(pmlmepriv)); + if (psta != NULL) + rtw_sta_media_status_rpt(adapter, psta, 0); + else + RTW_INFO("%s: psta is null\n", __func__); + } + + if (0) { + RTW_INFO("0x630:0x%02x\n", rtw_read8(adapter, 0x630)); + RTW_INFO("0x631:0x%02x\n", rtw_read8(adapter, 0x631)); + RTW_INFO("0x634:0x%02x\n", rtw_read8(adapter, 0x634)); + RTW_INFO("0x1c7:0x%02x\n", rtw_read8(adapter, 0x1c7)); + } + + pwrctl->wowlan_wake_reason = rtw_read8(adapter, REG_WOWLAN_WAKE_REASON); + + RTW_PRINT("wakeup_reason: 0x%02x\n", + pwrctl->wowlan_wake_reason); + #ifdef DBG_WAKEUP_REASON + _dbg_rtw_wake_up_reason(adapter, pwrctl->wowlan_wake_reason); + #endif + + rtw_hal_set_fw_wow_related_cmd(adapter, 0); + + res = rtw_hal_check_wow_ctrl(adapter, _FALSE); + + #if defined(CONFIG_RTL8188E) + if (IS_HARDWARE_TYPE_8188E(adapter)) + rtw_hal_enable_tx_report(adapter); + #endif + + if ((pwrctl->wowlan_wake_reason != RX_DISASSOC) && + (pwrctl->wowlan_wake_reason != RX_DEAUTH) && + (pwrctl->wowlan_wake_reason != FW_DECISION_DISCONNECT)) { + rtw_hal_get_aoac_rpt(adapter); + rtw_hal_update_sw_security_info(adapter); + } + + if (res == _FALSE) { + RTW_INFO("[Error]%s: disable WOW cmd fail\n!!", __func__); + rtw_hal_force_enable_rxdma(adapter); + } + + rtw_hal_gate_bb(adapter, _TRUE); + + res = rtw_hal_pause_rx_dma(adapter); + if (res == _FAIL) + RTW_PRINT("[WARNING] pause RX DMA fail\n"); + + /* clean HW pattern match */ + rtw_hal_dl_pattern(adapter, 0); + + #ifndef CONFIG_WOW_PATTERN_HW_CAM + /* config RXFF boundary to original */ + #ifndef CONFIG_WOW_PATTERN_IN_TXFIFO + rtw_hal_set_wow_rxff_boundary(adapter, _FALSE); + #endif /*CONFIG_WOW_PATTERN_IN_TXFIFO*/ + #endif + rtw_hal_release_rx_dma(adapter); + + rtw_hal_fw_dl(adapter, _FALSE); + +#ifdef CONFIG_GPIO_WAKEUP + +#ifdef CONFIG_RTW_ONE_PIN_GPIO + rtw_hal_set_input_gpio(adapter, pwrctl->wowlan_gpio_index); +#else +#ifdef CONFIG_WAKEUP_GPIO_INPUT_MODE + if (pwrctl->is_high_active == 0) + rtw_hal_set_input_gpio(adapter, pwrctl->wowlan_gpio_index); + else + rtw_hal_set_output_gpio(adapter, pwrctl->wowlan_gpio_index, + GPIO_OUTPUT_LOW); +#else + rtw_hal_set_output_gpio(adapter, pwrctl->wowlan_gpio_index + , pwrctl->wowlan_gpio_output_state); + RTW_INFO("%s: set GPIO_%d to OUTPUT %s state in wow resume and %s_ACTIVE.\n", + __func__, pwrctl->wowlan_gpio_index, + pwrctl->wowlan_gpio_output_state ? "HIGH" : "LOW", + pwrctl->is_high_active ? "HIGI" : "LOW"); +#endif /* CONFIG_WAKEUP_GPIO_INPUT_MODE */ +#endif /* CONFIG_RTW_ONE_PIN_GPIO */ +#endif /* CONFIG_GPIO_WAKEUP */ + if ((pwrctl->wowlan_wake_reason != FW_DECISION_DISCONNECT) && + (pwrctl->wowlan_wake_reason != RX_PAIRWISEKEY) && + (pwrctl->wowlan_wake_reason != RX_DISASSOC) && + (pwrctl->wowlan_wake_reason != RX_DEAUTH)) { + + media_status_rpt = RT_MEDIA_CONNECT; + rtw_hal_set_hwreg(adapter, HW_VAR_H2C_FW_JOINBSSRPT, + (u8 *)&media_status_rpt); + + if (psta != NULL) { + #ifdef CONFIG_FW_MULTI_PORT_SUPPORT + adapter_to_dvobj(adapter)->dft.port_id = 0xFF; + adapter_to_dvobj(adapter)->dft.mac_id = 0xFF; + rtw_hal_set_default_port_id_cmd(adapter, psta->cmn.mac_id); + #endif + rtw_sta_media_status_rpt(adapter, psta, 1); + } + } + rtw_hal_gate_bb(adapter, _FALSE); +} +#if defined(CONFIG_WOW_PATTERN_IN_TXFIFO) +static void rtw_hal_construct_pattern_info( + PADAPTER padapter, + u8 *pframe, + u32 *pLength +) +{ + + u32 pattern_info_len = 0; + int i = 0, total = 0, index; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + struct rtl_wow_pattern wow_pattern; + u32 page_sz = 0; + + total = pwrpriv->wowlan_pattern_idx; + + if (total > MAX_WKFM_CAM_NUM) + total = MAX_WKFM_CAM_NUM; + + /* Generate default pattern */ + rtw_set_default_pattern(padapter); + + /* Convert pattern to WKFM_CAM pattern */ + for (i = 0 ; i < total ; i++) { + if (_SUCCESS == rtw_hal_wow_pattern_generate(padapter, i, &wow_pattern)) { + + index = i; + if (!pwrpriv->bInSuspend) + index += 2; + #ifdef CONFIG_WOW_PATTERN_IN_TXFIFO_DBG + rtw_dump_wow_pattern(RTW_DBGDUMP, &wow_pattern, i); + #endif + + if (rtw_write_to_frame_mask_buf(padapter, index, &wow_pattern, + pframe, &pattern_info_len) == _FALSE) + RTW_INFO("%s: ERROR!! idx: %d write_to_frame_mask_cam fail\n", __func__, i); + } + } + *pLength = pattern_info_len; + + +} +#endif /* CONFIG_WOW_PATTERN_IN_TXFIFO */ +void rtw_hal_set_wow_fw_rsvd_page(_adapter *adapter, u8 *pframe, u16 index, + u8 tx_desc, u32 page_size, u8 *page_num, u32 *total_pkt_len, + RSVDPAGE_LOC *rsvd_page_loc) +{ + struct security_priv *psecuritypriv = &adapter->securitypriv; + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + struct mlme_ext_priv *pmlmeext; + struct mlme_ext_info *pmlmeinfo; + u32 ARPLength = 0, GTKLength = 0, PNOLength = 0, ScanInfoLength = 0; + u32 ProbeReqLength = 0, ns_len = 0, rc_len = 0; + u8 CurtPktPageNum = 0; +#ifdef CONFIG_WOW_KEEP_ALIVE_PATTERN + u32 keep_alive_len=0; + int i; +#endif /*CONFIG_WOW_KEEP_ALIVE_PATTERN */ +#ifdef CONFIG_WAR_OFFLOAD + u16 tmp_idx = 0; + u32 buf_len = 0; +#endif + +#ifdef CONFIG_GTK_OL + struct sta_priv *pstapriv = &adapter->stapriv; + struct sta_info *psta; + struct security_priv *psecpriv = &adapter->securitypriv; + u8 kek[RTW_KEK_LEN]; + u8 kck[RTW_KCK_LEN]; +#endif /* CONFIG_GTK_OL */ +#ifdef CONFIG_PNO_SUPPORT + int pno_index; + u8 ssid_num; +#endif /* CONFIG_PNO_SUPPORT */ +#ifdef CONFIG_WOW_PATTERN_IN_TXFIFO + u32 PatternLen = 0; + u32 cam_start_offset = 0; + u32 reg_cam_start_offset_val = 0; +#endif /* CONFIG_WOW_PATTERN_IN_TXFIFO */ + + pmlmeext = &adapter->mlmeextpriv; + pmlmeinfo = &pmlmeext->mlmext_info; + + if (pwrctl->wowlan_pno_enable == _FALSE) { + /* ARP RSP * 1 page */ + + rsvd_page_loc->LocArpRsp = *page_num; + + RTW_INFO("LocArpRsp: %d\n", rsvd_page_loc->LocArpRsp); + +#ifdef CONFIG_WAR_OFFLOAD + if ((0 != pwrctl->wowlan_war_offload_ipv4.ip_addr[0]) && + (_FALSE == _rtw_memcmp(&pwrctl->wowlan_war_offload_ipv4.ip_addr[0], pmlmeinfo->ip_addr, 4))) { + _rtw_memcpy(pmlmeinfo->ip_addr, &pwrctl->wowlan_war_offload_ipv4.ip_addr[0], 4); + RTW_INFO("Update IP(%d.%d.%d.%d) to arp rsvd page\n", + pmlmeinfo->ip_addr[0], pmlmeinfo->ip_addr[1], + pmlmeinfo->ip_addr[2], pmlmeinfo->ip_addr[3]); + } +#endif /* CONFIG_WAR_OFFLOAD */ + + + rtw_hal_construct_ARPRsp(adapter, &pframe[index], + &ARPLength, pmlmeinfo->ip_addr); + + rtw_hal_fill_fake_txdesc(adapter, + &pframe[index - tx_desc], + ARPLength, _FALSE, _FALSE, _TRUE); + + CurtPktPageNum = (u8)PageNum(tx_desc + ARPLength, page_size); + + *page_num += CurtPktPageNum; + + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-ARPRsp", CurtPktPageNum, *page_num, 0); +#ifdef CONFIG_WOW_KEEP_ALIVE_PATTERN + /* Keep Alive * ? page*/ + if(pwrctl->keep_alive_pattern_len){ + rsvd_page_loc->LocKeepAlive = *page_num; + pwrctl->keep_alive_pattern_loc = rsvd_page_loc->LocKeepAlive; + RTW_INFO("pwrctl->keep_alive_pattern_loc: %d\n", pwrctl->keep_alive_pattern_loc); + rtw_hal_construct_keepalive(adapter,&pframe[index],&keep_alive_len); + rtw_hal_fill_fake_txdesc(adapter, + &pframe[index - tx_desc], + keep_alive_len, _FALSE, _FALSE, _TRUE); + CurtPktPageNum = (u8)PageNum(tx_desc + keep_alive_len, page_size); + *page_num += CurtPktPageNum; + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-KeepAlive:", CurtPktPageNum, *page_num, 0); + } +#endif /* CONFIG_WOW_KEEP_ALIVE_PATTERN*/ + +#ifdef CONFIG_IPV6 + /* 2 NS offload and NDP Info*/ + if (pwrctl->wowlan_ns_offload_en == _TRUE) { + rsvd_page_loc->LocNbrAdv = *page_num; + RTW_INFO("LocNbrAdv: %d\n", rsvd_page_loc->LocNbrAdv); + rtw_hal_construct_na_message(adapter, + &pframe[index], &ns_len); + rtw_hal_fill_fake_txdesc(adapter, + &pframe[index - tx_desc], + ns_len, _FALSE, + _FALSE, _TRUE); + CurtPktPageNum = (u8)PageNum(tx_desc + ns_len, + page_size); + *page_num += CurtPktPageNum; + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-NbrAdv", CurtPktPageNum, *page_num, 0); + + rsvd_page_loc->LocNDPInfo = *page_num; + RTW_INFO("LocNDPInfo: %d\n", + rsvd_page_loc->LocNDPInfo); + + rtw_hal_construct_ndp_info(adapter, + &pframe[index - tx_desc], + &ns_len); + CurtPktPageNum = + (u8)PageNum(tx_desc + ns_len, page_size); + *page_num += CurtPktPageNum; + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-NDPInfo", CurtPktPageNum, *page_num, 0); + + } +#endif /*CONFIG_IPV6*/ + /* 3 Remote Control Info. * 1 page */ + rsvd_page_loc->LocRemoteCtrlInfo = *page_num; + RTW_INFO("LocRemoteCtrlInfo: %d\n", rsvd_page_loc->LocRemoteCtrlInfo); + rtw_hal_construct_remote_control_info(adapter, + &pframe[index - tx_desc], + &rc_len); + CurtPktPageNum = (u8)PageNum(rc_len, page_size); + *page_num += CurtPktPageNum; + *total_pkt_len = index + rc_len; + RSVD_PAGE_CFG("WOW-RCI", CurtPktPageNum, *page_num, *total_pkt_len); +#ifdef CONFIG_GTK_OL + index += (CurtPktPageNum * page_size); + + /* if the ap staion info. exists, get the kek, kck from staion info. */ + psta = rtw_get_stainfo(pstapriv, get_bssid(pmlmepriv)); + if (psta == NULL) { + _rtw_memset(kek, 0, RTW_KEK_LEN); + _rtw_memset(kck, 0, RTW_KCK_LEN); + RTW_INFO("%s, KEK, KCK download rsvd page all zero\n", + __func__); + } else { + _rtw_memcpy(kek, psta->kek, RTW_KEK_LEN); + _rtw_memcpy(kck, psta->kck, RTW_KCK_LEN); + } + + /* 3 KEK, KCK */ + rsvd_page_loc->LocGTKInfo = *page_num; + RTW_INFO("LocGTKInfo: %d\n", rsvd_page_loc->LocGTKInfo); + + if (IS_HARDWARE_TYPE_8188E(adapter) || IS_HARDWARE_TYPE_8812(adapter)) { + struct security_priv *psecpriv = NULL; + + psecpriv = &adapter->securitypriv; + _rtw_memcpy(pframe + index - tx_desc, + &psecpriv->dot11PrivacyAlgrthm, 1); + _rtw_memcpy(pframe + index - tx_desc + 1, + &psecpriv->dot118021XGrpPrivacy, 1); + _rtw_memcpy(pframe + index - tx_desc + 2, + kck, RTW_KCK_LEN); + _rtw_memcpy(pframe + index - tx_desc + 2 + RTW_KCK_LEN, + kek, RTW_KEK_LEN); + CurtPktPageNum = (u8)PageNum(tx_desc + 2 + RTW_KCK_LEN + RTW_KEK_LEN, page_size); + } else { + + _rtw_memcpy(pframe + index - tx_desc, kck, RTW_KCK_LEN); + _rtw_memcpy(pframe + index - tx_desc + RTW_KCK_LEN, + kek, RTW_KEK_LEN); + GTKLength = tx_desc + RTW_KCK_LEN + RTW_KEK_LEN; + + if (psta != NULL && + psecuritypriv->dot118021XGrpPrivacy == _TKIP_) { + _rtw_memcpy(pframe + index - tx_desc + 56, + &psta->dot11tkiptxmickey, RTW_TKIP_MIC_LEN); + GTKLength += RTW_TKIP_MIC_LEN; + } + CurtPktPageNum = (u8)PageNum(GTKLength, page_size); + } +#if 0 + { + int i; + printk("\ntoFW KCK: "); + for (i = 0; i < 16; i++) + printk(" %02x ", kck[i]); + printk("\ntoFW KEK: "); + for (i = 0; i < 16; i++) + printk(" %02x ", kek[i]); + printk("\n"); + } + + RTW_INFO("%s(): HW_VAR_SET_TX_CMD: KEK KCK %p %d\n", + __FUNCTION__, &pframe[index - tx_desc], + (tx_desc + RTW_KCK_LEN + RTW_KEK_LEN)); +#endif + + *page_num += CurtPktPageNum; + + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-GTKInfo", CurtPktPageNum, *page_num, 0); + + /* 3 GTK Response */ + rsvd_page_loc->LocGTKRsp = *page_num; + RTW_INFO("LocGTKRsp: %d\n", rsvd_page_loc->LocGTKRsp); + rtw_hal_construct_GTKRsp(adapter, &pframe[index], >KLength); + + rtw_hal_fill_fake_txdesc(adapter, &pframe[index - tx_desc], + GTKLength, _FALSE, _FALSE, _TRUE); +#if 0 + { + int gj; + printk("123GTK pkt=>\n"); + for (gj = 0; gj < GTKLength + tx_desc; gj++) { + printk(" %02x ", pframe[index - tx_desc + gj]); + if ((gj + 1) % 16 == 0) + printk("\n"); + } + printk(" <=end\n"); + } + + RTW_INFO("%s(): HW_VAR_SET_TX_CMD: GTK RSP %p %d\n", + __FUNCTION__, &pframe[index - tx_desc], + (tx_desc + GTKLength)); +#endif + + CurtPktPageNum = (u8)PageNum(tx_desc + GTKLength, page_size); + + *page_num += CurtPktPageNum; + + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-GTKRsp", CurtPktPageNum, *page_num, 0); + + /* below page is empty for GTK extension memory */ + /* 3(11) GTK EXT MEM */ + rsvd_page_loc->LocGTKEXTMEM = *page_num; + RTW_INFO("LocGTKEXTMEM: %d\n", rsvd_page_loc->LocGTKEXTMEM); + CurtPktPageNum = 2; + + if (page_size >= 256) + CurtPktPageNum = 1; + + *page_num += CurtPktPageNum; + /* extension memory for FW */ + *total_pkt_len = index + (page_size * CurtPktPageNum); + RSVD_PAGE_CFG("WOW-GTKEXTMEM", CurtPktPageNum, *page_num, *total_pkt_len); +#endif /* CONFIG_GTK_OL */ + + index += (CurtPktPageNum * page_size); + +#ifdef CONFIG_WAR_OFFLOAD + if(_TRUE == pwrctl->wowlan_war_offload_mode) { + u8 zero_ary[16] = {0x00}; + u8 war_tmp_cnt = 0; + + /* Reserve 2 page for Ip parameters */ + /* First page + | Byte 15 -----------Byte 0 | + | IP-4 | IP-3 | IP-2 | IP-1 | + | location of each feature | mac addr | + | NetBIOS name | + | location of each feature | + Second page + | IPv6 - 1 | + | IPv6 - 2 | + | IPv6 - 3 | + | IPv6 - 4 | + | IPv6 - 5 | + | IPv6 - 6 | + | IPv6 - 7 | + | IPv6 - 8 | + */ + + /* location of each feature : Byte 22 ~ Byte 31 + * Byte22 : location of SNMP RX + * Byte23 : location of SNMP V4 + * Byte24 : location of SNMP V6 + * Byte25 : location of MDNS Param + * Byte26 : location of MDNS V4 + * Byte27 : location of MDNS V6 + * Byte28 : location of SSDP pattern + * Byte29 : location of WSD pattern + * Byte30 : location of SLP pattern + * Byte31 : location of LLMNR + */ + + /* ipv4 : 4 */ + if (0 == pwrctl->wowlan_war_offload_ipv4.ip_addr[0]) + _rtw_memcpy(&pwrctl->wowlan_war_offload_ipv4.ip_addr[0], pmlmeinfo->ip_addr, 4); + for(war_tmp_cnt=0; war_tmp_cnt<4 ;war_tmp_cnt++) + _rtw_memcpy(pframe + index - tx_desc + (war_tmp_cnt*4), &pwrctl->wowlan_war_offload_ipv4.ip_addr[war_tmp_cnt], 4); + + if (is_zero_mac_addr(pwrctl->wowlan_war_offload_mac)) { + _rtw_memcpy(pwrctl->wowlan_war_offload_mac, adapter_mac_addr(adapter), 6); + } + _rtw_memcpy(pframe + index + 16 - tx_desc, pwrctl->wowlan_war_offload_mac, 6); + + + /* ipv6 : 8 */ + if (_TRUE == _rtw_memcmp(pwrctl->wowlan_war_offload_ipv6.ipv6_addr[0], zero_ary, RTW_IPv6_ADDR_LEN)) + _rtw_memcpy(pwrctl->wowlan_war_offload_ipv6.ipv6_addr[0], pmlmeinfo->ip6_addr, RTW_IPv6_ADDR_LEN); + + for(war_tmp_cnt=0; war_tmp_cnt<8 ;war_tmp_cnt++) + _rtw_memcpy(pframe + index + page_size - tx_desc + (war_tmp_cnt*16), pwrctl->wowlan_war_offload_ipv6.ipv6_addr[war_tmp_cnt], 16); + + rsvd_page_loc->LocIpParm = *page_num; + + tmp_idx = index; + CurtPktPageNum = 2; + *page_num += CurtPktPageNum; + *total_pkt_len = index + (page_size * CurtPktPageNum); + index += (CurtPktPageNum * page_size); + + +#if defined(CONFIG_OFFLOAD_MDNS_V4) || defined(CONFIG_OFFLOAD_MDNS_V6) + if ( (WAR_MDNS_V4_RSP_EN & pwrctl->wowlan_war_offload_ctrl) || + (WAR_MDNS_V6_RSP_EN & pwrctl->wowlan_war_offload_ctrl) || + (WAR_MDNS_V4_WAKEUP_EN & pwrctl->wowlan_war_offload_ctrl) || + (WAR_MDNS_V6_WAKEUP_EN & pwrctl->wowlan_war_offload_ctrl)) { + + struct war_mdns_service_info *psinfo = pwrctl->wowlan_war_offload_mdns_service; + u8 txt_in_ptr[31]={ 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0x00, 0x13, 0x09, 0x74, 0x78, 0x74, 0x76, 0x65, 0x72, 0x73, + 0x3d, 0x31, 0x08, 0x71, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x3d, 0x31}; + u16 mdns_offset = index - tx_desc; + u8 i = 0; + + rsvd_page_loc->LocMdnsPara = *page_num; + RTW_INFO("LocMdnsPara : %d\n", rsvd_page_loc->LocMdnsPara); + + /* 1. service info */ + pframe[mdns_offset] = 0x01; // TLV(T) + mdns_offset += 1; + _rtw_memcpy(pframe + mdns_offset, &pwrctl->wowlan_war_offload_mdns_service_info_num, 1); + mdns_offset += 1; + + for(i=0; iwowlan_war_offload_mdns_service_info_num ;i++) + { + u16 srv_rsp_len = 0; + + // 1.1 : construct service name string + // : length of total service name string (service+transport+domain) + pframe[mdns_offset] = psinfo[i].service_len + psinfo[i].transport_len + psinfo[i].domain_len + 4; + mdns_offset += 1; + + // : service name + pframe[mdns_offset] = psinfo[i].service_len; + mdns_offset += 1; + _rtw_memcpy(pframe + mdns_offset, &psinfo[i].service, psinfo[i].service_len); + mdns_offset += psinfo[i].service_len; + + // : transport name + pframe[mdns_offset] = psinfo[i].transport_len; + mdns_offset += 1; + _rtw_memcpy(pframe + mdns_offset, &psinfo[i].transport, psinfo[i].transport_len); + mdns_offset += psinfo[i].transport_len; + + // : domain name + pframe[mdns_offset] = psinfo[i].domain_len; + mdns_offset += 1; + _rtw_memcpy(pframe + mdns_offset, &psinfo[i].domain, psinfo[i].domain_len); + mdns_offset += psinfo[i].domain_len; + + // : delimiter + mdns_offset += 1; + + // 1.2 : construct type srv rsp + pframe[mdns_offset] = psinfo[i].target_len + 19; // length + pframe[mdns_offset + 2] = 0x21; // rsp type (srv) + pframe[mdns_offset + 4] = 0x01; // cache flush + class + _rtw_memcpy(pframe + mdns_offset + 5, &psinfo[i].ttl, 4); // ttl + pframe[mdns_offset + 5] = (u8) ( (psinfo[i].ttl & 0xff000000) >> 24); // ttl - byte0 + pframe[mdns_offset + 6] = (u8) ( (psinfo[i].ttl & 0x00ff0000) >> 16); // ttl - byte1 + pframe[mdns_offset + 7] = (u8) ( (psinfo[i].ttl & 0x0000ff00) >> 8 ); // ttl - byte2 + pframe[mdns_offset + 8] = (u8) (psinfo[i].ttl & 0x000000ff); // ttl - byte3 + pframe[mdns_offset + 10] = psinfo[i].target_len + 9; // data length + _rtw_memcpy(pframe + mdns_offset + 15, &psinfo[i].port, 2); // port + _rtw_memcpy(pframe + mdns_offset + 17, &psinfo[i].target_len, 1); // target len + _rtw_memcpy(pframe + mdns_offset + 18, &psinfo[i].target, psinfo[i].target_len); // target + pframe[mdns_offset + 18 + psinfo[i].target_len] = 0xc0; // message compresion, offset will be filled by fw. + mdns_offset += (1 + psinfo[i].target_len + 19); + + // 1.3 : set the idx of txt rsp + pframe[mdns_offset] = psinfo[i].txt_rsp_idx; + mdns_offset += 1; + } + + /* 2. machine name */ + pframe[mdns_offset] = 0x02; // TLV(T) + mdns_offset += 1; + _rtw_memcpy(pframe + mdns_offset, &pwrctl->wowlan_war_offload_mdns_mnane_num, 1); // NUM + mdns_offset += 1; + + for(i=0; iwowlan_war_offload_mdns_mnane_num; i++) + { + pframe[mdns_offset] = pwrctl->wowlan_war_offload_mdns_mnane[i].name_len; + _rtw_memcpy(pframe + mdns_offset + 1, pwrctl->wowlan_war_offload_mdns_mnane[i].name, + pwrctl->wowlan_war_offload_mdns_mnane[i].name_len); // machine name + mdns_offset += (1+pwrctl->wowlan_war_offload_mdns_mnane[i].name_len); + } + + /* 3. A rsp */ + pframe[mdns_offset] = 0x03; // TLV(T) + pframe[mdns_offset + 1] = 14; // TLV(L) + pframe[mdns_offset + 3] = 0x01; // rsp type (a) + pframe[mdns_offset + 5] = 0x01; // cache flush + class + pframe[mdns_offset + 9] = 0xf0; // ttl (240 sec) + pframe[mdns_offset + 11] = 4; // length of ipv4 addr. + _rtw_memcpy(pframe + mdns_offset + 12, &pwrctl->wowlan_war_offload_ipv4.ip_addr[0], 4); + mdns_offset += (2 + 14); + + /* 4. AAAA rsp */ + pframe[mdns_offset] = 0x04; // TLV(T) + pframe[mdns_offset + 1] = 26; // TLV(L) + pframe[mdns_offset + 3] = 0x1c; // rsp type (aaaa) + pframe[mdns_offset + 5] = 0x01; // cache flush + class + pframe[mdns_offset + 9] = 0xf0; // ttl (240 sec) + pframe[mdns_offset + 11] = 16; // length of ipv6 addr. + _rtw_memcpy(pframe + mdns_offset + 12, &pwrctl->wowlan_war_offload_ipv6.ipv6_addr[0], 16); + mdns_offset += (2 + 26); + + /* 5. PTR rsp */ + pframe[mdns_offset] = 0x05; // TLV(T) + pframe[mdns_offset + 1] = 13 + pwrctl->wowlan_war_offload_mdns_domain_name_len; // TLV(L) + pframe[mdns_offset + 3] = 0x0c; // rsp type (aaaa) + pframe[mdns_offset + 5] = 0x01; // cache flush + class + pframe[mdns_offset + 8] = 0x1c; // ttl + pframe[mdns_offset + 9] = 0x20; // ttl (7200 sec) + pframe[mdns_offset + 11] = 3 + pwrctl->wowlan_war_offload_mdns_domain_name_len; // data length + pframe[mdns_offset + 12] = pwrctl->wowlan_war_offload_mdns_domain_name_len; // domain name length + _rtw_memcpy(pframe + mdns_offset + 13, &pwrctl->wowlan_war_offload_mdns_domain_name, + pwrctl->wowlan_war_offload_mdns_domain_name_len); + pframe[mdns_offset + 13 + pwrctl->wowlan_war_offload_mdns_domain_name_len] = 0xc0; // message compression + mdns_offset += (2 + 13 + pwrctl->wowlan_war_offload_mdns_domain_name_len); + + /* 6. TXT in PTR rsp */ + pframe[mdns_offset] = 0x06; // TLV(T) + pframe[mdns_offset + 1] = 31; // TLV(L) + _rtw_memcpy(pframe + mdns_offset + 2, &txt_in_ptr, 31); + mdns_offset += (2 + 31); + + /* 7. TXT rsp */ + pframe[mdns_offset] = 0x07; // TLV(T) + mdns_offset += 1; + _rtw_memcpy(pframe + mdns_offset, &pwrctl->wowlan_war_offload_mdns_txt_rsp_num, 1); // NUM + mdns_offset += 1; + + for(i=0; iwowlan_war_offload_mdns_txt_rsp_num; i++) + { + u16 txt_rsp_len = pwrctl->wowlan_war_offload_mdns_txt_rsp[i].txt_len; + + if(pwrctl->wowlan_war_offload_mdns_txt_rsp[i].txt_len==0) + { + _rtw_memcpy(pframe + mdns_offset, &txt_rsp_len, 2); + mdns_offset += ( 2 + txt_rsp_len ); + continue; + } + + txt_rsp_len += 10; + _rtw_memcpy(pframe + mdns_offset, &txt_rsp_len, 2); + pframe[mdns_offset + 3] = 0x10; // rsp type (txt) + pframe[mdns_offset + 5] = 0x01; // cache flush + class + pframe[mdns_offset + 8] = 0x1c; // ttl + pframe[mdns_offset + 9] = 0x20; // ttl (7200 sec) + pframe[mdns_offset + 10] = (u8) ((pwrctl->wowlan_war_offload_mdns_txt_rsp[i].txt_len & 0xff00) >> 8); + pframe[mdns_offset + 11] = (u8) (pwrctl->wowlan_war_offload_mdns_txt_rsp[i].txt_len & 0x00ff); + _rtw_memcpy(pframe + mdns_offset + 12, &pwrctl->wowlan_war_offload_mdns_txt_rsp[i].txt, + pwrctl->wowlan_war_offload_mdns_txt_rsp[i].txt_len); + mdns_offset += ( 2 + txt_rsp_len ); + } + + CurtPktPageNum = (u8)PageNum(mdns_offset - index, page_size)+1; + *page_num += CurtPktPageNum; + *total_pkt_len = index + (page_size * CurtPktPageNum); + index += (CurtPktPageNum * page_size); + } +#endif /* defined(CONFIG_OFFLOAD_MDNS_V4) || defined(CONFIG_OFFLOAD_MDNS_V6) */ + +#ifdef CONFIG_OFFLOAD_MDNS_V4 + if (WAR_MDNS_V4_RSP_EN & pwrctl->wowlan_war_offload_ctrl) { + rsvd_page_loc->LocMdnsv4 = *page_num; + RTW_INFO("LocMdnsv4: %d\n", rsvd_page_loc->LocMdnsv4); + + rtw_hal_construct_mdns_rsp_v4(adapter, &pframe[index], &buf_len, pmlmeinfo->ip_addr); + rtw_hal_fill_fake_txdesc(adapter, &pframe[index - tx_desc], buf_len, _FALSE, _FALSE, _TRUE); + CurtPktPageNum = 16; + *page_num += CurtPktPageNum; + index += (CurtPktPageNum * page_size); + } +#endif /* CONFIG_OFFLOAD_MDNS_V4 */ + +#ifdef CONFIG_OFFLOAD_MDNS_V6 + if (WAR_MDNS_V6_RSP_EN & pwrctl->wowlan_war_offload_ctrl) { + rsvd_page_loc->LocMdnsv6 = *page_num; + RTW_INFO("LocMdnsv6: %d\n", rsvd_page_loc->LocMdnsv6); + + rtw_hal_construct_mdns_rsp_v6(adapter, &pframe[index], &buf_len, pmlmeinfo->ip_addr); + rtw_hal_fill_fake_txdesc(adapter, &pframe[index - tx_desc], buf_len, _FALSE, _FALSE, _TRUE); + CurtPktPageNum = 16; + *page_num += CurtPktPageNum; + index += (CurtPktPageNum * page_size); + } +#endif /* CONFIG_OFFLOAD_MDNS_V6 */ + +#if defined(CONFIG_OFFLOAD_MDNS_V4) || defined(CONFIG_OFFLOAD_MDNS_V6) + *(pframe+tmp_idx+25-tx_desc) = rsvd_page_loc->LocMdnsPara; + *(pframe+tmp_idx+26-tx_desc) = rsvd_page_loc->LocMdnsv4; + *(pframe+tmp_idx+27-tx_desc) = rsvd_page_loc->LocMdnsv6; +#endif /* defined(CONFIG_OFFLOAD_MDNS_V4) || defined(CONFIG_OFFLOAD_MDNS_V6) */ + + } + //rtw_dump_rsvd_page(RTW_DBGDUMP, adapter, rsvd_page_loc->LocIpParm, 46); +#endif /* CONFIG_WAR_OFFLOAD */ + +#if defined(CONFIG_WOW_PATTERN_IN_TXFIFO) + /* pattern_rsvd_page_loc will be used by rtw_read_from_frame_mask() */ + pwrctl->pattern_rsvd_page_loc = *page_num; + RTW_INFO("LocPatternInfo: %d\n", pwrctl->pattern_rsvd_page_loc); + rtw_hal_construct_pattern_info(adapter, + &pframe[index - tx_desc], + &PatternLen); + + /* Set cam_start_offset to REG_TXBUF_WKCAM_OFFSET + * Cam address(TxBufer pointer) access 8 bytes at a time + */ + + // Get rsvd page start page number + pattern located page + cam_start_offset = rtw_read8(adapter, REG_BCNQ_BDNY) + *page_num; + cam_start_offset *= page_size; + cam_start_offset /= 8; + + reg_cam_start_offset_val = rtw_read32(adapter, REG_TXBUF_WKCAM_OFFSET); + reg_cam_start_offset_val &= ~(WKCAM_OFFSET_BIT_MASK << WKCAM_OFFSET_BIT_MASK_OFFSET); + reg_cam_start_offset_val |= (cam_start_offset << WKCAM_OFFSET_BIT_MASK_OFFSET); + rtw_write32(adapter, REG_TXBUF_WKCAM_OFFSET, reg_cam_start_offset_val); + + /* Set pattern number to REG_WKFMCAM_NUM */ + rtw_write8(adapter, REG_WKFMCAM_NUM, PatternLen / WKFMCAM_SIZE); + + CurtPktPageNum = (u8)PageNum(PatternLen, page_size); + *page_num += CurtPktPageNum; + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-PatternInfo", CurtPktPageNum, *page_num, index); + +#endif /* CONFIG_WOW_PATTERN_IN_TXFIFO */ + + /*Reserve 1 page for AOAC report*/ + rsvd_page_loc->LocAOACReport = *page_num; + RTW_INFO("LocAOACReport: %d\n", rsvd_page_loc->LocAOACReport); + *page_num += 1; + *total_pkt_len = index + (page_size * 1); + RSVD_PAGE_CFG("WOW-AOAC", 1, *page_num, *total_pkt_len); + + + } else { +#ifdef CONFIG_PNO_SUPPORT + if (pwrctl->wowlan_in_resume == _FALSE && + pwrctl->pno_inited == _TRUE) { + + /* Broadcast Probe Request */ + rsvd_page_loc->LocProbePacket = *page_num; + + RTW_INFO("loc_probe_req: %d\n", + rsvd_page_loc->LocProbePacket); + + rtw_hal_construct_ProbeReq( + adapter, + &pframe[index], + &ProbeReqLength, + NULL); + + rtw_hal_fill_fake_txdesc(adapter, + &pframe[index - tx_desc], + ProbeReqLength, _FALSE, _FALSE, _FALSE); + + CurtPktPageNum = + (u8)PageNum(tx_desc + ProbeReqLength, page_size); + + *page_num += CurtPktPageNum; + + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-ProbeReq", CurtPktPageNum, *page_num, 0); + + /* Hidden SSID Probe Request */ + ssid_num = pwrctl->pnlo_info->hidden_ssid_num; + + for (pno_index = 0 ; pno_index < ssid_num ; pno_index++) { + pwrctl->pnlo_info->loc_probe_req[pno_index] = + *page_num; + + rtw_hal_construct_ProbeReq( + adapter, + &pframe[index], + &ProbeReqLength, + &pwrctl->pno_ssid_list->node[pno_index]); + + rtw_hal_fill_fake_txdesc(adapter, + &pframe[index - tx_desc], + ProbeReqLength, _FALSE, _FALSE, _FALSE); + + CurtPktPageNum = + (u8)PageNum(tx_desc + ProbeReqLength, page_size); + + *page_num += CurtPktPageNum; + + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-ProbeReq", CurtPktPageNum, *page_num, 0); + } + + /* PNO INFO Page */ + rsvd_page_loc->LocPNOInfo = *page_num; + RTW_INFO("LocPNOInfo: %d\n", rsvd_page_loc->LocPNOInfo); + rtw_hal_construct_PNO_info(adapter, + &pframe[index - tx_desc], + &PNOLength); + + CurtPktPageNum = (u8)PageNum(PNOLength, page_size); + *page_num += CurtPktPageNum; + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-PNOInfo", CurtPktPageNum, *page_num, 0); + + /* Scan Info Page */ + rsvd_page_loc->LocScanInfo = *page_num; + RTW_INFO("LocScanInfo: %d\n", rsvd_page_loc->LocScanInfo); + rtw_hal_construct_scan_info(adapter, + &pframe[index - tx_desc], + &ScanInfoLength); + + CurtPktPageNum = (u8)PageNum(ScanInfoLength, page_size); + *page_num += CurtPktPageNum; + *total_pkt_len = index + ScanInfoLength; + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-ScanInfo", CurtPktPageNum, *page_num, *total_pkt_len); + } +#endif /* CONFIG_PNO_SUPPORT */ + } +} +#endif /*CONFIG_WOWLAN*/ + +#ifdef CONFIG_P2P_WOWLAN +void rtw_hal_set_p2p_wow_fw_rsvd_page(_adapter *adapter, u8 *pframe, u16 index, + u8 tx_desc, u32 page_size, u8 *page_num, u32 *total_pkt_len, + RSVDPAGE_LOC *rsvd_page_loc) +{ + u32 P2PNegoRspLength = 0, P2PInviteRspLength = 0; + u32 P2PPDRspLength = 0, P2PProbeRspLength = 0, P2PBCNLength = 0; + u8 CurtPktPageNum = 0; + + /* P2P Beacon */ + rsvd_page_loc->LocP2PBeacon = *page_num; + rtw_hal_construct_P2PBeacon(adapter, &pframe[index], &P2PBCNLength); + rtw_hal_fill_fake_txdesc(adapter, &pframe[index - tx_desc], + P2PBCNLength, _FALSE, _FALSE, _FALSE); + +#if 0 + RTW_INFO("%s(): HW_VAR_SET_TX_CMD: PROBE RSP %p %d\n", + __FUNCTION__, &pframe[index - tx_desc], (P2PBCNLength + tx_desc)); +#endif + + CurtPktPageNum = (u8)PageNum(tx_desc + P2PBCNLength, page_size); + + *page_num += CurtPktPageNum; + + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-P2P-Beacon", CurtPktPageNum, *page_num, 0); + + /* P2P Probe rsp */ + rsvd_page_loc->LocP2PProbeRsp = *page_num; + rtw_hal_construct_P2PProbeRsp(adapter, &pframe[index], + &P2PProbeRspLength); + rtw_hal_fill_fake_txdesc(adapter, &pframe[index - tx_desc], + P2PProbeRspLength, _FALSE, _FALSE, _FALSE); + + /* RTW_INFO("%s(): HW_VAR_SET_TX_CMD: PROBE RSP %p %d\n", */ + /* __FUNCTION__, &pframe[index-tx_desc], (P2PProbeRspLength+tx_desc)); */ + + CurtPktPageNum = (u8)PageNum(tx_desc + P2PProbeRspLength, page_size); + + *page_num += CurtPktPageNum; + + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-P2P-ProbeRsp", CurtPktPageNum, *page_num, 0); + + /* P2P nego rsp */ + rsvd_page_loc->LocNegoRsp = *page_num; + rtw_hal_construct_P2PNegoRsp(adapter, &pframe[index], + &P2PNegoRspLength); + rtw_hal_fill_fake_txdesc(adapter, &pframe[index - tx_desc], + P2PNegoRspLength, _FALSE, _FALSE, _FALSE); + + /* RTW_INFO("%s(): HW_VAR_SET_TX_CMD: QOS NULL DATA %p %d\n", */ + /* __FUNCTION__, &pframe[index-tx_desc], (NegoRspLength+tx_desc)); */ + + CurtPktPageNum = (u8)PageNum(tx_desc + P2PNegoRspLength, page_size); + + *page_num += CurtPktPageNum; + + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-P2P-NegoRsp", CurtPktPageNum, *page_num, 0); + + /* P2P invite rsp */ + rsvd_page_loc->LocInviteRsp = *page_num; + rtw_hal_construct_P2PInviteRsp(adapter, &pframe[index], + &P2PInviteRspLength); + rtw_hal_fill_fake_txdesc(adapter, &pframe[index - tx_desc], + P2PInviteRspLength, _FALSE, _FALSE, _FALSE); + + /* RTW_INFO("%s(): HW_VAR_SET_TX_CMD: QOS NULL DATA %p %d\n", */ + /* __FUNCTION__, &pframe[index-tx_desc], (InviteRspLength+tx_desc)); */ + + CurtPktPageNum = (u8)PageNum(tx_desc + P2PInviteRspLength, page_size); + + *page_num += CurtPktPageNum; + + index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("WOW-P2P-InviteRsp", CurtPktPageNum, *page_num, 0); + + /* P2P provision discovery rsp */ + rsvd_page_loc->LocPDRsp = *page_num; + rtw_hal_construct_P2PProvisionDisRsp(adapter, + &pframe[index], &P2PPDRspLength); + + rtw_hal_fill_fake_txdesc(adapter, &pframe[index - tx_desc], + P2PPDRspLength, _FALSE, _FALSE, _FALSE); + + /* RTW_INFO("%s(): HW_VAR_SET_TX_CMD: QOS NULL DATA %p %d\n", */ + /* __FUNCTION__, &pframe[index-tx_desc], (PDRspLength+tx_desc)); */ + + CurtPktPageNum = (u8)PageNum(tx_desc + P2PPDRspLength, page_size); + + *page_num += CurtPktPageNum; + + *total_pkt_len = index + P2PPDRspLength; + RSVD_PAGE_CFG("WOW-P2P-PDR", CurtPktPageNum, *page_num, *total_pkt_len); + + index += (CurtPktPageNum * page_size); + + +} +#endif /* CONFIG_P2P_WOWLAN */ + +#ifdef CONFIG_LPS_PG +#ifndef DBG_LPSPG_INFO_DUMP +#define DBG_LPSPG_INFO_DUMP 1 +#endif + +#include "hal_halmac.h" + +#ifdef CONFIG_RTL8822C +static int rtw_lps_pg_set_dpk_info_rsvd_page(_adapter *adapter) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct dm_struct *dm = adapter_to_phydm(adapter); + struct rsvd_page_cache_t *cache = &pwrpriv->lpspg_dpk_info; + u8 *info = NULL; + u32 info_len; + int ret = _FAIL; + + /* get length */ + halrf_dpk_info_rsvd_page(dm, NULL, &info_len); + if (!info_len) { + RTW_ERR("get %s length fail\n", cache->name); + goto exit; + } + + /* allocate buf */ + info = rtw_zmalloc(info_len); + if (!info) { + RTW_ERR("alloc %s buffer fail(len=%d)\n", cache->name, info_len); + goto exit; + } + + /* get content */ + halrf_dpk_info_rsvd_page(dm, info, NULL); + + if (rsvd_page_cache_update_data(cache, info, info_len)) { + + #if (DBG_LPSPG_INFO_DUMP >= 1) + RTW_INFO_DUMP(cache->name, info, info_len); + #endif + + ret = rtw_halmac_download_rsvd_page(dvobj, cache->loc, info, info_len); + ret = !ret ? _SUCCESS : _FAIL; + if (ret != _SUCCESS) { + RTW_ERR("download %s rsvd page to offset:%u fail\n", cache->name, cache->loc); + goto free_mem; + } + + #if (DBG_LPSPG_INFO_DUMP >= 2) + RTW_INFO("get %s from rsvd page offset:%d\n", cache->name, cache->loc); + rtw_dump_rsvd_page(RTW_DBGDUMP, adapter, cache->loc, cache->page_num); + #endif + } + +free_mem: + rtw_mfree(info, info_len); + +exit: + return ret; +} + +static int rtw_lps_pg_set_iqk_info_rsvd_page(_adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct dm_struct *dm = adapter_to_phydm(adapter); + struct rsvd_page_cache_t *cache = &pwrpriv->lpspg_iqk_info; + u8 *info = NULL; + u32 info_len = 0; + int ret = _FAIL; + + if (hal_data->RegIQKFWOffload) { + rsvd_page_cache_free_data(cache); + ret = _SUCCESS; + goto exit; + } + + /* get length */ + halrf_iqk_info_rsvd_page(dm, NULL, &info_len); + if (!info_len) { + RTW_ERR("get %s length fail\n", cache->name); + goto exit; + } + + /* allocate buf */ + info = rtw_zmalloc(info_len); + if (!info) { + RTW_ERR("alloc %s buffer fail(len=%d)\n", cache->name, info_len); + goto exit; + } + + /* get content */ + halrf_iqk_info_rsvd_page(dm, info, NULL); + + if (rsvd_page_cache_update_data(cache, info, info_len)) { + + #if (DBG_LPSPG_INFO_DUMP >= 1) + RTW_INFO_DUMP(cache->name, info, info_len); + #endif + + ret = rtw_halmac_download_rsvd_page(dvobj, cache->loc, info, info_len); + ret = !ret ? _SUCCESS : _FAIL; + if (ret != _SUCCESS) { + RTW_ERR("download %s rsvd page to offset:%u fail\n", cache->name, cache->loc); + goto free_mem; + } + + #if (DBG_LPSPG_INFO_DUMP >= 2) + RTW_INFO("get %s from rsvd page offset:%d\n", cache->name, cache->loc); + rtw_dump_rsvd_page(RTW_DBGDUMP, adapter, cache->loc, cache->page_num); + #endif + } + +free_mem: + rtw_mfree(info, info_len); + +exit: + return ret; +} +#endif /* CONFIG_RTL8822C */ + +static void rtw_hal_build_lps_pg_info_rsvd_page(struct dvobj_priv *dvobj, _adapter *ld_sta_iface, u8 *buf, u32 *buf_size) +{ +#define LPS_PG_INFO_RSVD_LEN 16 + + if (buf) { + _adapter *adapter = dvobj_get_primary_adapter(dvobj); + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + struct sta_info *psta; +#ifdef CONFIG_MBSSID_CAM + u8 cam_id = INVALID_CAM_ID; +#endif + u8 *psec_cam_id = buf + 8; + u8 sec_cam_num = 0; + u8 drv_rsvdpage_num = 0; + + if (ld_sta_iface) { + psta = rtw_get_stainfo(&ld_sta_iface->stapriv, get_bssid(&ld_sta_iface->mlmepriv)); + if (!psta) { + RTW_ERR("%s [ERROR] sta is NULL\n", __func__); + rtw_warn_on(1); + goto size_chk; + } + /*Byte 0 - used macid*/ + LPSPG_RSVD_PAGE_SET_MACID(buf, psta->cmn.mac_id); + RTW_INFO("[LPSPG-INFO] mac_id:%d\n", psta->cmn.mac_id); + } + +#ifdef CONFIG_MBSSID_CAM + /*Byte 1 - used BSSID CAM entry*/ + cam_id = rtw_mbid_cam_search_by_ifaceid(adapter, adapter->iface_id); + if (cam_id != INVALID_CAM_ID) + LPSPG_RSVD_PAGE_SET_MBSSCAMID(buf, cam_id); + RTW_INFO("[LPSPG-INFO] mbss_cam_id:%d\n", cam_id); +#endif + +#ifdef CONFIG_WOWLAN /*&& pattern match cam used*/ + /*Btye 2 - Max used Pattern Match CAM entry*/ + if (pwrpriv->wowlan_mode == _TRUE + && ld_sta_iface && check_fwstate(&ld_sta_iface->mlmepriv, WIFI_ASOC_STATE) == _TRUE) { + LPSPG_RSVD_PAGE_SET_PMC_NUM(buf, pwrpriv->wowlan_pattern_idx); + RTW_INFO("[LPSPG-INFO] Max Pattern Match CAM entry :%d\n", pwrpriv->wowlan_pattern_idx); + } +#endif +#ifdef CONFIG_BEAMFORMING /*&& MU BF*/ + /*Btye 3 - Max MU rate table Group ID*/ + LPSPG_RSVD_PAGE_SET_MU_RAID_GID(buf, 0); + RTW_INFO("[LPSPG-INFO] Max MU rate table Group ID :%d\n", 0); +#endif + + /*Btye 8 ~15 - used Security CAM entry */ + sec_cam_num = rtw_get_sec_camid(adapter, 8, psec_cam_id); + + /*Btye 4 - used Security CAM entry number*/ + if (sec_cam_num < 8) + LPSPG_RSVD_PAGE_SET_SEC_CAM_NUM(buf, sec_cam_num); + RTW_INFO("[LPSPG-INFO] Security CAM entry number :%d\n", sec_cam_num); + + /*Btye 5 - Txbuf used page number for fw offload*/ + if (pwrpriv->wowlan_mode == _TRUE || pwrpriv->wowlan_ap_mode == _TRUE) + drv_rsvdpage_num = rtw_hal_get_txbuff_rsvd_page_num(adapter, _TRUE); + else + drv_rsvdpage_num = rtw_hal_get_txbuff_rsvd_page_num(adapter, _FALSE); + LPSPG_RSVD_PAGE_SET_DRV_RSVDPAGE_NUM(buf, drv_rsvdpage_num); + RTW_INFO("[LPSPG-INFO] DRV's rsvd page numbers :%d\n", drv_rsvdpage_num); + } + +size_chk: + if (buf_size) + *buf_size = LPS_PG_INFO_RSVD_LEN; +} + +static int rtw_hal_set_lps_pg_info_rsvd_page(_adapter *adapter) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct rsvd_page_cache_t *cache = &pwrpriv->lpspg_info; + u8 *info = NULL; + u32 info_len = 0; + int ret = _FAIL; + + /* get length */ + rtw_hal_build_lps_pg_info_rsvd_page(dvobj, adapter, NULL, &info_len); + if (!info_len) { + RTW_ERR("get %s length fail\n", cache->name); + goto exit; + } + + /* allocate buf */ + info = rtw_zmalloc(info_len); + if (!info) { + RTW_ERR("alloc %s buffer fail(len=%d)\n", cache->name, info_len); + goto exit; + } + + /* get content */ + rtw_hal_build_lps_pg_info_rsvd_page(dvobj, adapter, info, NULL); + + if (rsvd_page_cache_update_data(cache, info, info_len)) { + + #if (DBG_LPSPG_INFO_DUMP >= 1) + RTW_INFO_DUMP(cache->name, info, info_len); + #endif + + ret = rtw_halmac_download_rsvd_page(dvobj, cache->loc, info, info_len); + ret = !ret ? _SUCCESS : _FAIL; + if (ret != _SUCCESS) { + RTW_ERR("download %s rsvd page to offset:%u fail\n", cache->name, cache->loc); + goto free_mem; + } + + #if (DBG_LPSPG_INFO_DUMP >= 2) + RTW_INFO("get %s from rsvd page offset:%d\n", cache->name, cache->loc); + rtw_dump_rsvd_page(RTW_DBGDUMP, adapter, cache->loc, cache->page_num); + #endif + } + +free_mem: + rtw_mfree(info, info_len); + +exit: + return ret; +} + +static void rtw_lps_pg_set_rsvd_page(_adapter *adapter, u8 *frame, u16 *index + , u8 txdesc_size, u32 page_size, u8 *total_page_num + , bool is_wow_mode, _adapter *ld_sta_iface, bool only_get_page_num) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + struct rsvd_page_cache_t *cache; + bool rsvd = 1; + u8 *pos; + u32 len; + + if (is_wow_mode) { + /* lps_level will not change when enter wow_mode */ + if (pwrctl->lps_level != LPS_PG) + rsvd = 0; + } else { + if (!only_get_page_num && !ld_sta_iface) + rsvd = 0; + } + + pos = only_get_page_num ? NULL : frame + *index; + +#ifdef CONFIG_RTL8822C + if (IS_8822C_SERIES(hal_data->version_id)) { + /* LPSPG_DPK_INFO */ + cache = &pwrctl->lpspg_dpk_info; + if (rsvd) { + if (pwrctl->lps_level != LPS_PG) + pos = NULL; + len = 0; + halrf_dpk_info_rsvd_page(adapter_to_phydm(adapter), pos, &len); + #if (DBG_LPSPG_INFO_DUMP >= 1) + if (pos) + RTW_INFO_DUMP(cache->name, pos, len); + #endif + rsvd_page_cache_update_all(cache, *total_page_num, txdesc_size, page_size, pos, len); + *total_page_num += cache->page_num; + *index += page_size * cache->page_num; + pos = only_get_page_num ? NULL : frame + *index; + RSVD_PAGE_CFG(cache->name, cache->page_num, *total_page_num, *index); + } else + rsvd_page_cache_free(cache); + + /* LPSPG_IQK_INFO */ + cache = &pwrctl->lpspg_iqk_info; + if (rsvd + /* RegIQKFWOffload will not change when enter wow_mode */ + && !(is_wow_mode && hal_data->RegIQKFWOffload) + ) { + if (pwrctl->lps_level != LPS_PG || hal_data->RegIQKFWOffload) + pos = NULL; + len = 0; + halrf_iqk_info_rsvd_page(adapter_to_phydm(adapter), pos, &len); + #if (DBG_LPSPG_INFO_DUMP >= 1) + if (pos) + RTW_INFO_DUMP(cache->name, pos, len); + #endif + rsvd_page_cache_update_all(cache, *total_page_num, txdesc_size, page_size, pos, len); + *total_page_num += cache->page_num; + *index += page_size * cache->page_num; + pos = only_get_page_num ? NULL : frame + *index; + RSVD_PAGE_CFG(cache->name, cache->page_num, *total_page_num, *index); + } else + rsvd_page_cache_free(cache); + } +#endif + + /* LPSPG_INFO */ + cache = &pwrctl->lpspg_info; + if (rsvd) { + if (pwrctl->lps_level != LPS_PG) + pos = NULL; + rtw_hal_build_lps_pg_info_rsvd_page(adapter_to_dvobj(adapter), ld_sta_iface, pos, &len); + #if (DBG_LPSPG_INFO_DUMP >= 1) + if (pos) + RTW_INFO_DUMP(cache->name, pos, len); + #endif + rsvd_page_cache_update_all(cache, *total_page_num, txdesc_size, page_size, pos, len); + *total_page_num += cache->page_num; + *index += page_size * cache->page_num; + pos = only_get_page_num ? NULL : frame + *index; + RSVD_PAGE_CFG(cache->name, cache->page_num, *total_page_num, *index); + } else + rsvd_page_cache_free(cache); +} + +u8 rtw_hal_set_lps_pg_info_cmd(_adapter *adapter) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + + u8 lpspg_info[H2C_LPS_PG_INFO_LEN] = {0}; + u8 ret = _FAIL; + + if (_NO_PRIVACY_ != adapter->securitypriv.dot11PrivacyAlgrthm) + SET_H2CCMD_LPSPG_SEC_CAM_EN(lpspg_info, 1); /*SecurityCAM_En*/ + +#ifdef CONFIG_MBSSID_CAM + SET_H2CCMD_LPSPG_MBID_CAM_EN(lpspg_info, 1); /*BSSIDCAM_En*/ +#endif + +#if defined(CONFIG_WOWLAN) && defined(CONFIG_WOW_PATTERN_HW_CAM) + if (pwrpriv->wowlan_mode == _TRUE && + check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE) { + + SET_H2CCMD_LPSPG_PMC_CAM_EN(lpspg_info, 1); /*PatternMatchCAM_En*/ + } +#endif + +#ifdef CONFIG_MACID_SEARCH + SET_H2CCMD_LPSPG_MACID_SEARCH_EN(lpspg_info, 1); /*MACIDSearch_En*/ +#endif + +#ifdef CONFIG_TX_SC + SET_H2CCMD_LPSPG_TXSC_EN(lpspg_info, 1); /*TXSC_En*/ +#endif + +#ifdef CONFIG_BEAMFORMING /*&& MU BF*/ + SET_H2CCMD_LPSPG_MU_RATE_TB_EN(lpspg_info, 1); /*MURateTable_En*/ +#endif + + SET_H2CCMD_LPSPG_LOC(lpspg_info, pwrpriv->lpspg_info.loc); + +#ifdef CONFIG_RTL8822C + if (pwrpriv->bFwCurrentInPSMode == _FALSE) { + SET_H2CCMD_LPSPG_DPK_INFO_LOC(lpspg_info, pwrpriv->lpspg_dpk_info.loc); + if (!GET_HAL_DATA(adapter)->RegIQKFWOffload) + SET_H2CCMD_LPSPG_IQK_INFO_LOC(lpspg_info, pwrpriv->lpspg_iqk_info.loc); + } else { + SET_H2CCMD_LPSPG_DPK_INFO_LOC(lpspg_info, 0); + if (!GET_HAL_DATA(adapter)->RegIQKFWOffload) + SET_H2CCMD_LPSPG_IQK_INFO_LOC(lpspg_info, 0); + } +#endif + +#if (DBG_LPSPG_INFO_DUMP >= 1) + RTW_INFO_DUMP("H2C_LPS_PG_INFO: ", lpspg_info, H2C_LPS_PG_INFO_LEN); +#endif + + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_LPS_PG_INFO, + H2C_LPS_PG_INFO_LEN, + lpspg_info); + return ret; +} +u8 rtw_hal_set_lps_pg_info(_adapter *adapter) +{ + u8 ret = _FAIL; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + + if (pwrpriv->lpspg_info.loc == 0) { + RTW_ERR("%s lpspg_info.loc = 0\n", __func__); + rtw_warn_on(1); + return ret; + } + #ifdef CONFIG_RTL8822C + rtw_lps_pg_set_dpk_info_rsvd_page(adapter); + rtw_lps_pg_set_iqk_info_rsvd_page(adapter); + #endif + rtw_hal_set_lps_pg_info_rsvd_page(adapter); + + ret = rtw_hal_set_lps_pg_info_cmd(adapter); + + return ret; +} + +void rtw_hal_lps_pg_rssi_lv_decide(_adapter *adapter, struct sta_info *sta) +{ +#if 0 + if (sta->cmn.ra_info.rssi_level >= 4) + sta->lps_pg_rssi_lv = 3; /*RSSI High - 1SS_VHT_MCS7*/ + else if (sta->cmn.ra_info.rssi_level >= 2) + sta->lps_pg_rssi_lv = 2; /*RSSI Middle - 1SS_VHT_MCS3*/ + else + sta->lps_pg_rssi_lv = 1; /*RSSI Lower - Lowest_rate*/ +#else + sta->lps_pg_rssi_lv = 0; +#endif + RTW_INFO("%s mac-id:%d, rssi:%d, rssi_level:%d, lps_pg_rssi_lv:%d\n", + __func__, sta->cmn.mac_id, sta->cmn.rssi_stat.rssi, sta->cmn.ra_info.rssi_level, sta->lps_pg_rssi_lv); +} + +void rtw_hal_lps_pg_handler(_adapter *adapter, enum lps_pg_hdl_id hdl_id) +{ + struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct sta_priv *pstapriv = &adapter->stapriv; + struct sta_info *sta; + + sta = rtw_get_stainfo(pstapriv, pmlmeinfo->network.MacAddress); + + switch (hdl_id) { + case LPS_PG_INFO_CFG: + rtw_hal_set_lps_pg_info(adapter); + break; + case LPS_PG_REDLEMEM: + if (IS_8822C_SERIES(GET_HAL_DATA(adapter)->version_id)) + break; + + /*set xmit_block*/ + rtw_set_xmit_block(adapter, XMIT_BLOCK_REDLMEM); + if (_FAIL == rtw_hal_fw_mem_dl(adapter, FW_EMEM)) + rtw_warn_on(1); + /*clearn xmit_block*/ + rtw_clr_xmit_block(adapter, XMIT_BLOCK_REDLMEM); + break; + case LPS_PG_PHYDM_DIS:/*Disable RA and PT by H2C*/ + if (IS_8822C_SERIES(GET_HAL_DATA(adapter)->version_id)) + break; + + if (sta) + rtw_phydm_lps_pg_hdl(adapter, sta, _TRUE); + break; + case LPS_PG_PHYDM_EN:/*Enable RA and PT by H2C*/ + if (IS_8822C_SERIES(GET_HAL_DATA(adapter)->version_id)) + break; + + if (sta) { + rtw_hal_lps_pg_rssi_lv_decide(adapter, sta); + rtw_phydm_lps_pg_hdl(adapter, sta, _FALSE); + sta->lps_pg_rssi_lv = 0; + } + break; + + default: + break; + } +} + +#endif /*CONFIG_LPS_PG*/ + +static u8 _rtw_mi_assoc_if_num(_adapter *adapter) +{ + u8 mi_iface_num = 0; + + if (0) { + RTW_INFO("[IFS_ASSOC_STATUS] - STA :%d", DEV_STA_LD_NUM(adapter_to_dvobj(adapter))); + RTW_INFO("[IFS_ASSOC_STATUS] - AP:%d", DEV_AP_NUM(adapter_to_dvobj(adapter))); + RTW_INFO("[IFS_ASSOC_STATUS] - AP starting :%d", DEV_AP_STARTING_NUM(adapter_to_dvobj(adapter))); + RTW_INFO("[IFS_ASSOC_STATUS] - MESH :%d", DEV_MESH_NUM(adapter_to_dvobj(adapter))); + RTW_INFO("[IFS_ASSOC_STATUS] - ADHOC :%d", DEV_ADHOC_NUM(adapter_to_dvobj(adapter))); + /*RTW_INFO("[IFS_ASSOC_STATUS] - P2P-GC :%d", DEV_P2P_GC_NUM(adapter_to_dvobj(adapter)));*/ + /*RTW_INFO("[IFS_ASSOC_STATUS] - P2P-GO :%d", DEV_P2P_GO_NUM(adapter_to_dvobj(adapter)));*/ + } + + mi_iface_num = (DEV_STA_LD_NUM(adapter_to_dvobj(adapter)) + + DEV_AP_NUM(adapter_to_dvobj(adapter)) + + DEV_AP_STARTING_NUM(adapter_to_dvobj(adapter))); + return mi_iface_num; +} +#ifdef CONFIG_CONCURRENT_MODE +static _adapter *_rtw_search_sta_iface(_adapter *adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + _adapter *iface = NULL; + _adapter *sta_iface = NULL; + int i; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (check_fwstate(&iface->mlmepriv, WIFI_STATION_STATE) == _TRUE) { + if (check_fwstate(&iface->mlmepriv, WIFI_ASOC_STATE) == _TRUE) { + sta_iface = iface; + break; + } + } + } + return sta_iface; +} +#if defined(CONFIG_AP_MODE) && defined(CONFIG_BT_COEXIST) +static _adapter *_rtw_search_ap_iface(_adapter *adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + _adapter *iface = NULL; + _adapter *ap_iface = NULL; + int i; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (check_fwstate(&iface->mlmepriv, WIFI_AP_STATE) == _TRUE ) { + ap_iface = iface; + break; + } + } + return ap_iface; +} +#endif/*CONFIG_AP_MODE*/ +#endif/*CONFIG_CONCURRENT_MODE*/ + +#ifdef CONFIG_CUSTOMER01_SMART_ANTENNA +void rtw_hal_set_pathb_phase(_adapter *adapter, u8 phase_idx) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(adapter); + struct PHY_DM_STRUCT *pDM_Odm = &pHalData->odmpriv; + + return phydm_pathb_q_matrix_rotate(pDM_Odm, phase_idx); +} +#endif + +/* + * Description: Fill the reserved packets that FW will use to RSVD page. + * Now we just send 4 types packet to rsvd page. + * (1)Beacon, (2)Ps-poll, (3)Null data, (4)ProbeRsp. + * Input: + * finished - FALSE:At the first time we will send all the packets as a large packet to Hw, + * so we need to set the packet length to total lengh. + * TRUE: At the second time, we should send the first packet (default:beacon) + * to Hw again and set the lengh in descriptor to the real beacon lengh. + * page_num - The amount of reserved page which driver need. + * If this is not NULL, this function doesn't real download reserved + * page, but just count the number of reserved page. + * + * 2009.10.15 by tynli. + * 2017.06.20 modified by Lucas. + * + * Page Size = 128: 8188e, 8723a/b, 8192c/d, + * Page Size = 256: 8192e, 8821a + * Page Size = 512: 8812a + */ + +/*#define DBG_DUMP_SET_RSVD_PAGE*/ +static void _rtw_hal_set_fw_rsvd_page(_adapter *adapter, bool finished, u8 *page_num) +{ + PHAL_DATA_TYPE pHalData; + struct xmit_frame *pcmdframe = NULL; + struct pkt_attrib *pattrib; + struct xmit_priv *pxmitpriv; + struct pwrctrl_priv *pwrctl; + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + u32 BeaconLength = 0, ProbeRspLength = 0, PSPollLength = 0; + u32 NullDataLength = 0, QosNullLength = 0, BTQosNullLength = 0; + u32 ProbeReqLength = 0, NullFunctionDataLength = 0; + u8 TxDescLen = TXDESC_SIZE, TxDescOffset = TXDESC_OFFSET; + u8 TotalPageNum = 0 , CurtPktPageNum = 0 , RsvdPageNum = 0; + u8 *ReservedPagePacket; + u16 BufIndex = 0; + u32 TotalPacketLen = 0, MaxRsvdPageBufSize = 0, PageSize = 0; + RSVDPAGE_LOC RsvdPageLoc; + struct registry_priv *registry_par = &adapter->registrypriv; + +#ifdef DBG_FW_DEBUG_MSG_PKT + u32 fw_dbg_msg_pkt_len = 0; +#endif /*DBG_FW_DEBUG_MSG_PKT*/ + +#ifdef DBG_CONFIG_ERROR_DETECT + struct sreset_priv *psrtpriv; +#endif /* DBG_CONFIG_ERROR_DETECT */ + +#ifdef CONFIG_MCC_MODE + u8 dl_mcc_page = _FAIL; +#endif /* CONFIG_MCC_MODE */ + u8 nr_assoc_if; + + _adapter *sta_iface = NULL; + _adapter *ap_iface = NULL; + + bool is_wow_mode = _FALSE; + + pHalData = GET_HAL_DATA(adapter); +#ifdef DBG_CONFIG_ERROR_DETECT + psrtpriv = &pHalData->srestpriv; +#endif + pxmitpriv = &adapter->xmitpriv; + pwrctl = adapter_to_pwrctl(adapter); + + rtw_hal_get_def_var(adapter, HAL_DEF_TX_PAGE_SIZE, (u8 *)&PageSize); + + if (PageSize == 0) { + RTW_ERR("[Error]: %s, PageSize is zero!!\n", __func__); + return; + } + nr_assoc_if = _rtw_mi_assoc_if_num(adapter); + + if ((pwrctl->wowlan_mode == _TRUE && pwrctl->wowlan_in_resume == _FALSE) || + pwrctl->wowlan_ap_mode == _TRUE || + pwrctl->wowlan_p2p_mode == _TRUE) + is_wow_mode = _TRUE; + + /*page_num for init time to get rsvd page number*/ + /* Prepare ReservedPagePacket */ + if (page_num) { + ReservedPagePacket = rtw_zmalloc(MAX_CMDBUF_SZ); + if (!ReservedPagePacket) { + RTW_WARN("%s: alloc ReservedPagePacket fail!\n", __FUNCTION__); + *page_num = 0xFF; + return; + } + RTW_INFO(FUNC_ADPT_FMT" Get [ %s ] RsvdPageNUm ==>\n", + FUNC_ADPT_ARG(adapter), (is_wow_mode) ? "WOW" : "NOR"); + + } else { + if (is_wow_mode) + RsvdPageNum = rtw_hal_get_txbuff_rsvd_page_num(adapter, _TRUE); + else + RsvdPageNum = rtw_hal_get_txbuff_rsvd_page_num(adapter, _FALSE); + + RTW_INFO(FUNC_ADPT_FMT" PageSize: %d, [ %s ]-RsvdPageNUm: %d\n", + FUNC_ADPT_ARG(adapter), PageSize, (is_wow_mode) ? "WOW" : "NOR", RsvdPageNum); + + MaxRsvdPageBufSize = RsvdPageNum * PageSize; + if (MaxRsvdPageBufSize > MAX_CMDBUF_SZ) { + RTW_ERR("%s MaxRsvdPageBufSize(%d) is larger than MAX_CMDBUF_SZ(%d)", + __func__, MaxRsvdPageBufSize, MAX_CMDBUF_SZ); + rtw_warn_on(1); + return; + } + + pcmdframe = rtw_alloc_cmdxmitframe(pxmitpriv); + if (pcmdframe == NULL) { + RTW_ERR("%s: alloc ReservedPagePacket fail!\n", __FUNCTION__); + return; + } + + ReservedPagePacket = pcmdframe->buf_addr; + } + + _rtw_memset(&RsvdPageLoc, 0, sizeof(RSVDPAGE_LOC)); + + BufIndex = TxDescOffset; + + /*======== beacon content =======*/ + rtw_hal_construct_beacon(adapter, + &ReservedPagePacket[BufIndex], &BeaconLength); + + /* + * When we count the first page size, we need to reserve description size for the RSVD + * packet, it will be filled in front of the packet in TXPKTBUF. + */ + BeaconLength = MAX_BEACON_LEN - TxDescLen; + CurtPktPageNum = (u8)PageNum((TxDescLen + BeaconLength), PageSize); + +#if defined(CONFIG_FW_HANDLE_TXBCN) || defined(CONFIG_PORT_BASED_TXBCN) + CurtPktPageNum = CurtPktPageNum * CONFIG_LIMITED_AP_NUM; +#endif + TotalPageNum += CurtPktPageNum; + + BufIndex += (CurtPktPageNum * PageSize); + + RSVD_PAGE_CFG("Beacon", CurtPktPageNum, TotalPageNum, TotalPacketLen); + + /*======== probe response content ========*/ + if (pwrctl->wowlan_ap_mode == _TRUE) {/*WOW mode*/ + #ifdef CONFIG_CONCURRENT_MODE + if (nr_assoc_if >= 2) + RTW_ERR("Not support > 2 net-interface in WOW\n"); + #endif + /* (4) probe response*/ + RsvdPageLoc.LocProbeRsp = TotalPageNum; + rtw_hal_construct_ProbeRsp( + adapter, &ReservedPagePacket[BufIndex], + &ProbeRspLength, + _FALSE); + rtw_hal_fill_fake_txdesc(adapter, + &ReservedPagePacket[BufIndex - TxDescLen], + ProbeRspLength, _FALSE, _FALSE, _FALSE); + + CurtPktPageNum = (u8)PageNum(TxDescLen + ProbeRspLength, PageSize); + TotalPageNum += CurtPktPageNum; + TotalPacketLen = BufIndex + ProbeRspLength; + BufIndex += (CurtPktPageNum * PageSize); + RSVD_PAGE_CFG("ProbeRsp", CurtPktPageNum, TotalPageNum, TotalPacketLen); + goto download_page; + } + + /*======== ps-poll content * 1 page ========*/ + sta_iface = adapter; + #ifdef CONFIG_CONCURRENT_MODE + if (!MLME_IS_STA(sta_iface) && DEV_STA_LD_NUM(adapter_to_dvobj(sta_iface))) { + sta_iface = _rtw_search_sta_iface(adapter); + RTW_INFO("get ("ADPT_FMT") to create PS-Poll/Null/QosNull\n", ADPT_ARG(sta_iface)); + } + #endif + + if (MLME_IS_STA(sta_iface) || (nr_assoc_if == 0)) { + RsvdPageLoc.LocPsPoll = TotalPageNum; + RTW_INFO("LocPsPoll: %d\n", RsvdPageLoc.LocPsPoll); + rtw_hal_construct_PSPoll(sta_iface, + &ReservedPagePacket[BufIndex], &PSPollLength); + rtw_hal_fill_fake_txdesc(sta_iface, + &ReservedPagePacket[BufIndex - TxDescLen], + PSPollLength, _TRUE, _FALSE, _FALSE); + + CurtPktPageNum = (u8)PageNum((TxDescLen + PSPollLength), PageSize); + + TotalPageNum += CurtPktPageNum; + + BufIndex += (CurtPktPageNum * PageSize); + RSVD_PAGE_CFG("PSPoll", CurtPktPageNum, TotalPageNum, TotalPacketLen); + } + +#ifdef CONFIG_MCC_MODE + /*======== MCC * n page ======== */ + if (MCC_EN(adapter)) {/*Normal mode*/ + dl_mcc_page = rtw_hal_dl_mcc_fw_rsvd_page(adapter, ReservedPagePacket, + &BufIndex, TxDescLen, PageSize, &TotalPageNum, &RsvdPageLoc, page_num); + } else { + dl_mcc_page = _FAIL; + } + + if (dl_mcc_page == _FAIL) +#endif /* CONFIG_MCC_MODE */ + { /*======== null data * 1 page ======== */ + if (MLME_IS_STA(sta_iface) || (nr_assoc_if == 0)) { + RsvdPageLoc.LocNullData = TotalPageNum; + RTW_INFO("LocNullData: %d\n", RsvdPageLoc.LocNullData); + rtw_hal_construct_NullFunctionData( + sta_iface, + &ReservedPagePacket[BufIndex], + &NullDataLength, + _FALSE, 0, 0, _FALSE); + rtw_hal_fill_fake_txdesc(sta_iface, + &ReservedPagePacket[BufIndex - TxDescLen], + NullDataLength, _FALSE, _FALSE, _FALSE); + + CurtPktPageNum = (u8)PageNum(TxDescLen + NullDataLength, PageSize); + + TotalPageNum += CurtPktPageNum; + + BufIndex += (CurtPktPageNum * PageSize); + RSVD_PAGE_CFG("NullData", CurtPktPageNum, TotalPageNum, TotalPacketLen); + } + } + + /*======== Qos null data * 1 page ======== */ + if (pwrctl->wowlan_mode == _FALSE || + pwrctl->wowlan_in_resume == _TRUE) {/*Normal mode*/ + if (MLME_IS_STA(sta_iface) || (nr_assoc_if == 0)) { + RsvdPageLoc.LocQosNull = TotalPageNum; + RTW_INFO("LocQosNull: %d\n", RsvdPageLoc.LocQosNull); + rtw_hal_construct_NullFunctionData(sta_iface, + &ReservedPagePacket[BufIndex], + &QosNullLength, + _TRUE, 0, 0, _FALSE); + rtw_hal_fill_fake_txdesc(sta_iface, + &ReservedPagePacket[BufIndex - TxDescLen], + QosNullLength, _FALSE, _FALSE, _FALSE); + + CurtPktPageNum = (u8)PageNum(TxDescLen + QosNullLength, + PageSize); + + TotalPageNum += CurtPktPageNum; + + BufIndex += (CurtPktPageNum * PageSize); + RSVD_PAGE_CFG("QosNull", CurtPktPageNum, TotalPageNum, TotalPacketLen); + } + } + +#ifdef CONFIG_BT_COEXIST + /*======== BT Qos null data * 1 page ======== */ + if (pwrctl->wowlan_mode == _FALSE || + pwrctl->wowlan_in_resume == _TRUE) {/*Normal mode*/ + + ap_iface = adapter; + #if defined (CONFIG_CONCURRENT_MODE) && defined(CONFIG_AP_MODE) + if (!MLME_IS_AP(ap_iface) && DEV_AP_NUM(adapter_to_dvobj(ap_iface))) { /*DEV_AP_STARTING_NUM*/ + ap_iface = _rtw_search_ap_iface(adapter); + RTW_INFO("get ("ADPT_FMT") to create BTQoSNull\n", ADPT_ARG(ap_iface)); + } + #endif + + if (MLME_IS_AP(ap_iface) || (nr_assoc_if == 0)) { + RsvdPageLoc.LocBTQosNull = TotalPageNum; + + RTW_INFO("LocBTQosNull: %d\n", RsvdPageLoc.LocBTQosNull); + + rtw_hal_construct_NullFunctionData(ap_iface, + &ReservedPagePacket[BufIndex], + &BTQosNullLength, + _TRUE, 0, 0, _FALSE); + + rtw_hal_fill_fake_txdesc(ap_iface, + &ReservedPagePacket[BufIndex - TxDescLen], + BTQosNullLength, _FALSE, _TRUE, _FALSE); + + CurtPktPageNum = (u8)PageNum(TxDescLen + BTQosNullLength, + PageSize); + + TotalPageNum += CurtPktPageNum; + BufIndex += (CurtPktPageNum * PageSize); + + RSVD_PAGE_CFG("BTQosNull", CurtPktPageNum, TotalPageNum, TotalPacketLen); + } + } +#endif /* CONFIG_BT_COEXIT */ + + TotalPacketLen = BufIndex; + +#ifdef DBG_FW_DEBUG_MSG_PKT + /*======== FW DEBUG MSG * n page ======== */ + RsvdPageLoc.loc_fw_dbg_msg_pkt = TotalPageNum; + RTW_INFO("loc_fw_dbg_msg_pkt: %d\n", RsvdPageLoc.loc_fw_dbg_msg_pkt); + rtw_hal_construct_fw_dbg_msg_pkt( + adapter, + &ReservedPagePacket[BufIndex], + &fw_dbg_msg_pkt_len); + + rtw_hal_fill_fake_txdesc(adapter, + &ReservedPagePacket[BufIndex - TxDescLen], + fw_dbg_msg_pkt_len, _FALSE, _FALSE, _FALSE); + + CurtPktPageNum = (u8)PageNum(TxDescLen + fw_dbg_msg_pkt_len, PageSize); + + if (CurtPktPageNum < 2) + CurtPktPageNum = 2; /*Need at least 2 rsvd page*/ + TotalPageNum += CurtPktPageNum; + + TotalPacketLen = BufIndex + fw_dbg_msg_pkt_len; + BufIndex += (CurtPktPageNum * PageSize); +#endif /*DBG_FW_DEBUG_MSG_PKT*/ + +#ifdef CONFIG_LPS_PG + rtw_lps_pg_set_rsvd_page(adapter, ReservedPagePacket, &BufIndex + , TxDescLen, PageSize, &TotalPageNum, is_wow_mode + , (sta_iface && MLME_IS_STA(sta_iface) && MLME_IS_ASOC(sta_iface)) ? sta_iface : NULL + , page_num ? 1 : 0 + ); + TotalPacketLen = BufIndex; +#endif + +#ifdef CONFIG_WOWLAN + /*======== WOW * n page ======== */ + if (pwrctl->wowlan_mode == _TRUE && + pwrctl->wowlan_in_resume == _FALSE && + check_fwstate(pmlmepriv, WIFI_ASOC_STATE)) {/*WOW mode*/ + rtw_hal_set_wow_fw_rsvd_page(adapter, ReservedPagePacket, + BufIndex, TxDescLen, PageSize, + &TotalPageNum, &TotalPacketLen, &RsvdPageLoc); +#ifdef CONFIG_WAR_OFFLOAD + rtw_hal_set_war_offload_parm(adapter, &RsvdPageLoc); +#endif /* CONFIG_WAR_OFFLOAD */ + } +#endif /* CONFIG_WOWLAN */ + +#ifdef CONFIG_P2P_WOWLAN + /*======== P2P WOW * n page ======== */ + if (_TRUE == pwrctl->wowlan_p2p_mode) {/*WOW mode*/ + rtw_hal_set_p2p_wow_fw_rsvd_page(adapter, ReservedPagePacket, + BufIndex, TxDescLen, PageSize, + &TotalPageNum, &TotalPacketLen, &RsvdPageLoc); + } +#endif /* CONFIG_P2P_WOWLAN */ + + /*Note: BufIndex already add a TxDescOffset offset in first Beacon page + * The "TotalPacketLen" is calculate by BufIndex. + * We need to decrease TxDescOffset before doing length check. by yiwei + */ + TotalPacketLen = TotalPacketLen - TxDescOffset; + +download_page: + if (page_num) { + *page_num = TotalPageNum; + rtw_mfree(ReservedPagePacket, MAX_CMDBUF_SZ); + ReservedPagePacket = NULL; + RTW_INFO(FUNC_ADPT_FMT" Get [ %s ] RsvdPageNUm <==\n", + FUNC_ADPT_ARG(adapter), (is_wow_mode) ? "WOW" : "NOR"); + return; + } + + /* RTW_INFO("%s BufIndex(%d), TxDescLen(%d), PageSize(%d)\n",__func__, BufIndex, TxDescLen, PageSize);*/ + RTW_INFO("%s PageNum(%d), pktlen(%d)\n", + __func__, TotalPageNum, TotalPacketLen); + + if (TotalPacketLen > MaxRsvdPageBufSize) { + RTW_ERR("%s : rsvd page size is not enough!!TotalPacketLen %d, MaxRsvdPageBufSize %d\n", + __FUNCTION__, TotalPacketLen, MaxRsvdPageBufSize); + rtw_warn_on(1); + goto error; + } else { + /* update attribute */ + pattrib = &pcmdframe->attrib; + update_mgntframe_attrib(adapter, pattrib); + pattrib->qsel = QSLT_BEACON; + pattrib->pktlen = TotalPacketLen; + pattrib->last_txcmdsz = TotalPacketLen; +#ifdef CONFIG_PCI_HCI + dump_mgntframe(adapter, pcmdframe); +#else + dump_mgntframe_and_wait(adapter, pcmdframe, 100); +#endif + } + + RTW_INFO("%s: Set RSVD page location to Fw ,TotalPacketLen(%d), TotalPageNum(%d)\n", + __func__, TotalPacketLen, TotalPageNum); +#ifdef DBG_DUMP_SET_RSVD_PAGE + RTW_INFO(" ==================================================\n"); + RTW_INFO_DUMP("\n", ReservedPagePacket, TotalPacketLen); + RTW_INFO(" ==================================================\n"); +#endif + + + if (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) + || MLME_IS_AP(adapter) || MLME_IS_MESH(adapter)){ + rtw_hal_set_FwRsvdPage_cmd(adapter, &RsvdPageLoc); +#ifdef DBG_FW_DEBUG_MSG_PKT + rtw_hal_set_fw_dbg_msg_pkt_rsvd_page_cmd(adapter, &RsvdPageLoc); +#endif /*DBG_FW_DEBUG_MSG_PKT*/ +#ifdef CONFIG_WOWLAN + if (pwrctl->wowlan_mode == _TRUE && + pwrctl->wowlan_in_resume == _FALSE) + rtw_hal_set_FwAoacRsvdPage_cmd(adapter, &RsvdPageLoc); +#endif /* CONFIG_WOWLAN */ +#ifdef CONFIG_AP_WOWLAN + if (pwrctl->wowlan_ap_mode == _TRUE) + rtw_hal_set_ap_rsvdpage_loc_cmd(adapter, &RsvdPageLoc); +#endif /* CONFIG_AP_WOWLAN */ + } else if (pwrctl->wowlan_pno_enable) { +#ifdef CONFIG_PNO_SUPPORT + rtw_hal_set_FwAoacRsvdPage_cmd(adapter, &RsvdPageLoc); + if (pwrctl->wowlan_in_resume) + rtw_hal_set_scan_offload_info_cmd(adapter, + &RsvdPageLoc, 0); + else + rtw_hal_set_scan_offload_info_cmd(adapter, + &RsvdPageLoc, 1); +#endif /* CONFIG_PNO_SUPPORT */ + } + +#ifdef CONFIG_P2P_WOWLAN + if (_TRUE == pwrctl->wowlan_p2p_mode) + rtw_hal_set_FwP2PRsvdPage_cmd(adapter, &RsvdPageLoc); +#endif /* CONFIG_P2P_WOWLAN */ + + return; +error: + rtw_free_xmitframe(pxmitpriv, pcmdframe); +} + +void rtw_hal_set_fw_rsvd_page(struct _ADAPTER *adapter, bool finished) +{ +#ifdef CONFIG_AP_MODE + if (finished) + rtw_mi_tx_beacon_hdl(adapter); + else +#endif + _rtw_hal_set_fw_rsvd_page(adapter, finished, NULL); +} + +static u8 rtw_hal_set_fw_bcn_early_c2h_rpt_cmd(struct _ADAPTER *adapter, u8 enable) +{ + u8 u1H2CSetPwrMode[H2C_PWRMODE_LEN] = {0}; + u8 ret = _FAIL; + +#ifdef CONFIG_TDLS +#ifdef CONFIG_TDLS_CH_SW + if (ATOMIC_READ(&adapter->tdlsinfo.chsw_info.chsw_on) == _TRUE) + { + SET_H2CCMD_PWRMODE_PARM_RLBM(u1H2CSetPwrMode, 1); + SET_H2CCMD_PWRMODE_PARM_SMART_PS(u1H2CSetPwrMode, 0); + } +#endif +#endif + + SET_H2CCMD_PWRMODE_PARM_MODE(u1H2CSetPwrMode, 0); + SET_H2CCMD_PWRMODE_PARM_PWR_STATE(u1H2CSetPwrMode, 0x0C); + SET_H2CCMD_PWRMODE_PARM_BCN_EARLY_C2H_RPT(u1H2CSetPwrMode, enable); + + ret = rtw_hal_fill_h2c_cmd(adapter, + H2C_SET_PWR_MODE, + H2C_PWRMODE_LEN, + u1H2CSetPwrMode); + + RTW_PRINT("-%s()-\n", __func__); + + return ret; +} + +/** + * rtw_hal_get_rsvd_page_num() - Get needed reserved page number + * @adapter: struct _ADAPTER* + * + * Caculate needed reserved page number. + * In different state would get different number, for example normal mode and + * WOW mode would need different reserved page size. + * + * Return the number of reserved page which driver need. + */ +u8 rtw_hal_get_rsvd_page_num(struct _ADAPTER *adapter) +{ + u8 num = 0; + + + _rtw_hal_set_fw_rsvd_page(adapter, _FALSE, &num); + + return num; +} + +#ifndef CONFIG_HAS_HW_VAR_BCN_FUNC +static void hw_var_set_bcn_func(_adapter *adapter, u8 enable) +{ + u32 bcn_ctrl_reg; + +#ifdef CONFIG_CONCURRENT_MODE + if (adapter->hw_port == HW_PORT1) + bcn_ctrl_reg = REG_BCN_CTRL_1; + else +#endif + bcn_ctrl_reg = REG_BCN_CTRL; + + if (enable) + rtw_write8(adapter, bcn_ctrl_reg, (EN_BCN_FUNCTION | EN_TXBCN_RPT)); + else { + u8 val8; + + val8 = rtw_read8(adapter, bcn_ctrl_reg); + val8 &= ~(EN_BCN_FUNCTION | EN_TXBCN_RPT); + +#ifdef CONFIG_BT_COEXIST + if (GET_HAL_DATA(adapter)->EEPROMBluetoothCoexist == 1) { + /* Always enable port0 beacon function for PSTDMA */ + if (REG_BCN_CTRL == bcn_ctrl_reg) + val8 |= EN_BCN_FUNCTION; + } +#endif + + rtw_write8(adapter, bcn_ctrl_reg, val8); + } + +#ifdef CONFIG_RTL8192F + if (IS_HARDWARE_TYPE_8192F(adapter)) { + u16 val16, val16_ori; + + val16_ori = val16 = rtw_read16(adapter, REG_WLAN_ACT_MASK_CTRL_1); + + #ifdef CONFIG_CONCURRENT_MODE + if (adapter->hw_port == HW_PORT1) { + if (enable) + val16 |= EN_PORT_1_FUNCTION; + else + val16 &= ~EN_PORT_1_FUNCTION; + } else + #endif + { + if (enable) + val16 |= EN_PORT_0_FUNCTION; + else + val16 &= ~EN_PORT_0_FUNCTION; + + #ifdef CONFIG_BT_COEXIST + if (GET_HAL_DATA(adapter)->EEPROMBluetoothCoexist == 1) + val16 |= EN_PORT_0_FUNCTION; + #endif + } + + if (val16 != val16_ori) + rtw_write16(adapter, REG_WLAN_ACT_MASK_CTRL_1, val16); + } +#endif +} +#endif + +#ifndef CONFIG_HAS_HW_VAR_MLME_DISCONNECT +static void hw_var_set_mlme_disconnect(_adapter *adapter) +{ + u8 val8; + + /* reject all data frames */ +#ifdef CONFIG_CONCURRENT_MODE + if (rtw_mi_check_status(adapter, MI_LINKED) == _FALSE) +#endif + rtw_write16(adapter, REG_RXFLTMAP2, 0x0000); + +#ifdef CONFIG_CONCURRENT_MODE + if (adapter->hw_port == HW_PORT1) { + /* reset TSF1 */ + rtw_write8(adapter, REG_DUAL_TSF_RST, BIT(1)); + + /* disable update TSF1 */ + rtw_iface_disable_tsf_update(adapter); + + if (!IS_HARDWARE_TYPE_8723D(adapter) + && !IS_HARDWARE_TYPE_8192F(adapter) + && !IS_HARDWARE_TYPE_8710B(adapter) + ) { + /* disable Port1's beacon function */ + val8 = rtw_read8(adapter, REG_BCN_CTRL_1); + val8 &= ~EN_BCN_FUNCTION; + rtw_write8(adapter, REG_BCN_CTRL_1, val8); + } + } else +#endif + { + /* reset TSF */ + rtw_write8(adapter, REG_DUAL_TSF_RST, BIT(0)); + + /* disable update TSF */ + rtw_iface_disable_tsf_update(adapter); + } +} +#endif + +static void hw_var_set_mlme_sitesurvey(_adapter *adapter, u8 enable) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u16 value_rxfltmap2; + +#ifdef DBG_IFACE_STATUS + DBG_IFACE_STATUS_DUMP(adapter); +#endif + +#ifdef CONFIG_FIND_BEST_CHANNEL + /* Receive all data frames */ + value_rxfltmap2 = 0xFFFF; +#else + /* not to receive data frame */ + value_rxfltmap2 = 0; +#endif + + if (enable) { /* under sitesurvey */ + /* + * 1. configure REG_RXFLTMAP2 + * 2. disable TSF update & buddy TSF update to avoid updating wrong TSF due to clear RCR_CBSSID_BCN + * 3. config RCR to receive different BSSID BCN or probe rsp + */ + rtw_write16(adapter, REG_RXFLTMAP2, value_rxfltmap2); + + rtw_hal_rcr_set_chk_bssid(adapter, MLME_SCAN_ENTER); + + /* Save orignal RRSR setting, only 8812 set RRSR after set ch/bw/band */ + #if defined (CONFIG_RTL8812A) || defined(CONFIG_RTL8821A) + hal_data->RegRRSR = rtw_read32(adapter, REG_RRSR); + hal_data->RegRRSR &= 0x000FFFFF; + #endif + + #if defined(CONFIG_BEAMFORMING) && (defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A)) + if (IS_8812_SERIES(hal_data->version_id) || IS_8821_SERIES(hal_data->version_id)) { + /* set 718[1:0]=2'b00 to avoid BF scan hang */ + hal_data->backup_snd_ptcl_ctrl = rtw_read8(adapter, REG_SND_PTCL_CTRL_8812A); + rtw_write8(adapter, REG_SND_PTCL_CTRL_8812A, (hal_data->backup_snd_ptcl_ctrl & 0xfc)); + } + #endif + + if (rtw_mi_get_ap_num(adapter) || rtw_mi_get_mesh_num(adapter)) + StopTxBeacon(adapter); + } else { /* sitesurvey done */ + /* + * 1. enable rx data frame + * 2. config RCR not to receive different BSSID BCN or probe rsp + * 3. doesn't enable TSF update & buddy TSF right now to avoid HW conflict + * so, we enable TSF update when rx first BCN after sitesurvey done + */ + if (rtw_mi_check_fwstate(adapter, WIFI_ASOC_STATE | WIFI_AP_STATE | WIFI_MESH_STATE)) { + /* enable to rx data frame */ + rtw_write16(adapter, REG_RXFLTMAP2, 0xFFFF); + } + + rtw_hal_rcr_set_chk_bssid(adapter, MLME_SCAN_DONE); + + /* Restore orignal RRSR setting,only 8812 set RRSR after set ch/bw/band */ + #if defined (CONFIG_RTL8812A) || defined(CONFIG_RTL8821A) + rtw_phydm_set_rrsr(adapter, hal_data->RegRRSR, TRUE); + #endif + + #if defined(CONFIG_BEAMFORMING) && (defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A)) + if (IS_8812_SERIES(hal_data->version_id) || IS_8821_SERIES(hal_data->version_id)) { + /* Restore orignal 0x718 setting*/ + rtw_write8(adapter, REG_SND_PTCL_CTRL_8812A, hal_data->backup_snd_ptcl_ctrl); + } + #endif + + #ifdef CONFIG_AP_MODE + if (rtw_mi_get_ap_num(adapter) || rtw_mi_get_mesh_num(adapter)) { + ResumeTxBeacon(adapter); + rtw_mi_tx_beacon_hdl(adapter); + } + #endif + } +} + +#ifndef CONFIG_HAS_HW_VAR_MLME_JOIN +static void hw_var_set_mlme_join(_adapter *adapter, u8 type) +{ + u8 val8; + u16 val16; + u32 val32; + u8 RetryLimit = RL_VAL_STA; + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + +#ifdef CONFIG_CONCURRENT_MODE + if (type == 0) { + /* prepare to join */ + if (rtw_mi_get_ap_num(adapter) || rtw_mi_get_mesh_num(adapter)) + StopTxBeacon(adapter); + + /* enable to rx data frame.Accept all data frame */ + rtw_write16(adapter, REG_RXFLTMAP2, 0xFFFF); + + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == _TRUE) + RetryLimit = (hal_data->CustomerID == RT_CID_CCX) ? RL_VAL_AP : RL_VAL_STA; + else /* Ad-hoc Mode */ + RetryLimit = RL_VAL_AP; + + rtw_iface_enable_tsf_update(adapter); + + } else if (type == 1) { + /* joinbss_event call back when join res < 0 */ + if (rtw_mi_check_status(adapter, MI_LINKED) == _FALSE) + rtw_write16(adapter, REG_RXFLTMAP2, 0x00); + + rtw_iface_disable_tsf_update(adapter); + + if (rtw_mi_get_ap_num(adapter) || rtw_mi_get_mesh_num(adapter)) { + ResumeTxBeacon(adapter); + + /* reset TSF 1/2 after ResumeTxBeacon */ + rtw_write8(adapter, REG_DUAL_TSF_RST, BIT(1) | BIT(0)); + } + + } else if (type == 2) { + /* sta add event call back */ + if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE)) { + /* fixed beacon issue for 8191su........... */ + rtw_write8(adapter, 0x542 , 0x02); + RetryLimit = RL_VAL_AP; + } + + if (rtw_mi_get_ap_num(adapter) || rtw_mi_get_mesh_num(adapter)) { + ResumeTxBeacon(adapter); + + /* reset TSF 1/2 after ResumeTxBeacon */ + rtw_write8(adapter, REG_DUAL_TSF_RST, BIT(1) | BIT(0)); + } + } + + val16 = BIT_SRL(RetryLimit) | BIT_LRL(RetryLimit); + rtw_write16(adapter, REG_RETRY_LIMIT, val16); +#else /* !CONFIG_CONCURRENT_MODE */ + if (type == 0) { /* prepare to join */ + /* enable to rx data frame.Accept all data frame */ + rtw_write16(adapter, REG_RXFLTMAP2, 0xFFFF); + + if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == _TRUE) + RetryLimit = (hal_data->CustomerID == RT_CID_CCX) ? RL_VAL_AP : RL_VAL_STA; + else /* Ad-hoc Mode */ + RetryLimit = RL_VAL_AP; + + rtw_iface_enable_tsf_update(adapter); + + } else if (type == 1) { /* joinbss_event call back when join res < 0 */ + rtw_write16(adapter, REG_RXFLTMAP2, 0x00); + + rtw_iface_disable_tsf_update(adapter); + + } else if (type == 2) { /* sta add event call back */ + if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE)) + RetryLimit = RL_VAL_AP; + } + + val16 = BIT_SRL(RetryLimit) | BIT_LRL(RetryLimit); + rtw_write16(adapter, REG_RETRY_LIMIT, val16); +#endif /* !CONFIG_CONCURRENT_MODE */ +} +#endif + +#ifdef CONFIG_TSF_RESET_OFFLOAD +static int rtw_hal_h2c_reset_tsf(_adapter *adapter, u8 reset_port) +{ + u8 buf[2]; + int ret; + + if (reset_port == HW_PORT0) { + buf[0] = 0x1; + buf[1] = 0; + } else { + buf[0] = 0x0; + buf[1] = 0x1; + } + + ret = rtw_hal_fill_h2c_cmd(adapter, H2C_RESET_TSF, 2, buf); + + return ret; +} + +int rtw_hal_reset_tsf(_adapter *adapter, u8 reset_port) +{ + u8 reset_cnt_before = 0, reset_cnt_after = 0, loop_cnt = 0; + u32 reg_reset_tsf_cnt = (reset_port == HW_PORT0) ? + REG_FW_RESET_TSF_CNT_0 : REG_FW_RESET_TSF_CNT_1; + int ret; + + /* site survey will cause reset tsf fail */ + rtw_mi_buddy_scan_abort(adapter, _FALSE); + reset_cnt_after = reset_cnt_before = rtw_read8(adapter, reg_reset_tsf_cnt); + ret = rtw_hal_h2c_reset_tsf(adapter, reset_port); + if (ret != _SUCCESS) + return ret; + + while ((reset_cnt_after == reset_cnt_before) && (loop_cnt < 10)) { + rtw_msleep_os(100); + loop_cnt++; + reset_cnt_after = rtw_read8(adapter, reg_reset_tsf_cnt); + } + + return (loop_cnt >= 10) ? _FAIL : _SUCCESS; +} +#endif /* CONFIG_TSF_RESET_OFFLOAD */ + +#ifndef CONFIG_HAS_HW_VAR_CORRECT_TSF +#ifdef CONFIG_HW_P0_TSF_SYNC +#ifdef CONFIG_CONCURRENT_MODE +static void hw_port0_tsf_sync_sel(_adapter *adapter, u8 benable, u8 hw_port, u16 tr_offset) +{ + u8 val8; + u8 client_id = 0; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + +#ifdef CONFIG_MCC_MODE + if (MCC_EN(adapter) && (rtw_hal_check_mcc_status(adapter, MCC_STATUS_DOING_MCC))) { + RTW_INFO("[MCC] do not set HW TSF sync\n"); + return; + } +#endif + /* check if port0 is already synced */ + if (benable && dvobj->p0_tsf.sync_port != MAX_HW_PORT && dvobj->p0_tsf.sync_port == hw_port) { + RTW_WARN(FUNC_ADPT_FMT ": port0 already enable TSF sync(%d)\n", + FUNC_ADPT_ARG(adapter), dvobj->p0_tsf.sync_port); + return; + } + + /* check if port0 already disable sync */ + if (!benable && dvobj->p0_tsf.sync_port == MAX_HW_PORT) { + RTW_WARN(FUNC_ADPT_FMT ": port0 already disable TSF sync\n", FUNC_ADPT_ARG(adapter)); + return; + } + + /* check if port0 sync to port0 */ + if (benable && hw_port == HW_PORT0) { + RTW_ERR(FUNC_ADPT_FMT ": hw_port is port0 under enable\n", FUNC_ADPT_ARG(adapter)); + rtw_warn_on(1); + return; + } + + /*0x5B4 [6:4] :SYNC_CLI_SEL - The selector for the CLINT port of sync tsft source for port 0*/ + /* Bit[5:4] : 0 for clint0, 1 for clint1, 2 for clint2, 3 for clint3. + Bit6 : 1= enable sync to port 0. 0=disable sync to port 0.*/ + + val8 = rtw_read8(adapter, REG_TIMER0_SRC_SEL); + + if (benable) { + /*Disable Port0's beacon function*/ + rtw_write8(adapter, REG_BCN_CTRL, rtw_read8(adapter, REG_BCN_CTRL) & ~BIT_EN_BCN_FUNCTION); + + /*Reg 0x518[15:0]: TSFTR_SYN_OFFSET*/ + if (tr_offset) + rtw_write16(adapter, REG_TSFTR_SYN_OFFSET, tr_offset); + + /*reg 0x577[6]=1*/ /*auto sync by tbtt*/ + rtw_write8(adapter, REG_MISC_CTRL, rtw_read8(adapter, REG_MISC_CTRL) | BIT_AUTO_SYNC_BY_TBTT); + + if (HW_PORT1 == hw_port) + client_id = 0; + else if (HW_PORT2 == hw_port) + client_id = 1; + else if (HW_PORT3 == hw_port) + client_id = 2; + else if (HW_PORT4 == hw_port) + client_id = 3; + + val8 &= 0x8F; + val8 |= (BIT(6) | (client_id << 4)); + + dvobj->p0_tsf.sync_port = hw_port; + dvobj->p0_tsf.offset = tr_offset; + rtw_write8(adapter, REG_TIMER0_SRC_SEL, val8); + + /*Enable Port0's beacon function*/ + rtw_write8(adapter, REG_BCN_CTRL, rtw_read8(adapter, REG_BCN_CTRL) | BIT_EN_BCN_FUNCTION); + RTW_INFO("%s Port_%d TSF sync to P0, timer offset :%d\n", __func__, hw_port, tr_offset); + } else { + val8 &= ~BIT(6); + + dvobj->p0_tsf.sync_port = MAX_HW_PORT; + dvobj->p0_tsf.offset = 0; + rtw_write8(adapter, REG_TIMER0_SRC_SEL, val8); + RTW_INFO("%s P0 TSF sync disable\n", __func__); + } +} +static _adapter * _search_ld_sta(_adapter *adapter, u8 include_self) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + u8 i; + _adapter *iface = NULL; + + if (rtw_mi_get_assoced_sta_num(adapter) == 0) { + RTW_ERR("STA_LD_NUM == 0\n"); + rtw_warn_on(1); + } + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (!iface) + continue; + if (include_self == _FALSE && adapter == iface) + continue; + if (is_client_associated_to_ap(iface)) + break; + } + if (iface) + RTW_INFO("search STA iface -"ADPT_FMT"\n", ADPT_ARG(iface)); + return iface; +} +#endif /*CONFIG_CONCURRENT_MODE*/ +/*Correct port0's TSF*/ +/*#define DBG_P0_TSF_SYNC*/ +void hw_var_set_correct_tsf(PADAPTER adapter, u8 mlme_state) +{ +#ifdef CONFIG_CONCURRENT_MODE + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv; + _adapter *sta_if = NULL; + u8 hw_port; + + RTW_INFO(FUNC_ADPT_FMT "\n", FUNC_ADPT_ARG(adapter)); + #ifdef DBG_P0_TSF_SYNC + RTW_INFO("[TSF_SYNC] AP_NUM = %d\n", rtw_mi_get_ap_num(adapter)); + RTW_INFO("[TSF_SYNC] MESH_NUM = %d\n", rtw_mi_get_mesh_num(adapter)); + RTW_INFO("[TSF_SYNC] LD_STA_NUM = %d\n", rtw_mi_get_assoced_sta_num(adapter)); + if (dvobj->p0_tsf.sync_port == MAX_HW_PORT) + RTW_INFO("[TSF_SYNC] org p0 sync port = N/A\n"); + else + RTW_INFO("[TSF_SYNC] org p0 sync port = %d\n", dvobj->p0_tsf.sync_port); + RTW_INFO("[TSF_SYNC] timer offset = %d\n", dvobj->p0_tsf.offset); + #endif + switch (mlme_state) { + case MLME_STA_CONNECTED : + { + hw_port = rtw_hal_get_port(adapter); + + if (!MLME_IS_STA(adapter)) { + RTW_ERR("STA CON state,but iface("ADPT_FMT") is not STA\n", ADPT_ARG(adapter)); + rtw_warn_on(1); + } + + if ((dvobj->p0_tsf.sync_port != MAX_HW_PORT) && (hw_port == HW_PORT0)) { + RTW_ERR(ADPT_FMT" is STA with P0 connected => DIS P0_TSF_SYNC\n", ADPT_ARG(adapter)); + rtw_warn_on(1); + hw_port0_tsf_sync_sel(adapter, _FALSE, 0, 0); + } + + if ((dvobj->p0_tsf.sync_port == MAX_HW_PORT) && + (rtw_mi_get_ap_num(adapter) || rtw_mi_get_mesh_num(adapter))) { + hw_port0_tsf_sync_sel(adapter, _TRUE, hw_port, 50);/*timer offset 50ms*/ + #ifdef DBG_P0_TSF_SYNC + RTW_INFO("[TSF_SYNC] STA_LINKED => EN P0_TSF_SYNC\n"); + #endif + } + } + break; + case MLME_STA_DISCONNECTED : + { + hw_port = rtw_hal_get_port(adapter); + + if (!MLME_IS_STA(adapter)) { + RTW_ERR("STA DIS_CON state,but iface("ADPT_FMT") is not STA\n", ADPT_ARG(adapter)); + rtw_warn_on(1); + } + + if (dvobj->p0_tsf.sync_port == hw_port) { + if (rtw_mi_get_assoced_sta_num(adapter) >= 2) { + /* search next appropriate sta*/ + sta_if = _search_ld_sta(adapter, _FALSE); + if (sta_if) { + hw_port = rtw_hal_get_port(sta_if); + hw_port0_tsf_sync_sel(adapter, _TRUE, hw_port, 50);/*timer offset 50ms*/ + #ifdef DBG_P0_TSF_SYNC + RTW_INFO("[TSF_SYNC] STA_DIS_CON => CHANGE P0_TSF_SYNC\n"); + #endif + } + } else if (rtw_mi_get_assoced_sta_num(adapter) == 1) { + hw_port0_tsf_sync_sel(adapter, _FALSE, 0, 0); + #ifdef DBG_P0_TSF_SYNC + RTW_INFO("[TSF_SYNC] STA_DIS_CON => DIS P0_TSF_SYNC\n"); + #endif + } + } + } + break; +#ifdef CONFIG_AP_MODE + case MLME_AP_STARTED : + case MLME_MESH_STARTED : + { + if (!(MLME_IS_AP(adapter) || MLME_IS_MESH(adapter))) { + RTW_ERR("AP START state,but iface("ADPT_FMT") is not AP\n", ADPT_ARG(adapter)); + rtw_warn_on(1); + } + + if ((dvobj->p0_tsf.sync_port == MAX_HW_PORT) && + rtw_mi_get_assoced_sta_num(adapter)) { + /* get port of sta */ + sta_if = _search_ld_sta(adapter, _FALSE); + if (sta_if) { + hw_port = rtw_hal_get_port(sta_if); + hw_port0_tsf_sync_sel(adapter, _TRUE, hw_port, 50);/*timer offset 50ms*/ + #ifdef DBG_P0_TSF_SYNC + RTW_INFO("[TSF_SYNC] AP_START => EN P0_TSF_SYNC\n"); + #endif + } + } + } + break; + case MLME_AP_STOPPED : + case MLME_MESH_STOPPED : + { + if (!(MLME_IS_AP(adapter) || MLME_IS_MESH(adapter))) { + RTW_ERR("AP START state,but iface("ADPT_FMT") is not AP\n", ADPT_ARG(adapter)); + rtw_warn_on(1); + } + /*stop ap mode*/ + if ((rtw_mi_get_ap_num(adapter) + rtw_mi_get_mesh_num(adapter) == 1) && + (dvobj->p0_tsf.sync_port != MAX_HW_PORT)) { + hw_port0_tsf_sync_sel(adapter, _FALSE, 0, 0); + #ifdef DBG_P0_TSF_SYNC + RTW_INFO("[TSF_SYNC] AP_STOP => DIS P0_TSF_SYNC\n"); + #endif + } + } + break; +#endif /* CONFIG_AP_MODE */ + default : + RTW_ERR(FUNC_ADPT_FMT" unknow state(0x%02x)\n", FUNC_ADPT_ARG(adapter), mlme_state); + break; + } + + /*#ifdef DBG_P0_TSF_SYNC*/ + #if 1 + if (dvobj->p0_tsf.sync_port == MAX_HW_PORT) + RTW_INFO("[TSF_SYNC] p0 sync port = N/A\n"); + else + RTW_INFO("[TSF_SYNC] p0 sync port = %d\n", dvobj->p0_tsf.sync_port); + RTW_INFO("[TSF_SYNC] timer offset = %d\n", dvobj->p0_tsf.offset); + #endif +#endif /*CONFIG_CONCURRENT_MODE*/ +} + +#else /*! CONFIG_HW_P0_TSF_SYNC*/ + +#ifdef CONFIG_MI_WITH_MBSSID_CAM +static void hw_var_set_correct_tsf(_adapter *adapter, u8 mlme_state) +{ + /*do nothing*/ +} +#else /* !CONFIG_MI_WITH_MBSSID_CAM*/ +static void rtw_hal_correct_tsf(_adapter *padapter, u8 hw_port, u64 tsf) +{ + if (hw_port == HW_PORT0) { + /*disable related TSF function*/ + rtw_write8(padapter, REG_BCN_CTRL, rtw_read8(padapter, REG_BCN_CTRL) & (~EN_BCN_FUNCTION)); +#if defined(CONFIG_RTL8192F) + rtw_write16(padapter, REG_WLAN_ACT_MASK_CTRL_1, rtw_read16(padapter, + REG_WLAN_ACT_MASK_CTRL_1) & ~EN_PORT_0_FUNCTION); +#endif + + rtw_write32(padapter, REG_TSFTR, tsf); + rtw_write32(padapter, REG_TSFTR + 4, tsf >> 32); + + /*enable related TSF function*/ + rtw_write8(padapter, REG_BCN_CTRL, rtw_read8(padapter, REG_BCN_CTRL) | EN_BCN_FUNCTION); +#if defined(CONFIG_RTL8192F) + rtw_write16(padapter, REG_WLAN_ACT_MASK_CTRL_1, rtw_read16(padapter, + REG_WLAN_ACT_MASK_CTRL_1) | EN_PORT_0_FUNCTION); +#endif + } else if (hw_port == HW_PORT1) { + /*disable related TSF function*/ + rtw_write8(padapter, REG_BCN_CTRL_1, rtw_read8(padapter, REG_BCN_CTRL_1) & (~EN_BCN_FUNCTION)); +#if defined(CONFIG_RTL8192F) + rtw_write16(padapter, REG_WLAN_ACT_MASK_CTRL_1, rtw_read16(padapter, + REG_WLAN_ACT_MASK_CTRL_1) & ~EN_PORT_1_FUNCTION); +#endif + + rtw_write32(padapter, REG_TSFTR1, tsf); + rtw_write32(padapter, REG_TSFTR1 + 4, tsf >> 32); + + /*enable related TSF function*/ + rtw_write8(padapter, REG_BCN_CTRL_1, rtw_read8(padapter, REG_BCN_CTRL_1) | EN_BCN_FUNCTION); +#if defined(CONFIG_RTL8192F) + rtw_write16(padapter, REG_WLAN_ACT_MASK_CTRL_1, rtw_read16(padapter, + REG_WLAN_ACT_MASK_CTRL_1) | EN_PORT_1_FUNCTION); +#endif + } else + RTW_INFO("%s-[WARN] "ADPT_FMT" invalid hw_port:%d\n", __func__, ADPT_ARG(padapter), hw_port); +} +static void hw_var_set_correct_tsf(_adapter *adapter, u8 mlme_state) +{ + u64 tsf; + struct mlme_ext_priv *mlmeext = &adapter->mlmeextpriv; + struct mlme_ext_info *mlmeinfo = &(mlmeext->mlmext_info); + + tsf = mlmeext->TSFValue - rtw_modular64(mlmeext->TSFValue, (mlmeinfo->bcn_interval * 1024)) - 1024; /*us*/ + + if ((mlmeinfo->state & 0x03) == WIFI_FW_ADHOC_STATE + || (mlmeinfo->state & 0x03) == WIFI_FW_AP_STATE) + StopTxBeacon(adapter); + + rtw_hal_correct_tsf(adapter, adapter->hw_port, tsf); + +#ifdef CONFIG_CONCURRENT_MODE + /* Update buddy port's TSF if it is SoftAP/Mesh for beacon TX issue! */ + if ((mlmeinfo->state & 0x03) == WIFI_FW_STATION_STATE + && (rtw_mi_get_ap_num(adapter) || rtw_mi_get_mesh_num(adapter)) + ) { + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + int i; + _adapter *iface; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (!iface) + continue; + if (iface == adapter) + continue; + + #ifdef CONFIG_AP_MODE + if ((MLME_IS_AP(iface) || MLME_IS_MESH(iface)) + && check_fwstate(&iface->mlmepriv, WIFI_ASOC_STATE) == _TRUE + ) { + rtw_hal_correct_tsf(iface, iface->hw_port, tsf); + #ifdef CONFIG_TSF_RESET_OFFLOAD + if (rtw_hal_reset_tsf(iface, iface->hw_port) == _FAIL) + RTW_INFO("%s-[ERROR] "ADPT_FMT" Reset port%d TSF fail\n" + , __func__, ADPT_ARG(iface), iface->hw_port); + #endif /* CONFIG_TSF_RESET_OFFLOAD*/ + #ifdef CONFIG_TSF_SYNC + if(iface->hw_port == HW_PORT0) + rtw_write8(iface, REG_DUAL_TSF_RST, rtw_read8(iface, REG_DUAL_TSF_RST) | BIT(2)); + else if(iface->hw_port == HW_PORT1) + rtw_write8(iface, REG_DUAL_TSF_RST, rtw_read8(iface, REG_DUAL_TSF_RST) | BIT(3)); + #endif + } + #endif /* CONFIG_AP_MODE */ + } + } +#endif /* CONFIG_CONCURRENT_MODE */ + if ((mlmeinfo->state & 0x03) == WIFI_FW_ADHOC_STATE + || (mlmeinfo->state & 0x03) == WIFI_FW_AP_STATE) + ResumeTxBeacon(adapter); +} +#endif /*#ifdef CONFIG_MI_WITH_MBSSID_CAM*/ +#endif /*#ifdef CONFIG_HW_P0_TSF_SYNC*/ +#endif /*#ifndef CONFIG_HAS_HW_VAR_CORRECT_TSF*/ + +u64 rtw_hal_get_tsftr_by_port(_adapter *adapter, u8 port) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u64 tsftr = 0; + + if (port >= hal_spec->port_num) { + RTW_ERR("%s invalid port(%d) \n", __func__, port); + goto exit; + } + + switch (rtw_get_chip_type(adapter)) { +#if defined(CONFIG_RTL8814B) + case RTL8814B: + { + u8 val8; + + /* 0x1500[6:4] - BIT_BCN_TIMER_SEL_FWRD_V1 */ + val8 = rtw_read8(adapter, REG_PORT_CTRL_SEL); + val8 &= ~0x70; + val8 |= port << 4; + rtw_write8(adapter, REG_PORT_CTRL_SEL, val8); + + tsftr = rtw_read32(adapter, REG_TSFTR_HIGH); + tsftr = tsftr << 32; + tsftr |= rtw_read32(adapter, REG_TSFTR_LOW); + + break; + } +#endif +#if defined(CONFIG_RTL8814A) || defined(CONFIG_RTL8822B) || defined(CONFIG_RTL8821C) || defined(CONFIG_RTL8822C) + case RTL8814A: + case RTL8822B: + case RTL8821C: + case RTL8822C: + { + u8 val8; + + /* 0x554[30:28] - BIT_BCN_TIMER_SEL_FWRD */ + val8 = rtw_read8(adapter, REG_MBSSID_BCN_SPACE + 3); + val8 &= 0x8F; + val8 |= port << 4; + rtw_write8(adapter, REG_MBSSID_BCN_SPACE + 3, val8); + + tsftr = rtw_read32(adapter, REG_TSFTR + 4); + tsftr = tsftr << 32; + tsftr |= rtw_read32(adapter, REG_TSFTR); + + break; + } +#endif +#if defined(CONFIG_RTL8188E) || defined(CONFIG_RTL8188F) || defined(CONFIG_RTL8188GTV) \ + || defined(CONFIG_RTL8192E) || defined(CONFIG_RTL8192F) \ + || defined(CONFIG_RTL8723B) || defined(CONFIG_RTL8703B) || defined(CONFIG_RTL8723D) \ + || defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A) \ + || defined(CONFIG_RTL8710B) + case RTL8188E: + case RTL8188F: + case RTL8188GTV: + case RTL8192E: + case RTL8192F: + case RTL8723B: + case RTL8703B: + case RTL8723D: + case RTL8812: + case RTL8821: + case RTL8710B: + { + u32 addr; + + if (port == HW_PORT0) + addr = REG_TSFTR; + else if (port == HW_PORT1) + addr = REG_TSFTR1; + else { + RTW_ERR("%s unknown port(%d) \n", __func__, port); + goto exit; + } + + tsftr = rtw_read32(adapter, addr + 4); + tsftr = tsftr << 32; + tsftr |= rtw_read32(adapter, addr); + + break; + } +#endif + default: + RTW_ERR("%s unknow chip type\n", __func__); + } + +exit: + return tsftr; +} + +#ifdef CONFIG_TDLS +#ifdef CONFIG_TDLS_CH_SW +s32 rtw_hal_ch_sw_oper_offload(_adapter *padapter, u8 channel, u8 channel_offset, u16 bwmode) +{ + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); + u8 ch_sw_h2c_buf[4] = {0x00, 0x00, 0x00, 0x00}; + + + SET_H2CCMD_CH_SW_OPER_OFFLOAD_CH_NUM(ch_sw_h2c_buf, channel); + SET_H2CCMD_CH_SW_OPER_OFFLOAD_BW_MODE(ch_sw_h2c_buf, bwmode); + switch (bwmode) { + case CHANNEL_WIDTH_40: + SET_H2CCMD_CH_SW_OPER_OFFLOAD_BW_40M_SC(ch_sw_h2c_buf, channel_offset); + break; + case CHANNEL_WIDTH_80: + SET_H2CCMD_CH_SW_OPER_OFFLOAD_BW_80M_SC(ch_sw_h2c_buf, channel_offset); + break; + case CHANNEL_WIDTH_20: + default: + break; + } + SET_H2CCMD_CH_SW_OPER_OFFLOAD_RFE_TYPE(ch_sw_h2c_buf, pHalData->rfe_type); + + return rtw_hal_fill_h2c_cmd(padapter, H2C_CHNL_SWITCH_OPER_OFFLOAD, sizeof(ch_sw_h2c_buf), ch_sw_h2c_buf); +} +#endif +#endif + +#ifdef CONFIG_WMMPS_STA +void rtw_hal_update_uapsd_tid(_adapter *adapter) +{ + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + struct qos_priv *pqospriv = &pmlmepriv->qospriv; + + /* write complement of pqospriv->uapsd_tid to mac register 0x693 because + it's designed for "0" represents "enable" and "1" represents "disable" */ + rtw_write8(adapter, REG_WMMPS_UAPSD_TID, (u8)(~pqospriv->uapsd_tid)); +} +#endif /* CONFIG_WMMPS_STA */ + +#if defined(CONFIG_BT_COEXIST) && defined(CONFIG_FW_MULTI_PORT_SUPPORT) +/* For multi-port support, driver needs to inform the port ID to FW for btc operations */ +s32 rtw_hal_set_wifi_btc_port_id_cmd(_adapter *adapter) +{ + u8 h2c_buf[H2C_BTC_WL_PORT_ID_LEN] = {0}; + u8 hw_port = rtw_hal_get_port(adapter); + + SET_H2CCMD_BTC_WL_PORT_ID(h2c_buf, hw_port); + RTW_INFO("%s ("ADPT_FMT") - hw_port :%d\n", __func__, ADPT_ARG(adapter), hw_port); + return rtw_hal_fill_h2c_cmd(adapter, H2C_BTC_WL_PORT_ID, H2C_BTC_WL_PORT_ID_LEN, h2c_buf); +} +#endif + +#define LPS_ACTIVE_TIMEOUT 50 /*number of times*/ +void rtw_lps_state_chk(_adapter *adapter, u8 ps_mode) +{ + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + struct sta_priv *pstapriv = &adapter->stapriv; + struct sta_info *psta = NULL; + u8 ps_ready = _FALSE; + s8 leave_wait_count = LPS_ACTIVE_TIMEOUT; + + if (ps_mode == PS_MODE_ACTIVE) { +#ifdef CONFIG_LPS_ACK + if (rtw_sctx_wait(&pwrpriv->lps_ack_sctx, __func__)) { + if (pwrpriv->lps_ack_status > 0) { + psta = rtw_get_stainfo(pstapriv, pmlmeinfo->network.MacAddress); + if (psta != NULL) { + if(issue_nulldata(adapter, psta->cmn.mac_addr, PS_MODE_ACTIVE, 3, 1) == _FAIL) + RTW_INFO(FUNC_ADPT_FMT" LPS state sync not yet finished.\n", FUNC_ADPT_ARG(adapter)); + } + } + } else { + RTW_WARN("LPS sctx query timeout, operation abort!!\n"); + return; + } + pwrpriv->lps_ack_status = -1; +#else + do { + if ((rtw_read8(adapter, REG_TCR) & BIT_PWRBIT_OW_EN) == 0) { + ps_ready = _TRUE; + break; + } + rtw_msleep_os(1); + } while (leave_wait_count--); + + if (ps_ready == _FALSE) { + RTW_WARN(FUNC_ADPT_FMT" Power Bit Control is still in HW!\n", FUNC_ADPT_ARG(adapter)); + return; + } +#endif /* CONFIG_LPS_ACK */ + } + } + +void rtw_var_set_basic_rate(PADAPTER padapter, u8 *val) { + + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); + struct mlme_ext_info *mlmext_info = &padapter->mlmeextpriv.mlmext_info; + u16 input_b = 0, masked = 0, ioted = 0, BrateCfg = 0; + u16 rrsr_2g_force_mask = RRSR_CCK_RATES; + u16 rrsr_2g_allow_mask = (RRSR_24M | RRSR_12M | RRSR_6M | RRSR_CCK_RATES); + #if CONFIG_IEEE80211_BAND_5GHZ + u16 rrsr_5g_force_mask = (RRSR_6M); + u16 rrsr_5g_allow_mask = (RRSR_OFDM_RATES); + #endif + u32 temp_RRSR; + + HalSetBrateCfg(padapter, val, &BrateCfg); + input_b = BrateCfg; + + /* apply force and allow mask */ + #if CONFIG_IEEE80211_BAND_5GHZ + if (pHalData->current_band_type != BAND_ON_2_4G) { + BrateCfg |= rrsr_5g_force_mask; + BrateCfg &= rrsr_5g_allow_mask; + } else + #endif + { /* 2.4G */ + BrateCfg |= rrsr_2g_force_mask; + BrateCfg &= rrsr_2g_allow_mask; + } + masked = BrateCfg; + +#ifdef CONFIG_CMCC_TEST + BrateCfg |= (RRSR_11M | RRSR_5_5M | RRSR_1M); /* use 11M to send ACK */ + BrateCfg |= (RRSR_24M | RRSR_18M | RRSR_12M); /*CMCC_OFDM_ACK 12/18/24M */ +#endif + + /* IOT consideration */ + if (mlmext_info->assoc_AP_vendor == HT_IOT_PEER_CISCO) { + /* if peer is cisco and didn't use ofdm rate, we enable 6M ack */ + if ((BrateCfg & (RRSR_24M | RRSR_12M | RRSR_6M)) == 0) + BrateCfg |= RRSR_6M; + } + ioted = BrateCfg; + +#ifdef CONFIG_NARROWBAND_SUPPORTING + if ((padapter->registrypriv.rtw_nb_config == RTW_NB_CONFIG_WIDTH_10) + || (padapter->registrypriv.rtw_nb_config == RTW_NB_CONFIG_WIDTH_5)) { + BrateCfg &= ~RRSR_CCK_RATES; + BrateCfg |= RRSR_6M; + } +#endif + pHalData->BasicRateSet = BrateCfg; + + RTW_INFO("HW_VAR_BASIC_RATE: %#x->%#x->%#x\n", input_b, masked, ioted); + + /* Set RRSR rate table. */ + temp_RRSR = rtw_read32(padapter, REG_RRSR); + temp_RRSR &=0xFFFF0000; + temp_RRSR |=BrateCfg; + rtw_phydm_set_rrsr(padapter, temp_RRSR, TRUE); + + rtw_write8(padapter, REG_RRSR + 2, rtw_read8(padapter, REG_RRSR + 2) & 0xf0); + + #if defined(CONFIG_RTL8188E) + rtw_hal_set_hwreg(padapter, HW_VAR_INIT_RTS_RATE, (u8 *)&BrateCfg); + #endif +} + +u8 SetHwReg(_adapter *adapter, u8 variable, u8 *val) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u8 ret = _SUCCESS; + + switch (variable) { + case HW_VAR_MEDIA_STATUS: { + u8 net_type = *((u8 *)val); + + rtw_hal_set_msr(adapter, net_type); + } + break; + case HW_VAR_DO_IQK: + if (*val) + hal_data->bNeedIQK = _TRUE; + else + hal_data->bNeedIQK = _FALSE; + break; + case HW_VAR_MAC_ADDR: +#ifdef CONFIG_MI_WITH_MBSSID_CAM + rtw_hal_set_macaddr_mbid(adapter, val); +#else + rtw_hal_set_macaddr_port(adapter, val); +#endif + break; + case HW_VAR_BSSID: + rtw_hal_set_bssid(adapter, val); + break; + case HW_VAR_RCR: + ret = hw_var_rcr_config(adapter, *((u32 *)val)); + break; + case HW_VAR_ON_RCR_AM: + hw_var_set_rcr_am(adapter, 1); + break; + case HW_VAR_OFF_RCR_AM: + hw_var_set_rcr_am(adapter, 0); + break; + case HW_VAR_BEACON_INTERVAL: + hw_var_set_bcn_interval(adapter, *(u16 *)val); + break; +#ifdef CONFIG_MBSSID_CAM + case HW_VAR_MBSSID_CAM_WRITE: { + u32 cmd = 0; + u32 *cam_val = (u32 *)val; + + rtw_write32(adapter, REG_MBIDCAMCFG_1, cam_val[0]); + cmd = BIT_MBIDCAM_POLL | BIT_MBIDCAM_WT_EN | BIT_MBIDCAM_VALID | cam_val[1]; + rtw_write32(adapter, REG_MBIDCAMCFG_2, cmd); + } + break; + case HW_VAR_MBSSID_CAM_CLEAR: { + u32 cmd; + u8 entry_id = *(u8 *)val; + + rtw_write32(adapter, REG_MBIDCAMCFG_1, 0); + + cmd = BIT_MBIDCAM_POLL | BIT_MBIDCAM_WT_EN | ((entry_id & MBIDCAM_ADDR_MASK) << MBIDCAM_ADDR_SHIFT); + rtw_write32(adapter, REG_MBIDCAMCFG_2, cmd); + } + break; + case HW_VAR_RCR_MBSSID_EN: + if (*((u8 *)val)) + rtw_hal_rcr_add(adapter, RCR_ENMBID); + else + rtw_hal_rcr_clear(adapter, RCR_ENMBID); + break; +#endif + case HW_VAR_PORT_SWITCH: + hw_var_port_switch(adapter); + break; + case HW_VAR_INIT_RTS_RATE: { + u16 brate_cfg = *((u16 *)val); + u8 rate_index = 0; + HAL_VERSION *hal_ver = &hal_data->version_id; + + if (IS_8188E(*hal_ver)) { + + while (brate_cfg > 0x1) { + brate_cfg = (brate_cfg >> 1); + rate_index++; + } + rtw_write8(adapter, REG_INIRTS_RATE_SEL, rate_index); + } else + rtw_warn_on(1); + } + break; + case HW_VAR_SEC_CFG: { + u16 reg_scr_ori; + u16 reg_scr; + + reg_scr = reg_scr_ori = rtw_read16(adapter, REG_SECCFG); + reg_scr |= (SCR_CHK_KEYID | SCR_RxDecEnable | SCR_TxEncEnable); + + if (_rtw_camctl_chk_cap(adapter, SEC_CAP_CHK_BMC)) + reg_scr |= SCR_CHK_BMC; + + if (_rtw_camctl_chk_flags(adapter, SEC_STATUS_STA_PK_GK_CONFLICT_DIS_BMC_SEARCH)) + reg_scr |= SCR_NoSKMC; + + if (reg_scr != reg_scr_ori) + rtw_write16(adapter, REG_SECCFG, reg_scr); + } + break; + case HW_VAR_SEC_DK_CFG: { + struct security_priv *sec = &adapter->securitypriv; + u8 reg_scr = rtw_read8(adapter, REG_SECCFG); + + if (val) { /* Enable default key related setting */ + reg_scr |= SCR_TXBCUSEDK; + if (sec->dot11AuthAlgrthm != dot11AuthAlgrthm_8021X) + reg_scr |= (SCR_RxUseDK | SCR_TxUseDK); + } else /* Disable default key related setting */ + reg_scr &= ~(SCR_RXBCUSEDK | SCR_TXBCUSEDK | SCR_RxUseDK | SCR_TxUseDK); + + rtw_write8(adapter, REG_SECCFG, reg_scr); + } + break; + + case HW_VAR_ASIX_IOT: + /* enable ASIX IOT function */ + if (*((u8 *)val) == _TRUE) { + /* 0xa2e[0]=0 (disable rake receiver) */ + rtw_write8(adapter, rCCK0_FalseAlarmReport + 2, + rtw_read8(adapter, rCCK0_FalseAlarmReport + 2) & ~(BIT0)); + /* 0xa1c=0xa0 (reset channel estimation if signal quality is bad) */ + rtw_write8(adapter, rCCK0_DSPParameter2, 0xa0); + } else { + /* restore reg:0xa2e, reg:0xa1c */ + rtw_write8(adapter, rCCK0_FalseAlarmReport + 2, + rtw_read8(adapter, rCCK0_FalseAlarmReport + 2) | (BIT0)); + rtw_write8(adapter, rCCK0_DSPParameter2, 0x00); + } + break; +#if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN) + case HW_VAR_WOWLAN: { + struct wowlan_ioctl_param *poidparam; + + poidparam = (struct wowlan_ioctl_param *)val; + switch (poidparam->subcode) { +#ifdef CONFIG_WOWLAN + case WOWLAN_PATTERN_CLEAN: + rtw_hal_dl_pattern(adapter, 2); + break; + case WOWLAN_ENABLE: + rtw_hal_wow_enable(adapter); + break; + case WOWLAN_DISABLE: + rtw_hal_wow_disable(adapter); + break; +#endif /*CONFIG_WOWLAN*/ +#ifdef CONFIG_AP_WOWLAN + case WOWLAN_AP_ENABLE: + rtw_hal_ap_wow_enable(adapter); + break; + case WOWLAN_AP_DISABLE: + rtw_hal_ap_wow_disable(adapter); + break; +#endif /*CONFIG_AP_WOWLAN*/ + default: + break; + } + } + break; +#endif /*defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN)*/ + +#ifndef CONFIG_HAS_HW_VAR_BCN_FUNC + case HW_VAR_BCN_FUNC: + hw_var_set_bcn_func(adapter, *val); + break; +#endif + +#ifndef CONFIG_HAS_HW_VAR_MLME_DISCONNECT + case HW_VAR_MLME_DISCONNECT: + hw_var_set_mlme_disconnect(adapter); + break; +#endif + + case HW_VAR_MLME_SITESURVEY: + hw_var_set_mlme_sitesurvey(adapter, *val); + #ifdef CONFIG_BT_COEXIST + if (hal_data->EEPROMBluetoothCoexist == 1) + rtw_btcoex_ScanNotify(adapter, *val ? _TRUE : _FALSE); + #endif + break; + +#ifndef CONFIG_HAS_HW_VAR_MLME_JOIN + case HW_VAR_MLME_JOIN: + hw_var_set_mlme_join(adapter, *val); + break; +#endif + + case HW_VAR_EN_HW_UPDATE_TSF: + rtw_hal_set_hw_update_tsf(adapter); + break; +#ifndef CONFIG_HAS_HW_VAR_CORRECT_TSF + case HW_VAR_CORRECT_TSF: + hw_var_set_correct_tsf(adapter, *val); + break; +#endif + +#if defined(CONFIG_HW_P0_TSF_SYNC) && defined(CONFIG_CONCURRENT_MODE) && defined(CONFIG_MCC_MODE) + case HW_VAR_TSF_AUTO_SYNC: + if (*val == _TRUE) + hw_port0_tsf_sync_sel(adapter, _TRUE, adapter->hw_port, 50); + else + hw_port0_tsf_sync_sel(adapter, _FALSE, adapter->hw_port, 50); + break; +#endif + case HW_VAR_APFM_ON_MAC: + hal_data->bMacPwrCtrlOn = *val; + RTW_INFO("%s: bMacPwrCtrlOn=%d\n", __func__, hal_data->bMacPwrCtrlOn); + break; +#ifdef CONFIG_WMMPS_STA + case HW_VAR_UAPSD_TID: + rtw_hal_update_uapsd_tid(adapter); + break; +#endif /* CONFIG_WMMPS_STA */ +#ifdef CONFIG_LPS_PG + case HW_VAR_LPS_PG_HANDLE: + rtw_hal_lps_pg_handler(adapter, *val); + break; +#endif +#ifdef CONFIG_LPS_LCLK_WD_TIMER + case HW_VAR_DM_IN_LPS_LCLK: + rtw_phydm_wd_lps_lclk_hdl(adapter); + break; +#endif + case HW_VAR_ENABLE_RX_BAR: + if (*val == _TRUE) { + /* enable RX BAR */ + u16 val16 = rtw_read16(adapter, REG_RXFLTMAP1); + + val16 |= BIT(8); + rtw_write16(adapter, REG_RXFLTMAP1, val16); + } else { + /* disable RX BAR */ + u16 val16 = rtw_read16(adapter, REG_RXFLTMAP1); + + val16 &= (~BIT(8)); + rtw_write16(adapter, REG_RXFLTMAP1, val16); + } + RTW_INFO("[HW_VAR_ENABLE_RX_BAR] 0x%02X=0x%02X\n", + REG_RXFLTMAP1, rtw_read16(adapter, REG_RXFLTMAP1)); + break; + case HW_VAR_HCI_SUS_STATE: + hal_data->hci_sus_state = *(u8 *)val; + RTW_INFO("%s: hci_sus_state=%u\n", __func__, hal_data->hci_sus_state); + break; +#if defined(CONFIG_AP_MODE) && defined(CONFIG_FW_HANDLE_TXBCN) && defined(CONFIG_SUPPORT_MULTI_BCN) + case HW_VAR_BCN_HEAD_SEL: + { + u8 vap_id = *(u8 *)val; + + if ((vap_id >= CONFIG_LIMITED_AP_NUM) && (vap_id != 0xFF)) { + RTW_ERR(ADPT_FMT " vap_id(%d:%d) is invalid\n", ADPT_ARG(adapter),vap_id, adapter->vap_id); + rtw_warn_on(1); + } + if (MLME_IS_AP(adapter) || MLME_IS_MESH(adapter)) { + u16 drv_pg_bndy = 0, bcn_addr = 0; + u32 page_size = 0; + + /*rtw_hal_get_def_var(adapter, HAL_DEF_TX_PAGE_BOUNDARY, &drv_pg_bndy);*/ + rtw_halmac_get_rsvd_drv_pg_bndy(adapter_to_dvobj(adapter), &drv_pg_bndy); + rtw_hal_get_def_var(adapter, HAL_DEF_TX_PAGE_SIZE, (u8 *)&page_size); + + if (vap_id != 0xFF) + bcn_addr = drv_pg_bndy + (vap_id * (MAX_BEACON_LEN / page_size)); + else + bcn_addr = drv_pg_bndy; + RTW_INFO(ADPT_FMT" vap_id(%d) change BCN HEAD to 0x%04x\n", + ADPT_ARG(adapter), vap_id, bcn_addr); + rtw_write16(adapter, REG_FIFOPAGE_CTRL_2, + (bcn_addr & BIT_MASK_BCN_HEAD_1_V1) | BIT_BCN_VALID_V1); + } + } + break; +#endif + case HW_VAR_LPS_STATE_CHK : + rtw_lps_state_chk(adapter, *(u8 *)val); + break; + +#ifdef CONFIG_RTS_FULL_BW + case HW_VAR_SET_RTS_BW: + { + #ifdef RTW_HALMAC + rtw_halmac_set_rts_full_bw(adapter_to_dvobj(adapter), (*val)); + #else + u8 temp; + if(*val) + temp = (( rtw_read8(adapter, REG_INIRTS_RATE_SEL)) | BIT5 ); + else + temp = (( rtw_read8(adapter, REG_INIRTS_RATE_SEL)) & (~BIT5)); + rtw_write8(adapter, REG_INIRTS_RATE_SEL, temp); + /*RTW_INFO("HW_VAR_SET_RTS_BW val=%u REG480=0x%x\n", *val, rtw_read8(adapter, REG_INIRTS_RATE_SEL));*/ + #endif + } + break; +#endif/*CONFIG_RTS_FULL_BW*/ +#if defined(CONFIG_PCI_HCI) + case HW_VAR_ENSWBCN: + if (*val == _TRUE) { + rtw_write8(adapter, REG_CR + 1, + rtw_read8(adapter, REG_CR + 1) | BIT(0)); + } else + rtw_write8(adapter, REG_CR + 1, + rtw_read8(adapter, REG_CR + 1) & ~BIT(0)); + break; +#endif + case HW_VAR_BCN_EARLY_C2H_RPT: + rtw_hal_set_fw_bcn_early_c2h_rpt_cmd(adapter, *(u8 *)val); + break; + default: + if (0) + RTW_PRINT(FUNC_ADPT_FMT" variable(%d) not defined!\n", + FUNC_ADPT_ARG(adapter), variable); + ret = _FAIL; + break; + } + + return ret; +} + +void GetHwReg(_adapter *adapter, u8 variable, u8 *val) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u64 val64; + + + switch (variable) { + case HW_VAR_MAC_ADDR: + #ifndef CONFIG_MI_WITH_MBSSID_CAM + rtw_hal_get_macaddr_port(adapter, val); + #endif + break; + case HW_VAR_BASIC_RATE: + *((u16 *)val) = hal_data->BasicRateSet; + break; + case HW_VAR_MEDIA_STATUS: + rtw_hal_get_msr(adapter, val); + break; + case HW_VAR_DO_IQK: + *val = hal_data->bNeedIQK; + break; + case HW_VAR_CH_SW_NEED_TO_TAKE_CARE_IQK_INFO: + if (hal_is_band_support(adapter, BAND_ON_5G)) + *val = _TRUE; + else + *val = _FALSE; + break; + case HW_VAR_APFM_ON_MAC: + *val = hal_data->bMacPwrCtrlOn; + break; + case HW_VAR_RCR: + hw_var_rcr_get(adapter, (u32 *)val); + break; + case HW_VAR_FWLPS_RF_ON: + /* When we halt NIC, we should check if FW LPS is leave. */ + if (rtw_is_surprise_removed(adapter) + || (adapter_to_pwrctl(adapter)->rf_pwrstate == rf_off) + ) { + /* + * If it is in HW/SW Radio OFF or IPS state, + * we do not check Fw LPS Leave, + * because Fw is unload. + */ + *val = _TRUE; + } else { + u32 rcr = 0; + + rtw_hal_get_hwreg(adapter, HW_VAR_RCR, (u8 *)&rcr); + if (rcr & (RCR_UC_MD_EN | RCR_BC_MD_EN | RCR_TIM_PARSER_EN)) + *val = _FALSE; + else + *val = _TRUE; + } + break; + + case HW_VAR_HCI_SUS_STATE: + *((u8 *)val) = hal_data->hci_sus_state; + break; + +#ifndef CONFIG_HAS_HW_VAR_BCN_CTRL_ADDR + case HW_VAR_BCN_CTRL_ADDR: + *((u32 *)val) = hw_bcn_ctrl_addr(adapter, adapter->hw_port); + break; +#endif + +#ifdef CONFIG_WAPI_SUPPORT + case HW_VAR_CAM_EMPTY_ENTRY: { + u8 ucIndex = *((u8 *)val); + u8 i; + u32 ulCommand = 0; + u32 ulContent = 0; + u32 ulEncAlgo = CAM_AES; + + for (i = 0; i < CAM_CONTENT_COUNT; i++) { + /* filled id in CAM config 2 byte */ + if (i == 0) + ulContent |= (ucIndex & 0x03) | ((u16)(ulEncAlgo) << 2); + else + ulContent = 0; + /* polling bit, and No Write enable, and address */ + ulCommand = CAM_CONTENT_COUNT * ucIndex + i; + ulCommand = ulCommand | CAM_POLLINIG | CAM_WRITE; + /* write content 0 is equall to mark invalid */ + rtw_write32(adapter, REG_CAMWRITE, ulContent); /* delay_ms(40); */ + rtw_write32(adapter, REG_CAMCMD, ulCommand); /* delay_ms(40); */ + } + } +#endif + + default: + if (0) + RTW_PRINT(FUNC_ADPT_FMT" variable(%d) not defined!\n", + FUNC_ADPT_ARG(adapter), variable); + break; + } + +} + +static u32 _get_page_size(struct _ADAPTER *a) +{ +#ifdef RTW_HALMAC + struct dvobj_priv *d; + u32 size = 0; + int err = 0; + + + d = adapter_to_dvobj(a); + + err = rtw_halmac_get_page_size(d, &size); + if (!err) + return size; + + RTW_WARN(FUNC_ADPT_FMT ": Fail to get Page size!!(err=%d)\n", + FUNC_ADPT_ARG(a), err); +#endif /* RTW_HALMAC */ + + return PAGE_SIZE_128; +} + +u8 +SetHalDefVar(_adapter *adapter, HAL_DEF_VARIABLE variable, void *value) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u8 bResult = _SUCCESS; + + switch (variable) { + + case HAL_DEF_DBG_DUMP_RXPKT: + hal_data->bDumpRxPkt = *((u8 *)value); + break; + case HAL_DEF_DBG_DUMP_TXPKT: + hal_data->bDumpTxPkt = *((u8 *)value); + break; + case HAL_DEF_ANT_DETECT: + hal_data->AntDetection = *((u8 *)value); + break; + default: + RTW_PRINT("%s: [WARNING] HAL_DEF_VARIABLE(%d) not defined!\n", __FUNCTION__, variable); + bResult = _FAIL; + break; + } + + return bResult; +} + +#ifdef CONFIG_BEAMFORMING +u8 rtw_hal_query_txbfer_rf_num(_adapter *adapter) +{ + struct registry_priv *pregistrypriv = &adapter->registrypriv; + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + + if ((pregistrypriv->beamformer_rf_num) && (IS_HARDWARE_TYPE_8814AE(adapter) || IS_HARDWARE_TYPE_8814AU(adapter) || IS_HARDWARE_TYPE_8822BU(adapter) || IS_HARDWARE_TYPE_8821C(adapter))) + return pregistrypriv->beamformer_rf_num; + else if (IS_HARDWARE_TYPE_8814AE(adapter) +#if 0 +#if defined(CONFIG_USB_HCI) + || (IS_HARDWARE_TYPE_8814AU(adapter) && (pUsbModeMech->CurUsbMode == 2 || pUsbModeMech->HubUsbMode == 2)) /* for USB3.0 */ +#endif +#endif + ) { + /*BF cap provided by Yu Chen, Sean, 2015, 01 */ + if (hal_data->rf_type == RF_3T3R) + return 2; + else if (hal_data->rf_type == RF_4T4R) + return 3; + else + return 1; + } else + return 1; + +} +u8 rtw_hal_query_txbfee_rf_num(_adapter *adapter) +{ + struct registry_priv *pregistrypriv = &adapter->registrypriv; + struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + + if ((pregistrypriv->beamformee_rf_num) && (IS_HARDWARE_TYPE_8814AE(adapter) || IS_HARDWARE_TYPE_8814AU(adapter) || IS_HARDWARE_TYPE_8822BU(adapter) || IS_HARDWARE_TYPE_8821C(adapter))) + return pregistrypriv->beamformee_rf_num; + else if (IS_HARDWARE_TYPE_8814AE(adapter) || IS_HARDWARE_TYPE_8814AU(adapter)) { + if (pmlmeinfo->assoc_AP_vendor == HT_IOT_PEER_BROADCOM) + return 2; + else + return 2;/*TODO: May be 3 in the future, by ChenYu. */ + } else + return 1; + +} +#ifdef RTW_BEAMFORMING_VERSION_2 +void rtw_hal_beamforming_config_csirate(PADAPTER adapter) +{ + struct dm_struct *p_dm_odm; + struct beamforming_info *bf_info; + u8 fix_rate_enable = 0; + u8 new_csi_rate_idx; + u8 rrsr_54_en; + u32 temp_rrsr; + + /* Acting as BFee */ + if (IS_BEAMFORMEE(adapter)) { + #if 0 + /* Do not enable now because it will affect MU performance and CTS/BA rate. 2016.07.19. by tynli. [PCIE-1660] */ + if (IS_HARDWARE_TYPE_8821C(Adapter)) + FixRateEnable = 1; /* Support after 8821C */ + #endif + + p_dm_odm = adapter_to_phydm(adapter); + bf_info = GET_BEAMFORM_INFO(adapter); + + rtw_halmac_bf_cfg_csi_rate(adapter_to_dvobj(adapter), + p_dm_odm->rssi_min, + bf_info->cur_csi_rpt_rate, + fix_rate_enable, &new_csi_rate_idx, &rrsr_54_en); + + temp_rrsr = rtw_read32(adapter, REG_RRSR); + if (rrsr_54_en == 1) + temp_rrsr |= RRSR_54M; + else if (rrsr_54_en == 0) + temp_rrsr &= ~RRSR_54M; + rtw_phydm_set_rrsr(adapter, temp_rrsr, FALSE); + + if (new_csi_rate_idx != bf_info->cur_csi_rpt_rate) + bf_info->cur_csi_rpt_rate = new_csi_rate_idx; + } +} +#endif +#endif + +u8 +GetHalDefVar(_adapter *adapter, HAL_DEF_VARIABLE variable, void *value) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u8 bResult = _SUCCESS; + + switch (variable) { + case HAL_DEF_UNDERCORATEDSMOOTHEDPWDB: { + struct mlme_priv *pmlmepriv; + struct sta_priv *pstapriv; + struct sta_info *psta; + + pmlmepriv = &adapter->mlmepriv; + pstapriv = &adapter->stapriv; + psta = rtw_get_stainfo(pstapriv, pmlmepriv->cur_network.network.MacAddress); + if (psta) + *((int *)value) = psta->cmn.rssi_stat.rssi; + } + break; + case HAL_DEF_DBG_DUMP_RXPKT: + *((u8 *)value) = hal_data->bDumpRxPkt; + break; + case HAL_DEF_DBG_DUMP_TXPKT: + *((u8 *)value) = hal_data->bDumpTxPkt; + break; + case HAL_DEF_ANT_DETECT: + *((u8 *)value) = hal_data->AntDetection; + break; + case HAL_DEF_TX_PAGE_SIZE: + *((u32 *)value) = _get_page_size(adapter); + break; + case HAL_DEF_TX_STBC: + #ifdef CONFIG_ALPHA_SMART_ANTENNA + *(u8 *)value = 0; + #else + *(u8 *)value = hal_data->max_tx_cnt > 1 ? 1 : 0; + #endif + break; + case HAL_DEF_EXPLICIT_BEAMFORMER: + case HAL_DEF_EXPLICIT_BEAMFORMEE: + case HAL_DEF_VHT_MU_BEAMFORMER: + case HAL_DEF_VHT_MU_BEAMFORMEE: + *(u8 *)value = _FALSE; + break; +#ifdef CONFIG_BEAMFORMING + case HAL_DEF_BEAMFORMER_CAP: + *(u8 *)value = rtw_hal_query_txbfer_rf_num(adapter); + break; + case HAL_DEF_BEAMFORMEE_CAP: + *(u8 *)value = rtw_hal_query_txbfee_rf_num(adapter); + break; +#endif + default: + RTW_PRINT("%s: [WARNING] HAL_DEF_VARIABLE(%d) not defined!\n", __FUNCTION__, variable); + bResult = _FAIL; + break; + } + + return bResult; +} + +/* + * Description: + * Translate a character to hex digit. + * */ +u32 +MapCharToHexDigit( + char chTmp +) +{ + if (chTmp >= '0' && chTmp <= '9') + return chTmp - '0'; + else if (chTmp >= 'a' && chTmp <= 'f') + return 10 + (chTmp - 'a'); + else if (chTmp >= 'A' && chTmp <= 'F') + return 10 + (chTmp - 'A'); + else + return 0; +} + + + +/* + * Description: + * Parse hex number from the string pucStr. + * */ +BOOLEAN +GetHexValueFromString( + char *szStr, + u32 *pu4bVal, + u32 *pu4bMove +) +{ + char *szScan = szStr; + + /* Check input parameter. */ + if (szStr == NULL || pu4bVal == NULL || pu4bMove == NULL) { + RTW_INFO("GetHexValueFromString(): Invalid inpur argumetns! szStr: %p, pu4bVal: %p, pu4bMove: %p\n", szStr, pu4bVal, pu4bMove); + return _FALSE; + } + + /* Initialize output. */ + *pu4bMove = 0; + *pu4bVal = 0; + + /* Skip leading space. */ + while (*szScan != '\0' && + (*szScan == ' ' || *szScan == '\t')) { + szScan++; + (*pu4bMove)++; + } + + /* Skip leading '0x' or '0X'. */ + if (*szScan == '0' && (*(szScan + 1) == 'x' || *(szScan + 1) == 'X')) { + szScan += 2; + (*pu4bMove) += 2; + } + + /* Check if szScan is now pointer to a character for hex digit, */ + /* if not, it means this is not a valid hex number. */ + if (!IsHexDigit(*szScan)) + return _FALSE; + + /* Parse each digit. */ + do { + (*pu4bVal) <<= 4; + *pu4bVal += MapCharToHexDigit(*szScan); + + szScan++; + (*pu4bMove)++; + } while (IsHexDigit(*szScan)); + + return _TRUE; +} + +BOOLEAN +GetFractionValueFromString( + char *szStr, + u8 *pInteger, + u8 *pFraction, + u32 *pu4bMove +) +{ + char *szScan = szStr; + + /* Initialize output. */ + *pu4bMove = 0; + *pInteger = 0; + *pFraction = 0; + + /* Skip leading space. */ + while (*szScan != '\0' && (*szScan == ' ' || *szScan == '\t')) { + ++szScan; + ++(*pu4bMove); + } + + if (*szScan < '0' || *szScan > '9') + return _FALSE; + + /* Parse each digit. */ + do { + (*pInteger) *= 10; + *pInteger += (*szScan - '0'); + + ++szScan; + ++(*pu4bMove); + + if (*szScan == '.') { + ++szScan; + ++(*pu4bMove); + + if (*szScan < '0' || *szScan > '9') + return _FALSE; + + *pFraction += (*szScan - '0') * 10; + ++szScan; + ++(*pu4bMove); + + if (*szScan >= '0' && *szScan <= '9') { + *pFraction += *szScan - '0'; + ++szScan; + ++(*pu4bMove); + } + return _TRUE; + } + } while (*szScan >= '0' && *szScan <= '9'); + + return _TRUE; +} + +/* + * Description: + * Return TRUE if szStr is comment out with leading " */ /* ". + * */ +BOOLEAN +IsCommentString( + char *szStr +) +{ + if (*szStr == '/' && *(szStr + 1) == '/') + return _TRUE; + else + return _FALSE; +} + +BOOLEAN +GetU1ByteIntegerFromStringInDecimal( + char *Str, + u8 *pInt +) +{ + u16 i = 0; + *pInt = 0; + + while (Str[i] != '\0') { + if (Str[i] >= '0' && Str[i] <= '9') { + *pInt *= 10; + *pInt += (Str[i] - '0'); + } else + return _FALSE; + ++i; + } + + return _TRUE; +} + +/* <20121004, Kordan> For example, + * ParseQualifiedString(inString, 0, outString, '[', ']') gets "Kordan" from a string "Hello [Kordan]". + * If RightQualifier does not exist, it will hang on in the while loop */ +BOOLEAN +ParseQualifiedString( + char *In, + u32 *Start, + char *Out, + char LeftQualifier, + char RightQualifier +) +{ + u32 i = 0, j = 0; + char c = In[(*Start)++]; + + if (c != LeftQualifier) + return _FALSE; + + i = (*Start); + c = In[(*Start)++]; + while (c != RightQualifier && c != '\0') + c = In[(*Start)++]; + + if (c == '\0') + return _FALSE; + + j = (*Start) - 2; + strncpy((char *)Out, (const char *)(In + i), j - i + 1); + + return _TRUE; +} + +BOOLEAN +isAllSpaceOrTab( + u8 *data, + u8 size +) +{ + u8 cnt = 0, NumOfSpaceAndTab = 0; + + while (size > cnt) { + if (data[cnt] == ' ' || data[cnt] == '\t' || data[cnt] == '\0') + ++NumOfSpaceAndTab; + + ++cnt; + } + + return size == NumOfSpaceAndTab; +} + + +void rtw_hal_check_rxfifo_full(_adapter *adapter) +{ + struct dvobj_priv *psdpriv = adapter->dvobj; + struct debug_priv *pdbgpriv = &psdpriv->drv_dbg; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(adapter); + struct registry_priv *regsty = &adapter->registrypriv; + int save_cnt = _FALSE; + + if (regsty->check_hw_status == 1) { + /* switch counter to RX fifo */ + if (IS_8188E(pHalData->version_id) || + IS_8188F(pHalData->version_id) || + IS_8188GTV(pHalData->version_id) || + IS_8812_SERIES(pHalData->version_id) || + IS_8821_SERIES(pHalData->version_id) || + IS_8723B_SERIES(pHalData->version_id) || + IS_8192E(pHalData->version_id) || + IS_8703B_SERIES(pHalData->version_id) || + IS_8723D_SERIES(pHalData->version_id) || + IS_8192F_SERIES(pHalData->version_id) || + IS_8822C_SERIES(pHalData->version_id)) { + rtw_write8(adapter, REG_RXERR_RPT + 3, rtw_read8(adapter, REG_RXERR_RPT + 3) | 0xa0); + save_cnt = _TRUE; + } else { + /* todo: other chips */ + } + + + if (save_cnt) { + pdbgpriv->dbg_rx_fifo_last_overflow = pdbgpriv->dbg_rx_fifo_curr_overflow; + pdbgpriv->dbg_rx_fifo_curr_overflow = rtw_read16(adapter, REG_RXERR_RPT); + pdbgpriv->dbg_rx_fifo_diff_overflow = pdbgpriv->dbg_rx_fifo_curr_overflow - pdbgpriv->dbg_rx_fifo_last_overflow; + } else { + /* special value to indicate no implementation */ + pdbgpriv->dbg_rx_fifo_last_overflow = 1; + pdbgpriv->dbg_rx_fifo_curr_overflow = 1; + pdbgpriv->dbg_rx_fifo_diff_overflow = 1; + } + } +} + +void linked_info_dump(_adapter *padapter, u8 benable) +{ + struct pwrctrl_priv *pwrctrlpriv = adapter_to_pwrctl(padapter); + + if (padapter->bLinkInfoDump == benable) + return; + + RTW_INFO("%s %s\n", __FUNCTION__, (benable) ? "enable" : "disable"); + + if (benable) { +#ifdef CONFIG_LPS + pwrctrlpriv->org_power_mgnt = pwrctrlpriv->power_mgnt;/* keep org value */ + rtw_pm_set_lps(padapter, PS_MODE_ACTIVE); +#endif + +#ifdef CONFIG_IPS + pwrctrlpriv->ips_org_mode = pwrctrlpriv->ips_mode;/* keep org value */ + rtw_pm_set_ips(padapter, IPS_NONE); +#endif + } else { +#ifdef CONFIG_IPS + rtw_pm_set_ips(padapter, pwrctrlpriv->ips_org_mode); +#endif /* CONFIG_IPS */ + +#ifdef CONFIG_LPS + rtw_pm_set_lps(padapter, pwrctrlpriv->org_power_mgnt); +#endif /* CONFIG_LPS */ + } + padapter->bLinkInfoDump = benable ; +} + +#ifdef DBG_RX_SIGNAL_DISPLAY_RAW_DATA +void rtw_get_raw_rssi_info(void *sel, _adapter *padapter) +{ + u8 isCCKrate, rf_path; + struct hal_spec_t *hal_spec = GET_HAL_SPEC(padapter); + struct rx_raw_rssi *psample_pkt_rssi = &padapter->recvpriv.raw_rssi_info; + RTW_PRINT_SEL(sel, "RxRate = %s, PWDBALL = %d(%%), rx_pwr_all = %d(dBm)\n", + HDATA_RATE(psample_pkt_rssi->data_rate), psample_pkt_rssi->pwdball, psample_pkt_rssi->pwr_all); + isCCKrate = (psample_pkt_rssi->data_rate <= DESC_RATE11M) ? TRUE : FALSE; + + if (isCCKrate) + psample_pkt_rssi->mimo_signal_strength[0] = psample_pkt_rssi->pwdball; + + for (rf_path = 0; rf_path < hal_spec->rf_reg_path_num; rf_path++) { + if (!(GET_HAL_RX_PATH_BMP(padapter) & BIT(rf_path))) + continue; + RTW_PRINT_SEL(sel, "RF_PATH_%d=>signal_strength:%d(%%),signal_quality:%d(%%)\n" + , rf_path, psample_pkt_rssi->mimo_signal_strength[rf_path], psample_pkt_rssi->mimo_signal_quality[rf_path]); + + if (!isCCKrate) { + RTW_PRINT_SEL(sel, "\trx_ofdm_pwr:%d(dBm),rx_ofdm_snr:%d(dB)\n", + psample_pkt_rssi->ofdm_pwr[rf_path], psample_pkt_rssi->ofdm_snr[rf_path]); + } + } +} + +void rtw_dump_raw_rssi_info(_adapter *padapter, void *sel) +{ + u8 isCCKrate, rf_path; + struct hal_spec_t *hal_spec = GET_HAL_SPEC(padapter); + struct rx_raw_rssi *psample_pkt_rssi = &padapter->recvpriv.raw_rssi_info; + _RTW_PRINT_SEL(sel, "============ RAW Rx Info dump ===================\n"); + _RTW_PRINT_SEL(sel, "RxRate = %s, PWDBALL = %d(%%), rx_pwr_all = %d(dBm)\n", HDATA_RATE(psample_pkt_rssi->data_rate), psample_pkt_rssi->pwdball, psample_pkt_rssi->pwr_all); + + isCCKrate = (psample_pkt_rssi->data_rate <= DESC_RATE11M) ? TRUE : FALSE; + + if (isCCKrate) + psample_pkt_rssi->mimo_signal_strength[0] = psample_pkt_rssi->pwdball; + + for (rf_path = 0; rf_path < hal_spec->rf_reg_path_num; rf_path++) { + if (!(GET_HAL_RX_PATH_BMP(padapter) & BIT(rf_path))) + continue; + _RTW_PRINT_SEL(sel , "RF_PATH_%d=>signal_strength:%d(%%),signal_quality:%d(%%)" + , rf_path, psample_pkt_rssi->mimo_signal_strength[rf_path], psample_pkt_rssi->mimo_signal_quality[rf_path]); + + if (!isCCKrate) + _RTW_PRINT_SEL(sel , ",rx_ofdm_pwr:%d(dBm),rx_ofdm_snr:%d(dB)\n", psample_pkt_rssi->ofdm_pwr[rf_path], psample_pkt_rssi->ofdm_snr[rf_path]); + else + _RTW_PRINT_SEL(sel , "\n"); + + } +} +#endif + +#ifdef DBG_RX_DFRAME_RAW_DATA +void rtw_dump_rx_dframe_info(_adapter *padapter, void *sel) +{ +#define DBG_RX_DFRAME_RAW_DATA_UC 0 +#define DBG_RX_DFRAME_RAW_DATA_BMC 1 +#define DBG_RX_DFRAME_RAW_DATA_TYPES 2 + + _irqL irqL; + u8 isCCKrate, rf_path; + struct recv_priv *precvpriv = &(padapter->recvpriv); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(padapter); + struct sta_priv *pstapriv = &padapter->stapriv; + struct sta_info *psta; + struct sta_recv_dframe_info *psta_dframe_info; + int i, j; + _list *plist, *phead; + u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + u8 null_addr[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + + if (precvpriv->store_law_data_flag) { + + _enter_critical_bh(&pstapriv->sta_hash_lock, &irqL); + + for (i = 0; i < NUM_STA; i++) { + phead = &(pstapriv->sta_hash[i]); + plist = get_next(phead); + + while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) { + + psta = LIST_CONTAINOR(plist, struct sta_info, hash_list); + plist = get_next(plist); + + if (psta) { + if ((_rtw_memcmp(psta->cmn.mac_addr, bc_addr, ETH_ALEN) != _TRUE) + && (_rtw_memcmp(psta->cmn.mac_addr, null_addr, ETH_ALEN) != _TRUE) + && (_rtw_memcmp(psta->cmn.mac_addr, adapter_mac_addr(padapter), ETH_ALEN) != _TRUE)) { + + RTW_PRINT_SEL(sel, "==============================\n"); + RTW_PRINT_SEL(sel, "macaddr =" MAC_FMT "\n", MAC_ARG(psta->cmn.mac_addr)); + + for (j = 0; j < DBG_RX_DFRAME_RAW_DATA_TYPES; j++) { + if (j == DBG_RX_DFRAME_RAW_DATA_UC) { + psta_dframe_info = &psta->sta_dframe_info; + RTW_PRINT_SEL(sel, "\n"); + RTW_PRINT_SEL(sel, "Unicast:\n"); + } else if (j == DBG_RX_DFRAME_RAW_DATA_BMC) { + psta_dframe_info = &psta->sta_dframe_info_bmc; + RTW_PRINT_SEL(sel, "\n"); + RTW_PRINT_SEL(sel, "Broadcast/Multicast:\n"); + } + + isCCKrate = (psta_dframe_info->sta_data_rate <= DESC_RATE11M) ? TRUE : FALSE; + + RTW_PRINT_SEL(sel, "BW=%s, sgi =%d\n", ch_width_str(psta_dframe_info->sta_bw_mode), psta_dframe_info->sta_sgi); + RTW_PRINT_SEL(sel, "Rx_Data_Rate = %s\n", HDATA_RATE(psta_dframe_info->sta_data_rate)); + + for (rf_path = 0; rf_path < hal_spec->rf_reg_path_num; rf_path++) { + if (!(GET_HAL_RX_PATH_BMP(padapter) & BIT(rf_path))) + continue; + if (!isCCKrate) { + RTW_PRINT_SEL(sel , "RF_PATH_%d RSSI:%d(dBm)", rf_path, psta_dframe_info->sta_RxPwr[rf_path]); + _RTW_PRINT_SEL(sel , ",rx_ofdm_snr:%d(dB)\n", psta_dframe_info->sta_ofdm_snr[rf_path]); + } else + RTW_PRINT_SEL(sel , "RF_PATH_%d RSSI:%d(dBm)\n", rf_path, (psta_dframe_info->sta_mimo_signal_strength[rf_path]) - 100); + } + } + + } + } + } + } + _exit_critical_bh(&pstapriv->sta_hash_lock, &irqL); + } +} +#endif +void rtw_store_phy_info(_adapter *padapter, union recv_frame *prframe) +{ + u8 isCCKrate, rf_path , dframe_type; + u8 *ptr; + u8 bc_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; +#ifdef DBG_RX_DFRAME_RAW_DATA + struct sta_recv_dframe_info *psta_dframe_info; +#endif + struct recv_priv *precvpriv = &(padapter->recvpriv); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(padapter); + struct rx_pkt_attrib *pattrib = &prframe->u.hdr.attrib; + struct sta_info *psta = prframe->u.hdr.psta; + struct phydm_phyinfo_struct *p_phy_info = &pattrib->phy_info; + struct rx_raw_rssi *psample_pkt_rssi = &padapter->recvpriv.raw_rssi_info; + psample_pkt_rssi->data_rate = pattrib->data_rate; + ptr = prframe->u.hdr.rx_data; + dframe_type = GetFrameType(ptr); + /*RTW_INFO("=>%s\n", __FUNCTION__);*/ + + + if (precvpriv->store_law_data_flag) { + isCCKrate = (pattrib->data_rate <= DESC_RATE11M) ? TRUE : FALSE; + + psample_pkt_rssi->pwdball = p_phy_info->rx_pwdb_all; + psample_pkt_rssi->pwr_all = p_phy_info->recv_signal_power; + + for (rf_path = 0; rf_path < hal_spec->rf_reg_path_num; rf_path++) { + psample_pkt_rssi->mimo_signal_strength[rf_path] = p_phy_info->rx_mimo_signal_strength[rf_path]; + psample_pkt_rssi->mimo_signal_quality[rf_path] = p_phy_info->rx_mimo_signal_quality[rf_path]; + if (!isCCKrate) { + psample_pkt_rssi->ofdm_pwr[rf_path] = p_phy_info->rx_pwr[rf_path]; + psample_pkt_rssi->ofdm_snr[rf_path] = p_phy_info->rx_snr[rf_path]; + } + } +#ifdef DBG_RX_DFRAME_RAW_DATA + if ((dframe_type == WIFI_DATA_TYPE) || (dframe_type == WIFI_QOS_DATA_TYPE) || (padapter->registrypriv.mp_mode == 1)) { + + /*RTW_INFO("=>%s WIFI_DATA_TYPE or WIFI_QOS_DATA_TYPE\n", __FUNCTION__);*/ + if (psta) { + if (IS_MCAST(get_ra(get_recvframe_data(prframe)))) + psta_dframe_info = &psta->sta_dframe_info_bmc; + else + psta_dframe_info = &psta->sta_dframe_info; + /*RTW_INFO("=>%s psta->cmn.mac_addr="MAC_FMT" !\n", + __FUNCTION__, MAC_ARG(psta->cmn.mac_addr));*/ + if ((_rtw_memcmp(psta->cmn.mac_addr, bc_addr, ETH_ALEN) != _TRUE) || (padapter->registrypriv.mp_mode == 1)) { + psta_dframe_info->sta_data_rate = pattrib->data_rate; + psta_dframe_info->sta_sgi = pattrib->sgi; + psta_dframe_info->sta_bw_mode = pattrib->bw; + for (rf_path = 0; rf_path < hal_spec->rf_reg_path_num; rf_path++) { + + psta_dframe_info->sta_mimo_signal_strength[rf_path] = (p_phy_info->rx_mimo_signal_strength[rf_path]);/*Percentage to dbm*/ + + if (!isCCKrate) { + psta_dframe_info->sta_ofdm_snr[rf_path] = p_phy_info->rx_snr[rf_path]; + psta_dframe_info->sta_RxPwr[rf_path] = p_phy_info->rx_pwr[rf_path]; + } + } + } + } + } +#endif + } + +} + +int hal_efuse_macaddr_offset(_adapter *adapter) +{ + u8 interface_type = 0; + int addr_offset = -1; + + interface_type = rtw_get_intf_type(adapter); + + switch (rtw_get_chip_type(adapter)) { +#ifdef CONFIG_RTL8723B + case RTL8723B: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8723BU; + else if (interface_type == RTW_SDIO) + addr_offset = EEPROM_MAC_ADDR_8723BS; + else if (interface_type == RTW_PCIE) + addr_offset = EEPROM_MAC_ADDR_8723BE; + break; +#endif +#ifdef CONFIG_RTL8703B + case RTL8703B: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8703BU; + else if (interface_type == RTW_SDIO) + addr_offset = EEPROM_MAC_ADDR_8703BS; + break; +#endif +#ifdef CONFIG_RTL8723D + case RTL8723D: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8723DU; + else if (interface_type == RTW_SDIO) + addr_offset = EEPROM_MAC_ADDR_8723DS; + else if (interface_type == RTW_PCIE) + addr_offset = EEPROM_MAC_ADDR_8723DE; + break; +#endif + +#ifdef CONFIG_RTL8188E + case RTL8188E: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_88EU; + else if (interface_type == RTW_SDIO) + addr_offset = EEPROM_MAC_ADDR_88ES; + else if (interface_type == RTW_PCIE) + addr_offset = EEPROM_MAC_ADDR_88EE; + break; +#endif +#ifdef CONFIG_RTL8188F + case RTL8188F: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8188FU; + else if (interface_type == RTW_SDIO) + addr_offset = EEPROM_MAC_ADDR_8188FS; + break; +#endif +#ifdef CONFIG_RTL8188GTV + case RTL8188GTV: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8188GTVU; + else if (interface_type == RTW_SDIO) + addr_offset = EEPROM_MAC_ADDR_8188GTVS; + break; +#endif +#ifdef CONFIG_RTL8812A + case RTL8812: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8812AU; + else if (interface_type == RTW_PCIE) + addr_offset = EEPROM_MAC_ADDR_8812AE; + break; +#endif +#ifdef CONFIG_RTL8821A + case RTL8821: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8821AU; + else if (interface_type == RTW_SDIO) + addr_offset = EEPROM_MAC_ADDR_8821AS; + else if (interface_type == RTW_PCIE) + addr_offset = EEPROM_MAC_ADDR_8821AE; + break; +#endif +#ifdef CONFIG_RTL8192E + case RTL8192E: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8192EU; + else if (interface_type == RTW_SDIO) + addr_offset = EEPROM_MAC_ADDR_8192ES; + else if (interface_type == RTW_PCIE) + addr_offset = EEPROM_MAC_ADDR_8192EE; + break; +#endif +#ifdef CONFIG_RTL8814A + case RTL8814A: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8814AU; + else if (interface_type == RTW_PCIE) + addr_offset = EEPROM_MAC_ADDR_8814AE; + break; +#endif + +#ifdef CONFIG_RTL8822B + case RTL8822B: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8822BU; + else if (interface_type == RTW_SDIO) + addr_offset = EEPROM_MAC_ADDR_8822BS; + else if (interface_type == RTW_PCIE) + addr_offset = EEPROM_MAC_ADDR_8822BE; + break; +#endif /* CONFIG_RTL8822B */ + +#ifdef CONFIG_RTL8821C + case RTL8821C: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8821CU; + else if (interface_type == RTW_SDIO) + addr_offset = EEPROM_MAC_ADDR_8821CS; + else if (interface_type == RTW_PCIE) + addr_offset = EEPROM_MAC_ADDR_8821CE; + break; +#endif /* CONFIG_RTL8821C */ + +#ifdef CONFIG_RTL8710B + case RTL8710B: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8710B; + break; +#endif + +#ifdef CONFIG_RTL8192F + case RTL8192F: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8192FU; + else if (interface_type == RTW_SDIO) + addr_offset = EEPROM_MAC_ADDR_8192FS; + else if (interface_type == RTW_PCIE) + addr_offset = EEPROM_MAC_ADDR_8192FE; + break; +#endif /* CONFIG_RTL8192F */ + +#ifdef CONFIG_RTL8822C + case RTL8822C: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8822CU; + else if (interface_type == RTW_SDIO) + addr_offset = EEPROM_MAC_ADDR_8822CS; + else if (interface_type == RTW_PCIE) + addr_offset = EEPROM_MAC_ADDR_8822CE; + break; +#endif /* CONFIG_RTL8822C */ + +#ifdef CONFIG_RTL8814B + case RTL8814B: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8814BU; + else if (interface_type == RTW_PCIE) + addr_offset = EEPROM_MAC_ADDR_8814BE; + break; +#endif /* CONFIG_RTL8814B */ + +#ifdef CONFIG_RTL8723F + case RTL8723F: + if (interface_type == RTW_USB) + addr_offset = EEPROM_MAC_ADDR_8723FU; + else if (interface_type == RTW_SDIO) + addr_offset = EEPROM_MAC_ADDR_8723FS; + break; +#endif /* CONFIG_RTL8723F */ + } + + if (addr_offset == -1) { + RTW_ERR("%s: unknown combination - chip_type:%u, interface:%u\n" + , __func__, rtw_get_chip_type(adapter), rtw_get_intf_type(adapter)); + } + + return addr_offset; +} + +int Hal_GetPhyEfuseMACAddr(PADAPTER padapter, u8 *mac_addr) +{ + int ret = _FAIL; + int addr_offset; + + addr_offset = hal_efuse_macaddr_offset(padapter); + if (addr_offset == -1) + goto exit; + + ret = rtw_efuse_map_read(padapter, addr_offset, ETH_ALEN, mac_addr); + +exit: + return ret; +} + +void rtw_dump_cur_efuse(PADAPTER padapter) +{ + int mapsize =0; + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(padapter); + + EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN , (void *)&mapsize, _FALSE); + + if (mapsize <= 0 || mapsize > EEPROM_MAX_SIZE) { + RTW_ERR("wrong map size %d\n", mapsize); + return; + } + +#ifdef CONFIG_RTW_DEBUG + if (hal_data->efuse_file_status == EFUSE_FILE_LOADED) + RTW_MAP_DUMP_SEL(RTW_DBGDUMP, "EFUSE FILE", hal_data->efuse_eeprom_data, mapsize); + else { +#ifdef CONFIG_MP_INCLUDED + if (rtw_mp_mode_check(padapter) && GET_EFUSE_UPDATE_ON(padapter)) + RTW_MAP_DUMP_SEL(RTW_DBGDUMP, "FAKE EFUSE", hal_data->efuse_eeprom_data, mapsize); + else +#endif + RTW_MAP_DUMP_SEL(RTW_DBGDUMP, "HW EFUSE", hal_data->efuse_eeprom_data, mapsize); + } +#endif +} + + +#ifdef CONFIG_EFUSE_CONFIG_FILE +u32 Hal_readPGDataFromConfigFile(PADAPTER padapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(padapter); + u32 ret = _FALSE; + u32 maplen = 0; +#ifdef CONFIG_MP_INCLUDED + struct mp_priv *pmp_priv = &padapter->mppriv; +#endif + + EFUSE_GetEfuseDefinition(padapter, EFUSE_WIFI, TYPE_EFUSE_MAP_LEN , (void *)&maplen, _FALSE); + + if (maplen < 256 || maplen > EEPROM_MAX_SIZE) { + RTW_ERR("eFuse length error :%d\n", maplen); + return _FALSE; + } +#ifdef CONFIG_MP_INCLUDED + if (pmp_priv->efuse_update_file == _TRUE && (rtw_mp_mode_check(padapter))) { + RTW_INFO("%s, eFuse read from file :%s\n", __func__, pmp_priv->efuse_file_path); + ret = rtw_read_efuse_from_file(pmp_priv->efuse_file_path, hal_data->efuse_eeprom_data, maplen); + pmp_priv->efuse_update_file = _FALSE; + } else +#endif + { + ret = rtw_read_efuse_from_file(EFUSE_MAP_PATH, hal_data->efuse_eeprom_data, maplen); + } + + hal_data->efuse_file_status = ((ret == _FAIL) ? EFUSE_FILE_FAILED : EFUSE_FILE_LOADED); + + if (hal_data->efuse_file_status == EFUSE_FILE_LOADED) + rtw_dump_cur_efuse(padapter); + + return ret; +} + +u32 Hal_ReadMACAddrFromFile(PADAPTER padapter, u8 *mac_addr) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(padapter); + u32 ret = _FAIL; + + if (rtw_read_macaddr_from_file(WIFIMAC_PATH, mac_addr) == _SUCCESS + && rtw_check_invalid_mac_address(mac_addr, _TRUE) == _FALSE + ) { + hal_data->macaddr_file_status = MACADDR_FILE_LOADED; + ret = _SUCCESS; + } else + hal_data->macaddr_file_status = MACADDR_FILE_FAILED; + + return ret; +} +#endif /* CONFIG_EFUSE_CONFIG_FILE */ + +int hal_config_macaddr(_adapter *adapter, bool autoload_fail) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u8 addr[ETH_ALEN]; + int addr_offset = hal_efuse_macaddr_offset(adapter); + u8 *hw_addr = NULL; + int ret = _SUCCESS; +#if defined(CONFIG_RTL8822B) && defined(CONFIG_USB_HCI) + u8 ft_mac_addr[ETH_ALEN] = {0x00, 0xff, 0xff, 0xff, 0xff, 0xff}; /* FT USB2 for 8822B */ +#endif + + if (autoload_fail) + goto bypass_hw_pg; + + if (addr_offset != -1) + hw_addr = &hal_data->efuse_eeprom_data[addr_offset]; + +#ifdef CONFIG_EFUSE_CONFIG_FILE + /* if the hw_addr is written by efuse file, set to NULL */ + if (hal_data->efuse_file_status == EFUSE_FILE_LOADED) + hw_addr = NULL; +#endif + + if (!hw_addr) { + /* try getting hw pg data */ + if (Hal_GetPhyEfuseMACAddr(adapter, addr) == _SUCCESS) + hw_addr = addr; + } + +#if defined(CONFIG_RTL8822B) && defined(CONFIG_USB_HCI) + if (_rtw_memcmp(hw_addr, ft_mac_addr, ETH_ALEN)) + hw_addr[0] = 0xff; +#endif + + /* check hw pg data */ + if (hw_addr && rtw_check_invalid_mac_address(hw_addr, _TRUE) == _FALSE) { + _rtw_memcpy(hal_data->EEPROMMACAddr, hw_addr, ETH_ALEN); + goto exit; + } + +bypass_hw_pg: + +#ifdef CONFIG_EFUSE_CONFIG_FILE + /* check wifi mac file */ + if (Hal_ReadMACAddrFromFile(adapter, addr) == _SUCCESS) { + _rtw_memcpy(hal_data->EEPROMMACAddr, addr, ETH_ALEN); + goto exit; + } +#endif + + _rtw_memset(hal_data->EEPROMMACAddr, 0, ETH_ALEN); + ret = _FAIL; + +exit: + return ret; +} + +#ifdef CONFIG_RF_POWER_TRIM +u32 Array_kfreemap[] = { + 0x08, 0xe, + 0x06, 0xc, + 0x04, 0xa, + 0x02, 0x8, + 0x00, 0x6, + 0x03, 0x4, + 0x05, 0x2, + 0x07, 0x0, + 0x09, 0x0, + 0x0c, 0x0, +}; + +void rtw_bb_rf_gain_offset(_adapter *padapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct registry_priv *registry_par = &padapter->registrypriv; + struct kfree_data_t *kfree_data = &pHalData->kfree_data; + u8 value = pHalData->EEPROMRFGainOffset; + u8 tmp = 0x3e; + u32 res, i = 0; + u32 ArrayLen = sizeof(Array_kfreemap) / sizeof(u32); + u32 *Array = Array_kfreemap; + u32 v1 = 0, v2 = 0, GainValue = 0, target = 0; + + if (registry_par->RegPwrTrimEnable == 2) { + RTW_INFO("Registry kfree default force disable.\n"); + return; + } + +#if defined(CONFIG_RTL8723B) + if (value & BIT4 && (registry_par->RegPwrTrimEnable == 1)) { + RTW_INFO("Offset RF Gain.\n"); + RTW_INFO("Offset RF Gain. pHalData->EEPROMRFGainVal=0x%x\n", pHalData->EEPROMRFGainVal); + + if (pHalData->EEPROMRFGainVal != 0xff) { + + if (pHalData->ant_path == RF_PATH_A) + GainValue = (pHalData->EEPROMRFGainVal & 0x0f); + + else + GainValue = (pHalData->EEPROMRFGainVal & 0xf0) >> 4; + RTW_INFO("Ant PATH_%d GainValue Offset = 0x%x\n", (pHalData->ant_path == RF_PATH_A) ? (RF_PATH_A) : (RF_PATH_B), GainValue); + + for (i = 0; i < ArrayLen; i += 2) { + /* RTW_INFO("ArrayLen in =%d ,Array 1 =0x%x ,Array2 =0x%x\n",i,Array[i],Array[i]+1); */ + v1 = Array[i]; + v2 = Array[i + 1]; + if (v1 == GainValue) { + RTW_INFO("Offset RF Gain. got v1 =0x%x ,v2 =0x%x\n", v1, v2); + target = v2; + break; + } + } + RTW_INFO("pHalData->EEPROMRFGainVal=0x%x ,Gain offset Target Value=0x%x\n", pHalData->EEPROMRFGainVal, target); + + res = rtw_hal_read_rfreg(padapter, RF_PATH_A, 0x7f, 0xffffffff); + RTW_INFO("Offset RF Gain. before reg 0x7f=0x%08x\n", res); + phy_set_rf_reg(padapter, RF_PATH_A, REG_RF_BB_GAIN_OFFSET, BIT18 | BIT17 | BIT16 | BIT15, target); + res = rtw_hal_read_rfreg(padapter, RF_PATH_A, 0x7f, 0xffffffff); + + RTW_INFO("Offset RF Gain. After reg 0x7f=0x%08x\n", res); + + } else + + RTW_INFO("Offset RF Gain. pHalData->EEPROMRFGainVal=0x%x != 0xff, didn't run Kfree\n", pHalData->EEPROMRFGainVal); + } else + RTW_INFO("Using the default RF gain.\n"); + +#elif defined(CONFIG_RTL8188E) + if (value & BIT4 && (registry_par->RegPwrTrimEnable == 1)) { + RTW_INFO("8188ES Offset RF Gain.\n"); + RTW_INFO("8188ES Offset RF Gain. EEPROMRFGainVal=0x%x\n", + pHalData->EEPROMRFGainVal); + + if (pHalData->EEPROMRFGainVal != 0xff) { + res = rtw_hal_read_rfreg(padapter, RF_PATH_A, + REG_RF_BB_GAIN_OFFSET, 0xffffffff); + + RTW_INFO("Offset RF Gain. reg 0x55=0x%x\n", res); + res &= 0xfff87fff; + + res |= (pHalData->EEPROMRFGainVal & 0x0f) << 15; + RTW_INFO("Offset RF Gain. res=0x%x\n", res); + + rtw_hal_write_rfreg(padapter, RF_PATH_A, + REG_RF_BB_GAIN_OFFSET, + RF_GAIN_OFFSET_MASK, res); + } else { + RTW_INFO("Offset RF Gain. EEPROMRFGainVal=0x%x == 0xff, didn't run Kfree\n", + pHalData->EEPROMRFGainVal); + } + } else + RTW_INFO("Using the default RF gain.\n"); +#else + /* TODO: call this when channel switch */ + if (kfree_data->flag & KFREE_FLAG_ON) + rtw_rf_apply_tx_gain_offset(padapter, 6); /* input ch6 to select BB_GAIN_2G */ +#endif + +} +#endif /*CONFIG_RF_POWER_TRIM */ + +bool kfree_data_is_bb_gain_empty(struct kfree_data_t *data) +{ +#ifdef CONFIG_RF_POWER_TRIM + int i, j; + + for (i = 0; i < BB_GAIN_NUM; i++) + for (j = 0; j < RF_PATH_MAX; j++) + if (data->bb_gain[i][j] != 0) + return 0; +#endif + return 1; +} + +#ifdef CONFIG_USB_RX_AGGREGATION +void rtw_set_usb_agg_by_mode_normal(_adapter *padapter, u8 cur_wireless_mode) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + if (cur_wireless_mode < WIRELESS_11_24N + && cur_wireless_mode > 0) { /* ABG mode */ +#ifdef CONFIG_PREALLOC_RX_SKB_BUFFER + u32 remainder = 0; + u8 quotient = 0; + + remainder = MAX_RECVBUF_SZ % (4 * 1024); + quotient = (u8)(MAX_RECVBUF_SZ >> 12); + + if (quotient > 5) { + pHalData->rxagg_usb_size = 0x6; + pHalData->rxagg_usb_timeout = 0x10; + } else { + if (remainder >= 2048) { + pHalData->rxagg_usb_size = quotient; + pHalData->rxagg_usb_timeout = 0x10; + } else { + pHalData->rxagg_usb_size = (quotient - 1); + pHalData->rxagg_usb_timeout = 0x10; + } + } +#else /* !CONFIG_PREALLOC_RX_SKB_BUFFER */ + if (0x6 != pHalData->rxagg_usb_size || 0x10 != pHalData->rxagg_usb_timeout) { + pHalData->rxagg_usb_size = 0x6; + pHalData->rxagg_usb_timeout = 0x10; + rtw_write16(padapter, REG_RXDMA_AGG_PG_TH, + pHalData->rxagg_usb_size | (pHalData->rxagg_usb_timeout << 8)); + } +#endif /* CONFIG_PREALLOC_RX_SKB_BUFFER */ + + } else if (cur_wireless_mode >= WIRELESS_11_24N + && cur_wireless_mode <= WIRELESS_MODE_MAX) { /* N AC mode */ +#ifdef CONFIG_PREALLOC_RX_SKB_BUFFER + u32 remainder = 0; + u8 quotient = 0; + + remainder = MAX_RECVBUF_SZ % (4 * 1024); + quotient = (u8)(MAX_RECVBUF_SZ >> 12); + + if (quotient > 5) { + pHalData->rxagg_usb_size = 0x5; + pHalData->rxagg_usb_timeout = 0x20; + } else { + if (remainder >= 2048) { + pHalData->rxagg_usb_size = quotient; + pHalData->rxagg_usb_timeout = 0x10; + } else { + pHalData->rxagg_usb_size = (quotient - 1); + pHalData->rxagg_usb_timeout = 0x10; + } + } +#else /* !CONFIG_PREALLOC_RX_SKB_BUFFER */ + if ((0x5 != pHalData->rxagg_usb_size) || (0x20 != pHalData->rxagg_usb_timeout)) { + pHalData->rxagg_usb_size = 0x5; + pHalData->rxagg_usb_timeout = 0x20; + rtw_write16(padapter, REG_RXDMA_AGG_PG_TH, + pHalData->rxagg_usb_size | (pHalData->rxagg_usb_timeout << 8)); + } +#endif /* CONFIG_PREALLOC_RX_SKB_BUFFER */ + + } else { + /* RTW_INFO("%s: Unknow wireless mode(0x%x)\n",__func__,padapter->mlmeextpriv.cur_wireless_mode); */ + } +} + +void rtw_set_usb_agg_by_mode_customer(_adapter *padapter, u8 cur_wireless_mode, u8 UsbDmaSize, u8 Legacy_UsbDmaSize) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + + if (cur_wireless_mode < WIRELESS_11_24N + && cur_wireless_mode > 0) { /* ABG mode */ + if (Legacy_UsbDmaSize != pHalData->rxagg_usb_size + || 0x10 != pHalData->rxagg_usb_timeout) { + pHalData->rxagg_usb_size = Legacy_UsbDmaSize; + pHalData->rxagg_usb_timeout = 0x10; + rtw_write16(padapter, REG_RXDMA_AGG_PG_TH, + pHalData->rxagg_usb_size | (pHalData->rxagg_usb_timeout << 8)); + } + } else if (cur_wireless_mode >= WIRELESS_11_24N + && cur_wireless_mode <= WIRELESS_MODE_MAX) { /* N AC mode */ + if (UsbDmaSize != pHalData->rxagg_usb_size + || 0x20 != pHalData->rxagg_usb_timeout) { + pHalData->rxagg_usb_size = UsbDmaSize; + pHalData->rxagg_usb_timeout = 0x20; + rtw_write16(padapter, REG_RXDMA_AGG_PG_TH, + pHalData->rxagg_usb_size | (pHalData->rxagg_usb_timeout << 8)); + } + } else { + /* RTW_INFO("%s: Unknown wireless mode(0x%x)\n",__func__,padapter->mlmeextpriv.cur_wireless_mode); */ + } +} + +void rtw_set_usb_agg_by_mode(_adapter *padapter, u8 cur_wireless_mode) +{ +#ifdef CONFIG_PLATFORM_NOVATEK_NT72668 + rtw_set_usb_agg_by_mode_customer(padapter, cur_wireless_mode, 0x3, 0x3); + return; +#endif /* CONFIG_PLATFORM_NOVATEK_NT72668 */ + + rtw_set_usb_agg_by_mode_normal(padapter, cur_wireless_mode); +} +#endif /* CONFIG_USB_RX_AGGREGATION */ + +/* To avoid RX affect TX throughput */ +void dm_DynamicUsbTxAgg(_adapter *padapter, u8 from_timer) +{ + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter); + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct registry_priv *registry_par = &padapter->registrypriv; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + u8 cur_wireless_mode = WIRELESS_INVALID; + +#ifdef CONFIG_USB_RX_AGGREGATION + if (!registry_par->dynamic_agg_enable) + return; + +#ifdef RTW_HALMAC + if (IS_HARDWARE_TYPE_8822BU(padapter) || IS_HARDWARE_TYPE_8821CU(padapter) + || IS_HARDWARE_TYPE_8822CU(padapter) || IS_HARDWARE_TYPE_8814BU(padapter) + || IS_HARDWARE_TYPE_8723FU(padapter)) + rtw_hal_set_hwreg(padapter, HW_VAR_RXDMA_AGG_PG_TH, NULL); +#else /* !RTW_HALMAC */ + if (IS_HARDWARE_TYPE_8821U(padapter)) { /* || IS_HARDWARE_TYPE_8192EU(padapter)) */ + /* This AGG_PH_TH only for UsbRxAggMode == USB_RX_AGG_USB */ + if ((pHalData->rxagg_mode == RX_AGG_USB) && (check_fwstate(pmlmepriv, WIFI_ASOC_STATE) == _TRUE)) { + if (pdvobjpriv->traffic_stat.cur_tx_tp > 2 && pdvobjpriv->traffic_stat.cur_rx_tp < 30) + rtw_write16(padapter , REG_RXDMA_AGG_PG_TH , 0x1010); + else if (pdvobjpriv->traffic_stat.last_tx_bytes > 220000 && pdvobjpriv->traffic_stat.cur_rx_tp < 30) + rtw_write16(padapter , REG_RXDMA_AGG_PG_TH , 0x1006); + else + rtw_write16(padapter, REG_RXDMA_AGG_PG_TH, 0x2005); /* dmc agg th 20K */ + + /* RTW_INFO("TX_TP=%u, RX_TP=%u\n", pdvobjpriv->traffic_stat.cur_tx_tp, pdvobjpriv->traffic_stat.cur_rx_tp); */ + } + } else if (IS_HARDWARE_TYPE_8812(padapter)) { +#ifdef CONFIG_CONCURRENT_MODE + u8 i; + _adapter *iface; + u8 bassocaed = _FALSE; + struct mlme_ext_priv *mlmeext; + + for (i = 0; i < pdvobjpriv->iface_nums; i++) { + iface = pdvobjpriv->padapters[i]; + mlmeext = &iface->mlmeextpriv; + if (rtw_linked_check(iface) == _TRUE) { + if (mlmeext->cur_wireless_mode >= cur_wireless_mode) + cur_wireless_mode = mlmeext->cur_wireless_mode; + bassocaed = _TRUE; + } + } + if (bassocaed) +#endif + rtw_set_usb_agg_by_mode(padapter, cur_wireless_mode); +#ifdef CONFIG_PLATFORM_NOVATEK_NT72668 + } else { + rtw_set_usb_agg_by_mode(padapter, cur_wireless_mode); +#endif /* CONFIG_PLATFORM_NOVATEK_NT72668 */ + } +#endif /* RTW_HALMAC */ +#endif /* CONFIG_USB_RX_AGGREGATION */ + +} + +/* bus-agg check for SoftAP mode */ +inline u8 rtw_hal_busagg_qsel_check(_adapter *padapter, u8 pre_qsel, u8 next_qsel) +{ +#ifdef CONFIG_AP_MODE + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + u8 chk_rst = _SUCCESS; + + if (!MLME_IS_AP(padapter) && !MLME_IS_MESH(padapter)) + return chk_rst; + + /* if((pre_qsel == 0xFF)||(next_qsel== 0xFF)) */ + /* return chk_rst; */ + + if (((pre_qsel == QSLT_HIGH) || ((next_qsel == QSLT_HIGH))) + && (pre_qsel != next_qsel)) { + /* RTW_INFO("### bus-agg break cause of qsel misatch, pre_qsel=0x%02x,next_qsel=0x%02x ###\n", */ + /* pre_qsel,next_qsel); */ + chk_rst = _FAIL; + } + return chk_rst; +#else + return _SUCCESS; +#endif /* CONFIG_AP_MODE */ +} + +#ifdef CONFIG_WOWLAN +/* + * Description: + * dump_TX_FIFO: This is only used to dump TX_FIFO for debug WoW mode offload + * contant. + * + * Input: + * adapter: adapter pointer. + * page_num: The max. page number that user want to dump. + * page_size: page size of each page. eg. 128 bytes, 256 bytes, 512byte. + */ +void dump_TX_FIFO(_adapter *padapter, u8 page_num, u16 page_size) +{ + + int i; + u8 val = 0; + u8 base = 0; + u32 addr = 0; + u32 count = (page_size / 8); + + if (page_num <= 0) { + RTW_INFO("!!%s: incorrect input page_num paramter!\n", __func__); + return; + } + + if (page_size < 128 || page_size > 512) { + RTW_INFO("!!%s: incorrect input page_size paramter!\n", __func__); + return; + } + + RTW_INFO("+%s+\n", __func__); + val = rtw_read8(padapter, 0x106); + rtw_write8(padapter, 0x106, 0x69); + RTW_INFO("0x106: 0x%02x\n", val); + base = rtw_read8(padapter, 0x209); + RTW_INFO("0x209: 0x%02x\n", base); + + addr = ((base)*page_size) / 8; + for (i = 0 ; i < page_num * count ; i += 2) { + rtw_write32(padapter, 0x140, addr + i); + printk(" %08x %08x ", rtw_read32(padapter, 0x144), rtw_read32(padapter, 0x148)); + rtw_write32(padapter, 0x140, addr + i + 1); + printk(" %08x %08x\n", rtw_read32(padapter, 0x144), rtw_read32(padapter, 0x148)); + } +} +#endif + +#ifdef CONFIG_GPIO_API +u8 rtw_hal_get_gpio(_adapter *adapter, u8 gpio_num) +{ + u8 value = 0; + u8 direction = 0; + u32 gpio_pin_input_val = REG_GPIO_PIN_CTRL; + u32 gpio_pin_output_val = REG_GPIO_PIN_CTRL + 1; + u32 gpio_pin_output_en = REG_GPIO_PIN_CTRL + 2; + u8 gpio_num_to_set = gpio_num; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + + if (rtw_hal_gpio_func_check(adapter, gpio_num) == _FAIL) + return value; + + rtw_ps_deny(adapter, PS_DENY_IOCTL); + + RTW_INFO("rf_pwrstate=0x%02x\n", pwrpriv->rf_pwrstate); + LeaveAllPowerSaveModeDirect(adapter); + + if (gpio_num > 7) { + gpio_pin_input_val = REG_GPIO_PIN_CTRL_2; + gpio_pin_output_val = REG_GPIO_PIN_CTRL_2 + 1; + gpio_pin_output_en = REG_GPIO_PIN_CTRL_2 + 2; + gpio_num_to_set = gpio_num - 8; + } + + /* Read GPIO Direction */ + direction = (rtw_read8(adapter, gpio_pin_output_en) & BIT(gpio_num_to_set)) >> gpio_num_to_set; + + /* According the direction to read register value */ + if (direction) + value = (rtw_read8(adapter, gpio_pin_output_val) & BIT(gpio_num_to_set)) >> gpio_num_to_set; + else + value = (rtw_read8(adapter, gpio_pin_input_val) & BIT(gpio_num_to_set)) >> gpio_num_to_set; + + rtw_ps_deny_cancel(adapter, PS_DENY_IOCTL); + RTW_INFO("%s direction=%d value=%d\n", __FUNCTION__, direction, value); + + return value; +} + +int rtw_hal_set_gpio_output_value(_adapter *adapter, u8 gpio_num, bool isHigh) +{ + u8 direction = 0; + u8 res = -1; + u32 gpio_pin_output_val = REG_GPIO_PIN_CTRL + 1; + u32 gpio_pin_output_en = REG_GPIO_PIN_CTRL + 2; + u8 gpio_num_to_set = gpio_num; + + if (rtw_hal_gpio_func_check(adapter, gpio_num) == _FAIL) + return -1; + + rtw_ps_deny(adapter, PS_DENY_IOCTL); + + LeaveAllPowerSaveModeDirect(adapter); + + if (gpio_num > 7) { + gpio_pin_output_val = REG_GPIO_PIN_CTRL_2 + 1; + gpio_pin_output_en = REG_GPIO_PIN_CTRL_2 + 2; + gpio_num_to_set = gpio_num - 8; + } + + /* Read GPIO direction */ + direction = (rtw_read8(adapter, gpio_pin_output_en) & BIT(gpio_num_to_set)) >> gpio_num_to_set; + + /* If GPIO is output direction, setting value. */ + if (direction) { + if (isHigh) + rtw_write8(adapter, gpio_pin_output_val, rtw_read8(adapter, gpio_pin_output_val) | BIT(gpio_num_to_set)); + else + rtw_write8(adapter, gpio_pin_output_val, rtw_read8(adapter, gpio_pin_output_val) & ~BIT(gpio_num_to_set)); + + RTW_INFO("%s Set gpio %x[%d]=%d\n", __FUNCTION__, REG_GPIO_PIN_CTRL + 1, gpio_num, isHigh); + res = 0; + } else { + RTW_INFO("%s The gpio is input,not be set!\n", __FUNCTION__); + res = -1; + } + + rtw_ps_deny_cancel(adapter, PS_DENY_IOCTL); + return res; +} + +int rtw_hal_config_gpio(_adapter *adapter, u8 gpio_num, bool isOutput) +{ + u32 gpio_ctrl_reg_to_set = REG_GPIO_PIN_CTRL + 2; + u8 gpio_num_to_set = gpio_num; + + if (rtw_hal_gpio_func_check(adapter, gpio_num) == _FAIL) + return -1; + + RTW_INFO("%s gpio_num =%d direction=%d\n", __FUNCTION__, gpio_num, isOutput); + + rtw_ps_deny(adapter, PS_DENY_IOCTL); + + LeaveAllPowerSaveModeDirect(adapter); + + rtw_hal_gpio_multi_func_reset(adapter, gpio_num); + + if (gpio_num > 7) { + gpio_ctrl_reg_to_set = REG_GPIO_PIN_CTRL_2 + 2; + gpio_num_to_set = gpio_num - 8; + } + + if (isOutput) + rtw_write8(adapter, gpio_ctrl_reg_to_set, rtw_read8(adapter, gpio_ctrl_reg_to_set) | BIT(gpio_num_to_set)); + else + rtw_write8(adapter, gpio_ctrl_reg_to_set, rtw_read8(adapter, gpio_ctrl_reg_to_set) & ~BIT(gpio_num_to_set)); + + rtw_ps_deny_cancel(adapter, PS_DENY_IOCTL); + + return 0; +} +int rtw_hal_register_gpio_interrupt(_adapter *adapter, int gpio_num, void(*callback)(u8 level)) +{ + u8 value; + u8 direction; + PHAL_DATA_TYPE phal = GET_HAL_DATA(adapter); + + if (IS_HARDWARE_TYPE_8188E(adapter)) { + if (gpio_num > 7 || gpio_num < 4) { + RTW_PRINT("%s The gpio number does not included 4~7.\n", __FUNCTION__); + return -1; + } + } + + rtw_ps_deny(adapter, PS_DENY_IOCTL); + + LeaveAllPowerSaveModeDirect(adapter); + + /* Read GPIO direction */ + direction = (rtw_read8(adapter, REG_GPIO_PIN_CTRL + 2) & BIT(gpio_num)) >> gpio_num; + if (direction) { + RTW_PRINT("%s Can't register output gpio as interrupt.\n", __FUNCTION__); + return -1; + } + + /* Config GPIO Mode */ + rtw_write8(adapter, REG_GPIO_PIN_CTRL + 3, rtw_read8(adapter, REG_GPIO_PIN_CTRL + 3) | BIT(gpio_num)); + + /* Register GPIO interrupt handler*/ + adapter->gpiointpriv.callback[gpio_num] = callback; + + /* Set GPIO interrupt mode, 0:positive edge, 1:negative edge */ + value = rtw_read8(adapter, REG_GPIO_PIN_CTRL) & BIT(gpio_num); + adapter->gpiointpriv.interrupt_mode = rtw_read8(adapter, REG_HSIMR + 2) ^ value; + rtw_write8(adapter, REG_GPIO_INTM, adapter->gpiointpriv.interrupt_mode); + + /* Enable GPIO interrupt */ + adapter->gpiointpriv.interrupt_enable_mask = rtw_read8(adapter, REG_HSIMR + 2) | BIT(gpio_num); + rtw_write8(adapter, REG_HSIMR + 2, adapter->gpiointpriv.interrupt_enable_mask); + + rtw_hal_update_hisr_hsisr_ind(adapter, 1); + + rtw_ps_deny_cancel(adapter, PS_DENY_IOCTL); + + return 0; +} +int rtw_hal_disable_gpio_interrupt(_adapter *adapter, int gpio_num) +{ + u8 value; + u8 direction; + PHAL_DATA_TYPE phal = GET_HAL_DATA(adapter); + + if (IS_HARDWARE_TYPE_8188E(adapter)) { + if (gpio_num > 7 || gpio_num < 4) { + RTW_INFO("%s The gpio number does not included 4~7.\n", __FUNCTION__); + return -1; + } + } + + rtw_ps_deny(adapter, PS_DENY_IOCTL); + + LeaveAllPowerSaveModeDirect(adapter); + + /* Config GPIO Mode */ + rtw_write8(adapter, REG_GPIO_PIN_CTRL + 3, rtw_read8(adapter, REG_GPIO_PIN_CTRL + 3) & ~BIT(gpio_num)); + + /* Unregister GPIO interrupt handler*/ + adapter->gpiointpriv.callback[gpio_num] = NULL; + + /* Reset GPIO interrupt mode, 0:positive edge, 1:negative edge */ + adapter->gpiointpriv.interrupt_mode = rtw_read8(adapter, REG_GPIO_INTM) & ~BIT(gpio_num); + rtw_write8(adapter, REG_GPIO_INTM, 0x00); + + /* Disable GPIO interrupt */ + adapter->gpiointpriv.interrupt_enable_mask = rtw_read8(adapter, REG_HSIMR + 2) & ~BIT(gpio_num); + rtw_write8(adapter, REG_HSIMR + 2, adapter->gpiointpriv.interrupt_enable_mask); + + if (!adapter->gpiointpriv.interrupt_enable_mask) + rtw_hal_update_hisr_hsisr_ind(adapter, 0); + + rtw_ps_deny_cancel(adapter, PS_DENY_IOCTL); + + return 0; +} +#endif + +s8 rtw_hal_ch_sw_iqk_info_search(_adapter *padapter, u8 central_chnl, u8 bw_mode) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + u8 i; + + for (i = 0; i < MAX_IQK_INFO_BACKUP_CHNL_NUM; i++) { + if ((pHalData->iqk_reg_backup[i].central_chnl != 0)) { + if ((pHalData->iqk_reg_backup[i].central_chnl == central_chnl) + && (pHalData->iqk_reg_backup[i].bw_mode == bw_mode)) + return i; + } + } + + return -1; +} + +void rtw_hal_ch_sw_iqk_info_backup(_adapter *padapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + s8 res; + u8 i; + + /* If it's an existed record, overwrite it */ + res = rtw_hal_ch_sw_iqk_info_search(padapter, pHalData->current_channel, pHalData->current_channel_bw); + if ((res >= 0) && (res < MAX_IQK_INFO_BACKUP_CHNL_NUM)) { + rtw_hal_set_hwreg(padapter, HW_VAR_CH_SW_IQK_INFO_BACKUP, (u8 *)&(pHalData->iqk_reg_backup[res])); + return; + } + + /* Search for the empty record to use */ + for (i = 0; i < MAX_IQK_INFO_BACKUP_CHNL_NUM; i++) { + if (pHalData->iqk_reg_backup[i].central_chnl == 0) { + rtw_hal_set_hwreg(padapter, HW_VAR_CH_SW_IQK_INFO_BACKUP, (u8 *)&(pHalData->iqk_reg_backup[i])); + return; + } + } + + /* Else, overwrite the oldest record */ + for (i = 1; i < MAX_IQK_INFO_BACKUP_CHNL_NUM; i++) + _rtw_memcpy(&(pHalData->iqk_reg_backup[i - 1]), &(pHalData->iqk_reg_backup[i]), sizeof(struct hal_iqk_reg_backup)); + + rtw_hal_set_hwreg(padapter, HW_VAR_CH_SW_IQK_INFO_BACKUP, (u8 *)&(pHalData->iqk_reg_backup[MAX_IQK_INFO_BACKUP_CHNL_NUM - 1])); +} + +void rtw_hal_ch_sw_iqk_info_restore(_adapter *padapter, u8 ch_sw_use_case) +{ + rtw_hal_set_hwreg(padapter, HW_VAR_CH_SW_IQK_INFO_RESTORE, &ch_sw_use_case); +} + +void rtw_dump_mac_rx_counters(_adapter *padapter, struct dbg_rx_counter *rx_counter) +{ + u32 mac_cck_ok = 0, mac_ofdm_ok = 0, mac_ht_ok = 0, mac_vht_ok = 0; + u32 mac_cck_err = 0, mac_ofdm_err = 0, mac_ht_err = 0, mac_vht_err = 0; + u32 mac_cck_fa = 0, mac_ofdm_fa = 0, mac_ht_fa = 0; + u32 DropPacket = 0; + + if (!rx_counter) { + rtw_warn_on(1); + return; + } + if (IS_HARDWARE_TYPE_JAGUAR(padapter) || IS_HARDWARE_TYPE_JAGUAR2(padapter)) + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT26, 0x0);/*clear bit-26*/ + + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT28 | BIT29 | BIT30 | BIT31, 0x3); + mac_cck_ok = phy_query_mac_reg(padapter, REG_RXERR_RPT, bMaskLWord);/* [15:0] */ + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT28 | BIT29 | BIT30 | BIT31, 0x0); + mac_ofdm_ok = phy_query_mac_reg(padapter, REG_RXERR_RPT, bMaskLWord);/* [15:0] */ + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT28 | BIT29 | BIT30 | BIT31, 0x6); + mac_ht_ok = phy_query_mac_reg(padapter, REG_RXERR_RPT, bMaskLWord);/* [15:0] */ + mac_vht_ok = 0; + if (IS_HARDWARE_TYPE_JAGUAR(padapter) || IS_HARDWARE_TYPE_JAGUAR2(padapter)) { + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT28 | BIT29 | BIT30 | BIT31, 0x0); + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT26, 0x1); + mac_vht_ok = phy_query_mac_reg(padapter, REG_RXERR_RPT, bMaskLWord);/* [15:0]*/ + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT26, 0x0);/*clear bit-26*/ + } + + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT28 | BIT29 | BIT30 | BIT31, 0x4); + mac_cck_err = phy_query_mac_reg(padapter, REG_RXERR_RPT, bMaskLWord);/* [15:0] */ + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT28 | BIT29 | BIT30 | BIT31, 0x1); + mac_ofdm_err = phy_query_mac_reg(padapter, REG_RXERR_RPT, bMaskLWord);/* [15:0] */ + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT28 | BIT29 | BIT30 | BIT31, 0x7); + mac_ht_err = phy_query_mac_reg(padapter, REG_RXERR_RPT, bMaskLWord);/* [15:0] */ + mac_vht_err = 0; + if (IS_HARDWARE_TYPE_JAGUAR(padapter) || IS_HARDWARE_TYPE_JAGUAR2(padapter)) { + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT28 | BIT29 | BIT30 | BIT31, 0x1); + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT26, 0x1); + mac_vht_err = phy_query_mac_reg(padapter, REG_RXERR_RPT, bMaskLWord);/* [15:0]*/ + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT26, 0x0);/*clear bit-26*/ + } + + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT28 | BIT29 | BIT30 | BIT31, 0x5); + mac_cck_fa = phy_query_mac_reg(padapter, REG_RXERR_RPT, bMaskLWord);/* [15:0] */ + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT28 | BIT29 | BIT30 | BIT31, 0x2); + mac_ofdm_fa = phy_query_mac_reg(padapter, REG_RXERR_RPT, bMaskLWord);/* [15:0] */ + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT28 | BIT29 | BIT30 | BIT31, 0x9); + mac_ht_fa = phy_query_mac_reg(padapter, REG_RXERR_RPT, bMaskLWord);/* [15:0] */ + + /* Mac_DropPacket */ + rtw_write32(padapter, REG_RXERR_RPT, (rtw_read32(padapter, REG_RXERR_RPT) & 0x0FFFFFFF) | Mac_DropPacket); + DropPacket = rtw_read32(padapter, REG_RXERR_RPT) & 0x0000FFFF; + + rx_counter->rx_pkt_ok = mac_cck_ok + mac_ofdm_ok + mac_ht_ok + mac_vht_ok; + rx_counter->rx_pkt_crc_error = mac_cck_err + mac_ofdm_err + mac_ht_err + mac_vht_err; + rx_counter->rx_cck_fa = mac_cck_fa; + rx_counter->rx_ofdm_fa = mac_ofdm_fa; + rx_counter->rx_ht_fa = mac_ht_fa; + rx_counter->rx_pkt_drop = DropPacket; +} +void rtw_reset_mac_rx_counters(_adapter *padapter) +{ + + /* If no packet rx, MaxRx clock be gating ,BIT_DISGCLK bit19 set 1 for fix*/ + if (IS_HARDWARE_TYPE_8703B(padapter) || + IS_HARDWARE_TYPE_8723D(padapter) || + IS_HARDWARE_TYPE_8188F(padapter) || + IS_HARDWARE_TYPE_8188GTV(padapter) || + IS_HARDWARE_TYPE_8192F(padapter) || + IS_HARDWARE_TYPE_8822C(padapter)) + phy_set_mac_reg(padapter, REG_RCR, BIT19, 0x1); + + /* reset mac counter */ + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT27, 0x1); + phy_set_mac_reg(padapter, REG_RXERR_RPT, BIT27, 0x0); +} + +void rtw_dump_phy_rx_counters(_adapter *padapter, struct dbg_rx_counter *rx_counter) +{ + u32 cckok = 0, cckcrc = 0, ofdmok = 0, ofdmcrc = 0, htok = 0, htcrc = 0, OFDM_FA = 0, CCK_FA = 0, vht_ok = 0, vht_err = 0; + if (!rx_counter) { + rtw_warn_on(1); + return; + } + if (IS_HARDWARE_TYPE_JAGUAR(padapter) || IS_HARDWARE_TYPE_JAGUAR2(padapter)) { + cckok = phy_query_bb_reg(padapter, 0xF04, 0x3FFF); /* [13:0] */ + ofdmok = phy_query_bb_reg(padapter, 0xF14, 0x3FFF); /* [13:0] */ + htok = phy_query_bb_reg(padapter, 0xF10, 0x3FFF); /* [13:0] */ + vht_ok = phy_query_bb_reg(padapter, 0xF0C, 0x3FFF); /* [13:0] */ + cckcrc = phy_query_bb_reg(padapter, 0xF04, 0x3FFF0000); /* [29:16] */ + ofdmcrc = phy_query_bb_reg(padapter, 0xF14, 0x3FFF0000); /* [29:16] */ + htcrc = phy_query_bb_reg(padapter, 0xF10, 0x3FFF0000); /* [29:16] */ + vht_err = phy_query_bb_reg(padapter, 0xF0C, 0x3FFF0000); /* [29:16] */ + CCK_FA = phy_query_bb_reg(padapter, 0xA5C, bMaskLWord); + OFDM_FA = phy_query_bb_reg(padapter, 0xF48, bMaskLWord); + } else if(IS_HARDWARE_TYPE_JAGUAR3(padapter)){ + cckok = phy_query_bb_reg(padapter, 0x2c04, 0xffff); + ofdmok = phy_query_bb_reg(padapter, 0x2c14, 0xffff); + htok = phy_query_bb_reg(padapter, 0x2c10, 0xffff); + vht_ok = phy_query_bb_reg(padapter, 0x2c0c, 0xffff); + cckcrc = phy_query_bb_reg(padapter, 0x2c04, 0xffff0000); + ofdmcrc = phy_query_bb_reg(padapter, 0x2c14, 0xffff0000); + htcrc = phy_query_bb_reg(padapter, 0x2c10, 0xffff0000); + vht_err = phy_query_bb_reg(padapter, 0x2c0c, 0xffff0000); + CCK_FA = phy_query_bb_reg(padapter, 0x1a5c, bMaskLWord); + OFDM_FA = phy_query_bb_reg(padapter, 0x2d00, bMaskLWord) - phy_query_bb_reg(padapter, 0x2de0, bMaskLWord); + } else if(IS_HARDWARE_TYPE_JAGUAR3_11N(padapter)){ + cckok = phy_query_bb_reg(padapter, 0x2aac, 0xffff); + ofdmok = phy_query_bb_reg(padapter, 0x2c14, 0xffff); + htok = phy_query_bb_reg(padapter, 0x2c10, 0xffff); + cckcrc = phy_query_bb_reg(padapter, 0x2aac, 0xffff0000); + ofdmcrc = phy_query_bb_reg(padapter, 0x2c14, 0xffff0000); + htcrc = phy_query_bb_reg(padapter, 0x2c10, 0xffff0000); + CCK_FA = phy_query_bb_reg(padapter, 0x2aa8, 0xffff0000) + phy_query_bb_reg(padapter, 0x2aa8, 0x0000ffff); + OFDM_FA = phy_query_bb_reg(padapter, 0x2d00, bMaskLWord) - phy_query_bb_reg(padapter, 0x2de0, bMaskLWord); + } else { + cckok = phy_query_bb_reg(padapter, 0xF88, bMaskDWord); + ofdmok = phy_query_bb_reg(padapter, 0xF94, bMaskLWord); + htok = phy_query_bb_reg(padapter, 0xF90, bMaskLWord); + vht_ok = 0; + cckcrc = phy_query_bb_reg(padapter, 0xF84, bMaskDWord); + ofdmcrc = phy_query_bb_reg(padapter, 0xF94, bMaskHWord); + htcrc = phy_query_bb_reg(padapter, 0xF90, bMaskHWord); + vht_err = 0; + OFDM_FA = phy_query_bb_reg(padapter, 0xCF0, bMaskLWord) + phy_query_bb_reg(padapter, 0xCF0, bMaskHWord) + + phy_query_bb_reg(padapter, 0xDA0, bMaskHWord) + phy_query_bb_reg(padapter, 0xDA4, bMaskLWord) + + phy_query_bb_reg(padapter, 0xDA4, bMaskHWord) + phy_query_bb_reg(padapter, 0xDA8, bMaskLWord); + + CCK_FA = (rtw_read8(padapter, 0xA5B) << 8) | (rtw_read8(padapter, 0xA5C)); + } + + rx_counter->rx_pkt_ok = cckok + ofdmok + htok + vht_ok; + rx_counter->rx_pkt_crc_error = cckcrc + ofdmcrc + htcrc + vht_err; + rx_counter->rx_ofdm_fa = OFDM_FA; + rx_counter->rx_cck_fa = CCK_FA; + +} + +void rtw_reset_phy_trx_ok_counters(_adapter *padapter) +{ + if (IS_HARDWARE_TYPE_JAGUAR(padapter) || IS_HARDWARE_TYPE_JAGUAR2(padapter)) { + phy_set_bb_reg(padapter, 0xB58, BIT0, 0x1); + phy_set_bb_reg(padapter, 0xB58, BIT0, 0x0); + } else if(IS_HARDWARE_TYPE_JAGUAR3(padapter) || IS_HARDWARE_TYPE_JAGUAR3_11N(padapter)) { + phy_set_bb_reg(padapter, 0x1EB4, BIT25, 0x1); + phy_set_bb_reg(padapter, 0x1EB4, BIT25, 0x0); + } else { + phy_set_bb_reg(padapter, 0xF14, BIT16, 0x1); + phy_set_bb_reg(padapter, 0xF14, BIT16, 0x0); + } +} + +void rtw_reset_phy_rx_counters(_adapter *padapter) +{ + /* reset phy counter */ + if (IS_HARDWARE_TYPE_JAGUAR3(padapter)) { + /* reset CCK FA counter */ + phy_set_bb_reg(padapter, 0x1a2c, BIT(15) | BIT(14), 0); + phy_set_bb_reg(padapter, 0x1a2c, BIT(15) | BIT(14), 2); + + /* reset CCK CCA counter */ + phy_set_bb_reg(padapter, 0x1a2c, BIT(13) | BIT(12), 0); + phy_set_bb_reg(padapter, 0x1a2c, BIT(13) | BIT(12), 2); + + } else if (IS_HARDWARE_TYPE_JAGUAR3_11N(padapter)) { + /* reset CCK FA and CCK CCA counter */ + phy_set_bb_reg(padapter, 0x2a44, BIT21, 0); + phy_set_bb_reg(padapter, 0x2a44, BIT21, 1); + rtw_reset_phy_trx_ok_counters(padapter); + + } else if (IS_HARDWARE_TYPE_JAGUAR(padapter) || IS_HARDWARE_TYPE_JAGUAR2(padapter)) { + rtw_reset_phy_trx_ok_counters(padapter); + + phy_set_bb_reg(padapter, 0x9A4, BIT17, 0x1);/* reset OFDA FA counter */ + phy_set_bb_reg(padapter, 0x9A4, BIT17, 0x0); + + phy_set_bb_reg(padapter, 0xA2C, BIT15, 0x0);/* reset CCK FA counter */ + phy_set_bb_reg(padapter, 0xA2C, BIT15, 0x1); + } else { + phy_set_bb_reg(padapter, 0xF14, BIT16, 0x1); + rtw_msleep_os(10); + phy_set_bb_reg(padapter, 0xF14, BIT16, 0x0); + + phy_set_bb_reg(padapter, 0xD00, BIT27, 0x1);/* reset OFDA FA counter */ + phy_set_bb_reg(padapter, 0xC0C, BIT31, 0x1);/* reset OFDA FA counter */ + phy_set_bb_reg(padapter, 0xD00, BIT27, 0x0); + phy_set_bb_reg(padapter, 0xC0C, BIT31, 0x0); + + phy_set_bb_reg(padapter, 0xA2C, BIT15, 0x0);/* reset CCK FA counter */ + phy_set_bb_reg(padapter, 0xA2C, BIT15, 0x1); + } +} +#ifdef DBG_RX_COUNTER_DUMP +void rtw_dump_drv_rx_counters(_adapter *padapter, struct dbg_rx_counter *rx_counter) +{ + struct recv_priv *precvpriv = &padapter->recvpriv; + if (!rx_counter) { + rtw_warn_on(1); + return; + } + rx_counter->rx_pkt_ok = padapter->drv_rx_cnt_ok; + rx_counter->rx_pkt_crc_error = padapter->drv_rx_cnt_crcerror; + rx_counter->rx_pkt_drop = precvpriv->rx_drop - padapter->drv_rx_cnt_drop; +} +void rtw_reset_drv_rx_counters(_adapter *padapter) +{ + struct recv_priv *precvpriv = &padapter->recvpriv; + padapter->drv_rx_cnt_ok = 0; + padapter->drv_rx_cnt_crcerror = 0; + padapter->drv_rx_cnt_drop = precvpriv->rx_drop; +} +void rtw_dump_phy_rxcnts_preprocess(_adapter *padapter, u8 rx_cnt_mode) +{ + u8 initialgain; + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(padapter); + + if ((!(padapter->dump_rx_cnt_mode & DUMP_PHY_RX_COUNTER)) && (rx_cnt_mode & DUMP_PHY_RX_COUNTER)) { + rtw_hal_get_odm_var(padapter, HAL_ODM_INITIAL_GAIN, &initialgain, NULL); + RTW_INFO("%s CurIGValue:0x%02x\n", __FUNCTION__, initialgain); + rtw_hal_set_odm_var(padapter, HAL_ODM_INITIAL_GAIN, &initialgain, _FALSE); + /*disable dynamic functions, such as high power, DIG*/ + rtw_phydm_ability_backup(padapter); + rtw_phydm_func_clr(padapter, (ODM_BB_DIG | ODM_BB_FA_CNT)); + } else if ((padapter->dump_rx_cnt_mode & DUMP_PHY_RX_COUNTER) && (!(rx_cnt_mode & DUMP_PHY_RX_COUNTER))) { + /* turn on phy-dynamic functions */ + rtw_phydm_ability_restore(padapter); + initialgain = 0xff; /* restore RX GAIN */ + rtw_hal_set_odm_var(padapter, HAL_ODM_INITIAL_GAIN, &initialgain, _FALSE); + + } +} + +void rtw_dump_rx_counters(_adapter *padapter) +{ + struct dbg_rx_counter rx_counter; + + if (padapter->dump_rx_cnt_mode & DUMP_DRV_RX_COUNTER) { + _rtw_memset(&rx_counter, 0, sizeof(struct dbg_rx_counter)); + rtw_dump_drv_rx_counters(padapter, &rx_counter); + RTW_INFO("Drv Received packet OK:%d CRC error:%d Drop Packets: %d\n", + rx_counter.rx_pkt_ok, rx_counter.rx_pkt_crc_error, rx_counter.rx_pkt_drop); + rtw_reset_drv_rx_counters(padapter); + } + + if (padapter->dump_rx_cnt_mode & DUMP_MAC_RX_COUNTER) { + _rtw_memset(&rx_counter, 0, sizeof(struct dbg_rx_counter)); + rtw_dump_mac_rx_counters(padapter, &rx_counter); + RTW_INFO("Mac Received packet OK:%d CRC error:%d FA Counter: %d Drop Packets: %d\n", + rx_counter.rx_pkt_ok, rx_counter.rx_pkt_crc_error, + rx_counter.rx_cck_fa + rx_counter.rx_ofdm_fa + rx_counter.rx_ht_fa, + rx_counter.rx_pkt_drop); + rtw_reset_mac_rx_counters(padapter); + } + + if (padapter->dump_rx_cnt_mode & DUMP_PHY_RX_COUNTER) { + _rtw_memset(&rx_counter, 0, sizeof(struct dbg_rx_counter)); + rtw_dump_phy_rx_counters(padapter, &rx_counter); + /* RTW_INFO("%s: OFDM_FA =%d\n", __FUNCTION__, rx_counter.rx_ofdm_fa); */ + /* RTW_INFO("%s: CCK_FA =%d\n", __FUNCTION__, rx_counter.rx_cck_fa); */ + RTW_INFO("Phy Received packet OK:%d CRC error:%d FA Counter: %d\n", rx_counter.rx_pkt_ok, rx_counter.rx_pkt_crc_error, + rx_counter.rx_ofdm_fa + rx_counter.rx_cck_fa); + rtw_reset_phy_rx_counters(padapter); + } +} +#endif +u8 rtw_get_current_tx_sgi(_adapter *padapter, struct sta_info *psta) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(padapter); + u8 curr_tx_sgi = 0; + struct ra_sta_info *ra_info; + + if (!psta) + return curr_tx_sgi; + + if (padapter->fix_rate == 0xff) { +#if defined(CONFIG_RTL8188E) +#if (RATE_ADAPTIVE_SUPPORT == 1) + curr_tx_sgi = hal_data->odmpriv.ra_info[psta->cmn.mac_id].rate_sgi; +#endif /* (RATE_ADAPTIVE_SUPPORT == 1)*/ +#else + ra_info = &psta->cmn.ra_info; + curr_tx_sgi = ((ra_info->curr_tx_rate) & 0x80) >> 7; +#endif + } else { + curr_tx_sgi = ((padapter->fix_rate) & 0x80) >> 7; + } + + return curr_tx_sgi; +} + +u8 rtw_get_current_tx_rate(_adapter *padapter, struct sta_info *psta) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(padapter); + u8 rate_id = 0; + struct ra_sta_info *ra_info; + + if (!psta) + return rate_id; + + if (padapter->fix_rate == 0xff) { +#if defined(CONFIG_RTL8188E) +#if (RATE_ADAPTIVE_SUPPORT == 1) + rate_id = hal_data->odmpriv.ra_info[psta->cmn.mac_id].decision_rate; +#endif /* (RATE_ADAPTIVE_SUPPORT == 1)*/ +#else + ra_info = &psta->cmn.ra_info; + rate_id = ra_info->curr_tx_rate & 0x7f; +#endif + } else { + rate_id = padapter->fix_rate & 0x7f; + } + + return rate_id; +} + +void update_IOT_info(_adapter *padapter) +{ + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + + switch (pmlmeinfo->assoc_AP_vendor) { + case HT_IOT_PEER_MARVELL: + pmlmeinfo->turboMode_cts2self = 1; + pmlmeinfo->turboMode_rtsen = 0; + break; + + case HT_IOT_PEER_RALINK: + pmlmeinfo->turboMode_cts2self = 0; + pmlmeinfo->turboMode_rtsen = 1; + break; + case HT_IOT_PEER_REALTEK: + /* rtw_write16(padapter, 0x4cc, 0xffff); */ + /* rtw_write16(padapter, 0x546, 0x01c0); */ + break; + default: + pmlmeinfo->turboMode_cts2self = 0; + pmlmeinfo->turboMode_rtsen = 1; + break; + } + +} +#ifdef CONFIG_RTS_FULL_BW +/* +8188E: not support full RTS BW feature(mac REG no define 480[5]) +*/ +void rtw_set_rts_bw(_adapter *padapter) { + int i; + u8 enable = 1; + bool connect_to_8812 = _FALSE; + u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct macid_ctl_t *macid_ctl = dvobj_to_macidctl(dvobj); + struct sta_info *station = NULL; + + for (i = 0; i < macid_ctl->num; i++) { + if (rtw_macid_is_used(macid_ctl, i)) { + + station = NULL; + station = macid_ctl->sta[i]; + if(station) { + + _adapter *sta_adapter =station->padapter; + struct mlme_ext_priv *pmlmeext = &(sta_adapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + + if ( pmlmeinfo->state != WIFI_FW_NULL_STATE) { + if(_rtw_memcmp(macid_ctl->sta[i]->cmn.mac_addr, bc_addr, ETH_ALEN) != _TRUE) { + if ( macid_ctl->sta[i]->vendor_8812) { + connect_to_8812 = _TRUE; + enable = 0; + } + } + } + } + } + + if(connect_to_8812) + break; + } + + RTW_INFO("%s connect_to_8812=%d,enable=%u\n", __FUNCTION__,connect_to_8812,enable); + rtw_hal_set_hwreg(padapter, HW_VAR_SET_RTS_BW, &enable); +} +#endif/*CONFIG_RTS_FULL_BW*/ + +int hal_spec_init(_adapter *adapter) +{ + u8 interface_type = 0; + int ret = _SUCCESS; + + interface_type = rtw_get_intf_type(adapter); + + switch (rtw_get_chip_type(adapter)) { +#ifdef CONFIG_RTL8723B + case RTL8723B: + init_hal_spec_8723b(adapter); + break; +#endif +#ifdef CONFIG_RTL8703B + case RTL8703B: + init_hal_spec_8703b(adapter); + break; +#endif +#ifdef CONFIG_RTL8723D + case RTL8723D: + init_hal_spec_8723d(adapter); + break; +#endif +#ifdef CONFIG_RTL8188E + case RTL8188E: + init_hal_spec_8188e(adapter); + break; +#endif +#ifdef CONFIG_RTL8188F + case RTL8188F: + init_hal_spec_8188f(adapter); + break; +#endif +#ifdef CONFIG_RTL8188GTV + case RTL8188GTV: + init_hal_spec_8188gtv(adapter); + break; +#endif +#ifdef CONFIG_RTL8812A + case RTL8812: + init_hal_spec_8812a(adapter); + break; +#endif +#ifdef CONFIG_RTL8821A + case RTL8821: + init_hal_spec_8821a(adapter); + break; +#endif +#ifdef CONFIG_RTL8192E + case RTL8192E: + init_hal_spec_8192e(adapter); + break; +#endif +#ifdef CONFIG_RTL8814A + case RTL8814A: + init_hal_spec_8814a(adapter); + break; +#endif +#ifdef CONFIG_RTL8822B + case RTL8822B: + rtl8822b_init_hal_spec(adapter); + break; +#endif +#ifdef CONFIG_RTL8821C + case RTL8821C: + init_hal_spec_rtl8821c(adapter); + break; +#endif +#ifdef CONFIG_RTL8710B + case RTL8710B: + init_hal_spec_8710b(adapter); + break; +#endif +#ifdef CONFIG_RTL8192F + case RTL8192F: + init_hal_spec_8192f(adapter); + break; +#endif +#ifdef CONFIG_RTL8822C + case RTL8822C: + rtl8822c_init_hal_spec(adapter); + break; +#endif +#ifdef CONFIG_RTL8814B + case RTL8814B: + rtl8814b_init_hal_spec(adapter); + break; +#endif +#ifdef CONFIG_RTL8723F + case RTL8723F: + rtl8723f_init_hal_spec(adapter); + break; +#endif + default: + RTW_ERR("%s: unknown chip_type:%u\n" + , __func__, rtw_get_chip_type(adapter)); + ret = _FAIL; + break; + } + + return ret; +} + +static const char *const _band_cap_str[] = { + /* BIT0 */"2G", + /* BIT1 */"5G", +}; + +static const char *const _bw_cap_str[] = { + /* BIT0 */"5M", + /* BIT1 */"10M", + /* BIT2 */"20M", + /* BIT3 */"40M", + /* BIT4 */"80M", + /* BIT5 */"160M", + /* BIT6 */"80_80M", +}; + +static const char *const _proto_cap_str[] = { + /* BIT0 */"b", + /* BIT1 */"g", + /* BIT2 */"n", + /* BIT3 */"ac", +}; + +static const char *const _wl_func_str[] = { + /* BIT0 */"P2P", + /* BIT1 */"MIRACAST", + /* BIT2 */"TDLS", + /* BIT3 */"FTM", +}; + +void dump_hal_spec(void *sel, _adapter *adapter) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + int i; + + RTW_PRINT_SEL(sel, "macid_num:%u\n", hal_spec->macid_num); + RTW_PRINT_SEL(sel, "macid_cap:%u\n", hal_spec->macid_cap); + RTW_PRINT_SEL(sel, "sec_cap:0x%02x\n", hal_spec->sec_cap); + RTW_PRINT_SEL(sel, "sec_cam_ent_num:%u\n", hal_spec->sec_cam_ent_num); + + RTW_PRINT_SEL(sel, "rfpath_num_2g:%u\n", hal_spec->rfpath_num_2g); + RTW_PRINT_SEL(sel, "rfpath_num_5g:%u\n", hal_spec->rfpath_num_5g); + RTW_PRINT_SEL(sel, "rf_reg_path_num:%u\n", hal_spec->rf_reg_path_num); + RTW_PRINT_SEL(sel, "rf_reg_path_avail_num:%u\n", hal_spec->rf_reg_path_avail_num); + RTW_PRINT_SEL(sel, "rf_reg_trx_path_bmp:0x%02x\n", hal_spec->rf_reg_trx_path_bmp); + RTW_PRINT_SEL(sel, "max_tx_cnt:%u\n", hal_spec->max_tx_cnt); + + RTW_PRINT_SEL(sel, "tx_nss_num:%u\n", hal_spec->tx_nss_num); + RTW_PRINT_SEL(sel, "rx_nss_num:%u\n", hal_spec->rx_nss_num); + + RTW_PRINT_SEL(sel, "band_cap:"); + for (i = 0; i < BAND_CAP_BIT_NUM; i++) { + if (((hal_spec->band_cap) >> i) & BIT0 && _band_cap_str[i]) + _RTW_PRINT_SEL(sel, "%s ", _band_cap_str[i]); + } + _RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "bw_cap:"); + for (i = 0; i < BW_CAP_BIT_NUM; i++) { + if (((hal_spec->bw_cap) >> i) & BIT0 && _bw_cap_str[i]) + _RTW_PRINT_SEL(sel, "%s ", _bw_cap_str[i]); + } + _RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "proto_cap:"); + for (i = 0; i < PROTO_CAP_BIT_NUM; i++) { + if (((hal_spec->proto_cap) >> i) & BIT0 && _proto_cap_str[i]) + _RTW_PRINT_SEL(sel, "%s ", _proto_cap_str[i]); + } + _RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "txgi_max:%u\n", hal_spec->txgi_max); + RTW_PRINT_SEL(sel, "txgi_pdbm:%u\n", hal_spec->txgi_pdbm); + + RTW_PRINT_SEL(sel, "wl_func:"); + for (i = 0; i < WL_FUNC_BIT_NUM; i++) { + if (((hal_spec->wl_func) >> i) & BIT0 && _wl_func_str[i]) + _RTW_PRINT_SEL(sel, "%s ", _wl_func_str[i]); + } + _RTW_PRINT_SEL(sel, "\n"); + +#if CONFIG_TX_AC_LIFETIME + RTW_PRINT_SEL(sel, "tx_aclt_unit_factor:%u (unit:%uus)\n" + , hal_spec->tx_aclt_unit_factor, hal_spec->tx_aclt_unit_factor * 32); +#endif + + RTW_PRINT_SEL(sel, "rx_tsf_filter:%u\n", hal_spec->rx_tsf_filter); + + RTW_PRINT_SEL(sel, "pg_txpwr_saddr:0x%X\n", hal_spec->pg_txpwr_saddr); + RTW_PRINT_SEL(sel, "pg_txgi_diff_factor:%u\n", hal_spec->pg_txgi_diff_factor); +} + +inline bool hal_chk_band_cap(_adapter *adapter, u8 cap) +{ + return GET_HAL_SPEC(adapter)->band_cap & cap; +} + +inline bool hal_chk_bw_cap(_adapter *adapter, u8 cap) +{ + return GET_HAL_SPEC(adapter)->bw_cap & cap; +} + +inline bool hal_chk_proto_cap(_adapter *adapter, u8 cap) +{ + return GET_HAL_SPEC(adapter)->proto_cap & cap; +} + +inline bool hal_chk_wl_func(_adapter *adapter, u8 func) +{ + return GET_HAL_SPEC(adapter)->wl_func & func; +} + +inline bool hal_is_band_support(_adapter *adapter, u8 band) +{ + return GET_HAL_SPEC(adapter)->band_cap & band_to_band_cap(band); +} + +inline bool hal_is_bw_support(_adapter *adapter, u8 bw) +{ + return GET_HAL_SPEC(adapter)->bw_cap & ch_width_to_bw_cap(bw); +} + +inline bool hal_is_wireless_mode_support(_adapter *adapter, u8 mode) +{ + u8 proto_cap = GET_HAL_SPEC(adapter)->proto_cap; + + if (mode == WIRELESS_11B) + if ((proto_cap & PROTO_CAP_11B) && hal_chk_band_cap(adapter, BAND_CAP_2G)) + return 1; + + if (mode == WIRELESS_11G) + if ((proto_cap & PROTO_CAP_11G) && hal_chk_band_cap(adapter, BAND_CAP_2G)) + return 1; + + if (mode == WIRELESS_11A) + if ((proto_cap & PROTO_CAP_11G) && hal_chk_band_cap(adapter, BAND_CAP_5G)) + return 1; + + if (mode == WIRELESS_11_24N) + if ((proto_cap & PROTO_CAP_11N) && hal_chk_band_cap(adapter, BAND_CAP_2G)) + return 1; + + if (mode == WIRELESS_11_5N) + if ((proto_cap & PROTO_CAP_11N) && hal_chk_band_cap(adapter, BAND_CAP_5G)) + return 1; + + if (mode == WIRELESS_11AC) + if ((proto_cap & PROTO_CAP_11AC) && hal_chk_band_cap(adapter, BAND_CAP_5G)) + return 1; + + return 0; +} +inline bool hal_is_mimo_support(_adapter *adapter) +{ + if ((GET_HAL_TX_NSS(adapter) == 1) && + (GET_HAL_RX_NSS(adapter) == 1)) + return 0; + return 1; +} + +/* +* hal_largest_bw - starting from in_bw, get largest bw supported by HAL +* @adapter: +* @in_bw: starting bw, value of enum channel_width +* +* Returns: value of enum channel_width +*/ +u8 hal_largest_bw(_adapter *adapter, u8 in_bw) +{ + for (; in_bw > CHANNEL_WIDTH_20; in_bw--) { + if (hal_is_bw_support(adapter, in_bw)) + break; + } + + if (!hal_is_bw_support(adapter, in_bw)) + rtw_warn_on(1); + + return in_bw; +} + +#ifndef CONFIG_HAS_TX_BEACON_PAUSE +void ResumeTxBeacon(_adapter *padapter) +{ + RTW_DBG("ResumeTxBeacon\n"); + #ifdef CONFIG_STOP_RESUME_BCN_BY_TXPAUSE + rtw_write8(padapter, REG_TXPAUSE, + rtw_read8(padapter, REG_TXPAUSE) & (~BIT6)); + #else + rtw_write8(padapter, REG_FWHW_TXQ_CTRL + 2, + rtw_read8(padapter, REG_FWHW_TXQ_CTRL + 2) | BIT(6)); + #endif + +#ifdef RTW_HALMAC + /* Add this for driver using HALMAC because driver doesn't have setup time init by self */ + /* TBTT setup time */ + rtw_write8(padapter, REG_TBTT_PROHIBIT, TBTT_PROHIBIT_SETUP_TIME); +#endif + + /* TBTT hold time: 0x540[19:8] */ + rtw_write8(padapter, REG_TBTT_PROHIBIT + 1, TBTT_PROHIBIT_HOLD_TIME & 0xFF); + rtw_write8(padapter, REG_TBTT_PROHIBIT + 2, + (rtw_read8(padapter, REG_TBTT_PROHIBIT + 2) & 0xF0) | (TBTT_PROHIBIT_HOLD_TIME >> 8)); +} + +void StopTxBeacon(_adapter *padapter) +{ + RTW_DBG("StopTxBeacon\n"); + #ifdef CONFIG_STOP_RESUME_BCN_BY_TXPAUSE + rtw_write8(padapter, REG_TXPAUSE, + rtw_read8(padapter, REG_TXPAUSE) | BIT6); + #else + rtw_write8(padapter, REG_FWHW_TXQ_CTRL + 2, + rtw_read8(padapter, REG_FWHW_TXQ_CTRL + 2) & (~BIT6)); + #endif + + /* TBTT hold time: 0x540[19:8] */ + rtw_write8(padapter, REG_TBTT_PROHIBIT + 1, TBTT_PROHIBIT_HOLD_TIME_STOP_BCN & 0xFF); + rtw_write8(padapter, REG_TBTT_PROHIBIT + 2, + (rtw_read8(padapter, REG_TBTT_PROHIBIT + 2) & 0xF0) | (TBTT_PROHIBIT_HOLD_TIME_STOP_BCN >> 8)); +} +#endif /* CONFIG_HAS_TX_BEACON_PAUSE */ + +#ifdef CONFIG_MI_WITH_MBSSID_CAM /*HW port0 - MBSS*/ + +#ifdef CONFIG_CLIENT_PORT_CFG +const u8 _clt_port_id[MAX_CLIENT_PORT_NUM] = { + CLT_PORT0, + CLT_PORT1, + CLT_PORT2, + CLT_PORT3 +}; + +void rtw_clt_port_init(struct clt_port_t *cltp) +{ + cltp->bmp = 0; + cltp->num = 0; + _rtw_spinlock_init(&cltp->lock); +} +void rtw_clt_port_deinit(struct clt_port_t *cltp) +{ + _rtw_spinlock_free(&cltp->lock); +} +static void _hw_client_port_alloc(_adapter *adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct clt_port_t *cltp = &dvobj->clt_port; + _irqL irql; + int i; + + #if 0 + if (cltp->num > MAX_CLIENT_PORT_NUM) { + RTW_ERR(ADPT_FMT" cann't alloc client (%d)\n", ADPT_ARG(adapter), cltp->num); + rtw_warn_on(1); + return; + } + #endif + + if (adapter->client_id != MAX_CLIENT_PORT_NUM) { + RTW_INFO(ADPT_FMT" client_id %d has allocated port:%d\n", + ADPT_ARG(adapter), adapter->client_id, adapter->client_port); + return; + } + _enter_critical_bh(&cltp->lock, &irql); + for (i = 0; i < MAX_CLIENT_PORT_NUM; i++) { + if (!(cltp->bmp & BIT(i))) + break; + } + + if (i < MAX_CLIENT_PORT_NUM) { + adapter->client_id = i; + cltp->bmp |= BIT(i); + adapter->client_port = _clt_port_id[i]; + } + cltp->num++; + _exit_critical_bh(&cltp->lock, &irql); + RTW_INFO("%s("ADPT_FMT")id:%d, port:%d clt_num:%d\n", + __func__, ADPT_ARG(adapter), adapter->client_id, adapter->client_port, cltp->num); +} +static void _hw_client_port_free(_adapter *adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct clt_port_t *cltp = &dvobj->clt_port; + _irqL irql; + + #if 0 + if (adapter->client_id >= MAX_CLIENT_PORT_NUM) { + RTW_ERR(ADPT_FMT" client_id %d is invalid\n", ADPT_ARG(adapter), adapter->client_id); + /*rtw_warn_on(1);*/ + } + #endif + + RTW_INFO("%s ("ADPT_FMT") id:%d, port:%d clt_num:%d\n", + __func__, ADPT_ARG(adapter), adapter->client_id, adapter->client_port, cltp->num); + + _enter_critical_bh(&cltp->lock, &irql); + if (adapter->client_id != MAX_CLIENT_PORT_NUM) { + cltp->bmp &= ~ BIT(adapter->client_id); + adapter->client_id = MAX_CLIENT_PORT_NUM; + adapter->client_port = CLT_PORT_INVALID; + } + cltp->num--; + if (cltp->num < 0) + cltp->num = 0; + _exit_critical_bh(&cltp->lock, &irql); +} +void rtw_hw_client_port_allocate(_adapter *adapter) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + + if (hal_spec->port_num != 5) + return; + + _hw_client_port_alloc(adapter); +} +void rtw_hw_client_port_release(_adapter *adapter) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + + if (hal_spec->port_num != 5) + return; + + _hw_client_port_free(adapter); +} +#endif /*CONFIG_CLIENT_PORT_CFG*/ + +void hw_var_set_opmode_mbid(_adapter *Adapter, u8 mode) +{ + RTW_INFO("%s()-"ADPT_FMT" mode = %d\n", __func__, ADPT_ARG(Adapter), mode); + + rtw_hal_rcr_set_chk_bssid(Adapter, MLME_ACTION_NONE); + + /* set net_type */ + Set_MSR(Adapter, mode); + + if ((mode == _HW_STATE_STATION_) || (mode == _HW_STATE_NOLINK_)) { + if (!rtw_mi_get_ap_num(Adapter) && !rtw_mi_get_mesh_num(Adapter)) + StopTxBeacon(Adapter); + } else if (mode == _HW_STATE_ADHOC_) + ResumeTxBeacon(Adapter); + else if (mode == _HW_STATE_AP_) + /* enable rx ps-poll */ + rtw_write16(Adapter, REG_RXFLTMAP1, rtw_read16(Adapter, REG_RXFLTMAP1) | BIT_CTRLFLT10EN); + + /* enable rx data frame */ + rtw_write16(Adapter, REG_RXFLTMAP2, 0xFFFF); + +#ifdef CONFIG_CLIENT_PORT_CFG + if (mode == _HW_STATE_STATION_) + rtw_hw_client_port_allocate(Adapter); + else + rtw_hw_client_port_release(Adapter); +#endif +#if defined(CONFIG_RTL8192F) + rtw_write16(Adapter, REG_WLAN_ACT_MASK_CTRL_1, rtw_read16(Adapter, + REG_WLAN_ACT_MASK_CTRL_1) | EN_PORT_0_FUNCTION); +#endif +} +#endif + +#ifdef CONFIG_ANTENNA_DIVERSITY +u8 rtw_hal_antdiv_before_linked(_adapter *padapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + u8 cur_ant, change_ant; + + if (!pHalData->AntDivCfg) + return _FALSE; + + if (pHalData->sw_antdiv_bl_state == 0) { + pHalData->sw_antdiv_bl_state = 1; + + rtw_hal_get_odm_var(padapter, HAL_ODM_ANTDIV_SELECT, &cur_ant, NULL); + change_ant = (cur_ant == MAIN_ANT) ? AUX_ANT : MAIN_ANT; + + return rtw_antenna_select_cmd(padapter, change_ant, _FALSE); + } + + pHalData->sw_antdiv_bl_state = 0; + return _FALSE; +} + +void rtw_hal_antdiv_rssi_compared(_adapter *padapter, WLAN_BSSID_EX *dst, WLAN_BSSID_EX *src) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + + if (pHalData->AntDivCfg) { + /*RTW_INFO("update_network=> org-RSSI(%d), new-RSSI(%d)\n", dst->Rssi, src->Rssi);*/ + /*select optimum_antenna for before linked =>For antenna diversity*/ + if (dst->Rssi >= src->Rssi) {/*keep org parameter*/ + src->Rssi = dst->Rssi; + src->PhyInfo.Optimum_antenna = dst->PhyInfo.Optimum_antenna; + } + } +} +#endif + +#ifdef CONFIG_PROC_DEBUG +#ifdef CONFIG_PHY_CAPABILITY_QUERY +void rtw_dump_phy_cap_by_phydmapi(void *sel, _adapter *adapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(adapter); + struct phy_spec_t *phy_spec = &pHalData->phy_spec; + + RTW_PRINT_SEL(sel, "[PHY SPEC] TRx Capability : 0x%08x\n", phy_spec->trx_cap); + RTW_PRINT_SEL(sel, "[PHY SPEC] Tx Stream Num Index : %d\n", (phy_spec->trx_cap >> 24) & 0xFF); /*Tx Stream Num Index [31:24]*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] Rx Stream Num Index : %d\n", (phy_spec->trx_cap >> 16) & 0xFF); /*Rx Stream Num Index [23:16]*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] Tx Path Num Index : %d\n", (phy_spec->trx_cap >> 8) & 0xFF);/*Tx Path Num Index [15:8]*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] Rx Path Num Index : %d\n\n", (phy_spec->trx_cap & 0xFF));/*Rx Path Num Index [7:0]*/ + + RTW_PRINT_SEL(sel, "[PHY SPEC] STBC Capability : 0x%08x\n", phy_spec->stbc_cap); + RTW_PRINT_SEL(sel, "[PHY SPEC] VHT STBC Tx : %s\n", ((phy_spec->stbc_cap >> 24) & 0xFF) ? "Supported" : "N/A"); /*VHT STBC Tx [31:24]*/ + /*VHT STBC Rx [23:16] + 0 = not support + 1 = support for 1 spatial stream + 2 = support for 1 or 2 spatial streams + 3 = support for 1 or 2 or 3 spatial streams + 4 = support for 1 or 2 or 3 or 4 spatial streams*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] VHT STBC Rx :%d\n", ((phy_spec->stbc_cap >> 16) & 0xFF)); + RTW_PRINT_SEL(sel, "[PHY SPEC] HT STBC Tx : %s\n", ((phy_spec->stbc_cap >> 8) & 0xFF) ? "Supported" : "N/A"); /*HT STBC Tx [15:8]*/ + /*HT STBC Rx [7:0] + 0 = not support + 1 = support for 1 spatial stream + 2 = support for 1 or 2 spatial streams + 3 = support for 1 or 2 or 3 spatial streams*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] HT STBC Rx : %d\n\n", (phy_spec->stbc_cap & 0xFF)); + + RTW_PRINT_SEL(sel, "[PHY SPEC] LDPC Capability : 0x%08x\n", phy_spec->ldpc_cap); + RTW_PRINT_SEL(sel, "[PHY SPEC] VHT LDPC Tx : %s\n", ((phy_spec->ldpc_cap >> 24) & 0xFF) ? "Supported" : "N/A"); /*VHT LDPC Tx [31:24]*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] VHT LDPC Rx : %s\n", ((phy_spec->ldpc_cap >> 16) & 0xFF) ? "Supported" : "N/A"); /*VHT LDPC Rx [23:16]*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] HT LDPC Tx : %s\n", ((phy_spec->ldpc_cap >> 8) & 0xFF) ? "Supported" : "N/A"); /*HT LDPC Tx [15:8]*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] HT LDPC Rx : %s\n\n", (phy_spec->ldpc_cap & 0xFF) ? "Supported" : "N/A"); /*HT LDPC Rx [7:0]*/ + #ifdef CONFIG_BEAMFORMING + RTW_PRINT_SEL(sel, "[PHY SPEC] TxBF Capability : 0x%08x\n", phy_spec->txbf_cap); + RTW_PRINT_SEL(sel, "[PHY SPEC] VHT MU Bfer : %s\n", ((phy_spec->txbf_cap >> 28) & 0xF) ? "Supported" : "N/A"); /*VHT MU Bfer [31:28]*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] VHT MU Bfee : %s\n", ((phy_spec->txbf_cap >> 24) & 0xF) ? "Supported" : "N/A"); /*VHT MU Bfee [27:24]*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] VHT SU Bfer : %s\n", ((phy_spec->txbf_cap >> 20) & 0xF) ? "Supported" : "N/A"); /*VHT SU Bfer [23:20]*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] VHT SU Bfee : %s\n", ((phy_spec->txbf_cap >> 16) & 0xF) ? "Supported" : "N/A"); /*VHT SU Bfee [19:16]*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] HT Bfer : %s\n", ((phy_spec->txbf_cap >> 4) & 0xF) ? "Supported" : "N/A"); /*HT Bfer [7:4]*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] HT Bfee : %s\n\n", (phy_spec->txbf_cap & 0xF) ? "Supported" : "N/A"); /*HT Bfee [3:0]*/ + + RTW_PRINT_SEL(sel, "[PHY SPEC] TxBF parameter : 0x%08x\n", phy_spec->txbf_param); + RTW_PRINT_SEL(sel, "[PHY SPEC] VHT Sounding Dim : %d\n", (phy_spec->txbf_param >> 24) & 0xFF); /*VHT Sounding Dim [31:24]*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] VHT Steering Ant : %d\n", (phy_spec->txbf_param >> 16) & 0xFF); /*VHT Steering Ant [23:16]*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] HT Sounding Dim : %d\n", (phy_spec->txbf_param >> 8) & 0xFF); /*HT Sounding Dim [15:8]*/ + RTW_PRINT_SEL(sel, "[PHY SPEC] HT Steering Ant : %d\n", phy_spec->txbf_param & 0xFF); /*HT Steering Ant [7:0]*/ + #endif +} +#else +void rtw_dump_phy_cap_by_hal(void *sel, _adapter *adapter) +{ + u8 phy_cap = _FALSE; + + /* STBC */ + rtw_hal_get_def_var(adapter, HAL_DEF_TX_STBC, (u8 *)&phy_cap); + RTW_PRINT_SEL(sel, "[HAL] STBC Tx : %s\n", (_TRUE == phy_cap) ? "Supported" : "N/A"); + + phy_cap = _FALSE; + rtw_hal_get_def_var(adapter, HAL_DEF_RX_STBC, (u8 *)&phy_cap); + RTW_PRINT_SEL(sel, "[HAL] STBC Rx : %s\n\n", (_TRUE == phy_cap) ? "Supported" : "N/A"); + + /* LDPC support */ + phy_cap = _FALSE; + rtw_hal_get_def_var(adapter, HAL_DEF_TX_LDPC, (u8 *)&phy_cap); + RTW_PRINT_SEL(sel, "[HAL] LDPC Tx : %s\n", (_TRUE == phy_cap) ? "Supported" : "N/A"); + + phy_cap = _FALSE; + rtw_hal_get_def_var(adapter, HAL_DEF_RX_LDPC, (u8 *)&phy_cap); + RTW_PRINT_SEL(sel, "[HAL] LDPC Rx : %s\n\n", (_TRUE == phy_cap) ? "Supported" : "N/A"); + + #ifdef CONFIG_BEAMFORMING + phy_cap = _FALSE; + rtw_hal_get_def_var(adapter, HAL_DEF_EXPLICIT_BEAMFORMER, (u8 *)&phy_cap); + RTW_PRINT_SEL(sel, "[HAL] Beamformer: %s\n", (_TRUE == phy_cap) ? "Supported" : "N/A"); + + phy_cap = _FALSE; + rtw_hal_get_def_var(adapter, HAL_DEF_EXPLICIT_BEAMFORMEE, (u8 *)&phy_cap); + RTW_PRINT_SEL(sel, "[HAL] Beamformee: %s\n", (_TRUE == phy_cap) ? "Supported" : "N/A"); + + phy_cap = _FALSE; + rtw_hal_get_def_var(adapter, HAL_DEF_VHT_MU_BEAMFORMER, &phy_cap); + RTW_PRINT_SEL(sel, "[HAL] VHT MU Beamformer: %s\n", (_TRUE == phy_cap) ? "Supported" : "N/A"); + + phy_cap = _FALSE; + rtw_hal_get_def_var(adapter, HAL_DEF_VHT_MU_BEAMFORMEE, &phy_cap); + RTW_PRINT_SEL(sel, "[HAL] VHT MU Beamformee: %s\n", (_TRUE == phy_cap) ? "Supported" : "N/A"); + #endif +} +#endif +void rtw_dump_phy_cap(void *sel, _adapter *adapter) +{ + RTW_PRINT_SEL(sel, "\n ======== PHY Capability ========\n"); +#ifdef CONFIG_PHY_CAPABILITY_QUERY + rtw_dump_phy_cap_by_phydmapi(sel, adapter); +#else + rtw_dump_phy_cap_by_hal(sel, adapter); +#endif +} +#endif + +inline s16 translate_dbm_to_percentage(s16 signal) +{ + if ((signal <= -100) || (signal >= 20)) + return 0; + else if (signal >= 0) + return 100; + else + return 100 + signal; +} + +#ifdef CONFIG_SWTIMER_BASED_TXBCN +#ifdef CONFIG_BCN_RECOVERY +#define REG_CPU_MGQ_INFO 0x041C +#define BIT_BCN_POLL BIT(28) +u8 rtw_ap_bcn_recovery(_adapter *padapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(padapter); + + if (hal_data->issue_bcn_fail >= 2) { + RTW_ERR("%s ISSUE BCN Fail\n", __func__); + rtw_write8(padapter, REG_CPU_MGQ_INFO + 3, 0x10); + hal_data->issue_bcn_fail = 0; + } + return _SUCCESS; +} +#endif /*CONFIG_BCN_RECOVERY*/ + +#ifdef CONFIG_BCN_XMIT_PROTECT +u8 rtw_ap_bcn_queue_empty_check(_adapter *padapter, u32 txbcn_timer_ms) +{ + u32 start_time = rtw_get_current_time(); + u8 bcn_queue_empty = _FALSE; + + do { + if (rtw_read16(padapter, REG_TXPKT_EMPTY) & BIT(11)) { + bcn_queue_empty = _TRUE; + break; + } + } while (rtw_get_passing_time_ms(start_time) <= (txbcn_timer_ms + 10)); + + if (bcn_queue_empty == _FALSE) + RTW_ERR("%s BCN queue not empty\n", __func__); + + return bcn_queue_empty; +} +#endif /*CONFIG_BCN_XMIT_PROTECT*/ +#endif /*CONFIG_SWTIMER_BASED_TXBCN*/ + +/** + * rtw_hal_get_trx_path() - Get RF path related information + * @d: struct dvobj_priv* + * @type: RF type, nTnR + * @tx: Tx path + * @rx: Rx path + * + * Get RF type, TX path and RX path information. + */ +void rtw_hal_get_trx_path(struct dvobj_priv *d, enum rf_type *type, + enum bb_path *tx, enum bb_path *rx) +{ + struct _ADAPTER *a = dvobj_get_primary_adapter(d); + enum rf_type t = GET_HAL_RFPATH(a); + + if (type) + *type = t; + + if (tx || rx) { + u8 tx_bmp = GET_HAL_TX_PATH_BMP(a); + u8 rx_bmp = GET_HAL_RX_PATH_BMP(a); + + if (!tx_bmp && !rx_bmp) + rf_type_to_default_trx_bmp(t, tx, rx); + else { + if (tx) + *tx = GET_HAL_TX_PATH_BMP(a); + if (rx) + *rx = GET_HAL_RX_PATH_BMP(a); + } + } +} + +#ifdef RTW_CHANNEL_SWITCH_OFFLOAD +void rtw_hal_switch_chnl_and_set_bw_offload(_adapter *adapter, u8 central_ch, u8 pri_ch_idx, u8 bw) +{ + u8 h2c[H2C_SINGLE_CHANNELSWITCH_V2_LEN] = {0}; + PHAL_DATA_TYPE hal; + struct submit_ctx *chsw_sctx; + + hal = GET_HAL_DATA(adapter); + chsw_sctx = &hal->chsw_sctx; + + SET_H2CCMD_SINGLE_CH_SWITCH_V2_CENTRAL_CH_NUM(h2c, central_ch); + SET_H2CCMD_SINGLE_CH_SWITCH_V2_PRIMARY_CH_IDX(h2c, pri_ch_idx); + SET_H2CCMD_SINGLE_CH_SWITCH_V2_BW(h2c, bw); + SET_H2CCMD_SINGLE_CH_SWITCH_V2_IQK_UPDATE_EN(h2c, 1); + + rtw_sctx_init(chsw_sctx, 10); + rtw_hal_fill_h2c_cmd(adapter, H2C_SINGLE_CHANNELSWITCH_V2, H2C_SINGLE_CHANNELSWITCH_V2_LEN, h2c); + rtw_sctx_wait(chsw_sctx, __func__); +} +#endif /* RTW_CHANNEL_SWITCH_OFFLOAD */ + +u8 phy_get_capable_tx_num(_adapter *adapter, enum MGN_RATE rate) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u8 tx_num = 0; + + if (IS_1T_RATE(rate)) + tx_num = hal_data->txpath_cap_num_nss[0]; + else if (IS_2T_RATE(rate)) + tx_num = hal_data->txpath_cap_num_nss[1]; + else if (IS_3T_RATE(rate)) + tx_num = hal_data->txpath_cap_num_nss[2]; + else if (IS_4T_RATE(rate)) + tx_num = hal_data->txpath_cap_num_nss[3]; + else + rtw_warn_on(1); + + return tx_num == 0 ? RF_1TX : tx_num - 1; +} + +u8 phy_get_current_tx_num(_adapter *adapter, enum MGN_RATE rate) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u8 tx_num = 0; + + if (IS_1T_RATE(rate)) + tx_num = hal_data->txpath_num_nss[0]; + else if (IS_2T_RATE(rate)) + tx_num = hal_data->txpath_num_nss[1]; + else if (IS_3T_RATE(rate)) + tx_num = hal_data->txpath_num_nss[2]; + else if (IS_4T_RATE(rate)) + tx_num = hal_data->txpath_num_nss[3]; + else + rtw_warn_on(1); + + return tx_num == 0 ? RF_1TX : tx_num - 1; +} + +#ifdef CONFIG_RTL8812A +u8 * rtw_hal_set_8812a_vendor_ie(_adapter *padapter , u8 *pframe ,uint *frlen ) { + int vender_len = 7; + unsigned char vendor_info[vender_len]; + unsigned char REALTEK_OUI[] = {0x00, 0xe0, 0x4c}; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + + if( !IS_HARDWARE_TYPE_8812(padapter) ) + return pframe; + + _rtw_memset(vendor_info,0,vender_len); + _rtw_memcpy(vendor_info, REALTEK_OUI, 3); + vendor_info[4] =2; + if(pHalData->version_id.CUTVersion > B_CUT_VERSION ) + vendor_info[6] = RT_HT_CAP_USE_JAGUAR_CCUT; + else + vendor_info[6] = RT_HT_CAP_USE_JAGUAR_BCUT; + pframe = rtw_set_ie(pframe, _VENDOR_SPECIFIC_IE_,vender_len,vendor_info , frlen); + + return pframe; +} +#endif /*CONFIG_RTL8812A*/ + +static inline void rtw_enter_protsel(struct protsel *protsel, u32 sel) +{ + int refcnt; + + _enter_critical_mutex(&protsel->mutex, NULL); + + refcnt = ATOMIC_INC_RETURN(&protsel->refcnt); + + WARN_ON(refcnt > 1 && protsel->sel != sel); + + protsel->sel = sel; + + _exit_critical_mutex(&protsel->mutex, NULL); +} + +static inline void rtw_leave_protsel(struct protsel *protsel) +{ + int refcnt; + + _enter_critical_mutex(&protsel->mutex, NULL); + + refcnt = ATOMIC_DEC_RETURN(&protsel->refcnt); + + _exit_critical_mutex(&protsel->mutex, NULL); + + WARN_ON(refcnt < 0); +} + +static inline bool rtw_assert_protsel(struct protsel *protsel) +{ + int refcnt = ATOMIC_READ(&protsel->refcnt); + + if (refcnt > 0) + return true; + + return false; +} + +#ifdef CONFIG_PROTSEL_PORT +void rtw_enter_protsel_port(_adapter *padapter, u8 port_sel) +{ + u8 val8; + + rtw_enter_protsel(&padapter->dvobj->protsel_port, port_sel); + + val8 = rtw_read8(padapter, REG_PORT_CTRL_SEL); + val8 &= ~BIT_MASK_PORT_CTRL_SEL; + val8 |= BIT_PORT_CTRL_SEL(port_sel); + rtw_write8(padapter, REG_PORT_CTRL_SEL, val8); +} + +bool rtw_assert_protsel_port(_adapter *padapter, u32 addr, u8 len) +{ + if (!padapter->bup) /* don't assert before IF up */ + return true; + + return rtw_assert_protsel(&padapter->dvobj->protsel_port); +} + +void rtw_leave_protsel_port(_adapter *padapter) +{ + rtw_leave_protsel(&padapter->dvobj->protsel_port); +} +#endif + +#ifdef CONFIG_PROTSEL_ATIMDTIM +void rtw_enter_protsel_atimdtim(_adapter *padapter, u8 port_sel) +{ + /* 0~15 is for port 0 MBSSID setting + * 16 is for port 1 setting + * 17 is for port 2 setting + * 18 is for port 3 setting + * 19 is for port 4 setting + */ + u8 val8; + + if (port_sel >= 1 && port_sel <= 4) + port_sel += 15; + + rtw_enter_protsel(&padapter->dvobj->protsel_atimdtim, port_sel); + + val8 = rtw_read8(padapter, REG_ATIM_DTIM_CTRL_SEL); + val8 &= ~BIT_MASK_ATIM_DTIM_SEL; + val8 |= BIT_ATIM_DTIM_SEL(port_sel); + rtw_write8(padapter, REG_ATIM_DTIM_CTRL_SEL, val8); +} + +bool rtw_assert_protsel_atimdtim(_adapter *padapter, u32 addr, u8 len) +{ + return rtw_assert_protsel(&padapter->dvobj->protsel_atimdtim); +} + +void rtw_leave_protsel_atimdtim(_adapter *padapter) +{ + rtw_leave_protsel(&padapter->dvobj->protsel_atimdtim); +} +#endif + +#ifdef CONFIG_PROTSEL_MACSLEEP +void rtw_enter_protsel_macsleep(_adapter *padapter, u8 sel) +{ + u32 val32; + + rtw_enter_protsel(&padapter->dvobj->protsel_macsleep, sel); + + val32 = rtw_read32(padapter, REG_MACID_SLEEP_CTRL); + val32 &= ~BIT_MASK_MACID_SLEEP_SEL; + val32 |= BIT_MACID_SLEEP_SEL(sel); + rtw_write32(padapter, REG_MACID_SLEEP_CTRL, val32); +} + +bool rtw_assert_protsel_macsleep(_adapter *padapter, u32 addr, u8 len) +{ + return rtw_assert_protsel(&padapter->dvobj->protsel_macsleep); +} + +void rtw_leave_protsel_macsleep(_adapter *padapter) +{ + rtw_leave_protsel(&padapter->dvobj->protsel_macsleep); +} +#endif + +void rtw_hal_bcn_early_rpt_c2h_handler(_adapter *padapter) +{ + + if(0) + RTW_INFO("Recv Bcn Early report!!\n"); + +#ifdef CONFIG_TDLS +#ifdef CONFIG_TDLS_CH_SW + if (ATOMIC_READ(&padapter->tdlsinfo.chsw_info.chsw_on) == _TRUE) + rtw_tdls_ch_sw_back_to_base_chnl(padapter); +#endif +#endif +} diff --git a/hal/hal_com_c2h.h b/hal/hal_com_c2h.h new file mode 100644 index 0000000..b9aa9ff --- /dev/null +++ b/hal/hal_com_c2h.h @@ -0,0 +1,142 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#ifndef __COMMON_C2H_H__ +#define __COMMON_C2H_H__ + +#define C2H_TYPE_REG 0 +#define C2H_TYPE_PKT 1 + +/* +* C2H event format: +* Fields TRIGGER PAYLOAD SEQ PLEN ID +* BITS [127:120] [119:16] [15:8] [7:4] [3:0] +*/ +#define C2H_ID(_c2h) LE_BITS_TO_1BYTE(((u8*)(_c2h)), 0, 4) +#define C2H_PLEN(_c2h) LE_BITS_TO_1BYTE(((u8*)(_c2h)), 4, 4) +#define C2H_SEQ(_c2h) LE_BITS_TO_1BYTE(((u8*)(_c2h)) + 1, 0, 8) +#define C2H_PAYLOAD(_c2h) (((u8*)(_c2h)) + 2) + +#define SET_C2H_ID(_c2h, _val) SET_BITS_TO_LE_1BYTE(((u8*)(_c2h)), 0, 4, _val) +#define SET_C2H_PLEN(_c2h, _val) SET_BITS_TO_LE_1BYTE(((u8*)(_c2h)), 4, 4, _val) +#define SET_C2H_SEQ(_c2h, _val) SET_BITS_TO_LE_1BYTE(((u8*)(_c2h)) + 1 , 0, 8, _val) + +/* +* C2H event format: +* Fields TRIGGER PLEN PAYLOAD SEQ ID +* BITS [127:120] [119:112] [111:16] [15:8] [7:0] +*/ +#define C2H_ID_88XX(_c2h) LE_BITS_TO_1BYTE(((u8*)(_c2h)), 0, 8) +#define C2H_SEQ_88XX(_c2h) LE_BITS_TO_1BYTE(((u8*)(_c2h)) + 1, 0, 8) +#define C2H_PAYLOAD_88XX(_c2h) (((u8*)(_c2h)) + 2) +#define C2H_PLEN_88XX(_c2h) LE_BITS_TO_1BYTE(((u8*)(_c2h)) + 14, 0, 8) +#define C2H_TRIGGER_88XX(_c2h) LE_BITS_TO_1BYTE(((u8*)(_c2h)) + 15, 0, 8) + +#define SET_C2H_ID_88XX(_c2h, _val) SET_BITS_TO_LE_1BYTE(((u8*)(_c2h)), 0, 8, _val) +#define SET_C2H_SEQ_88XX(_c2h, _val) SET_BITS_TO_LE_1BYTE(((u8*)(_c2h)) + 1, 0, 8, _val) +#define SET_C2H_PLEN_88XX(_c2h, _val) SET_BITS_TO_LE_1BYTE(((u8*)(_c2h)) + 14, 0, 8, _val) + +typedef enum _C2H_EVT { + C2H_DBG = 0x00, + C2H_LB = 0x01, + C2H_TXBF = 0x02, + C2H_CCX_TX_RPT = 0x03, + C2H_AP_REQ_TXRPT = 0x04, + C2H_FW_SCAN_COMPLETE = 0x7, + C2H_BT_INFO = 0x09, + C2H_BT_MP_INFO = 0x0B, + C2H_RA_RPT = 0x0C, + C2H_SPC_STAT = 0x0D, + C2H_RA_PARA_RPT = 0x0E, + C2H_FW_CHNL_SWITCH_COMPLETE = 0x10, + C2H_IQK_FINISH = 0x11, + C2H_MAILBOX_STATUS = 0x15, + C2H_P2P_RPORT = 0x16, + C2H_MCC = 0x17, + C2H_MAC_HIDDEN_RPT = 0x19, + C2H_MAC_HIDDEN_RPT_2 = 0x1A, + C2H_BCN_EARLY_RPT = 0x1E, + C2H_DEFEATURE_DBG = 0x22, + C2H_CUSTOMER_STR_RPT = 0x24, + C2H_CUSTOMER_STR_RPT_2 = 0x25, + C2H_WLAN_INFO = 0x27, +#ifdef RTW_PER_CMD_SUPPORT_FW + C2H_PER_RATE_RPT = 0x2c, +#endif + C2H_LPS_STATUS_RPT = 0x32, + C2H_SET_TXPWR_FINISH = 0x70, + C2H_DEFEATURE_RSVD = 0xFD, + C2H_EXTEND = 0xff, +} C2H_EVT; + +typedef enum _EXTEND_C2H_EVT { + EXTEND_C2H_DBG_PRINT = 0 +} EXTEND_C2H_EVT; + +#define C2H_REG_LEN 16 + +/* C2H_IQK_FINISH, 0x11 */ +#define IQK_OFFLOAD_LEN 1 +void c2h_iqk_offload(_adapter *adapter, u8 *data, u8 len); +int c2h_iqk_offload_wait(_adapter *adapter, u32 timeout_ms); +#define rtl8812_iqk_wait c2h_iqk_offload_wait /* TODO: remove this after phydm call c2h_iqk_offload_wait instead */ + +#ifdef CONFIG_RTW_MAC_HIDDEN_RPT +/* C2H_MAC_HIDDEN_RPT, 0x19 */ +#define MAC_HIDDEN_RPT_LEN 8 +int c2h_mac_hidden_rpt_hdl(_adapter *adapter, u8 *data, u8 len); + +/* C2H_MAC_HIDDEN_RPT_2, 0x1A */ +#define MAC_HIDDEN_RPT_2_LEN 5 +int c2h_mac_hidden_rpt_2_hdl(_adapter *adapter, u8 *data, u8 len); +int hal_read_mac_hidden_rpt(_adapter *adapter); +#else +#define hal_read_mac_hidden_rpt(adapter) _SUCCESS +#endif /* CONFIG_RTW_MAC_HIDDEN_RPT */ + +/* C2H_DEFEATURE_DBG, 0x22 */ +#define DEFEATURE_DBG_LEN 1 +int c2h_defeature_dbg_hdl(_adapter *adapter, u8 *data, u8 len); + +#ifdef CONFIG_RTW_CUSTOMER_STR +/* C2H_CUSTOMER_STR_RPT, 0x24 */ +#define CUSTOMER_STR_RPT_LEN 8 +int c2h_customer_str_rpt_hdl(_adapter *adapter, u8 *data, u8 len); + +/* C2H_CUSTOMER_STR_RPT_2, 0x25 */ +#define CUSTOMER_STR_RPT_2_LEN 8 +int c2h_customer_str_rpt_2_hdl(_adapter *adapter, u8 *data, u8 len); +#endif /* CONFIG_RTW_CUSTOMER_STR */ + +#ifdef RTW_PER_CMD_SUPPORT_FW +/* C2H_PER_RATE_RPT, 0x2c */ +int c2h_per_rate_rpt_hdl(_adapter *adapter, u8 *data, u8 len); +#endif + +#ifdef CONFIG_LPS_ACK +/* C2H_LPS_STATUS_RPT, 0x32 */ +#define LPS_STATUS_RPT_LEN 2 +int c2h_lps_status_rpt(PADAPTER adapter, u8 *data, u8 len); +#endif /* CONFIG_LPS_ACK */ + +#ifdef CONFIG_FW_OFFLOAD_SET_TXPWR_IDX +/* C2H_SET_TXPWR_FINISH, 0x70 */ +#define SET_TXPWR_FINISH_LEN 1 +void c2h_txpwr_idx_offload_done(_adapter *adapter, u8 *data, u8 len); +int c2h_txpwr_idx_offload_wait(_adapter *adapter); +#endif + +void rtw_hal_bcn_early_rpt_c2h_handler(_adapter *adapter); + +#endif /* __COMMON_C2H_H__ */ diff --git a/hal/hal_com_phycfg.c b/hal/hal_com_phycfg.c new file mode 100644 index 0000000..38c07c7 --- /dev/null +++ b/hal/hal_com_phycfg.c @@ -0,0 +1,6279 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#define _HAL_COM_PHYCFG_C_ + +#include +#include + +#define PG_TXPWR_1PATH_BYTE_NUM_2G 18 +#define PG_TXPWR_BASE_BYTE_NUM_2G 11 + +#define PG_TXPWR_1PATH_BYTE_NUM_5G 24 +#define PG_TXPWR_BASE_BYTE_NUM_5G 14 + +#define PG_TXPWR_MSB_DIFF_S4BIT(_pg_v) (((_pg_v) & 0xf0) >> 4) +#define PG_TXPWR_LSB_DIFF_S4BIT(_pg_v) ((_pg_v) & 0x0f) +#define PG_TXPWR_MSB_DIFF_TO_S8BIT(_pg_v) ((PG_TXPWR_MSB_DIFF_S4BIT(_pg_v) & BIT3) ? (PG_TXPWR_MSB_DIFF_S4BIT(_pg_v) | 0xF0) : PG_TXPWR_MSB_DIFF_S4BIT(_pg_v)) +#define PG_TXPWR_LSB_DIFF_TO_S8BIT(_pg_v) ((PG_TXPWR_LSB_DIFF_S4BIT(_pg_v) & BIT3) ? (PG_TXPWR_LSB_DIFF_S4BIT(_pg_v) | 0xF0) : PG_TXPWR_LSB_DIFF_S4BIT(_pg_v)) +#define IS_PG_TXPWR_BASE_INVALID(hal_spec, _base) ((_base) > hal_spec->txgi_max) +#define IS_PG_TXPWR_DIFF_INVALID(_diff) ((_diff) > 7 || (_diff) < -8) +#define PG_TXPWR_INVALID_BASE 255 +#define PG_TXPWR_INVALID_DIFF 8 + +#if !IS_PG_TXPWR_DIFF_INVALID(PG_TXPWR_INVALID_DIFF) +#error "PG_TXPWR_DIFF definition has problem" +#endif + +#define PG_TXPWR_SRC_PG_DATA 0 +#define PG_TXPWR_SRC_IC_DEF 1 +#define PG_TXPWR_SRC_DEF 2 +#define PG_TXPWR_SRC_NUM 3 + +const char *const _pg_txpwr_src_str[] = { + "PG_DATA", + "IC_DEF", + "DEF", + "UNKNOWN" +}; + +#define pg_txpwr_src_str(src) (((src) >= PG_TXPWR_SRC_NUM) ? _pg_txpwr_src_str[PG_TXPWR_SRC_NUM] : _pg_txpwr_src_str[(src)]) + +const char *const _txpwr_pg_mode_str[] = { + "PWR_IDX", + "TSSI_OFFSET", + "UNKNOWN", +}; + +static const u8 rate_sec_base[RATE_SECTION_NUM] = { + MGN_11M, + MGN_54M, + MGN_MCS7, + MGN_MCS15, + MGN_MCS23, + MGN_MCS31, + MGN_VHT1SS_MCS7, + MGN_VHT2SS_MCS7, + MGN_VHT3SS_MCS7, + MGN_VHT4SS_MCS7, +}; + +#ifdef CONFIG_TXPWR_PG_WITH_PWR_IDX +typedef struct _TxPowerInfo24G { + u8 IndexCCK_Base[MAX_RF_PATH][MAX_CHNL_GROUP_24G]; + u8 IndexBW40_Base[MAX_RF_PATH][MAX_CHNL_GROUP_24G]; + /* If only one tx, only BW20 and OFDM are used. */ + s8 CCK_Diff[MAX_RF_PATH][MAX_TX_COUNT]; + s8 OFDM_Diff[MAX_RF_PATH][MAX_TX_COUNT]; + s8 BW20_Diff[MAX_RF_PATH][MAX_TX_COUNT]; + s8 BW40_Diff[MAX_RF_PATH][MAX_TX_COUNT]; +} TxPowerInfo24G; + +typedef struct _TxPowerInfo5G { + u8 IndexBW40_Base[MAX_RF_PATH][MAX_CHNL_GROUP_5G]; + /* If only one tx, only BW20, OFDM, BW80 and BW160 are used. */ + s8 OFDM_Diff[MAX_RF_PATH][MAX_TX_COUNT]; + s8 BW20_Diff[MAX_RF_PATH][MAX_TX_COUNT]; + s8 BW40_Diff[MAX_RF_PATH][MAX_TX_COUNT]; + s8 BW80_Diff[MAX_RF_PATH][MAX_TX_COUNT]; + s8 BW160_Diff[MAX_RF_PATH][MAX_TX_COUNT]; +} TxPowerInfo5G; + +#ifndef DBG_PG_TXPWR_READ +#define DBG_PG_TXPWR_READ 0 +#endif + +#if DBG_PG_TXPWR_READ +static void dump_pg_txpwr_info_2g(void *sel, TxPowerInfo24G *txpwr_info, u8 rfpath_num, u8 max_tx_cnt) +{ + int path, group, tx_idx; + + RTW_PRINT_SEL(sel, "2.4G\n"); + RTW_PRINT_SEL(sel, "CCK-1T base:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (group = 0; group < MAX_CHNL_GROUP_24G; group++) + _RTW_PRINT_SEL(sel, "G%02d ", group); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (group = 0; group < MAX_CHNL_GROUP_24G; group++) + _RTW_PRINT_SEL(sel, "%3u ", txpwr_info->IndexCCK_Base[path][group]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "CCK diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) + _RTW_PRINT_SEL(sel, "%dT ", path + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", txpwr_info->CCK_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "BW40-1S base:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (group = 0; group < MAX_CHNL_GROUP_24G - 1; group++) + _RTW_PRINT_SEL(sel, "G%02d ", group); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (group = 0; group < MAX_CHNL_GROUP_24G - 1; group++) + _RTW_PRINT_SEL(sel, "%3u ", txpwr_info->IndexBW40_Base[path][group]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "OFDM diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) + _RTW_PRINT_SEL(sel, "%dT ", path + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", txpwr_info->OFDM_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "BW20 diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) + _RTW_PRINT_SEL(sel, "%dS ", path + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", txpwr_info->BW20_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "BW40 diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) + _RTW_PRINT_SEL(sel, "%dS ", path + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", txpwr_info->BW40_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); +} + +static void dump_pg_txpwr_info_5g(void *sel, TxPowerInfo5G *txpwr_info, u8 rfpath_num, u8 max_tx_cnt) +{ + int path, group, tx_idx; + + RTW_PRINT_SEL(sel, "5G\n"); + RTW_PRINT_SEL(sel, "BW40-1S base:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (group = 0; group < MAX_CHNL_GROUP_5G; group++) + _RTW_PRINT_SEL(sel, "G%02d ", group); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (group = 0; group < MAX_CHNL_GROUP_5G; group++) + _RTW_PRINT_SEL(sel, "%3u ", txpwr_info->IndexBW40_Base[path][group]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "OFDM diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) + _RTW_PRINT_SEL(sel, "%dT ", path + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", txpwr_info->OFDM_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "BW20 diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) + _RTW_PRINT_SEL(sel, "%dS ", path + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", txpwr_info->BW20_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "BW40 diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) + _RTW_PRINT_SEL(sel, "%dS ", path + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", txpwr_info->BW40_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "BW80 diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) + _RTW_PRINT_SEL(sel, "%dS ", path + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", txpwr_info->BW80_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "BW160 diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) + _RTW_PRINT_SEL(sel, "%dS ", path + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", txpwr_info->BW160_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); +} +#endif /* DBG_PG_TXPWR_READ */ + +const struct map_t pg_txpwr_def_info = + MAP_ENT(0xB8, 1, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 168, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x24, 0xEE, 0xEE, 0xEE, 0xEE, + 0xEE, 0xEE, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, + 0x04, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x24, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0x2A, 0x2A, 0x2A, 0x2A, + 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x04, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, + 0xEE, 0xEE, 0xEE, 0xEE, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x24, + 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, + 0x2A, 0x2A, 0x2A, 0x2A, 0x04, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0x2D, 0x2D, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x24, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, + 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x04, 0xEE, + 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE) + ); + +#ifdef CONFIG_RTL8188E +static const struct map_t rtl8188e_pg_txpwr_def_info = + MAP_ENT(0xB8, 1, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 12, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x24) + ); +#endif + +#ifdef CONFIG_RTL8188F +static const struct map_t rtl8188f_pg_txpwr_def_info = + MAP_ENT(0xB8, 1, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 12, + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x27, 0x27, 0x27, 0x27, 0x27, 0x24) + ); +#endif + +#ifdef CONFIG_RTL8188GTV +static const struct map_t rtl8188gtv_pg_txpwr_def_info = + MAP_ENT(0xB8, 1, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 12, + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x27, 0x27, 0x27, 0x27, 0x27, 0x24) + ); +#endif + +#ifdef CONFIG_RTL8723B +static const struct map_t rtl8723b_pg_txpwr_def_info = + MAP_ENT(0xB8, 2, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 12, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0xE0) + , MAPSEG_ARRAY_ENT(0x3A, 12, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0xE0) + ); +#endif + +#ifdef CONFIG_RTL8703B +static const struct map_t rtl8703b_pg_txpwr_def_info = + MAP_ENT(0xB8, 1, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 12, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x02) + ); +#endif + +#ifdef CONFIG_RTL8723D +static const struct map_t rtl8723d_pg_txpwr_def_info = + MAP_ENT(0xB8, 2, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 12, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x02) + , MAPSEG_ARRAY_ENT(0x3A, 12, + 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x21, 0x21, 0x21, 0x21, 0x21, 0x02) + ); +#endif + +#ifdef CONFIG_RTL8192E +static const struct map_t rtl8192e_pg_txpwr_def_info = + MAP_ENT(0xB8, 2, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 14, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x24, 0xEE, 0xEE) + , MAPSEG_ARRAY_ENT(0x3A, 14, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x24, 0xEE, 0xEE) + ); +#endif + +#ifdef CONFIG_RTL8821A +static const struct map_t rtl8821a_pg_txpwr_def_info = + MAP_ENT(0xB8, 1, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 39, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x24, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, + 0x04, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00) + ); +#endif + +#ifdef CONFIG_RTL8821C +static const struct map_t rtl8821c_pg_txpwr_def_info = + MAP_ENT(0xB8, 1, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 54, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, + 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEC, 0xFF, 0xFF, 0xFF, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x02) + ); +#endif + +#ifdef CONFIG_RTL8710B +static const struct map_t rtl8710b_pg_txpwr_def_info = + MAP_ENT(0xC8, 1, 0xFF + , MAPSEG_ARRAY_ENT(0x20, 12, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x20) + ); +#endif + +#ifdef CONFIG_RTL8812A +static const struct map_t rtl8812a_pg_txpwr_def_info = + MAP_ENT(0xB8, 1, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 82, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x02, 0xEE, 0xEE, 0xFF, 0xFF, + 0xFF, 0xFF, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, + 0x02, 0xEE, 0xFF, 0xFF, 0xEE, 0xFF, 0x00, 0xEE, 0xFF, 0xFF, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x02, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF, 0x2A, 0x2A, 0x2A, 0x2A, + 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x02, 0xEE, 0xFF, 0xFF, 0xEE, 0xFF, + 0x00, 0xEE) + ); +#endif + +#ifdef CONFIG_RTL8822B +static const struct map_t rtl8822b_pg_txpwr_def_info = + MAP_ENT(0xB8, 1, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 82, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x02, 0xEE, 0xEE, 0xFF, 0xFF, + 0xFF, 0xFF, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, + 0x02, 0xEE, 0xFF, 0xFF, 0xEE, 0xFF, 0xEC, 0xEC, 0xFF, 0xFF, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x02, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF, 0xFF, 0x2A, 0x2A, 0x2A, 0x2A, + 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x02, 0xEE, 0xFF, 0xFF, 0xEE, 0xFF, + 0xEC, 0xEC) + ); +#endif + +#ifdef CONFIG_RTL8822C +static const struct map_t rtl8822c_pg_txpwr_def_info = + MAP_ENT(0xB8, 1, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 82, + 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x02, 0x00, 0x00, 0xFF, 0xFF, + 0xFF, 0xFF, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, + 0x02, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, + 0x33, 0x33, 0x33, 0x33, 0x33, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x33, 0x33, 0x33, 0x33, + 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x02, 0x00, 0xFF, 0xFF, 0x00, 0xFF, + 0x00, 0x00) + ); +#endif + +#ifdef CONFIG_RTL8723F +static const struct map_t rtl8723f_pg_txpwr_def_info = + MAP_ENT(0xB8, 1, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 82, + 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x02, 0x00, 0x00, 0xFF, 0xFF, + 0xFF, 0xFF, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, + 0x02, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, + 0x33, 0x33, 0x33, 0x33, 0x33, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x33, 0x33, 0x33, 0x33, + 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x02, 0x00, 0xFF, 0xFF, 0x00, 0xFF, + 0x00, 0x00) + ); +#endif + +#ifdef CONFIG_RTL8814A +static const struct map_t rtl8814a_pg_txpwr_def_info = + MAP_ENT(0xB8, 1, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 168, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x02, 0xEE, 0xEE, 0xEE, 0xEE, + 0xEE, 0xEE, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, + 0x02, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0x00, 0xEE, 0xEE, 0xEE, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x02, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0x2A, 0x2A, 0x2A, 0x2A, + 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x02, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, + 0x00, 0xEE, 0xEE, 0xEE, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x02, + 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, + 0x2A, 0x2A, 0x2A, 0x2A, 0x02, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0x00, 0xEE, 0xEE, 0xEE, 0x2D, 0x2D, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x02, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, 0xEE, + 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x2A, 0x02, 0xEE, + 0xEE, 0xEE, 0xEE, 0xEE, 0x00, 0xEE, 0xEE, 0xEE) + ); +#endif + +#ifdef CONFIG_RTL8192F/*use 8192F default,no document*/ +static const struct map_t rtl8192f_pg_txpwr_def_info = + MAP_ENT(0xB8, 2, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 14, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x24, 0xEE, 0xEE) + , MAPSEG_ARRAY_ENT(0x3A, 14, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x24, 0xEE, 0xEE) + ); +#endif + +#ifdef CONFIG_RTL8814B +static const struct map_t rtl8814b_pg_txpwr_def_info = + MAP_ENT(0xB8, 1, 0xFF + , MAPSEG_ARRAY_ENT(0x10, 168, + 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, + 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEC, 0xFF, 0xFF, 0xFF, 0x28, 0x28, 0x28, 0x28, 0x28, 0x28, + 0x28, 0x28, 0x28, 0x28, 0x28, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF) + ); +#endif + +const struct map_t *hal_pg_txpwr_def_info(_adapter *adapter) +{ + u8 interface_type = 0; + const struct map_t *map = NULL; + + interface_type = rtw_get_intf_type(adapter); + + switch (rtw_get_chip_type(adapter)) { +#ifdef CONFIG_RTL8723B + case RTL8723B: + map = &rtl8723b_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8703B + case RTL8703B: + map = &rtl8703b_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8723D + case RTL8723D: + map = &rtl8723d_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8188E + case RTL8188E: + map = &rtl8188e_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8188F + case RTL8188F: + map = &rtl8188f_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8188GTV + case RTL8188GTV: + map = &rtl8188gtv_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8812A + case RTL8812: + map = &rtl8812a_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8821A + case RTL8821: + map = &rtl8821a_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8192E + case RTL8192E: + map = &rtl8192e_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8814A + case RTL8814A: + map = &rtl8814a_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8822B + case RTL8822B: + map = &rtl8822b_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8821C + case RTL8821C: + map = &rtl8821c_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8710B + case RTL8710B: + map = &rtl8710b_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8192F + case RTL8192F: + map = &rtl8192f_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8822C + case RTL8822C: + map = &rtl8822c_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8814B + case RTL8814B: + map = &rtl8814b_pg_txpwr_def_info; + break; +#endif +#ifdef CONFIG_RTL8723F + case RTL8723F: + map = &rtl8723f_pg_txpwr_def_info; + break; +#endif + } + + if (map == NULL) { + RTW_ERR("%s: unknown chip_type:%u\n" + , __func__, rtw_get_chip_type(adapter)); + rtw_warn_on(1); + } + + return map; +} + +static u8 hal_chk_pg_txpwr_info_2g(_adapter *adapter, TxPowerInfo24G *pwr_info) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u8 path, group, tx_idx; + + if (pwr_info == NULL || !hal_chk_band_cap(adapter, BAND_CAP_2G)) + return _SUCCESS; + + for (path = 0; path < MAX_RF_PATH; path++) { + if (!HAL_SPEC_CHK_RF_PATH_2G(hal_spec, path)) + continue; + for (group = 0; group < MAX_CHNL_GROUP_24G; group++) { + if (IS_PG_TXPWR_BASE_INVALID(hal_spec, pwr_info->IndexCCK_Base[path][group]) + || IS_PG_TXPWR_BASE_INVALID(hal_spec, pwr_info->IndexBW40_Base[path][group])) + return _FAIL; + } + for (tx_idx = 0; tx_idx < MAX_TX_COUNT; tx_idx++) { + if (tx_idx + 1 > hal_data->max_tx_cnt) + continue; + if (IS_PG_TXPWR_DIFF_INVALID(pwr_info->CCK_Diff[path][tx_idx]) + || IS_PG_TXPWR_DIFF_INVALID(pwr_info->OFDM_Diff[path][tx_idx]) + || IS_PG_TXPWR_DIFF_INVALID(pwr_info->BW20_Diff[path][tx_idx]) + || IS_PG_TXPWR_DIFF_INVALID(pwr_info->BW40_Diff[path][tx_idx])) + return _FAIL; + } + } + + return _SUCCESS; +} + +static u8 hal_chk_pg_txpwr_info_5g(_adapter *adapter, TxPowerInfo5G *pwr_info) +{ +#if CONFIG_IEEE80211_BAND_5GHZ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u8 path, group, tx_idx; + + if (pwr_info == NULL || !hal_chk_band_cap(adapter, BAND_CAP_5G)) + return _SUCCESS; + + for (path = 0; path < MAX_RF_PATH; path++) { + if (!HAL_SPEC_CHK_RF_PATH_5G(hal_spec, path)) + continue; + for (group = 0; group < MAX_CHNL_GROUP_5G; group++) + if (IS_PG_TXPWR_BASE_INVALID(hal_spec, pwr_info->IndexBW40_Base[path][group])) + return _FAIL; + for (tx_idx = 0; tx_idx < MAX_TX_COUNT; tx_idx++) { + if (tx_idx + 1 > hal_data->max_tx_cnt) + continue; + if (IS_PG_TXPWR_DIFF_INVALID(pwr_info->OFDM_Diff[path][tx_idx]) + || IS_PG_TXPWR_DIFF_INVALID(pwr_info->BW20_Diff[path][tx_idx]) + || IS_PG_TXPWR_DIFF_INVALID(pwr_info->BW40_Diff[path][tx_idx]) + || IS_PG_TXPWR_DIFF_INVALID(pwr_info->BW80_Diff[path][tx_idx]) + || IS_PG_TXPWR_DIFF_INVALID(pwr_info->BW160_Diff[path][tx_idx])) + return _FAIL; + } + } +#endif /* CONFIG_IEEE80211_BAND_5GHZ */ + return _SUCCESS; +} + +static inline void hal_init_pg_txpwr_info_2g(_adapter *adapter, TxPowerInfo24G *pwr_info) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u8 path, group, tx_idx; + + if (pwr_info == NULL) + return; + + _rtw_memset(pwr_info, 0, sizeof(TxPowerInfo24G)); + + /* init with invalid value */ + for (path = 0; path < MAX_RF_PATH; path++) { + for (group = 0; group < MAX_CHNL_GROUP_24G; group++) { + pwr_info->IndexCCK_Base[path][group] = PG_TXPWR_INVALID_BASE; + pwr_info->IndexBW40_Base[path][group] = PG_TXPWR_INVALID_BASE; + } + for (tx_idx = 0; tx_idx < MAX_TX_COUNT; tx_idx++) { + pwr_info->CCK_Diff[path][tx_idx] = PG_TXPWR_INVALID_DIFF; + pwr_info->OFDM_Diff[path][tx_idx] = PG_TXPWR_INVALID_DIFF; + pwr_info->BW20_Diff[path][tx_idx] = PG_TXPWR_INVALID_DIFF; + pwr_info->BW40_Diff[path][tx_idx] = PG_TXPWR_INVALID_DIFF; + } + } + + /* init for dummy base and diff */ + for (path = 0; path < MAX_RF_PATH; path++) { + if (!HAL_SPEC_CHK_RF_PATH_2G(hal_spec, path)) + break; + /* 2.4G BW40 base has 1 less group than CCK base*/ + pwr_info->IndexBW40_Base[path][MAX_CHNL_GROUP_24G - 1] = 0; + + /* dummy diff */ + pwr_info->CCK_Diff[path][0] = 0; /* 2.4G CCK-1TX */ + pwr_info->BW40_Diff[path][0] = 0; /* 2.4G BW40-1S */ + } +} + +static inline void hal_init_pg_txpwr_info_5g(_adapter *adapter, TxPowerInfo5G *pwr_info) +{ +#if CONFIG_IEEE80211_BAND_5GHZ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u8 path, group, tx_idx; + + if (pwr_info == NULL) + return; + + _rtw_memset(pwr_info, 0, sizeof(TxPowerInfo5G)); + + /* init with invalid value */ + for (path = 0; path < MAX_RF_PATH; path++) { + for (group = 0; group < MAX_CHNL_GROUP_5G; group++) + pwr_info->IndexBW40_Base[path][group] = PG_TXPWR_INVALID_BASE; + for (tx_idx = 0; tx_idx < MAX_TX_COUNT; tx_idx++) { + pwr_info->OFDM_Diff[path][tx_idx] = PG_TXPWR_INVALID_DIFF; + pwr_info->BW20_Diff[path][tx_idx] = PG_TXPWR_INVALID_DIFF; + pwr_info->BW40_Diff[path][tx_idx] = PG_TXPWR_INVALID_DIFF; + pwr_info->BW80_Diff[path][tx_idx] = PG_TXPWR_INVALID_DIFF; + pwr_info->BW160_Diff[path][tx_idx] = PG_TXPWR_INVALID_DIFF; + } + } + + for (path = 0; path < MAX_RF_PATH; path++) { + if (!HAL_SPEC_CHK_RF_PATH_5G(hal_spec, path)) + break; + /* dummy diff */ + pwr_info->BW40_Diff[path][0] = 0; /* 5G BW40-1S */ + } +#endif /* CONFIG_IEEE80211_BAND_5GHZ */ +} + +#if DBG_PG_TXPWR_READ +#define LOAD_PG_TXPWR_WARN_COND(_txpwr_src) 1 +#else +#define LOAD_PG_TXPWR_WARN_COND(_txpwr_src) (_txpwr_src > PG_TXPWR_SRC_PG_DATA) +#endif + +u16 hal_load_pg_txpwr_info_path_2g( + _adapter *adapter, + TxPowerInfo24G *pwr_info, + u32 path, + u8 txpwr_src, + const struct map_t *txpwr_map, + u16 pg_offset) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u16 offset = pg_offset; + u8 group, tx_idx; + u8 val; + u8 tmp_base; + s8 tmp_diff; + + if (pwr_info == NULL || !hal_chk_band_cap(adapter, BAND_CAP_2G)) { + offset += PG_TXPWR_1PATH_BYTE_NUM_2G; + goto exit; + } + + if (DBG_PG_TXPWR_READ) + RTW_INFO("%s [%c] offset:0x%03x\n", __func__, rf_path_char(path), offset); + + for (group = 0; group < MAX_CHNL_GROUP_24G; group++) { + if (HAL_SPEC_CHK_RF_PATH_2G(hal_spec, path)) { + tmp_base = map_read8(txpwr_map, offset); + if (!IS_PG_TXPWR_BASE_INVALID(hal_spec, tmp_base) + && IS_PG_TXPWR_BASE_INVALID(hal_spec, pwr_info->IndexCCK_Base[path][group]) + ) { + pwr_info->IndexCCK_Base[path][group] = tmp_base; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 2G G%02d CCK-1T base:%u from %s\n", rf_path_char(path), group, tmp_base, pg_txpwr_src_str(txpwr_src)); + } + } + offset++; + } + + for (group = 0; group < MAX_CHNL_GROUP_24G - 1; group++) { + if (HAL_SPEC_CHK_RF_PATH_2G(hal_spec, path)) { + tmp_base = map_read8(txpwr_map, offset); + if (!IS_PG_TXPWR_BASE_INVALID(hal_spec, tmp_base) + && IS_PG_TXPWR_BASE_INVALID(hal_spec, pwr_info->IndexBW40_Base[path][group]) + ) { + pwr_info->IndexBW40_Base[path][group] = tmp_base; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 2G G%02d BW40-1S base:%u from %s\n", rf_path_char(path), group, tmp_base, pg_txpwr_src_str(txpwr_src)); + } + } + offset++; + } + + for (tx_idx = 0; tx_idx < MAX_TX_COUNT; tx_idx++) { + if (tx_idx == 0) { + if (HAL_SPEC_CHK_RF_PATH_2G(hal_spec, path)) { + val = map_read8(txpwr_map, offset); + tmp_diff = PG_TXPWR_MSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->BW20_Diff[path][tx_idx]) + ) { + pwr_info->BW20_Diff[path][tx_idx] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 2G BW20-%dS diff:%d from %s\n", rf_path_char(path), tx_idx + 1, tmp_diff, pg_txpwr_src_str(txpwr_src)); + } + tmp_diff = PG_TXPWR_LSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->OFDM_Diff[path][tx_idx]) + ) { + pwr_info->OFDM_Diff[path][tx_idx] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 2G OFDM-%dT diff:%d from %s\n", rf_path_char(path), tx_idx + 1, tmp_diff, pg_txpwr_src_str(txpwr_src)); + } + } + offset++; + } else { + if (HAL_SPEC_CHK_RF_PATH_2G(hal_spec, path) && tx_idx + 1 <= hal_data->max_tx_cnt) { + val = map_read8(txpwr_map, offset); + tmp_diff = PG_TXPWR_MSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->BW40_Diff[path][tx_idx]) + ) { + pwr_info->BW40_Diff[path][tx_idx] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 2G BW40-%dS diff:%d from %s\n", rf_path_char(path), tx_idx + 1, tmp_diff, pg_txpwr_src_str(txpwr_src)); + + } + tmp_diff = PG_TXPWR_LSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->BW20_Diff[path][tx_idx]) + ) { + pwr_info->BW20_Diff[path][tx_idx] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 2G BW20-%dS diff:%d from %s\n", rf_path_char(path), tx_idx + 1, tmp_diff, pg_txpwr_src_str(txpwr_src)); + } + } + offset++; + + if (HAL_SPEC_CHK_RF_PATH_2G(hal_spec, path) && tx_idx + 1 <= hal_data->max_tx_cnt) { + val = map_read8(txpwr_map, offset); + tmp_diff = PG_TXPWR_MSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->OFDM_Diff[path][tx_idx]) + ) { + pwr_info->OFDM_Diff[path][tx_idx] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 2G OFDM-%dT diff:%d from %s\n", rf_path_char(path), tx_idx + 1, tmp_diff, pg_txpwr_src_str(txpwr_src)); + } + tmp_diff = PG_TXPWR_LSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->CCK_Diff[path][tx_idx]) + ) { + pwr_info->CCK_Diff[path][tx_idx] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 2G CCK-%dT diff:%d from %s\n", rf_path_char(path), tx_idx + 1, tmp_diff, pg_txpwr_src_str(txpwr_src)); + } + } + offset++; + } + } + + if (offset != pg_offset + PG_TXPWR_1PATH_BYTE_NUM_2G) { + RTW_ERR("%s parse %d bytes != %d\n", __func__, offset - pg_offset, PG_TXPWR_1PATH_BYTE_NUM_2G); + rtw_warn_on(1); + } + +exit: + return offset; +} + +u16 hal_load_pg_txpwr_info_path_5g( + _adapter *adapter, + TxPowerInfo5G *pwr_info, + u32 path, + u8 txpwr_src, + const struct map_t *txpwr_map, + u16 pg_offset) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u16 offset = pg_offset; + u8 group, tx_idx; + u8 val; + u8 tmp_base; + s8 tmp_diff; + +#if CONFIG_IEEE80211_BAND_5GHZ + if (pwr_info == NULL || !hal_chk_band_cap(adapter, BAND_CAP_5G)) +#endif + { + offset += PG_TXPWR_1PATH_BYTE_NUM_5G; + goto exit; + } + +#if CONFIG_IEEE80211_BAND_5GHZ + if (DBG_PG_TXPWR_READ) + RTW_INFO("%s[%c] eaddr:0x%03x\n", __func__, rf_path_char(path), offset); + + for (group = 0; group < MAX_CHNL_GROUP_5G; group++) { + if (HAL_SPEC_CHK_RF_PATH_5G(hal_spec, path)) { + tmp_base = map_read8(txpwr_map, offset); + if (!IS_PG_TXPWR_BASE_INVALID(hal_spec, tmp_base) + && IS_PG_TXPWR_BASE_INVALID(hal_spec, pwr_info->IndexBW40_Base[path][group]) + ) { + pwr_info->IndexBW40_Base[path][group] = tmp_base; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 5G G%02d BW40-1S base:%u from %s\n", rf_path_char(path), group, tmp_base, pg_txpwr_src_str(txpwr_src)); + } + } + offset++; + } + + for (tx_idx = 0; tx_idx < MAX_TX_COUNT; tx_idx++) { + if (tx_idx == 0) { + if (HAL_SPEC_CHK_RF_PATH_5G(hal_spec, path)) { + val = map_read8(txpwr_map, offset); + tmp_diff = PG_TXPWR_MSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->BW20_Diff[path][tx_idx]) + ) { + pwr_info->BW20_Diff[path][tx_idx] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 5G BW20-%dS diff:%d from %s\n", rf_path_char(path), tx_idx + 1, tmp_diff, pg_txpwr_src_str(txpwr_src)); + } + tmp_diff = PG_TXPWR_LSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->OFDM_Diff[path][tx_idx]) + ) { + pwr_info->OFDM_Diff[path][tx_idx] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 5G OFDM-%dT diff:%d from %s\n", rf_path_char(path), tx_idx + 1, tmp_diff, pg_txpwr_src_str(txpwr_src)); + } + } + offset++; + } else { + if (HAL_SPEC_CHK_RF_PATH_5G(hal_spec, path) && tx_idx + 1 <= hal_data->max_tx_cnt) { + val = map_read8(txpwr_map, offset); + tmp_diff = PG_TXPWR_MSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->BW40_Diff[path][tx_idx]) + ) { + pwr_info->BW40_Diff[path][tx_idx] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 5G BW40-%dS diff:%d from %s\n", rf_path_char(path), tx_idx + 1, tmp_diff, pg_txpwr_src_str(txpwr_src)); + } + tmp_diff = PG_TXPWR_LSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->BW20_Diff[path][tx_idx]) + ) { + pwr_info->BW20_Diff[path][tx_idx] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 5G BW20-%dS diff:%d from %s\n", rf_path_char(path), tx_idx + 1, tmp_diff, pg_txpwr_src_str(txpwr_src)); + } + } + offset++; + } + } + + /* OFDM diff 2T ~ 3T */ + if (HAL_SPEC_CHK_RF_PATH_5G(hal_spec, path) && hal_data->max_tx_cnt > 1) { + val = map_read8(txpwr_map, offset); + tmp_diff = PG_TXPWR_MSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->OFDM_Diff[path][1]) + ) { + pwr_info->OFDM_Diff[path][1] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 5G OFDM-%dT diff:%d from %s\n", rf_path_char(path), 2, tmp_diff, pg_txpwr_src_str(txpwr_src)); + } + if (hal_data->max_tx_cnt > 2) { + tmp_diff = PG_TXPWR_LSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->OFDM_Diff[path][2]) + ) { + pwr_info->OFDM_Diff[path][2] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 5G OFDM-%dT diff:%d from %s\n", rf_path_char(path), 3, tmp_diff, pg_txpwr_src_str(txpwr_src)); + } + } + } + offset++; + + /* OFDM diff 4T */ + if (HAL_SPEC_CHK_RF_PATH_5G(hal_spec, path) && hal_data->max_tx_cnt > 3) { + val = map_read8(txpwr_map, offset); + tmp_diff = PG_TXPWR_LSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->OFDM_Diff[path][3]) + ) { + pwr_info->OFDM_Diff[path][3] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 5G OFDM-%dT diff:%d from %s\n", rf_path_char(path), 4, tmp_diff, pg_txpwr_src_str(txpwr_src)); + } + } + offset++; + + for (tx_idx = 0; tx_idx < MAX_TX_COUNT; tx_idx++) { + if (HAL_SPEC_CHK_RF_PATH_5G(hal_spec, path) && tx_idx + 1 <= hal_data->max_tx_cnt) { + val = map_read8(txpwr_map, offset); + tmp_diff = PG_TXPWR_MSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->BW80_Diff[path][tx_idx]) + ) { + pwr_info->BW80_Diff[path][tx_idx] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 5G BW80-%dS diff:%d from %s\n", rf_path_char(path), tx_idx + 1, tmp_diff, pg_txpwr_src_str(txpwr_src)); + } + tmp_diff = PG_TXPWR_LSB_DIFF_TO_S8BIT(val); + if (!IS_PG_TXPWR_DIFF_INVALID(tmp_diff) + && IS_PG_TXPWR_DIFF_INVALID(pwr_info->BW160_Diff[path][tx_idx]) + ) { + pwr_info->BW160_Diff[path][tx_idx] = tmp_diff; + if (LOAD_PG_TXPWR_WARN_COND(txpwr_src)) + RTW_INFO("[%c] 5G BW160-%dS diff:%d from %s\n", rf_path_char(path), tx_idx + 1, tmp_diff, pg_txpwr_src_str(txpwr_src)); + } + } + offset++; + } + + if (offset != pg_offset + PG_TXPWR_1PATH_BYTE_NUM_5G) { + RTW_ERR("%s parse %d bytes != %d\n", __func__, offset - pg_offset, PG_TXPWR_1PATH_BYTE_NUM_5G); + rtw_warn_on(1); + } + +#endif /* CONFIG_IEEE80211_BAND_5GHZ */ + +exit: + return offset; +} + +void hal_load_pg_txpwr_info( + _adapter *adapter, + TxPowerInfo24G *pwr_info_2g, + TxPowerInfo5G *pwr_info_5g, + u8 *pg_data, + BOOLEAN AutoLoadFail +) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u8 path; + u16 pg_offset; + u8 txpwr_src = PG_TXPWR_SRC_PG_DATA; + struct map_t pg_data_map = MAP_ENT(184, 1, 0xFF, MAPSEG_PTR_ENT(0x00, 184, pg_data)); + const struct map_t *txpwr_map = NULL; + + /* init with invalid value and some dummy base and diff */ + hal_init_pg_txpwr_info_2g(adapter, pwr_info_2g); + hal_init_pg_txpwr_info_5g(adapter, pwr_info_5g); + +select_src: + pg_offset = hal_spec->pg_txpwr_saddr; + + switch (txpwr_src) { + case PG_TXPWR_SRC_PG_DATA: + txpwr_map = &pg_data_map; + break; + case PG_TXPWR_SRC_IC_DEF: + txpwr_map = hal_pg_txpwr_def_info(adapter); + break; + case PG_TXPWR_SRC_DEF: + default: + txpwr_map = &pg_txpwr_def_info; + break; + }; + + if (txpwr_map == NULL) + goto end_parse; + + for (path = 0; path < MAX_RF_PATH ; path++) { + if (!HAL_SPEC_CHK_RF_PATH_2G(hal_spec, path) && !HAL_SPEC_CHK_RF_PATH_5G(hal_spec, path)) + break; + pg_offset = hal_load_pg_txpwr_info_path_2g(adapter, pwr_info_2g, path, txpwr_src, txpwr_map, pg_offset); + pg_offset = hal_load_pg_txpwr_info_path_5g(adapter, pwr_info_5g, path, txpwr_src, txpwr_map, pg_offset); + } + + if (hal_chk_pg_txpwr_info_2g(adapter, pwr_info_2g) == _SUCCESS + && hal_chk_pg_txpwr_info_5g(adapter, pwr_info_5g) == _SUCCESS) + goto exit; + +end_parse: + txpwr_src++; + if (txpwr_src < PG_TXPWR_SRC_NUM) + goto select_src; + + if (hal_chk_pg_txpwr_info_2g(adapter, pwr_info_2g) != _SUCCESS + || hal_chk_pg_txpwr_info_5g(adapter, pwr_info_5g) != _SUCCESS) + rtw_warn_on(1); + +exit: + #if DBG_PG_TXPWR_READ + if (pwr_info_2g) + dump_pg_txpwr_info_2g(RTW_DBGDUMP, pwr_info_2g, 4, 4); + if (pwr_info_5g) + dump_pg_txpwr_info_5g(RTW_DBGDUMP, pwr_info_5g, 4, 4); + #endif + + return; +} +#endif /* CONFIG_TXPWR_PG_WITH_PWR_IDX */ + +#ifdef CONFIG_EFUSE_CONFIG_FILE + +#define EFUSE_POWER_INDEX_INVALID 0xFF + +static u8 _check_phy_efuse_tx_power_info_valid(u8 *pg_data, int chk_len, u16 pg_offset) +{ + int ff_cnt = 0; + int i; + + for (i = 0; i < chk_len; i++) { + if (*(pg_data + pg_offset + i) == 0xFF) + ff_cnt++; + } + + if (ff_cnt == 0) + return _TRUE; + else if (ff_cnt == chk_len) + return _FALSE; + else + return EFUSE_POWER_INDEX_INVALID; +} + +int check_phy_efuse_tx_power_info_valid(_adapter *adapter) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u8 *pg_data = hal_data->efuse_eeprom_data; + u16 pg_offset = hal_spec->pg_txpwr_saddr; + u8 path; + u8 valid_2g_path_bmp = 0; +#if CONFIG_IEEE80211_BAND_5GHZ + u8 valid_5g_path_bmp = 0; +#endif +#ifdef CONFIG_MP_INCLUDED + struct mp_priv *pmp_priv = &adapter->mppriv; + + + if (pmp_priv->efuse_update_file == _TRUE && (rtw_mp_mode_check(adapter))) { + RTW_INFO("%s: To use efuse_update_file !!!\n", __func__); + return _FALSE; + } +#endif + /* NOTE: TSSI offset use the same layout as TXPWR base */ + + for (path = 0; path < MAX_RF_PATH; path++) { + u8 ret = _FALSE; + + if (!HAL_SPEC_CHK_RF_PATH_2G(hal_spec, path) && !HAL_SPEC_CHK_RF_PATH_5G(hal_spec, path)) + break; + + if (HAL_SPEC_CHK_RF_PATH_2G(hal_spec, path)) { + ret = _check_phy_efuse_tx_power_info_valid(pg_data, PG_TXPWR_BASE_BYTE_NUM_2G, pg_offset); + if (ret == _TRUE) + valid_2g_path_bmp |= BIT(path); + else if (ret == EFUSE_POWER_INDEX_INVALID) + return _FALSE; + } + pg_offset += PG_TXPWR_1PATH_BYTE_NUM_2G; + + #if CONFIG_IEEE80211_BAND_5GHZ + if (HAL_SPEC_CHK_RF_PATH_5G(hal_spec, path)) { + ret = _check_phy_efuse_tx_power_info_valid(pg_data, PG_TXPWR_BASE_BYTE_NUM_5G, pg_offset); + if (ret == _TRUE) + valid_5g_path_bmp |= BIT(path); + else if (ret == EFUSE_POWER_INDEX_INVALID) + return _FALSE; + } + #endif + pg_offset += PG_TXPWR_1PATH_BYTE_NUM_5G; + } + + if ((hal_chk_band_cap(adapter, BAND_CAP_2G) && valid_2g_path_bmp) + #if CONFIG_IEEE80211_BAND_5GHZ + || (hal_chk_band_cap(adapter, BAND_CAP_5G) && valid_5g_path_bmp) + #endif + ) + return _TRUE; + + return _FALSE; +} +#endif /* CONFIG_EFUSE_CONFIG_FILE */ + +#ifdef CONFIG_TXPWR_PG_WITH_PWR_IDX +void hal_load_txpwr_info(_adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u8 max_tx_cnt = hal_data->max_tx_cnt; + u8 *pg_data = hal_data->efuse_eeprom_data; + TxPowerInfo24G *pwr_info_2g = NULL; + TxPowerInfo5G *pwr_info_5g = NULL; + u8 rfpath, ch_idx, group, tx_idx; + + if (hal_chk_band_cap(adapter, BAND_CAP_2G)) + pwr_info_2g = rtw_vmalloc(sizeof(TxPowerInfo24G)); +#if CONFIG_IEEE80211_BAND_5GHZ + if (hal_chk_band_cap(adapter, BAND_CAP_5G)) + pwr_info_5g = rtw_vmalloc(sizeof(TxPowerInfo5G)); +#endif + + /* load from pg data (or default value) */ + hal_load_pg_txpwr_info(adapter, pwr_info_2g, pwr_info_5g, pg_data, _FALSE); + + /* transform to hal_data */ + for (rfpath = 0; rfpath < MAX_RF_PATH; rfpath++) { + + if (!pwr_info_2g || !HAL_SPEC_CHK_RF_PATH_2G(hal_spec, rfpath)) + goto bypass_2g; + + /* 2.4G base */ + for (ch_idx = 0; ch_idx < CENTER_CH_2G_NUM; ch_idx++) { + u8 cck_group; + + if (rtw_get_ch_group(ch_idx + 1, &group, &cck_group) != BAND_ON_2_4G) + continue; + + hal_data->Index24G_CCK_Base[rfpath][ch_idx] = pwr_info_2g->IndexCCK_Base[rfpath][cck_group]; + hal_data->Index24G_BW40_Base[rfpath][ch_idx] = pwr_info_2g->IndexBW40_Base[rfpath][group]; + } + + /* 2.4G diff */ + for (tx_idx = 0; tx_idx < MAX_TX_COUNT; tx_idx++) { + if (tx_idx + 1 > max_tx_cnt) + break; + + hal_data->CCK_24G_Diff[rfpath][tx_idx] = pwr_info_2g->CCK_Diff[rfpath][tx_idx] * hal_spec->pg_txgi_diff_factor; + hal_data->OFDM_24G_Diff[rfpath][tx_idx] = pwr_info_2g->OFDM_Diff[rfpath][tx_idx] * hal_spec->pg_txgi_diff_factor; + hal_data->BW20_24G_Diff[rfpath][tx_idx] = pwr_info_2g->BW20_Diff[rfpath][tx_idx] * hal_spec->pg_txgi_diff_factor; + hal_data->BW40_24G_Diff[rfpath][tx_idx] = pwr_info_2g->BW40_Diff[rfpath][tx_idx] * hal_spec->pg_txgi_diff_factor; + } +bypass_2g: + ; + +#if CONFIG_IEEE80211_BAND_5GHZ + if (!pwr_info_5g || !HAL_SPEC_CHK_RF_PATH_5G(hal_spec, rfpath)) + goto bypass_5g; + + /* 5G base */ + for (ch_idx = 0; ch_idx < CENTER_CH_5G_ALL_NUM; ch_idx++) { + if (rtw_get_ch_group(center_ch_5g_all[ch_idx], &group, NULL) != BAND_ON_5G) + continue; + hal_data->Index5G_BW40_Base[rfpath][ch_idx] = pwr_info_5g->IndexBW40_Base[rfpath][group]; + } + + for (ch_idx = 0 ; ch_idx < CENTER_CH_5G_80M_NUM; ch_idx++) { + u8 upper, lower; + + if (rtw_get_ch_group(center_ch_5g_80m[ch_idx], &group, NULL) != BAND_ON_5G) + continue; + + upper = pwr_info_5g->IndexBW40_Base[rfpath][group]; + lower = pwr_info_5g->IndexBW40_Base[rfpath][group + 1]; + hal_data->Index5G_BW80_Base[rfpath][ch_idx] = (upper + lower) / 2; + } + + /* 5G diff */ + for (tx_idx = 0; tx_idx < MAX_TX_COUNT; tx_idx++) { + if (tx_idx + 1 > max_tx_cnt) + break; + + hal_data->OFDM_5G_Diff[rfpath][tx_idx] = pwr_info_5g->OFDM_Diff[rfpath][tx_idx] * hal_spec->pg_txgi_diff_factor; + hal_data->BW20_5G_Diff[rfpath][tx_idx] = pwr_info_5g->BW20_Diff[rfpath][tx_idx] * hal_spec->pg_txgi_diff_factor; + hal_data->BW40_5G_Diff[rfpath][tx_idx] = pwr_info_5g->BW40_Diff[rfpath][tx_idx] * hal_spec->pg_txgi_diff_factor; + hal_data->BW80_5G_Diff[rfpath][tx_idx] = pwr_info_5g->BW80_Diff[rfpath][tx_idx] * hal_spec->pg_txgi_diff_factor; + } +bypass_5g: + ; +#endif /* CONFIG_IEEE80211_BAND_5GHZ */ + } + + if (pwr_info_2g) + rtw_vmfree(pwr_info_2g, sizeof(TxPowerInfo24G)); + if (pwr_info_5g) + rtw_vmfree(pwr_info_5g, sizeof(TxPowerInfo5G)); +} + +void dump_hal_txpwr_info_2g(void *sel, _adapter *adapter, u8 rfpath_num, u8 max_tx_cnt) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + int path, ch_idx, tx_idx; + + RTW_PRINT_SEL(sel, "2.4G\n"); + RTW_PRINT_SEL(sel, "CCK-1T base:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (ch_idx = 0; ch_idx < CENTER_CH_2G_NUM; ch_idx++) + _RTW_PRINT_SEL(sel, "%3d ", center_ch_2g[ch_idx]); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (ch_idx = 0; ch_idx < CENTER_CH_2G_NUM; ch_idx++) + _RTW_PRINT_SEL(sel, "%3u ", hal_data->Index24G_CCK_Base[path][ch_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "CCK diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%dT ", tx_idx + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", hal_data->CCK_24G_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "BW40-1S base:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (ch_idx = 0; ch_idx < CENTER_CH_2G_NUM; ch_idx++) + _RTW_PRINT_SEL(sel, "%3d ", center_ch_2g[ch_idx]); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (ch_idx = 0; ch_idx < CENTER_CH_2G_NUM; ch_idx++) + _RTW_PRINT_SEL(sel, "%3u ", hal_data->Index24G_BW40_Base[path][ch_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "OFDM diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%dT ", tx_idx + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", hal_data->OFDM_24G_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "BW20 diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%dS ", tx_idx + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", hal_data->BW20_24G_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "BW40 diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%dS ", tx_idx + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", hal_data->BW40_24G_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); +} + +void dump_hal_txpwr_info_5g(void *sel, _adapter *adapter, u8 rfpath_num, u8 max_tx_cnt) +{ +#if CONFIG_IEEE80211_BAND_5GHZ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + int path, ch_idx, tx_idx; + u8 dump_section = 0; + u8 ch_idx_s = 0; + + RTW_PRINT_SEL(sel, "5G\n"); + RTW_PRINT_SEL(sel, "BW40-1S base:\n"); + do { + #define DUMP_5G_BW40_BASE_SECTION_NUM 3 + u8 end[DUMP_5G_BW40_BASE_SECTION_NUM] = {64, 144, 177}; + + RTW_PRINT_SEL(sel, "%4s ", ""); + for (ch_idx = ch_idx_s; ch_idx < CENTER_CH_5G_ALL_NUM; ch_idx++) { + _RTW_PRINT_SEL(sel, "%3d ", center_ch_5g_all[ch_idx]); + if (end[dump_section] == center_ch_5g_all[ch_idx]) + break; + } + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (ch_idx = ch_idx_s; ch_idx < CENTER_CH_5G_ALL_NUM; ch_idx++) { + _RTW_PRINT_SEL(sel, "%3u ", hal_data->Index5G_BW40_Base[path][ch_idx]); + if (end[dump_section] == center_ch_5g_all[ch_idx]) + break; + } + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + ch_idx_s = ch_idx + 1; + dump_section++; + if (dump_section >= DUMP_5G_BW40_BASE_SECTION_NUM) + break; + } while (1); + + RTW_PRINT_SEL(sel, "BW80-1S base:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (ch_idx = 0; ch_idx < CENTER_CH_5G_80M_NUM; ch_idx++) + _RTW_PRINT_SEL(sel, "%3d ", center_ch_5g_80m[ch_idx]); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (ch_idx = 0; ch_idx < CENTER_CH_5G_80M_NUM; ch_idx++) + _RTW_PRINT_SEL(sel, "%3u ", hal_data->Index5G_BW80_Base[path][ch_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "OFDM diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%dT ", tx_idx + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", hal_data->OFDM_5G_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "BW20 diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%dS ", tx_idx + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", hal_data->BW20_5G_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "BW40 diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%dS ", tx_idx + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", hal_data->BW40_5G_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); + + RTW_PRINT_SEL(sel, "BW80 diff:\n"); + RTW_PRINT_SEL(sel, "%4s ", ""); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%dS ", tx_idx + 1); + _RTW_PRINT_SEL(sel, "\n"); + for (path = 0; path < MAX_RF_PATH && path < rfpath_num; path++) { + RTW_PRINT_SEL(sel, "[%c]: ", rf_path_char(path)); + for (tx_idx = RF_1TX; tx_idx < MAX_TX_COUNT && tx_idx < max_tx_cnt; tx_idx++) + _RTW_PRINT_SEL(sel, "%2d ", hal_data->BW80_5G_Diff[path][tx_idx]); + _RTW_PRINT_SEL(sel, "\n"); + } + RTW_PRINT_SEL(sel, "\n"); +#endif /* CONFIG_IEEE80211_BAND_5GHZ */ +} +#endif /* CONFIG_TXPWR_PG_WITH_PWR_IDX */ + +/* +* rtw_regsty_get_target_tx_power - +* +* Return dBm or -1 for undefined +*/ +s8 rtw_regsty_get_target_tx_power( + PADAPTER Adapter, + u8 Band, + u8 RfPath, + RATE_SECTION RateSection +) +{ + struct registry_priv *regsty = adapter_to_regsty(Adapter); + s8 value = 0; + + if (RfPath > RF_PATH_D) { + RTW_PRINT("%s invalid RfPath:%d\n", __func__, RfPath); + return -1; + } + + if (Band != BAND_ON_2_4G + #if CONFIG_IEEE80211_BAND_5GHZ + && Band != BAND_ON_5G + #endif + ) { + RTW_PRINT("%s invalid Band:%d\n", __func__, Band); + return -1; + } + + if (RateSection >= RATE_SECTION_NUM + #if CONFIG_IEEE80211_BAND_5GHZ + || (Band == BAND_ON_5G && RateSection == CCK) + #endif + ) { + RTW_PRINT("%s invalid RateSection:%d in Band:%d, RfPath:%d\n", __func__ + , RateSection, Band, RfPath); + return -1; + } + + if (Band == BAND_ON_2_4G) + value = regsty->target_tx_pwr_2g[RfPath][RateSection]; +#if CONFIG_IEEE80211_BAND_5GHZ + else /* BAND_ON_5G */ + value = regsty->target_tx_pwr_5g[RfPath][RateSection - 1]; +#endif + + return value; +} + +bool rtw_regsty_chk_target_tx_power_valid(_adapter *adapter) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + int path, tx_num, band, rs; + s8 target; + + for (band = BAND_ON_2_4G; band <= BAND_ON_5G; band++) { + if (!hal_is_band_support(adapter, band)) + continue; + + for (path = 0; path < RF_PATH_MAX; path++) { + if (!HAL_SPEC_CHK_RF_PATH(hal_spec, band, path)) + break; + + for (rs = 0; rs < RATE_SECTION_NUM; rs++) { + tx_num = rate_section_to_tx_num(rs); + if (tx_num + 1 > GET_HAL_TX_NSS(adapter)) + continue; + + if (band == BAND_ON_5G && IS_CCK_RATE_SECTION(rs)) + continue; + + if (IS_VHT_RATE_SECTION(rs) && !IS_HARDWARE_TYPE_JAGUAR_ALL(adapter)) + continue; + + target = rtw_regsty_get_target_tx_power(adapter, band, path, rs); + if (target == -1) { + RTW_PRINT("%s return _FALSE for band:%d, path:%d, rs:%d, t:%d\n", __func__, band, path, rs, target); + return _FALSE; + } + } + } + } + + return _TRUE; +} + +/* +* phy_get_target_txpwr - +* +* Return value in unit of TX Gain Index +*/ +u8 phy_get_target_txpwr( + PADAPTER Adapter, + u8 Band, + u8 RfPath, + RATE_SECTION RateSection +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + u8 value = 0; + + if (RfPath > RF_PATH_D) { + RTW_PRINT("%s invalid RfPath:%d\n", __func__, RfPath); + return 0; + } + + if (Band != BAND_ON_2_4G && Band != BAND_ON_5G) { + RTW_PRINT("%s invalid Band:%d\n", __func__, Band); + return 0; + } + + if (RateSection >= RATE_SECTION_NUM + || (Band == BAND_ON_5G && RateSection == CCK) + ) { + RTW_PRINT("%s invalid RateSection:%d in Band:%d, RfPath:%d\n", __func__ + , RateSection, Band, RfPath); + return 0; + } + + if (Band == BAND_ON_2_4G) + value = pHalData->target_txpwr_2g[RfPath][RateSection]; + else if (Band == BAND_ON_5G) + value = pHalData->target_txpwr_5g[RfPath][RateSection - 1]; + + return value; +} + +static void phy_set_target_txpwr( + PADAPTER Adapter, + u8 Band, + u8 RfPath, + RATE_SECTION RateSection, + u8 Value +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + + if (RfPath > RF_PATH_D) { + RTW_PRINT("%s invalid RfPath:%d\n", __func__, RfPath); + return; + } + + if (Band != BAND_ON_2_4G && Band != BAND_ON_5G) { + RTW_PRINT("%s invalid Band:%d\n", __func__, Band); + return; + } + + if (RateSection >= RATE_SECTION_NUM + || (Band == BAND_ON_5G && RateSection == CCK) + ) { + RTW_PRINT("%s invalid RateSection:%d in %sG, RfPath:%d\n", __func__ + , RateSection, (Band == BAND_ON_2_4G) ? "2.4" : "5", RfPath); + return; + } + + if (Band == BAND_ON_2_4G) + pHalData->target_txpwr_2g[RfPath][RateSection] = Value; + else /* BAND_ON_5G */ + pHalData->target_txpwr_5g[RfPath][RateSection - 1] = Value; +} + +static inline BOOLEAN phy_is_txpwr_by_rate_undefined_of_band_path(_adapter *adapter, u8 band, u8 path) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u8 rate_idx = 0; + + for (rate_idx = 0; rate_idx < TX_PWR_BY_RATE_NUM_RATE; rate_idx++) { + if (hal_data->TxPwrByRate[band][path][rate_idx] != hal_spec->txgi_max) + goto exit; + } + +exit: + return rate_idx >= TX_PWR_BY_RATE_NUM_RATE ? _TRUE : _FALSE; +} + +static inline void phy_txpwr_by_rate_duplicate_band_path(_adapter *adapter, u8 band, u8 s_path, u8 t_path) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u8 rate_idx = 0; + + for (rate_idx = 0; rate_idx < TX_PWR_BY_RATE_NUM_RATE; rate_idx++) + hal_data->TxPwrByRate[band][t_path][rate_idx] = hal_data->TxPwrByRate[band][s_path][rate_idx]; +} + +static void phy_txpwr_by_rate_chk_for_path_dup(_adapter *adapter) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u8 band, path; + s8 src_path; + + for (band = BAND_ON_2_4G; band <= BAND_ON_5G; band++) + for (path = RF_PATH_A; path < RF_PATH_MAX; path++) + hal_data->txpwr_by_rate_undefined_band_path[band][path] = 0; + + for (band = BAND_ON_2_4G; band <= BAND_ON_5G; band++) { + if (!hal_is_band_support(adapter, band)) + continue; + + for (path = RF_PATH_A; path < RF_PATH_MAX; path++) { + if (!HAL_SPEC_CHK_RF_PATH(hal_spec, band, path)) + continue; + + if (phy_is_txpwr_by_rate_undefined_of_band_path(adapter, band, path)) + hal_data->txpwr_by_rate_undefined_band_path[band][path] = 1; + } + } + + for (band = BAND_ON_2_4G; band <= BAND_ON_5G; band++) { + if (!hal_is_band_support(adapter, band)) + continue; + + src_path = -1; + for (path = RF_PATH_A; path < RF_PATH_MAX; path++) { + if (!HAL_SPEC_CHK_RF_PATH(hal_spec, band, path)) + continue; + + /* find src */ + if (src_path == -1 && hal_data->txpwr_by_rate_undefined_band_path[band][path] == 0) + src_path = path; + } + + if (src_path == -1) { + RTW_ERR("%s all power by rate undefined\n", __func__); + continue; + } + + for (path = RF_PATH_A; path < RF_PATH_MAX; path++) { + if (!HAL_SPEC_CHK_RF_PATH(hal_spec, band, path)) + continue; + + /* duplicate src to undefined one */ + if (hal_data->txpwr_by_rate_undefined_band_path[band][path] == 1) { + RTW_INFO("%s duplicate %s [%c] to [%c]\n", __func__ + , band_str(band), rf_path_char(src_path), rf_path_char(path)); + phy_txpwr_by_rate_duplicate_band_path(adapter, band, src_path, path); + } + } + } +} + +static s8 _phy_get_txpwr_by_rate(_adapter *adapter + , BAND_TYPE band, enum rf_path rfpath, enum MGN_RATE rate); + +void phy_store_target_tx_power(PADAPTER pAdapter) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(pAdapter); + struct registry_priv *regsty = adapter_to_regsty(pAdapter); + + u8 band, path, rs, tx_num, base; + + for (band = BAND_ON_2_4G; band <= BAND_ON_5G; band++) { + if (!hal_is_band_support(pAdapter, band)) + continue; + + for (path = RF_PATH_A; path < RF_PATH_MAX; path++) { + if (!HAL_SPEC_CHK_RF_PATH(hal_spec, band, path)) + break; + + for (rs = 0; rs < RATE_SECTION_NUM; rs++) { + tx_num = rate_section_to_tx_num(rs); + if (tx_num + 1 > GET_HAL_TX_NSS(pAdapter)) + continue; + + if (band == BAND_ON_5G && IS_CCK_RATE_SECTION(rs)) + continue; + + if (IS_VHT_RATE_SECTION(rs) && !IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter)) + continue; + + if (regsty->target_tx_pwr_valid == _TRUE) + base = hal_spec->txgi_pdbm * rtw_regsty_get_target_tx_power(pAdapter, band, path, rs); + else + base = _phy_get_txpwr_by_rate(pAdapter, band, path, rate_sec_base[rs]); + phy_set_target_txpwr(pAdapter, band, path, rs, base); + } + } + } +} + +static u8 get_val_from_dhex(u32 dhex, u8 i) +{ + return (((dhex >> (i * 8 + 4)) & 0xF)) * 10 + ((dhex >> (i * 8)) & 0xF); +} + +static u8 get_val_from_hex(u32 hex, u8 i) +{ + return (hex >> (i * 8)) & 0xFF; +} + +void +PHY_GetRateValuesOfTxPowerByRate( + PADAPTER pAdapter, + u32 RegAddr, + u32 BitMask, + u32 Value, + u8 *Rate, + s8 *PwrByRateVal, + u8 *RateNum +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + u8 i = 0; + u8 (*get_val)(u32, u8); + + if (pDM_Odm->phy_reg_pg_version == 1) + get_val = get_val_from_dhex; + else + get_val = get_val_from_hex; + + switch (RegAddr) { + case rTxAGC_A_Rate18_06: + case rTxAGC_B_Rate18_06: + Rate[0] = MGN_6M; + Rate[1] = MGN_9M; + Rate[2] = MGN_12M; + Rate[3] = MGN_18M; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case rTxAGC_A_Rate54_24: + case rTxAGC_B_Rate54_24: + Rate[0] = MGN_24M; + Rate[1] = MGN_36M; + Rate[2] = MGN_48M; + Rate[3] = MGN_54M; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case rTxAGC_A_CCK1_Mcs32: + Rate[0] = MGN_1M; + PwrByRateVal[0] = (s8)get_val(Value, 1); + *RateNum = 1; + break; + + case rTxAGC_B_CCK11_A_CCK2_11: + if (BitMask == 0xffffff00) { + Rate[0] = MGN_2M; + Rate[1] = MGN_5_5M; + Rate[2] = MGN_11M; + for (i = 1; i < 4; ++i) + PwrByRateVal[i - 1] = (s8)get_val(Value, i); + *RateNum = 3; + } else if (BitMask == 0x000000ff) { + Rate[0] = MGN_11M; + PwrByRateVal[0] = (s8)get_val(Value, 0); + *RateNum = 1; + } + break; + + case rTxAGC_A_Mcs03_Mcs00: + case rTxAGC_B_Mcs03_Mcs00: + Rate[0] = MGN_MCS0; + Rate[1] = MGN_MCS1; + Rate[2] = MGN_MCS2; + Rate[3] = MGN_MCS3; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case rTxAGC_A_Mcs07_Mcs04: + case rTxAGC_B_Mcs07_Mcs04: + Rate[0] = MGN_MCS4; + Rate[1] = MGN_MCS5; + Rate[2] = MGN_MCS6; + Rate[3] = MGN_MCS7; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case rTxAGC_A_Mcs11_Mcs08: + case rTxAGC_B_Mcs11_Mcs08: + Rate[0] = MGN_MCS8; + Rate[1] = MGN_MCS9; + Rate[2] = MGN_MCS10; + Rate[3] = MGN_MCS11; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case rTxAGC_A_Mcs15_Mcs12: + case rTxAGC_B_Mcs15_Mcs12: + Rate[0] = MGN_MCS12; + Rate[1] = MGN_MCS13; + Rate[2] = MGN_MCS14; + Rate[3] = MGN_MCS15; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case rTxAGC_B_CCK1_55_Mcs32: + Rate[0] = MGN_1M; + Rate[1] = MGN_2M; + Rate[2] = MGN_5_5M; + for (i = 1; i < 4; ++i) + PwrByRateVal[i - 1] = (s8)get_val(Value, i); + *RateNum = 3; + break; + + case 0xC20: + case 0xE20: + case 0x1820: + case 0x1a20: + Rate[0] = MGN_1M; + Rate[1] = MGN_2M; + Rate[2] = MGN_5_5M; + Rate[3] = MGN_11M; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xC24: + case 0xE24: + case 0x1824: + case 0x1a24: + Rate[0] = MGN_6M; + Rate[1] = MGN_9M; + Rate[2] = MGN_12M; + Rate[3] = MGN_18M; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xC28: + case 0xE28: + case 0x1828: + case 0x1a28: + Rate[0] = MGN_24M; + Rate[1] = MGN_36M; + Rate[2] = MGN_48M; + Rate[3] = MGN_54M; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xC2C: + case 0xE2C: + case 0x182C: + case 0x1a2C: + Rate[0] = MGN_MCS0; + Rate[1] = MGN_MCS1; + Rate[2] = MGN_MCS2; + Rate[3] = MGN_MCS3; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xC30: + case 0xE30: + case 0x1830: + case 0x1a30: + Rate[0] = MGN_MCS4; + Rate[1] = MGN_MCS5; + Rate[2] = MGN_MCS6; + Rate[3] = MGN_MCS7; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xC34: + case 0xE34: + case 0x1834: + case 0x1a34: + Rate[0] = MGN_MCS8; + Rate[1] = MGN_MCS9; + Rate[2] = MGN_MCS10; + Rate[3] = MGN_MCS11; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xC38: + case 0xE38: + case 0x1838: + case 0x1a38: + Rate[0] = MGN_MCS12; + Rate[1] = MGN_MCS13; + Rate[2] = MGN_MCS14; + Rate[3] = MGN_MCS15; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xC3C: + case 0xE3C: + case 0x183C: + case 0x1a3C: + Rate[0] = MGN_VHT1SS_MCS0; + Rate[1] = MGN_VHT1SS_MCS1; + Rate[2] = MGN_VHT1SS_MCS2; + Rate[3] = MGN_VHT1SS_MCS3; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xC40: + case 0xE40: + case 0x1840: + case 0x1a40: + Rate[0] = MGN_VHT1SS_MCS4; + Rate[1] = MGN_VHT1SS_MCS5; + Rate[2] = MGN_VHT1SS_MCS6; + Rate[3] = MGN_VHT1SS_MCS7; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xC44: + case 0xE44: + case 0x1844: + case 0x1a44: + Rate[0] = MGN_VHT1SS_MCS8; + Rate[1] = MGN_VHT1SS_MCS9; + Rate[2] = MGN_VHT2SS_MCS0; + Rate[3] = MGN_VHT2SS_MCS1; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xC48: + case 0xE48: + case 0x1848: + case 0x1a48: + Rate[0] = MGN_VHT2SS_MCS2; + Rate[1] = MGN_VHT2SS_MCS3; + Rate[2] = MGN_VHT2SS_MCS4; + Rate[3] = MGN_VHT2SS_MCS5; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xC4C: + case 0xE4C: + case 0x184C: + case 0x1a4C: + Rate[0] = MGN_VHT2SS_MCS6; + Rate[1] = MGN_VHT2SS_MCS7; + Rate[2] = MGN_VHT2SS_MCS8; + Rate[3] = MGN_VHT2SS_MCS9; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xCD8: + case 0xED8: + case 0x18D8: + case 0x1aD8: + Rate[0] = MGN_MCS16; + Rate[1] = MGN_MCS17; + Rate[2] = MGN_MCS18; + Rate[3] = MGN_MCS19; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xCDC: + case 0xEDC: + case 0x18DC: + case 0x1aDC: + Rate[0] = MGN_MCS20; + Rate[1] = MGN_MCS21; + Rate[2] = MGN_MCS22; + Rate[3] = MGN_MCS23; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0x3a24: /* HT MCS24-27 */ + Rate[0] = MGN_MCS24; + Rate[1] = MGN_MCS25; + Rate[2] = MGN_MCS26; + Rate[3] = MGN_MCS27; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0x3a28: /* HT MCS28-31 */ + Rate[0] = MGN_MCS28; + Rate[1] = MGN_MCS29; + Rate[2] = MGN_MCS30; + Rate[3] = MGN_MCS31; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xCE0: + case 0xEE0: + case 0x18E0: + case 0x1aE0: + Rate[0] = MGN_VHT3SS_MCS0; + Rate[1] = MGN_VHT3SS_MCS1; + Rate[2] = MGN_VHT3SS_MCS2; + Rate[3] = MGN_VHT3SS_MCS3; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xCE4: + case 0xEE4: + case 0x18E4: + case 0x1aE4: + Rate[0] = MGN_VHT3SS_MCS4; + Rate[1] = MGN_VHT3SS_MCS5; + Rate[2] = MGN_VHT3SS_MCS6; + Rate[3] = MGN_VHT3SS_MCS7; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0xCE8: + case 0xEE8: + case 0x18E8: + case 0x1aE8: + case 0x3a48: + Rate[0] = MGN_VHT3SS_MCS8; + Rate[1] = MGN_VHT3SS_MCS9; + Rate[2] = MGN_VHT4SS_MCS0; + Rate[3] = MGN_VHT4SS_MCS1; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0x3a4c: + Rate[0] = MGN_VHT4SS_MCS2; + Rate[1] = MGN_VHT4SS_MCS3; + Rate[2] = MGN_VHT4SS_MCS4; + Rate[3] = MGN_VHT4SS_MCS5; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + case 0x3a50: + Rate[0] = MGN_VHT4SS_MCS6; + Rate[1] = MGN_VHT4SS_MCS7; + Rate[2] = MGN_VHT4SS_MCS8; + Rate[3] = MGN_VHT4SS_MCS9; + for (i = 0; i < 4; ++i) + PwrByRateVal[i] = (s8)get_val(Value, i); + *RateNum = 4; + break; + + default: + RTW_PRINT("Invalid RegAddr 0x%x in %s()\n", RegAddr, __func__); + break; + }; +} + +void +PHY_StoreTxPowerByRateNew( + PADAPTER pAdapter, + u32 Band, + u32 RfPath, + u32 RegAddr, + u32 BitMask, + u32 Data +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + u8 i = 0, rates[4] = {0}, rateNum = 0; + s8 PwrByRateVal[4] = {0}; + + PHY_GetRateValuesOfTxPowerByRate(pAdapter, RegAddr, BitMask, Data, rates, PwrByRateVal, &rateNum); + + if (Band != BAND_ON_2_4G && Band != BAND_ON_5G) { + RTW_PRINT("Invalid Band %d\n", Band); + return; + } + + if (RfPath > RF_PATH_D) { + RTW_PRINT("Invalid RfPath %d\n", RfPath); + return; + } + + for (i = 0; i < rateNum; ++i) { + u8 rate_idx = phy_get_rate_idx_of_txpwr_by_rate(rates[i]); + + pHalData->TxPwrByRate[Band][RfPath][rate_idx] = PwrByRateVal[i]; + } +} + +void +PHY_InitTxPowerByRate( + PADAPTER pAdapter +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(pAdapter); + u8 band = 0, rfPath = 0, rate = 0; + + for (band = BAND_ON_2_4G; band <= BAND_ON_5G; ++band) + for (rfPath = 0; rfPath < TX_PWR_BY_RATE_NUM_RF; ++rfPath) + for (rate = 0; rate < TX_PWR_BY_RATE_NUM_RATE; ++rate) + pHalData->TxPwrByRate[band][rfPath][rate] = hal_spec->txgi_max; +} + +void +phy_store_tx_power_by_rate( + PADAPTER pAdapter, + u32 Band, + u32 RfPath, + u32 TxNum, + u32 RegAddr, + u32 BitMask, + u32 Data +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + + if (pDM_Odm->phy_reg_pg_version > 0) + PHY_StoreTxPowerByRateNew(pAdapter, Band, RfPath, RegAddr, BitMask, Data); + else + RTW_INFO("Invalid PHY_REG_PG.txt version %d\n", pDM_Odm->phy_reg_pg_version); + +} + +/* + * This function must be called if the value in the PHY_REG_PG.txt(or header) + * is exact dBm values + */ +void +PHY_TxPowerByRateConfiguration( + PADAPTER pAdapter +) +{ + phy_txpwr_by_rate_chk_for_path_dup(pAdapter); + phy_store_target_tx_power(pAdapter); +} + +#ifdef CONFIG_FW_OFFLOAD_SET_TXPWR_IDX +extern bool phy_set_txpwr_idx_offload(_adapter *adapter); +#endif + +void +phy_set_tx_power_index_by_rate_section( + PADAPTER pAdapter, + enum rf_path RFPath, + u8 Channel, + u8 rs +) +{ + PHAL_DATA_TYPE hal_data = GET_HAL_DATA(pAdapter); + u8 band = hal_data->current_band_type; + u8 bw = hal_data->current_channel_bw; + u32 powerIndex = 0; + int i = 0; + + if (rs >= RATE_SECTION_NUM) { + RTW_INFO("Invalid RateSection %d in %s", rs, __func__); + rtw_warn_on(1); + goto exit; + } + + if (rs == CCK && bw != BAND_ON_2_4G) + goto exit; + + for (i = 0; i < rates_by_sections[rs].rate_num; ++i) { +#if DBG_TX_POWER_IDX + struct txpwr_idx_comp tic; + + powerIndex = rtw_hal_get_tx_power_index(pAdapter, RFPath + , rs, rates_by_sections[rs].rates[i], bw, band, Channel, 0, &tic); + dump_tx_power_index_inline(RTW_DBGDUMP, pAdapter, RFPath, bw, Channel + , rates_by_sections[rs].rates[i], powerIndex, &tic); +#else + powerIndex = phy_get_tx_power_index_ex(pAdapter, RFPath + , rs, rates_by_sections[rs].rates[i], bw, band, Channel, 0); +#endif + PHY_SetTxPowerIndex(pAdapter, powerIndex, RFPath, rates_by_sections[rs].rates[i]); + } + +#ifdef CONFIG_FW_OFFLOAD_SET_TXPWR_IDX + if (!hal_data->set_entire_txpwr + && phy_set_txpwr_idx_offload(pAdapter)) + rtw_hal_set_txpwr_done(pAdapter); +#endif + +exit: + return; +} + +bool phy_get_ch_idx(u8 ch, u8 *ch_idx) +{ + u8 i = 0; + BOOLEAN bIn24G = _TRUE; + + if (ch > 0 && ch <= 14) { + bIn24G = _TRUE; + *ch_idx = ch - 1; + } else { + bIn24G = _FALSE; + + for (i = 0; i < CENTER_CH_5G_ALL_NUM; ++i) { + if (center_ch_5g_all[i] == ch) { + *ch_idx = i; + break; + } + } + } + + return bIn24G; +} + +bool phy_chk_ch_setting_consistency(_adapter *adapter, u8 ch) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u8 ch_idx = 0xFF; + u8 ret = _FAIL; + + phy_get_ch_idx(ch, &ch_idx); + if (ch_idx == 0xFF) { + rtw_warn_on(1); + goto exit; + } + + if (ch != hal_data->current_channel) { + rtw_warn_on(1); + goto exit; + } + + if (ch <= 14) { + if (hal_data->current_band_type != BAND_ON_2_4G) { + rtw_warn_on(1); + goto exit; + } + if (hal_data->current_channel_bw > CHANNEL_WIDTH_40) { + rtw_warn_on(1); + goto exit; + } + } + if (ch > 14) { + if (hal_data->current_band_type != BAND_ON_5G) { + rtw_warn_on(1); + goto exit; + } + if (hal_data->current_channel_bw > CHANNEL_WIDTH_160) { + rtw_warn_on(1); + goto exit; + } + } + + ret = _SUCCESS; + +exit: + if (ret != _SUCCESS) + RTW_WARN("ch:%u, hal band:%u, ch:%u, bw:%u\n", ch + , hal_data->current_band_type, hal_data->current_channel, hal_data->current_channel_bw); + + return ret; +} + +#ifdef CONFIG_TXPWR_PG_WITH_PWR_IDX +u8 phy_get_pg_txpwr_idx(_adapter *pAdapter + , enum rf_path RFPath, RATE_SECTION rs, u8 ntx_idx + , enum channel_width BandWidth, u8 band, u8 Channel) +{ + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter); + u8 i; + u8 txPower = 0; + u8 chnlIdx = (Channel - 1); + + if (HAL_IsLegalChannel(pAdapter, Channel) == _FALSE) { + chnlIdx = 0; + RTW_INFO("Illegal channel!!\n"); + } + + phy_get_ch_idx(Channel, &chnlIdx); + + if (0) + RTW_INFO("[%s] Channel Index: %d\n", band_str(band), chnlIdx); + + if (band == BAND_ON_2_4G) { + if (IS_CCK_RATE_SECTION(rs)) { + /* CCK-nTX */ + txPower = pHalData->Index24G_CCK_Base[RFPath][chnlIdx]; + txPower += pHalData->CCK_24G_Diff[RFPath][RF_1TX]; + if (ntx_idx >= RF_2TX) + txPower += pHalData->CCK_24G_Diff[RFPath][RF_2TX]; + if (ntx_idx >= RF_3TX) + txPower += pHalData->CCK_24G_Diff[RFPath][RF_3TX]; + if (ntx_idx >= RF_4TX) + txPower += pHalData->CCK_24G_Diff[RFPath][RF_4TX]; + goto exit; + } + + txPower = pHalData->Index24G_BW40_Base[RFPath][chnlIdx]; + + /* OFDM-nTX */ + if (IS_OFDM_RATE_SECTION(rs)) { + txPower += pHalData->OFDM_24G_Diff[RFPath][RF_1TX]; + if (ntx_idx >= RF_2TX) + txPower += pHalData->OFDM_24G_Diff[RFPath][RF_2TX]; + if (ntx_idx >= RF_3TX) + txPower += pHalData->OFDM_24G_Diff[RFPath][RF_3TX]; + if (ntx_idx >= RF_4TX) + txPower += pHalData->OFDM_24G_Diff[RFPath][RF_4TX]; + goto exit; + } + + /* BW20-nS */ + if (BandWidth == CHANNEL_WIDTH_20) { + txPower += pHalData->BW20_24G_Diff[RFPath][RF_1TX]; + if (rate_section_to_tx_num(rs) >= RF_2TX) + txPower += pHalData->BW20_24G_Diff[RFPath][RF_2TX]; + if (rate_section_to_tx_num(rs) >= RF_3TX) + txPower += pHalData->BW20_24G_Diff[RFPath][RF_3TX]; + if (rate_section_to_tx_num(rs) >= RF_4TX) + txPower += pHalData->BW20_24G_Diff[RFPath][RF_4TX]; + goto exit; + } + + /* BW40-nS */ + if (BandWidth == CHANNEL_WIDTH_40 + /* Willis suggest adopt BW 40M power index while in BW 80 mode */ + || BandWidth == CHANNEL_WIDTH_80 + ) { + txPower += pHalData->BW40_24G_Diff[RFPath][RF_1TX]; + if (rate_section_to_tx_num(rs) >= RF_2TX) + txPower += pHalData->BW40_24G_Diff[RFPath][RF_2TX]; + if (rate_section_to_tx_num(rs) >= RF_3TX) + txPower += pHalData->BW40_24G_Diff[RFPath][RF_3TX]; + if (rate_section_to_tx_num(rs) >= RF_4TX) + txPower += pHalData->BW40_24G_Diff[RFPath][RF_4TX]; + goto exit; + } + } +#if CONFIG_IEEE80211_BAND_5GHZ + else if (band == BAND_ON_5G) { + if (IS_CCK_RATE_SECTION(rs)) { + RTW_WARN("===>%s: INVALID, CCK on 5G\n", __func__); + goto exit; + } + + txPower = pHalData->Index5G_BW40_Base[RFPath][chnlIdx]; + + /* OFDM-nTX */ + if (IS_OFDM_RATE_SECTION(rs)) { + txPower += pHalData->OFDM_5G_Diff[RFPath][RF_1TX]; + if (ntx_idx >= RF_2TX) + txPower += pHalData->OFDM_5G_Diff[RFPath][RF_2TX]; + if (ntx_idx >= RF_3TX) + txPower += pHalData->OFDM_5G_Diff[RFPath][RF_3TX]; + if (ntx_idx >= RF_4TX) + txPower += pHalData->OFDM_5G_Diff[RFPath][RF_4TX]; + goto exit; + } + + /* BW20-nS */ + if (BandWidth == CHANNEL_WIDTH_20) { + txPower += pHalData->BW20_5G_Diff[RFPath][RF_1TX]; + if (rate_section_to_tx_num(rs) >= RF_2TX) + txPower += pHalData->BW20_5G_Diff[RFPath][RF_2TX]; + if (rate_section_to_tx_num(rs) >= RF_3TX) + txPower += pHalData->BW20_5G_Diff[RFPath][RF_3TX]; + if (rate_section_to_tx_num(rs) >= RF_4TX) + txPower += pHalData->BW20_5G_Diff[RFPath][RF_4TX]; + goto exit; + } + + /* BW40-nS */ + if (BandWidth == CHANNEL_WIDTH_40) { + txPower += pHalData->BW40_5G_Diff[RFPath][RF_1TX]; + if (rate_section_to_tx_num(rs) >= RF_2TX) + txPower += pHalData->BW40_5G_Diff[RFPath][RF_2TX]; + if (rate_section_to_tx_num(rs) >= RF_3TX) + txPower += pHalData->BW40_5G_Diff[RFPath][RF_3TX]; + if (rate_section_to_tx_num(rs) >= RF_4TX) + txPower += pHalData->BW40_5G_Diff[RFPath][RF_4TX]; + goto exit; + } + + /* BW80-nS */ + if (BandWidth == CHANNEL_WIDTH_80) { + /* get 80MHz cch index */ + for (i = 0; i < CENTER_CH_5G_80M_NUM; ++i) { + if (center_ch_5g_80m[i] == Channel) { + chnlIdx = i; + break; + } + } + if (i >= CENTER_CH_5G_80M_NUM) { + #ifdef CONFIG_MP_INCLUDED + if (rtw_mp_mode_check(pAdapter) == _FALSE) + #endif + rtw_warn_on(1); + txPower = 0; + goto exit; + } + + txPower = pHalData->Index5G_BW80_Base[RFPath][chnlIdx]; + + txPower += + pHalData->BW80_5G_Diff[RFPath][RF_1TX]; + if (rate_section_to_tx_num(rs) >= RF_2TX) + txPower += pHalData->BW80_5G_Diff[RFPath][RF_2TX]; + if (rate_section_to_tx_num(rs) >= RF_3TX) + txPower += pHalData->BW80_5G_Diff[RFPath][RF_3TX]; + if (rate_section_to_tx_num(rs) >= RF_4TX) + txPower += pHalData->BW80_5G_Diff[RFPath][RF_4TX]; + goto exit; + } + + /* TODO: BW160-nS */ + rtw_warn_on(1); + } +#endif /* CONFIG_IEEE80211_BAND_5GHZ */ + +exit: + return txPower; +} +#endif /* CONFIG_TXPWR_PG_WITH_PWR_IDX */ + +s8 +PHY_GetTxPowerTrackingOffset( + PADAPTER pAdapter, + enum rf_path RFPath, + u8 Rate +) +{ + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(pAdapter); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + s8 offset = 0; + + if (pDM_Odm->rf_calibrate_info.txpowertrack_control == _FALSE) + return offset; + + if ((Rate == MGN_1M) || (Rate == MGN_2M) || (Rate == MGN_5_5M) || (Rate == MGN_11M)) { + offset = pDM_Odm->rf_calibrate_info.remnant_cck_swing_idx; + /*RTW_INFO("+Remnant_CCKSwingIdx = 0x%x\n", RFPath, Rate, pRFCalibrateInfo->Remnant_CCKSwingIdx);*/ + } else { + offset = pDM_Odm->rf_calibrate_info.remnant_ofdm_swing_idx[RFPath]; + /*RTW_INFO("+Remanant_OFDMSwingIdx[RFPath %u][Rate 0x%x] = 0x%x\n", RFPath, Rate, pRFCalibrateInfo->Remnant_OFDMSwingIdx[RFPath]); */ + + } + + return offset; +} + +static const u8 _phy_get_rate_idx_of_txpwr_by_rate[MGN_UNKNOWN] = { + [MGN_1M] = 0, + [MGN_2M] = 1, + [MGN_5_5M] = 2, + [MGN_11M] = 3, + [MGN_6M] = 4, + [MGN_9M] = 5, + [MGN_12M] = 6, + [MGN_18M] = 7, + [MGN_24M] = 8, + [MGN_36M] = 9, + [MGN_48M] = 10, + [MGN_54M] = 11, + [MGN_MCS0] = 12, + [MGN_MCS1] = 13, + [MGN_MCS2] = 14, + [MGN_MCS3] = 15, + [MGN_MCS4] = 16, + [MGN_MCS5] = 17, + [MGN_MCS6] = 18, + [MGN_MCS7] = 19, + [MGN_MCS8] = 20, + [MGN_MCS9] = 21, + [MGN_MCS10] = 22, + [MGN_MCS11] = 23, + [MGN_MCS12] = 24, + [MGN_MCS13] = 25, + [MGN_MCS14] = 26, + [MGN_MCS15] = 27, + [MGN_MCS16] = 28, + [MGN_MCS17] = 29, + [MGN_MCS18] = 30, + [MGN_MCS19] = 31, + [MGN_MCS20] = 32, + [MGN_MCS21] = 33, + [MGN_MCS22] = 34, + [MGN_MCS23] = 35, + [MGN_MCS24] = 36, + [MGN_MCS25] = 37, + [MGN_MCS26] = 38, + [MGN_MCS27] = 39, + [MGN_MCS28] = 40, + [MGN_MCS29] = 41, + [MGN_MCS30] = 42, + [MGN_MCS31] = 43, + [MGN_VHT1SS_MCS0] = 44, + [MGN_VHT1SS_MCS1] = 45, + [MGN_VHT1SS_MCS2] = 46, + [MGN_VHT1SS_MCS3] = 47, + [MGN_VHT1SS_MCS4] = 48, + [MGN_VHT1SS_MCS5] = 49, + [MGN_VHT1SS_MCS6] = 50, + [MGN_VHT1SS_MCS7] = 51, + [MGN_VHT1SS_MCS8] = 52, + [MGN_VHT1SS_MCS9] = 53, + [MGN_VHT2SS_MCS0] = 54, + [MGN_VHT2SS_MCS1] = 55, + [MGN_VHT2SS_MCS2] = 56, + [MGN_VHT2SS_MCS3] = 57, + [MGN_VHT2SS_MCS4] = 58, + [MGN_VHT2SS_MCS5] = 59, + [MGN_VHT2SS_MCS6] = 60, + [MGN_VHT2SS_MCS7] = 61, + [MGN_VHT2SS_MCS8] = 62, + [MGN_VHT2SS_MCS9] = 63, + [MGN_VHT3SS_MCS0] = 64, + [MGN_VHT3SS_MCS1] = 65, + [MGN_VHT3SS_MCS2] = 66, + [MGN_VHT3SS_MCS3] = 67, + [MGN_VHT3SS_MCS4] = 68, + [MGN_VHT3SS_MCS5] = 69, + [MGN_VHT3SS_MCS6] = 70, + [MGN_VHT3SS_MCS7] = 71, + [MGN_VHT3SS_MCS8] = 72, + [MGN_VHT3SS_MCS9] = 73, + [MGN_VHT4SS_MCS0] = 74, + [MGN_VHT4SS_MCS1] = 75, + [MGN_VHT4SS_MCS2] = 76, + [MGN_VHT4SS_MCS3] = 77, + [MGN_VHT4SS_MCS4] = 78, + [MGN_VHT4SS_MCS5] = 79, + [MGN_VHT4SS_MCS6] = 80, + [MGN_VHT4SS_MCS7] = 81, + [MGN_VHT4SS_MCS8] = 82, + [MGN_VHT4SS_MCS9] = 83, +}; + +/*The same as MRateToHwRate in hal_com.c*/ +u8 phy_get_rate_idx_of_txpwr_by_rate(enum MGN_RATE rate) +{ + u8 index = 0; + + if (rate < MGN_UNKNOWN) + index = _phy_get_rate_idx_of_txpwr_by_rate[rate]; + + if (rate != MGN_1M && index == 0) + RTW_WARN("Invalid rate 0x%x in %s\n", rate, __FUNCTION__); + + return index; +} + +static s8 _phy_get_txpwr_by_rate(_adapter *adapter + , BAND_TYPE band, enum rf_path rfpath, enum MGN_RATE rate) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(adapter); + s8 value = 0; + u8 rate_idx = phy_get_rate_idx_of_txpwr_by_rate(rate); + + if (band != BAND_ON_2_4G && band != BAND_ON_5G) { + RTW_INFO("Invalid band %d in %s\n", band, __func__); + goto exit; + } + if (rfpath > RF_PATH_D) { + RTW_INFO("Invalid RfPath %d in %s\n", rfpath, __func__); + goto exit; + } + if (rate_idx >= TX_PWR_BY_RATE_NUM_RATE) { + RTW_INFO("Invalid RateIndex %d in %s\n", rate_idx, __func__); + goto exit; + } + + value = pHalData->TxPwrByRate[band][rfpath][rate_idx]; + +exit: + return value; +} + +/* +* Return value in unit of TX Gain Index +*/ +s8 phy_get_txpwr_by_rate(_adapter *adapter + , BAND_TYPE band, enum rf_path rfpath, RATE_SECTION rs, enum MGN_RATE rate) +{ + if (phy_is_tx_power_by_rate_needed(adapter)) + return _phy_get_txpwr_by_rate(adapter, band, rfpath, rate); + return phy_get_target_txpwr(adapter, band, rfpath, rs); +} + +/* get txpowr in mBm for single path */ +s16 phy_get_txpwr_by_rate_single_mbm(_adapter *adapter + , BAND_TYPE band, enum rf_path rfpath, RATE_SECTION rs, enum MGN_RATE rate, bool eirp) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + s16 val; + + val = phy_get_txpwr_by_rate(adapter, band, rfpath, rs, rate); + if (val == hal_spec->txgi_max) + val = UNSPECIFIED_MBM; + else { + val = (val * MBM_PDBM) / hal_spec->txgi_pdbm; + if (eirp) + val += rfctl->antenna_gain; + } + + return val; +} + +/* get txpowr in mBm with effect of N-TX */ +s16 phy_get_txpwr_by_rate_total_mbm(_adapter *adapter + , BAND_TYPE band, RATE_SECTION rs, enum MGN_RATE rate, bool cap, bool eirp) +{ + s16 val; + u8 tx_num; + + if (cap) + tx_num = phy_get_capable_tx_num(adapter, rate) + 1; + else + tx_num = phy_get_current_tx_num(adapter, rate) + 1; + + /* assume all path have same txpower target */ + val = phy_get_txpwr_by_rate_single_mbm(adapter, band, RF_PATH_A, rs, rate, eirp); + if (val != UNSPECIFIED_MBM) + val += mb_of_ntx(tx_num); + + return val; +} + +static s16 _phy_get_txpwr_by_rate_max_mbm(_adapter *adapter, BAND_TYPE band, s8 rfpath, bool cap, bool eirp) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u8 tx_num; + RATE_SECTION rs; + int i; + s16 max = UNSPECIFIED_MBM, mbm; + + for (rs = 0; rs < RATE_SECTION_NUM; rs++) { + tx_num = rate_section_to_tx_num(rs); + if (tx_num + 1 > hal_data->tx_nss) + continue; + + if (band == BAND_ON_5G && IS_CCK_RATE_SECTION(rs)) + continue; + + if (IS_VHT_RATE_SECTION(rs) && !IS_HARDWARE_TYPE_JAGUAR_ALL(adapter)) + continue; + + for (i = 0; i < rates_by_sections[rs].rate_num; i++) { + if (rfpath < 0) /* total */ + mbm = phy_get_txpwr_by_rate_total_mbm(adapter, band, rs, rates_by_sections[rs].rates[i], cap, eirp); + else + mbm = phy_get_txpwr_by_rate_single_mbm(adapter, band, rfpath, rs, rates_by_sections[rs].rates[i], eirp); + if (mbm == UNSPECIFIED_MBM) + continue; + if (max == UNSPECIFIED_MBM || mbm > max) + max = mbm; + } + } + + return max; +} + +/* get txpowr in mBm for single path */ +s16 phy_get_txpwr_by_rate_single_max_mbm(_adapter *adapter, BAND_TYPE band, enum rf_path rfpath, bool eirp) +{ + return _phy_get_txpwr_by_rate_max_mbm(adapter, band, rfpath, 0 /* single don't care */, eirp); +} + +/* get txpowr in mBm with effect of N-TX */ +s16 phy_get_txpwr_by_rate_total_max_mbm(_adapter *adapter, BAND_TYPE band, bool cap, bool eirp) +{ + return _phy_get_txpwr_by_rate_max_mbm(adapter, band, -1, cap, eirp); +} + +u8 phy_check_under_survey_ch(_adapter *adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + _adapter *iface; + struct mlme_ext_priv *mlmeext; + u8 ret = _FALSE; + int i; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (!iface) + continue; + mlmeext = &iface->mlmeextpriv; + + /* check scan state */ + if (mlmeext_scan_state(mlmeext) != SCAN_DISABLE + && mlmeext_scan_state(mlmeext) != SCAN_COMPLETE + && mlmeext_scan_state(mlmeext) != SCAN_BACKING_OP) { + ret = _TRUE; + } else if (mlmeext_scan_state(mlmeext) == SCAN_BACKING_OP + && !mlmeext_chk_scan_backop_flags(mlmeext, SS_BACKOP_TX_RESUME)) { + ret = _TRUE; + } + } + + return ret; +} + +void +phy_set_tx_power_level_by_path( + PADAPTER Adapter, + u8 channel, + u8 path +) +{ + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(Adapter); + BOOLEAN bIsIn24G = (pHalData->current_band_type == BAND_ON_2_4G); + u8 under_survey_ch = phy_check_under_survey_ch(Adapter); + + + /* if ( pMgntInfo->RegNByteAccess == 0 ) */ + { + if (bIsIn24G) + phy_set_tx_power_index_by_rate_section(Adapter, path, channel, CCK); + + phy_set_tx_power_index_by_rate_section(Adapter, path, channel, OFDM); + + if (!under_survey_ch) { + phy_set_tx_power_index_by_rate_section(Adapter, path, channel, HT_MCS0_MCS7); + + if (IS_HARDWARE_TYPE_JAGUAR(Adapter) || IS_HARDWARE_TYPE_8814A(Adapter)) + phy_set_tx_power_index_by_rate_section(Adapter, path, channel, VHT_1SSMCS0_1SSMCS9); + + if (pHalData->tx_nss >= 2) { + phy_set_tx_power_index_by_rate_section(Adapter, path, channel, HT_MCS8_MCS15); + + if (IS_HARDWARE_TYPE_JAGUAR(Adapter) || IS_HARDWARE_TYPE_8814A(Adapter)) + phy_set_tx_power_index_by_rate_section(Adapter, path, channel, VHT_2SSMCS0_2SSMCS9); + + if (IS_HARDWARE_TYPE_8814A(Adapter)) { + phy_set_tx_power_index_by_rate_section(Adapter, path, channel, HT_MCS16_MCS23); + phy_set_tx_power_index_by_rate_section(Adapter, path, channel, VHT_3SSMCS0_3SSMCS9); + } + } + } + } +} + +#if CONFIG_TXPWR_LIMIT +const char *const _txpwr_lmt_rs_str[] = { + "CCK", + "OFDM", + "HT", + "VHT", + "UNKNOWN", +}; + +static s8 +phy_GetChannelIndexOfTxPowerLimit( + u8 Band, + u8 Channel +) +{ + s8 channelIndex = -1; + u8 i = 0; + + if (Band == BAND_ON_2_4G) + channelIndex = Channel - 1; + else if (Band == BAND_ON_5G) { + for (i = 0; i < CENTER_CH_5G_ALL_NUM; ++i) { + if (center_ch_5g_all[i] == Channel) + channelIndex = i; + } + } else + RTW_PRINT("Invalid Band %d in %s\n", Band, __func__); + + if (channelIndex == -1) + RTW_PRINT("Invalid Channel %d of Band %d in %s\n", Channel, Band, __func__); + + return channelIndex; +} + +static s8 phy_txpwr_ww_lmt_value(_adapter *adapter) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + + if (hal_spec->txgi_max == 63) + return -63; + else if (hal_spec->txgi_max == 127) + return -128; + + rtw_warn_on(1); + return -128; +} + +/* +* return txpwr limit in unit of TX Gain Index +* hsl_spec->txgi_max is returned when NO limit +*/ +s8 phy_get_txpwr_lmt( + PADAPTER Adapter, + const char *regd_name, + BAND_TYPE Band, + enum channel_width bw, + u8 tlrs, + u8 ntx_idx, + u8 cch, + u8 lock +) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(Adapter); + struct rf_ctl_t *rfctl = adapter_to_rfctl(Adapter); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(Adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(Adapter); + struct txpwr_lmt_ent *ent = NULL; + _irqL irqL; + _list *cur, *head; + s8 ch_idx; + u8 is_ww_regd = 0; + s8 ww_lmt_val = phy_txpwr_ww_lmt_value(Adapter); + s8 lmt = hal_spec->txgi_max; + + if ((Adapter->registrypriv.RegEnableTxPowerLimit == 2 && hal_data->EEPROMRegulatory != 1) || + Adapter->registrypriv.RegEnableTxPowerLimit == 0) + goto exit; + + if (Band != BAND_ON_2_4G && Band != BAND_ON_5G) { + RTW_ERR("%s invalid band:%u\n", __func__, Band); + rtw_warn_on(1); + goto exit; + } + + if (Band == BAND_ON_5G && tlrs == TXPWR_LMT_RS_CCK) { + RTW_ERR("5G has no CCK\n"); + goto exit; + } + + if (lock) + _enter_critical_mutex(&rfctl->txpwr_lmt_mutex, &irqL); + + if (!regd_name) /* no regd_name specified, use currnet */ + regd_name = rfctl->regd_name; + + if (rfctl->txpwr_regd_num == 0 + || strcmp(regd_name, regd_str(TXPWR_LMT_NONE)) == 0) + goto release_lock; + + if (strcmp(regd_name, regd_str(TXPWR_LMT_WW)) == 0) + is_ww_regd = 1; + + if (!is_ww_regd) { + ent = _rtw_txpwr_lmt_get_by_name(rfctl, regd_name); + if (!ent) + goto release_lock; + } + + ch_idx = phy_GetChannelIndexOfTxPowerLimit(Band, cch); + if (ch_idx == -1) + goto release_lock; + + if (Band == BAND_ON_2_4G) { + if (!is_ww_regd) { + lmt = ent->lmt_2g[bw][tlrs][ch_idx][ntx_idx]; + if (lmt != ww_lmt_val) + goto release_lock; + } + + /* search for min value for WW regd or WW limit */ + lmt = hal_spec->txgi_max; + head = &rfctl->txpwr_lmt_list; + cur = get_next(head); + while ((rtw_end_of_queue_search(head, cur)) == _FALSE) { + ent = LIST_CONTAINOR(cur, struct txpwr_lmt_ent, list); + cur = get_next(cur); + if (ent->lmt_2g[bw][tlrs][ch_idx][ntx_idx] != ww_lmt_val) + lmt = rtw_min(lmt, ent->lmt_2g[bw][tlrs][ch_idx][ntx_idx]); + } + } + #if CONFIG_IEEE80211_BAND_5GHZ + else if (Band == BAND_ON_5G) { + if (!is_ww_regd) { + lmt = ent->lmt_5g[bw][tlrs - 1][ch_idx][ntx_idx]; + if (lmt != ww_lmt_val) + goto release_lock; + } + + /* search for min value for WW regd or WW limit */ + lmt = hal_spec->txgi_max; + head = &rfctl->txpwr_lmt_list; + cur = get_next(head); + while ((rtw_end_of_queue_search(head, cur)) == _FALSE) { + ent = LIST_CONTAINOR(cur, struct txpwr_lmt_ent, list); + cur = get_next(cur); + if (ent->lmt_5g[bw][tlrs - 1][ch_idx][ntx_idx] != ww_lmt_val) + lmt = rtw_min(lmt, ent->lmt_5g[bw][tlrs - 1][ch_idx][ntx_idx]); + } + } + #endif + +release_lock: + if (lock) + _exit_critical_mutex(&rfctl->txpwr_lmt_mutex, &irqL); + +exit: + return lmt; +} + +/* +* return txpwr limit diff value to target of its rate section in unit of TX Gain Index +* hal_spec->txgi_max is returned when NO limit +*/ +inline s8 phy_get_txpwr_lmt_diff(_adapter *adapter + , const char *regd_name + , BAND_TYPE band, enum channel_width bw + , u8 rfpath, u8 rs, u8 tlrs, u8 ntx_idx, u8 cch, u8 lock +) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + s8 lmt = phy_get_txpwr_lmt(adapter, regd_name, band, bw, tlrs, ntx_idx, cch, lock); + + if (lmt != hal_spec->txgi_max) { + /* return diff value */ + lmt = lmt - phy_get_target_txpwr(adapter, band, rfpath, rs); + } + + return lmt; +} + +/* +* May search for secondary channels for max/min limit +* @opch: used to specify operating channel position to get +* cch of every bandwidths which differ from current hal_data.cch20, 40, 80... +* +* return txpwr limit in unit of TX Gain Index +* hsl_spec->txgi_max is returned when NO limit +*/ +s8 phy_get_txpwr_lmt_sub_chs(_adapter *adapter + , const char *regd_name + , BAND_TYPE band, enum channel_width bw + , u8 rfpath, u8 rate, u8 ntx_idx, u8 cch, u8 opch, bool reg_max) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + BOOLEAN no_sc = _FALSE; + u8 cch_20 = hal_data->cch_20, cch_40 = hal_data->cch_40, cch_80 = hal_data->cch_80; + s8 tlrs = -1; + s8 lmt = hal_spec->txgi_max; + u8 tmp_cch = 0; + u8 tmp_bw; + u8 bw_bmp = 0; + s8 final_lmt = reg_max ? 0 : hal_spec->txgi_max; + u8 final_bw = CHANNEL_WIDTH_MAX, final_cch = cch; + _irqL irqL; + + if ((adapter->registrypriv.RegEnableTxPowerLimit == 2 && hal_data->EEPROMRegulatory != 1) || + adapter->registrypriv.RegEnableTxPowerLimit == 0 + ) { + final_lmt = hal_spec->txgi_max; + goto exit; + } + +#ifdef CONFIG_MP_INCLUDED + /* MP mode channel don't use secondary channel */ + if (rtw_mp_mode_check(adapter) == _TRUE) + no_sc = _TRUE; +#endif + if (IS_CCK_RATE(rate)) + tlrs = TXPWR_LMT_RS_CCK; + else if (IS_OFDM_RATE(rate)) + tlrs = TXPWR_LMT_RS_OFDM; + else if (IS_HT_RATE(rate)) + tlrs = TXPWR_LMT_RS_HT; + else if (IS_VHT_RATE(rate)) + tlrs = TXPWR_LMT_RS_VHT; + else { + RTW_ERR("%s invalid rate 0x%x\n", __func__, rate); + rtw_warn_on(1); + goto exit; + } + + if (no_sc == _TRUE) { + /* use the input center channel and bandwidth directly */ + tmp_cch = cch; + bw_bmp = ch_width_to_bw_cap(bw); + } else { + /* decide center channel of each bandwidth */ + if (opch != 0) { + cch_80 = bw == CHANNEL_WIDTH_80 ? cch : 0; + cch_40 = bw == CHANNEL_WIDTH_40 ? cch : 0; + cch_20 = bw == CHANNEL_WIDTH_20 ? cch : 0; + if (cch_80 != 0) + cch_40 = rtw_get_scch_by_cch_opch(cch_80, CHANNEL_WIDTH_80, opch); + if (cch_40 != 0) + cch_20 = rtw_get_scch_by_cch_opch(cch_40, CHANNEL_WIDTH_40, opch); + } + + /* + * reg_max: + * get valid full bandwidth bmp up to @bw + * + * !reg_max: + * find the possible tx bandwidth bmp for this rate + * if no possible tx bandwidth bmp, select valid bandwidth bmp up to @bw + */ + if (tlrs == TXPWR_LMT_RS_CCK || tlrs == TXPWR_LMT_RS_OFDM) + bw_bmp = BW_CAP_20M; /* CCK, OFDM only BW 20M */ + else if (tlrs == TXPWR_LMT_RS_HT) { + if (reg_max) + bw_bmp = ch_width_to_bw_cap(bw > CHANNEL_WIDTH_40 ? CHANNEL_WIDTH_40 + 1 : bw + 1) - 1; + else { + bw_bmp = rtw_get_tx_bw_bmp_of_ht_rate(dvobj, rate, bw); + if (bw_bmp == 0) + bw_bmp = ch_width_to_bw_cap(bw > CHANNEL_WIDTH_40 ? CHANNEL_WIDTH_40 : bw); + } + } else if (tlrs == TXPWR_LMT_RS_VHT) { + if (reg_max) + bw_bmp = ch_width_to_bw_cap(bw > CHANNEL_WIDTH_160 ? CHANNEL_WIDTH_160 + 1 : bw + 1) - 1; + else { + bw_bmp = rtw_get_tx_bw_bmp_of_vht_rate(dvobj, rate, bw); + if (bw_bmp == 0) + bw_bmp = ch_width_to_bw_cap(bw > CHANNEL_WIDTH_160 ? CHANNEL_WIDTH_160 : bw); + } + } else + rtw_warn_on(1); + } + + if (bw_bmp == 0) + goto exit; + + _enter_critical_mutex(&rfctl->txpwr_lmt_mutex, &irqL); + + /* loop for each possible tx bandwidth to find final limit */ + for (tmp_bw = CHANNEL_WIDTH_20; tmp_bw <= bw; tmp_bw++) { + if (!(ch_width_to_bw_cap(tmp_bw) & bw_bmp)) + continue; + + if (no_sc == _FALSE) { + /* get center channel for each bandwidth */ + if (tmp_bw == CHANNEL_WIDTH_20) + tmp_cch = cch_20; + else if (tmp_bw == CHANNEL_WIDTH_40) + tmp_cch = cch_40; + else if (tmp_bw == CHANNEL_WIDTH_80) + tmp_cch = cch_80; + else { + tmp_cch = 0; + rtw_warn_on(1); + } + } + + lmt = phy_get_txpwr_lmt(adapter, regd_name, band, tmp_bw, tlrs, ntx_idx, tmp_cch, 0); + + if (final_lmt > lmt) { + if (reg_max) + continue; + } else if (final_lmt < lmt) { + if (!reg_max) + continue; + } else { /* equal */ + if (final_bw == bw) + continue; + } + + final_lmt = lmt; + final_cch = tmp_cch; + final_bw = tmp_bw; + } + + _exit_critical_mutex(&rfctl->txpwr_lmt_mutex, &irqL); + + if (0) { + if (final_bw != bw && (IS_HT_RATE(rate) || IS_VHT_RATE(rate))) + RTW_INFO("%s final_lmt: %s ch%u -> %s ch%u\n" + , MGN_RATE_STR(rate) + , ch_width_str(bw), cch + , ch_width_str(final_bw), final_cch); + } + +exit: + return final_lmt; +} + +static void phy_txpwr_lmt_cck_ofdm_mt_chk(_adapter *adapter) +{ + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + struct txpwr_lmt_ent *ent; + _list *cur, *head; + u8 channel, tlrs, ntx_idx; + + rfctl->txpwr_lmt_2g_cck_ofdm_state = 0; +#if CONFIG_IEEE80211_BAND_5GHZ + rfctl->txpwr_lmt_5g_cck_ofdm_state = 0; +#endif + + head = &rfctl->txpwr_lmt_list; + cur = get_next(head); + + while ((rtw_end_of_queue_search(head, cur)) == _FALSE) { + ent = LIST_CONTAINOR(cur, struct txpwr_lmt_ent, list); + cur = get_next(cur); + + /* check 2G CCK, OFDM state*/ + for (tlrs = TXPWR_LMT_RS_CCK; tlrs <= TXPWR_LMT_RS_OFDM; tlrs++) { + for (ntx_idx = RF_1TX; ntx_idx < MAX_TX_COUNT; ntx_idx++) { + for (channel = 0; channel < CENTER_CH_2G_NUM; ++channel) { + if (ent->lmt_2g[CHANNEL_WIDTH_20][tlrs][channel][ntx_idx] != hal_spec->txgi_max) { + if (tlrs == TXPWR_LMT_RS_CCK) + rfctl->txpwr_lmt_2g_cck_ofdm_state |= TXPWR_LMT_HAS_CCK_1T << ntx_idx; + else + rfctl->txpwr_lmt_2g_cck_ofdm_state |= TXPWR_LMT_HAS_OFDM_1T << ntx_idx; + break; + } + } + } + } + + /* if 2G OFDM multi-TX is not defined, reference HT20 */ + for (channel = 0; channel < CENTER_CH_2G_NUM; ++channel) { + for (ntx_idx = RF_2TX; ntx_idx < MAX_TX_COUNT; ntx_idx++) { + if (rfctl->txpwr_lmt_2g_cck_ofdm_state & (TXPWR_LMT_HAS_OFDM_1T << ntx_idx)) + continue; + ent->lmt_2g[CHANNEL_WIDTH_20][TXPWR_LMT_RS_OFDM][channel][ntx_idx] = + ent->lmt_2g[CHANNEL_WIDTH_20][TXPWR_LMT_RS_HT][channel][ntx_idx]; + } + } + +#if CONFIG_IEEE80211_BAND_5GHZ + /* check 5G OFDM state*/ + for (ntx_idx = RF_1TX; ntx_idx < MAX_TX_COUNT; ntx_idx++) { + for (channel = 0; channel < CENTER_CH_5G_ALL_NUM; ++channel) { + if (ent->lmt_5g[CHANNEL_WIDTH_20][TXPWR_LMT_RS_OFDM - 1][channel][ntx_idx] != hal_spec->txgi_max) { + rfctl->txpwr_lmt_5g_cck_ofdm_state |= TXPWR_LMT_HAS_OFDM_1T << ntx_idx; + break; + } + } + } + + for (channel = 0; channel < CENTER_CH_5G_ALL_NUM; ++channel) { + for (ntx_idx = RF_2TX; ntx_idx < MAX_TX_COUNT; ntx_idx++) { + if (rfctl->txpwr_lmt_5g_cck_ofdm_state & (TXPWR_LMT_HAS_OFDM_1T << ntx_idx)) + continue; + /* if 5G OFDM multi-TX is not defined, reference HT20 */ + ent->lmt_5g[CHANNEL_WIDTH_20][TXPWR_LMT_RS_OFDM - 1][channel][ntx_idx] = + ent->lmt_5g[CHANNEL_WIDTH_20][TXPWR_LMT_RS_HT - 1][channel][ntx_idx]; + } + } +#endif /* CONFIG_IEEE80211_BAND_5GHZ */ + } +} + +#if CONFIG_IEEE80211_BAND_5GHZ +static void phy_txpwr_lmt_cross_ref_ht_vht(_adapter *adapter) +{ + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + struct txpwr_lmt_ent *ent; + _list *cur, *head; + u8 bw, channel, tlrs, ref_tlrs, ntx_idx; + int ht_ref_vht_5g_20_40 = 0; + int vht_ref_ht_5g_20_40 = 0; + int ht_has_ref_5g_20_40 = 0; + int vht_has_ref_5g_20_40 = 0; + + rfctl->txpwr_lmt_5g_20_40_ref = 0; + + head = &rfctl->txpwr_lmt_list; + cur = get_next(head); + + while ((rtw_end_of_queue_search(head, cur)) == _FALSE) { + ent = LIST_CONTAINOR(cur, struct txpwr_lmt_ent, list); + cur = get_next(cur); + + for (bw = 0; bw < MAX_5G_BANDWIDTH_NUM; ++bw) { + + for (channel = 0; channel < CENTER_CH_5G_ALL_NUM; ++channel) { + + for (tlrs = TXPWR_LMT_RS_HT; tlrs < TXPWR_LMT_RS_NUM; ++tlrs) { + + /* 5G 20M 40M VHT and HT can cross reference */ + if (bw == CHANNEL_WIDTH_20 || bw == CHANNEL_WIDTH_40) { + if (tlrs == TXPWR_LMT_RS_HT) + ref_tlrs = TXPWR_LMT_RS_VHT; + else if (tlrs == TXPWR_LMT_RS_VHT) + ref_tlrs = TXPWR_LMT_RS_HT; + else + continue; + + for (ntx_idx = RF_1TX; ntx_idx < MAX_TX_COUNT; ntx_idx++) { + + if (ent->lmt_5g[bw][ref_tlrs - 1][channel][ntx_idx] == hal_spec->txgi_max) + continue; + + if (tlrs == TXPWR_LMT_RS_HT) + ht_has_ref_5g_20_40++; + else if (tlrs == TXPWR_LMT_RS_VHT) + vht_has_ref_5g_20_40++; + else + continue; + + if (ent->lmt_5g[bw][tlrs - 1][channel][ntx_idx] != hal_spec->txgi_max) + continue; + + if (tlrs == TXPWR_LMT_RS_HT && ref_tlrs == TXPWR_LMT_RS_VHT) + ht_ref_vht_5g_20_40++; + else if (tlrs == TXPWR_LMT_RS_VHT && ref_tlrs == TXPWR_LMT_RS_HT) + vht_ref_ht_5g_20_40++; + + if (0) + RTW_INFO("reg:%s, bw:%u, ch:%u, %s-%uT ref %s-%uT\n" + , ent->regd_name, bw, channel + , txpwr_lmt_rs_str(tlrs), ntx_idx + 1 + , txpwr_lmt_rs_str(ref_tlrs), ntx_idx + 1); + + ent->lmt_5g[bw][tlrs - 1][channel][ntx_idx] = + ent->lmt_5g[bw][ref_tlrs - 1][channel][ntx_idx]; + } + } + + } + } + } + } + + if (0) { + RTW_INFO("ht_ref_vht_5g_20_40:%d, ht_has_ref_5g_20_40:%d\n", ht_ref_vht_5g_20_40, ht_has_ref_5g_20_40); + RTW_INFO("vht_ref_ht_5g_20_40:%d, vht_has_ref_5g_20_40:%d\n", vht_ref_ht_5g_20_40, vht_has_ref_5g_20_40); + } + + /* 5G 20M&40M HT all come from VHT*/ + if (ht_ref_vht_5g_20_40 && ht_has_ref_5g_20_40 == ht_ref_vht_5g_20_40) + rfctl->txpwr_lmt_5g_20_40_ref |= TXPWR_LMT_REF_HT_FROM_VHT; + + /* 5G 20M&40M VHT all come from HT*/ + if (vht_ref_ht_5g_20_40 && vht_has_ref_5g_20_40 == vht_ref_ht_5g_20_40) + rfctl->txpwr_lmt_5g_20_40_ref |= TXPWR_LMT_REF_VHT_FROM_HT; +} +#endif /* CONFIG_IEEE80211_BAND_5GHZ */ + +#ifndef DBG_TXPWR_LMT_BAND_CHK +#define DBG_TXPWR_LMT_BAND_CHK 0 +#endif + +#if DBG_TXPWR_LMT_BAND_CHK +/* check if larger bandwidth limit is less than smaller bandwidth for HT & VHT rate */ +void phy_txpwr_limit_bandwidth_chk(_adapter *adapter) +{ + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u8 band, bw, path, tlrs, ntx_idx, cch, offset, scch; + u8 ch_num, n, i; + + for (band = BAND_ON_2_4G; band <= BAND_ON_5G; band++) { + if (!hal_is_band_support(adapter, band)) + continue; + + for (bw = CHANNEL_WIDTH_40; bw <= CHANNEL_WIDTH_80; bw++) { + if (bw >= CHANNEL_WIDTH_160) + continue; + if (band == BAND_ON_2_4G && bw >= CHANNEL_WIDTH_80) + continue; + + if (band == BAND_ON_2_4G) + ch_num = center_chs_2g_num(bw); + else + ch_num = center_chs_5g_num(bw); + + if (ch_num == 0) { + rtw_warn_on(1); + break; + } + + for (tlrs = TXPWR_LMT_RS_HT; tlrs < TXPWR_LMT_RS_NUM; tlrs++) { + + if (band == BAND_ON_2_4G && tlrs == TXPWR_LMT_RS_VHT) + continue; + if (band == BAND_ON_5G && tlrs == TXPWR_LMT_RS_CCK) + continue; + if (bw > CHANNEL_WIDTH_20 && (tlrs == TXPWR_LMT_RS_CCK || tlrs == TXPWR_LMT_RS_OFDM)) + continue; + if (bw > CHANNEL_WIDTH_40 && tlrs == TXPWR_LMT_RS_HT) + continue; + if (tlrs == TXPWR_LMT_RS_VHT && !IS_HARDWARE_TYPE_JAGUAR_ALL(adapter)) + continue; + + for (ntx_idx = RF_1TX; ntx_idx < MAX_TX_COUNT; ntx_idx++) { + struct txpwr_lmt_ent *ent; + _list *cur, *head; + + if (ntx_idx + 1 > hal_data->max_tx_cnt) + continue; + + /* bypass CCK multi-TX is not defined */ + if (tlrs == TXPWR_LMT_RS_CCK && ntx_idx > RF_1TX) { + if (band == BAND_ON_2_4G + && !(rfctl->txpwr_lmt_2g_cck_ofdm_state & (TXPWR_LMT_HAS_CCK_1T << ntx_idx))) + continue; + } + + /* bypass OFDM multi-TX is not defined */ + if (tlrs == TXPWR_LMT_RS_OFDM && ntx_idx > RF_1TX) { + if (band == BAND_ON_2_4G + && !(rfctl->txpwr_lmt_2g_cck_ofdm_state & (TXPWR_LMT_HAS_OFDM_1T << ntx_idx))) + continue; + #if CONFIG_IEEE80211_BAND_5GHZ + if (band == BAND_ON_5G + && !(rfctl->txpwr_lmt_5g_cck_ofdm_state & (TXPWR_LMT_HAS_OFDM_1T << ntx_idx))) + continue; + #endif + } + + /* bypass 5G 20M, 40M pure reference */ + #if CONFIG_IEEE80211_BAND_5GHZ + if (band == BAND_ON_5G && (bw == CHANNEL_WIDTH_20 || bw == CHANNEL_WIDTH_40)) { + if (rfctl->txpwr_lmt_5g_20_40_ref == TXPWR_LMT_REF_HT_FROM_VHT) { + if (tlrs == TXPWR_LMT_RS_HT) + continue; + } else if (rfctl->txpwr_lmt_5g_20_40_ref == TXPWR_LMT_REF_VHT_FROM_HT) { + if (tlrs == TXPWR_LMT_RS_VHT && bw <= CHANNEL_WIDTH_40) + continue; + } + } + #endif + + for (n = 0; n < ch_num; n++) { + u8 cch_by_bw[3]; + u8 offset_by_bw; /* bitmap, 0 for lower, 1 for upper */ + u8 bw_pos; + s8 lmt[3]; + + if (band == BAND_ON_2_4G) + cch = center_chs_2g(bw, n); + else + cch = center_chs_5g(bw, n); + + if (cch == 0) { + rtw_warn_on(1); + break; + } + + _rtw_memset(cch_by_bw, 0, 3); + cch_by_bw[bw] = cch; + offset_by_bw = 0x01; + + do { + for (bw_pos = bw; bw_pos >= CHANNEL_WIDTH_40; bw_pos--) + cch_by_bw[bw_pos - 1] = rtw_get_scch_by_cch_offset(cch_by_bw[bw_pos], bw_pos, offset_by_bw & BIT(bw_pos) ? HAL_PRIME_CHNL_OFFSET_UPPER : HAL_PRIME_CHNL_OFFSET_LOWER); + + head = &rfctl->txpwr_lmt_list; + cur = get_next(head); + while ((rtw_end_of_queue_search(head, cur)) == _FALSE) { + ent = LIST_CONTAINOR(cur, struct txpwr_lmt_ent, list); + cur = get_next(cur); + + for (bw_pos = bw; bw_pos < CHANNEL_WIDTH_160; bw_pos--) + lmt[bw_pos] = phy_get_txpwr_lmt(adapter, ent->regd_name, band, bw_pos, tlrs, ntx_idx, cch_by_bw[bw_pos], 0); + + for (bw_pos = bw; bw_pos > CHANNEL_WIDTH_20; bw_pos--) + if (lmt[bw_pos] > lmt[bw_pos - 1]) + break; + if (bw_pos == CHANNEL_WIDTH_20) + continue; + + RTW_PRINT_SEL(RTW_DBGDUMP, "[%s][%s][%s][%uT][%-4s] cch:" + , band_str(band) + , ch_width_str(bw) + , txpwr_lmt_rs_str(tlrs) + , ntx_idx + 1 + , ent->regd_name + ); + for (bw_pos = bw; bw_pos < CHANNEL_WIDTH_160; bw_pos--) + _RTW_PRINT_SEL(RTW_DBGDUMP, "%03u ", cch_by_bw[bw_pos]); + _RTW_PRINT_SEL(RTW_DBGDUMP, "limit:"); + for (bw_pos = bw; bw_pos < CHANNEL_WIDTH_160; bw_pos--) { + if (lmt[bw_pos] == hal_spec->txgi_max) + _RTW_PRINT_SEL(RTW_DBGDUMP, "N/A "); + else if (lmt[bw_pos] > -hal_spec->txgi_pdbm && lmt[bw_pos] < 0) /* -1 < value < 0 */ + _RTW_PRINT_SEL(RTW_DBGDUMP, "-0.%d", (rtw_abs(lmt[bw_pos]) % hal_spec->txgi_pdbm) * 100 / hal_spec->txgi_pdbm); + else if (lmt[bw_pos] % hal_spec->txgi_pdbm) + _RTW_PRINT_SEL(RTW_DBGDUMP, "%2d.%d ", lmt[bw_pos] / hal_spec->txgi_pdbm, (rtw_abs(lmt[bw_pos]) % hal_spec->txgi_pdbm) * 100 / hal_spec->txgi_pdbm); + else + _RTW_PRINT_SEL(RTW_DBGDUMP, "%2d ", lmt[bw_pos] / hal_spec->txgi_pdbm); + } + _RTW_PRINT_SEL(RTW_DBGDUMP, "\n"); + } + for (bw_pos = bw; bw_pos < CHANNEL_WIDTH_160; bw_pos--) + lmt[bw_pos] = phy_get_txpwr_lmt(adapter, regd_str(TXPWR_LMT_WW), band, bw_pos, tlrs, ntx_idx, cch_by_bw[bw_pos], 0); + + for (bw_pos = bw; bw_pos > CHANNEL_WIDTH_20; bw_pos--) + if (lmt[bw_pos] > lmt[bw_pos - 1]) + break; + if (bw_pos != CHANNEL_WIDTH_20) { + RTW_PRINT_SEL(RTW_DBGDUMP, "[%s][%s][%s][%uT][%-4s] cch:" + , band_str(band) + , ch_width_str(bw) + , txpwr_lmt_rs_str(tlrs) + , ntx_idx + 1 + , regd_str(TXPWR_LMT_WW) + ); + for (bw_pos = bw; bw_pos < CHANNEL_WIDTH_160; bw_pos--) + _RTW_PRINT_SEL(RTW_DBGDUMP, "%03u ", cch_by_bw[bw_pos]); + _RTW_PRINT_SEL(RTW_DBGDUMP, "limit:"); + for (bw_pos = bw; bw_pos < CHANNEL_WIDTH_160; bw_pos--) { + if (lmt[bw_pos] == hal_spec->txgi_max) + _RTW_PRINT_SEL(RTW_DBGDUMP, "N/A "); + else if (lmt[bw_pos] > -hal_spec->txgi_pdbm && lmt[bw_pos] < 0) /* -1 < value < 0 */ + _RTW_PRINT_SEL(RTW_DBGDUMP, "-0.%d", (rtw_abs(lmt[bw_pos]) % hal_spec->txgi_pdbm) * 100 / hal_spec->txgi_pdbm); + else if (lmt[bw_pos] % hal_spec->txgi_pdbm) + _RTW_PRINT_SEL(RTW_DBGDUMP, "%2d.%d ", lmt[bw_pos] / hal_spec->txgi_pdbm, (rtw_abs(lmt[bw_pos]) % hal_spec->txgi_pdbm) * 100 / hal_spec->txgi_pdbm); + else + _RTW_PRINT_SEL(RTW_DBGDUMP, "%2d ", lmt[bw_pos] / hal_spec->txgi_pdbm); + } + _RTW_PRINT_SEL(RTW_DBGDUMP, "\n"); + } + + offset_by_bw += 2; + if (offset_by_bw & BIT(bw + 1)) + break; + } while (1); /* loop for all ch combinations */ + } /* loop for center channels */ + } /* loop fo each ntx_idx */ + } /* loop for tlrs */ + } /* loop for bandwidth */ + } /* loop for band */ +} +#endif /* DBG_TXPWR_LMT_BAND_CHK */ + +static void phy_txpwr_lmt_post_hdl(_adapter *adapter) +{ + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + _irqL irqL; + + _enter_critical_mutex(&rfctl->txpwr_lmt_mutex, &irqL); + +#if CONFIG_IEEE80211_BAND_5GHZ + if (IS_HARDWARE_TYPE_JAGUAR_ALL(adapter)) + phy_txpwr_lmt_cross_ref_ht_vht(adapter); +#endif + phy_txpwr_lmt_cck_ofdm_mt_chk(adapter); + +#if DBG_TXPWR_LMT_BAND_CHK + phy_txpwr_limit_bandwidth_chk(adapter); +#endif + + _exit_critical_mutex(&rfctl->txpwr_lmt_mutex, &irqL); +} + +BOOLEAN +GetS1ByteIntegerFromStringInDecimal( + char *str, + s8 *val +) +{ + u8 negative = 0; + u16 i = 0; + + *val = 0; + + while (str[i] != '\0') { + if (i == 0 && (str[i] == '+' || str[i] == '-')) { + if (str[i] == '-') + negative = 1; + } else if (str[i] >= '0' && str[i] <= '9') { + *val *= 10; + *val += (str[i] - '0'); + } else + return _FALSE; + ++i; + } + + if (negative) + *val = -*val; + + return _TRUE; +} +#endif /* CONFIG_TXPWR_LIMIT */ + +/* +* phy_set_tx_power_limit - Parsing TX power limit from phydm array, called by odm_ConfigBB_TXPWR_LMT_XXX in phydm +*/ +void +phy_set_tx_power_limit( + struct dm_struct *pDM_Odm, + u8 *Regulation, + u8 *Band, + u8 *Bandwidth, + u8 *RateSection, + u8 *ntx, + u8 *Channel, + u8 *PowerLimit +) +{ +#if CONFIG_TXPWR_LIMIT + PADAPTER Adapter = pDM_Odm->adapter; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(Adapter); + u8 band = 0, bandwidth = 0, tlrs = 0, channel; + u8 ntx_idx; + s8 powerLimit = 0, prevPowerLimit, channelIndex; + s8 ww_lmt_val = phy_txpwr_ww_lmt_value(Adapter); + + if (0) + RTW_INFO("Index of power limit table [regulation %s][band %s][bw %s][rate section %s][ntx %s][chnl %s][val %s]\n" + , Regulation, Band, Bandwidth, RateSection, ntx, Channel, PowerLimit); + + if (GetU1ByteIntegerFromStringInDecimal((char *)Channel, &channel) == _FALSE + || GetS1ByteIntegerFromStringInDecimal((char *)PowerLimit, &powerLimit) == _FALSE + ) { + RTW_PRINT("Illegal index of power limit table [ch %s][val %s]\n", Channel, PowerLimit); + return; + } + + if (powerLimit != ww_lmt_val) { + if (powerLimit < -hal_spec->txgi_max || powerLimit > hal_spec->txgi_max) + RTW_PRINT("Illegal power limit value [ch %s][val %s]\n", Channel, PowerLimit); + + if (powerLimit > hal_spec->txgi_max) + powerLimit = hal_spec->txgi_max; + else if (powerLimit < -hal_spec->txgi_max) + powerLimit = ww_lmt_val + 1; + } + + if (strncmp(RateSection, "CCK", 3) == 0) + tlrs = TXPWR_LMT_RS_CCK; + else if (strncmp(RateSection, "OFDM", 4) == 0) + tlrs = TXPWR_LMT_RS_OFDM; + else if (strncmp(RateSection, "HT", 2) == 0) + tlrs = TXPWR_LMT_RS_HT; + else if (strncmp(RateSection, "VHT", 3) == 0) + tlrs = TXPWR_LMT_RS_VHT; + else { + RTW_PRINT("Wrong rate section:%s\n", RateSection); + return; + } + + if (strncmp(ntx, "1T", 2) == 0) + ntx_idx = RF_1TX; + else if (strncmp(ntx, "2T", 2) == 0) + ntx_idx = RF_2TX; + else if (strncmp(ntx, "3T", 2) == 0) + ntx_idx = RF_3TX; + else if (strncmp(ntx, "4T", 2) == 0) + ntx_idx = RF_4TX; + else { + RTW_PRINT("Wrong tx num:%s\n", ntx); + return; + } + + if (strncmp(Bandwidth, "20M", 3) == 0) + bandwidth = CHANNEL_WIDTH_20; + else if (strncmp(Bandwidth, "40M", 3) == 0) + bandwidth = CHANNEL_WIDTH_40; + else if (strncmp(Bandwidth, "80M", 3) == 0) + bandwidth = CHANNEL_WIDTH_80; + else if (strncmp(Bandwidth, "160M", 4) == 0) + bandwidth = CHANNEL_WIDTH_160; + else { + RTW_PRINT("unknown bandwidth: %s\n", Bandwidth); + return; + } + + if (strncmp(Band, "2.4G", 4) == 0) { + band = BAND_ON_2_4G; + channelIndex = phy_GetChannelIndexOfTxPowerLimit(BAND_ON_2_4G, channel); + + if (channelIndex == -1) { + RTW_PRINT("unsupported channel: %d at 2.4G\n", channel); + return; + } + + if (bandwidth >= MAX_2_4G_BANDWIDTH_NUM) { + RTW_PRINT("unsupported bandwidth: %s at 2.4G\n", Bandwidth); + return; + } + + rtw_txpwr_lmt_add(adapter_to_rfctl(Adapter), Regulation, band, bandwidth, tlrs, ntx_idx, channelIndex, powerLimit); + } +#if CONFIG_IEEE80211_BAND_5GHZ + else if (strncmp(Band, "5G", 2) == 0) { + band = BAND_ON_5G; + channelIndex = phy_GetChannelIndexOfTxPowerLimit(BAND_ON_5G, channel); + + if (channelIndex == -1) { + RTW_PRINT("unsupported channel: %d at 5G\n", channel); + return; + } + + rtw_txpwr_lmt_add(adapter_to_rfctl(Adapter), Regulation, band, bandwidth, tlrs, ntx_idx, channelIndex, powerLimit); + } +#endif + else { + RTW_PRINT("unknown/unsupported band:%s\n", Band); + return; + } +#endif +} + +void +phy_set_tx_power_limit_ex( + struct dm_struct *pDM_Odm, + u8 Regulation, + u8 Band, + u8 Bandwidth, + u8 RateSection, + u8 ntx, + u8 channel, + s8 powerLimit +) +{ +#if CONFIG_TXPWR_LIMIT + PADAPTER Adapter = pDM_Odm->adapter; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(Adapter); + u8 regd; + u8 band = 0, bandwidth = 0, tlrs = 0; + u8 ntx_idx; + s8 prevPowerLimit, channelIndex; + s8 ww_lmt_val = phy_txpwr_ww_lmt_value(Adapter); + + if (0) + RTW_INFO("Index of power limit table [regulation %d][band %d][bw %d][rate section %d][ntx %d][chnl %d][val %d]\n" + , Regulation, Band, Bandwidth, RateSection, ntx, channel, powerLimit); + + if (powerLimit != ww_lmt_val) { + if (powerLimit < -hal_spec->txgi_max || powerLimit > hal_spec->txgi_max) + RTW_PRINT("Illegal power limit value [ch %d][val %d]\n", channel, powerLimit); + + if (powerLimit > hal_spec->txgi_max) + powerLimit = hal_spec->txgi_max; + else if (powerLimit < -hal_spec->txgi_max) + powerLimit = ww_lmt_val + 1; + } + + switch (Regulation) { + case PW_LMT_REGU_FCC: + regd = TXPWR_LMT_FCC; + break; + case PW_LMT_REGU_ETSI: + regd = TXPWR_LMT_ETSI; + break; + case PW_LMT_REGU_MKK: + regd = TXPWR_LMT_MKK; + break; + case PW_LMT_REGU_IC: + regd = TXPWR_LMT_IC; + break; + case PW_LMT_REGU_KCC: + regd = TXPWR_LMT_KCC; + break; + case PW_LMT_REGU_ACMA: + regd = TXPWR_LMT_ACMA; + break; + case PW_LMT_REGU_CHILE: + regd = TXPWR_LMT_CHILE; + break; + case PW_LMT_REGU_UKRAINE: + regd = TXPWR_LMT_UKRAINE; + break; + case PW_LMT_REGU_MEXICO: + regd = TXPWR_LMT_MEXICO; + break; + case PW_LMT_REGU_CN: + regd = TXPWR_LMT_CN; + break; + case PW_LMT_REGU_WW13: + default: + RTW_PRINT("Wrong regulation:%d\n", Regulation); + return; + } + + switch (RateSection) { + case PW_LMT_RS_CCK: + tlrs = TXPWR_LMT_RS_CCK; + break; + case PW_LMT_RS_OFDM: + tlrs = TXPWR_LMT_RS_OFDM; + break; + case PW_LMT_RS_HT: + tlrs = TXPWR_LMT_RS_HT; + break; + case PW_LMT_RS_VHT: + tlrs = TXPWR_LMT_RS_VHT; + break; + default: + RTW_PRINT("Wrong rate section:%d\n", RateSection); + return; + } + + switch (ntx) { + case PW_LMT_PH_1T: + ntx_idx = RF_1TX; + break; + case PW_LMT_PH_2T: + ntx_idx = RF_2TX; + break; + case PW_LMT_PH_3T: + ntx_idx = RF_3TX; + break; + case PW_LMT_PH_4T: + ntx_idx = RF_4TX; + break; + default: + RTW_PRINT("Wrong tx num:%d\n", ntx); + return; + } + + switch (Bandwidth) { + case PW_LMT_BW_20M: + bandwidth = CHANNEL_WIDTH_20; + break; + case PW_LMT_BW_40M: + bandwidth = CHANNEL_WIDTH_40; + break; + case PW_LMT_BW_80M: + bandwidth = CHANNEL_WIDTH_80; + break; + case PW_LMT_BW_160M: + bandwidth = CHANNEL_WIDTH_160; + break; + default: + RTW_PRINT("unknown bandwidth: %d\n", Bandwidth); + return; + } + + if (Band == PW_LMT_BAND_2_4G) { + band = BAND_ON_2_4G; + channelIndex = phy_GetChannelIndexOfTxPowerLimit(BAND_ON_2_4G, channel); + + if (channelIndex == -1) { + RTW_PRINT("unsupported channel: %d at 2.4G\n", channel); + return; + } + + if (bandwidth >= MAX_2_4G_BANDWIDTH_NUM) { + RTW_PRINT("unsupported bandwidth: %s at 2.4G\n", ch_width_str(bandwidth)); + return; + } + + rtw_txpwr_lmt_add(adapter_to_rfctl(Adapter), regd_str(regd), band, bandwidth, tlrs, ntx_idx, channelIndex, powerLimit); + } +#if CONFIG_IEEE80211_BAND_5GHZ + else if (Band == PW_LMT_BAND_5G) { + band = BAND_ON_5G; + channelIndex = phy_GetChannelIndexOfTxPowerLimit(BAND_ON_5G, channel); + + if (channelIndex == -1) { + RTW_PRINT("unsupported channel: %d at 5G\n", channel); + return; + } + + rtw_txpwr_lmt_add(adapter_to_rfctl(Adapter), regd_str(regd), band, bandwidth, tlrs, ntx_idx, channelIndex, powerLimit); + } +#endif + else { + RTW_PRINT("unknown/unsupported band:%d\n", Band); + return; + } +#endif +} + +u8 phy_get_tx_power_index_ex(_adapter *adapter + , enum rf_path rfpath, RATE_SECTION rs, enum MGN_RATE rate + , enum channel_width bw, BAND_TYPE band, u8 cch, u8 opch) +{ + return rtw_hal_get_tx_power_index(adapter, rfpath, rs, rate, bw, band, cch, opch, NULL); +} + +u8 phy_get_tx_power_index( + PADAPTER pAdapter, + enum rf_path RFPath, + u8 Rate, + enum channel_width BandWidth, + u8 Channel +) +{ + RATE_SECTION rs = mgn_rate_to_rs(Rate); + BAND_TYPE band = Channel <= 14 ? BAND_ON_2_4G : BAND_ON_5G; + + return rtw_hal_get_tx_power_index(pAdapter, RFPath, rs, Rate, BandWidth, band, Channel, 0, NULL); +} + +void +PHY_SetTxPowerIndex( + PADAPTER pAdapter, + u32 PowerIndex, + enum rf_path RFPath, + u8 Rate +) +{ + rtw_hal_set_tx_power_index(pAdapter, PowerIndex, RFPath, Rate); +} + +void dump_tx_power_index_inline(void *sel, _adapter *adapter, u8 rfpath, enum channel_width bw, u8 cch, enum MGN_RATE rate, u8 pwr_idx, struct txpwr_idx_comp *tic) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + + if (tic->utarget == hal_spec->txgi_max) { + RTW_PRINT_SEL(sel, "TXPWR: [%c][%s]cch:%u, %s %uT, idx:%u(0x%02x) = base(%d) + min((byr(%d) + btc(%d) + extra(%d)), rlmt(%d), lmt(%d), ulmt(%d)) + tpc(%d) + tpt(%d) + dpd(%d)\n" + , rf_path_char(rfpath), ch_width_str(bw), cch + , MGN_RATE_STR(rate), tic->ntx_idx + 1 + , pwr_idx, pwr_idx, tic->base + , tic->by_rate, tic->btc, tic->extra, tic->rlimit, tic->limit, tic->ulimit + , tic->tpc + , tic->tpt, tic->dpd); + } else { + RTW_PRINT_SEL(sel, "TXPWR: [%c][%s]cch:%u, %s %uT, idx:%u(0x%02x) = base(%d) + min(utgt(%d), rlmt(%d), lmt(%d), ulmt(%d)) + tpc(%d) + tpt(%d) + dpd(%d)\n" + , rf_path_char(rfpath), ch_width_str(bw), cch + , MGN_RATE_STR(rate), tic->ntx_idx + 1 + , pwr_idx, pwr_idx, tic->base + , tic->utarget, tic->rlimit, tic->limit, tic->ulimit + , tic->tpc + , tic->tpt, tic->dpd); + } +} + +#ifdef CONFIG_PROC_DEBUG +void dump_tx_power_idx_value(void *sel, _adapter *adapter, u8 rfpath, enum MGN_RATE rate, u8 pwr_idx, struct txpwr_idx_comp *tic) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + char tmp_str[8]; + + txpwr_idx_get_dbm_str(tic->target, hal_spec->txgi_max, hal_spec->txgi_pdbm, 0, tmp_str, 8); + + if (tic->utarget == hal_spec->txgi_max) { + RTW_PRINT_SEL(sel, "%4c %9s %uT %s %3u(0x%02x)" + " %4d ((%4d %3d %5d) %4d %4d %4d) %3d %3d %3d\n" + , rf_path_char(rfpath), MGN_RATE_STR(rate), tic->ntx_idx + 1 + , tmp_str, pwr_idx, pwr_idx + , tic->base, tic->by_rate, tic->btc, tic->extra, tic->rlimit, tic->limit, tic->ulimit + , tic->tpc + , tic->tpt, tic->dpd); + } else { + RTW_PRINT_SEL(sel, "%4c %9s %uT %s %3u(0x%02x)" + " %4d (%4d %4d %4d %4d) %3d %3d %3d\n" + , rf_path_char(rfpath), MGN_RATE_STR(rate), tic->ntx_idx + 1 + , tmp_str, pwr_idx, pwr_idx + , tic->base, tic->utarget, tic->rlimit, tic->limit, tic->ulimit + , tic->tpc + , tic->tpt, tic->dpd); + } +} + +void dump_tx_power_idx_title(void *sel, _adapter *adapter, enum channel_width bw, u8 cch, u8 opch) +{ + u8 cch_20, cch_40, cch_80; + + cch_80 = bw == CHANNEL_WIDTH_80 ? cch : 0; + cch_40 = bw == CHANNEL_WIDTH_40 ? cch : 0; + cch_20 = bw == CHANNEL_WIDTH_20 ? cch : 0; + if (cch_80 != 0) + cch_40 = rtw_get_scch_by_cch_opch(cch_80, CHANNEL_WIDTH_80, opch); + if (cch_40 != 0) + cch_20 = rtw_get_scch_by_cch_opch(cch_40, CHANNEL_WIDTH_40, opch); + + RTW_PRINT_SEL(sel, "%s", ch_width_str(bw)); + if (bw >= CHANNEL_WIDTH_80) + _RTW_PRINT_SEL(sel, ", cch80:%u", cch_80); + if (bw >= CHANNEL_WIDTH_40) + _RTW_PRINT_SEL(sel, ", cch40:%u", cch_40); + _RTW_PRINT_SEL(sel, ", cch20:%u\n", cch_20); + + if (!phy_is_txpwr_user_target_specified(adapter)) { + RTW_PRINT_SEL(sel, "%-4s %-9s %2s %-6s %-3s%6s" + " = %-4s + min((%-4s + %-3s + %-5s), %-4s, %-4s, %-4s) + %-3s + %-3s + %-3s\n" + , "path", "rate", "", "dBm", "idx", "" + , "base", "byr", "btc", "extra", "rlmt", "lmt", "ulmt" + , "tpc" + , "tpt", "dpd"); + } else { + RTW_PRINT_SEL(sel, "%-4s %-9s %2s %-6s %-3s%6s" + " = %-4s + min(%-4s, %-4s, %-4s, %-4s) + %-3s + %-3s + %-3s\n" + , "path", "rate", "", "dBm", "idx", "" + , "base", "utgt", "rlmt", "lmt", "ulmt" + , "tpc" + , "tpt", "dpd"); + } +} + +void dump_tx_power_idx_by_path_rs(void *sel, _adapter *adapter, u8 rfpath + , RATE_SECTION rs, enum channel_width bw, u8 cch, u8 opch) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u8 power_idx; + struct txpwr_idx_comp tic; + u8 tx_num, i; + u8 band = cch > 14 ? BAND_ON_5G : BAND_ON_2_4G; + + if (!HAL_SPEC_CHK_RF_PATH(hal_spec, band, rfpath)) + return; + + if (rs >= RATE_SECTION_NUM) + return; + + tx_num = rate_section_to_tx_num(rs); + if (tx_num + 1 > hal_data->tx_nss) + return; + + if (band == BAND_ON_5G && IS_CCK_RATE_SECTION(rs)) + return; + + if (IS_VHT_RATE_SECTION(rs) && !IS_HARDWARE_TYPE_JAGUAR_ALL(adapter)) + return; + + for (i = 0; i < rates_by_sections[rs].rate_num; i++) { + power_idx = rtw_hal_get_tx_power_index(adapter, rfpath, rs, rates_by_sections[rs].rates[i], bw, band, cch, opch, &tic); + dump_tx_power_idx_value(sel, adapter, rfpath, rates_by_sections[rs].rates[i], power_idx, &tic); + } +} + +void dump_tx_power_idx(void *sel, _adapter *adapter, enum channel_width bw, u8 cch, u8 opch) +{ + u8 rfpath, rs; + + dump_tx_power_idx_title(sel, adapter, bw, cch, opch); + for (rfpath = RF_PATH_A; rfpath < RF_PATH_MAX; rfpath++) + for (rs = CCK; rs < RATE_SECTION_NUM; rs++) + dump_tx_power_idx_by_path_rs(sel, adapter, rfpath, rs, bw, cch, opch); +} + +void dump_txpwr_total_dbm_value(void *sel, _adapter *adapter, enum MGN_RATE rate, u8 ntx_idx + , s16 target, s16 byr, s16 btc, s16 extra, s16 rlmt, s16 lmt, s16 ulmt, s16 tpc) +{ + char target_str[8]; + char byr_str[8]; + char btc_str[8]; + char extra_str[8]; + char rlmt_str[8]; + char lmt_str[8]; + char ulmt_str[8]; + char tpc_str[8]; + + txpwr_mbm_get_dbm_str(target, 0, target_str, 8); + txpwr_mbm_get_dbm_str(byr, 0, byr_str, 8); + txpwr_mbm_get_dbm_str(btc, 0, btc_str, 8); + txpwr_mbm_get_dbm_str(extra, 0, extra_str, 8); + txpwr_mbm_get_dbm_str(rlmt, 0, rlmt_str, 8); + txpwr_mbm_get_dbm_str(lmt, 0, lmt_str, 8); + txpwr_mbm_get_dbm_str(ulmt, 0, ulmt_str, 8); + txpwr_mbm_get_dbm_str(tpc, 0, tpc_str, 8); + + RTW_PRINT_SEL(sel, "%9s %uT %s = ((%s %s %s), %s, %s, %s) %s\n" + , MGN_RATE_STR(rate), ntx_idx + 1 + , target_str, byr_str, btc_str, extra_str, rlmt_str, lmt_str, ulmt_str, tpc_str); +} + +void dump_txpwr_total_dbm_value_utgt(void *sel, _adapter *adapter, enum MGN_RATE rate, u8 ntx_idx + , s16 target, s16 utgt, s16 rlmt, s16 lmt, s16 ulmt, s16 tpc) +{ + char target_str[8]; + char utgt_str[8]; + char rlmt_str[8]; + char lmt_str[8]; + char ulmt_str[8]; + char tpc_str[8]; + + txpwr_mbm_get_dbm_str(target, 0, target_str, 8); + txpwr_mbm_get_dbm_str(utgt, 0, utgt_str, 8); + txpwr_mbm_get_dbm_str(rlmt, 0, rlmt_str, 8); + txpwr_mbm_get_dbm_str(lmt, 0, lmt_str, 8); + txpwr_mbm_get_dbm_str(ulmt, 0, ulmt_str, 8); + txpwr_mbm_get_dbm_str(tpc, 0, tpc_str, 8); + + RTW_PRINT_SEL(sel, "%9s %uT %s = (%s, %s, %s, %s) %s\n" + , MGN_RATE_STR(rate), ntx_idx + 1 + , target_str, utgt_str, rlmt_str, lmt_str, ulmt_str, tpc_str); +} + +void dump_txpwr_total_dbm_title(void *sel, _adapter *adapter, enum channel_width bw, u8 cch, u8 opch) +{ + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + char antenna_gain_str[8]; + u8 cch_20, cch_40, cch_80; + + txpwr_mbm_get_dbm_str(rfctl->antenna_gain, 0, antenna_gain_str, 8); + RTW_PRINT_SEL(sel, "antenna_gain:%s\n", antenna_gain_str); + + cch_80 = bw == CHANNEL_WIDTH_80 ? cch : 0; + cch_40 = bw == CHANNEL_WIDTH_40 ? cch : 0; + cch_20 = bw == CHANNEL_WIDTH_20 ? cch : 0; + if (cch_80 != 0) + cch_40 = rtw_get_scch_by_cch_opch(cch_80, CHANNEL_WIDTH_80, opch); + if (cch_40 != 0) + cch_20 = rtw_get_scch_by_cch_opch(cch_40, CHANNEL_WIDTH_40, opch); + + RTW_PRINT_SEL(sel, "%s", ch_width_str(bw)); + if (bw >= CHANNEL_WIDTH_80) + _RTW_PRINT_SEL(sel, ", cch80:%u", cch_80); + if (bw >= CHANNEL_WIDTH_40) + _RTW_PRINT_SEL(sel, ", cch40:%u", cch_40); + _RTW_PRINT_SEL(sel, ", cch20:%u\n", cch_20); + + if (!phy_is_txpwr_user_target_specified(adapter)) { + RTW_PRINT_SEL(sel, "%-9s %2s %-6s = min((%-6s + %-6s + %-6s), %-6s, %-6s, %-6s) + %-6s\n" + , "rate", "", "target", "byr", "btc", "extra", "rlmt", "lmt", "ulmt", "tpc"); + } else { + RTW_PRINT_SEL(sel, "%-9s %2s %-6s = min(%-6s, %-6s, %-6s, %-6s) + %-6s\n" + , "rate", "", "target", "utgt", "rlmt", "lmt", "ulmt", "tpc"); + } +} + +void dump_txpwr_total_dbm_by_rs(void *sel, _adapter *adapter, u8 rs, enum channel_width bw, u8 cch, u8 opch) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u8 i; + u8 band = cch > 14 ? BAND_ON_5G : BAND_ON_2_4G; + + if (rs >= RATE_SECTION_NUM) + return; + + if (rate_section_to_tx_num(rs) + 1 > hal_data->tx_nss) + return; + + if (band == BAND_ON_5G && IS_CCK_RATE_SECTION(rs)) + return; + + if (IS_VHT_RATE_SECTION(rs) && !IS_HARDWARE_TYPE_JAGUAR_ALL(adapter)) + return; + + for (i = 0; i < rates_by_sections[rs].rate_num; i++) { + struct txpwr_idx_comp tic; + s16 target, byr, tpc, btc, extra, utgt, rlmt, lmt, ulmt; + u8 tx_num; + + target = phy_get_txpwr_total_mbm(adapter, rs, rates_by_sections[rs].rates[i], bw, cch, opch, 0, 0, &tic); + tx_num = tic.ntx_idx + 1; + if (tic.rlimit == hal_spec->txgi_max) + rlmt = UNSPECIFIED_MBM; + else + rlmt = ((tic.rlimit * MBM_PDBM) / hal_spec->txgi_pdbm) + mb_of_ntx(tx_num); + if (tic.limit == hal_spec->txgi_max) + lmt = UNSPECIFIED_MBM; + else + lmt = ((tic.limit * MBM_PDBM) / hal_spec->txgi_pdbm) + mb_of_ntx(tx_num); + if (tic.ulimit == hal_spec->txgi_max) + ulmt = UNSPECIFIED_MBM; + else + ulmt = ((tic.ulimit * MBM_PDBM) / hal_spec->txgi_pdbm) + mb_of_ntx(tx_num); + tpc = (tic.tpc * MBM_PDBM) / hal_spec->txgi_pdbm; + + if (tic.utarget == hal_spec->txgi_max) { + byr = ((tic.by_rate * MBM_PDBM) / hal_spec->txgi_pdbm) + mb_of_ntx(tx_num); + btc = (tic.btc * MBM_PDBM) / hal_spec->txgi_pdbm; + extra = (tic.extra * MBM_PDBM) / hal_spec->txgi_pdbm; + dump_txpwr_total_dbm_value(sel, adapter, rates_by_sections[rs].rates[i], tic.ntx_idx + , target, byr, btc, extra, rlmt, lmt, ulmt, tpc); + } else { + utgt = ((tic.utarget * MBM_PDBM) / hal_spec->txgi_pdbm) + mb_of_ntx(tx_num); + dump_txpwr_total_dbm_value_utgt(sel, adapter, rates_by_sections[rs].rates[i], tic.ntx_idx + , target, utgt, rlmt, lmt, ulmt, tpc); + } + } +} + +/* dump txpowr in dBm with effect of N-TX */ +void dump_txpwr_total_dbm(void *sel, _adapter *adapter, enum channel_width bw, u8 cch, u8 opch) +{ + u8 rs; + + dump_txpwr_total_dbm_title(sel, adapter, bw, cch, opch); + for (rs = CCK; rs < RATE_SECTION_NUM; rs++) + dump_txpwr_total_dbm_by_rs(sel, adapter, rs, bw, cch, opch); +} +#endif + +bool phy_is_tx_power_limit_needed(_adapter *adapter) +{ +#if CONFIG_TXPWR_LIMIT + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct registry_priv *regsty = dvobj_to_regsty(adapter_to_dvobj(adapter)); + + if (regsty->RegEnableTxPowerLimit == 1 + || (regsty->RegEnableTxPowerLimit == 2 && hal_data->EEPROMRegulatory == 1)) + return _TRUE; +#endif + + return _FALSE; +} + +bool phy_is_tx_power_by_rate_needed(_adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct registry_priv *regsty = dvobj_to_regsty(adapter_to_dvobj(adapter)); + + if (regsty->RegEnableTxPowerByRate == 1 + || (regsty->RegEnableTxPowerByRate == 2 && hal_data->EEPROMRegulatory != 2)) + return _TRUE; + + return _FALSE; +} + +int phy_load_tx_power_by_rate(_adapter *adapter, u8 chk_file) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + int ret = _FAIL; + + hal_data->txpwr_by_rate_loaded = 0; + PHY_InitTxPowerByRate(adapter); + + /* tx power limit is based on tx power by rate */ + hal_data->txpwr_limit_loaded = 0; + +#ifdef CONFIG_LOAD_PHY_PARA_FROM_FILE + if (chk_file + && phy_ConfigBBWithPgParaFile(adapter, PHY_FILE_PHY_REG_PG) == _SUCCESS + ) { + hal_data->txpwr_by_rate_from_file = 1; + goto post_hdl; + } +#endif + +#ifdef CONFIG_EMBEDDED_FWIMG + if (HAL_STATUS_SUCCESS == odm_config_bb_with_header_file(&hal_data->odmpriv, CONFIG_BB_PHY_REG_PG)) { + RTW_INFO("default power by rate loaded\n"); + hal_data->txpwr_by_rate_from_file = 0; + goto post_hdl; + } +#endif + + RTW_ERR("%s():Read Tx power by rate fail\n", __func__); + goto exit; + +post_hdl: + if (hal_data->odmpriv.phy_reg_pg_value_type != PHY_REG_PG_EXACT_VALUE) { + rtw_warn_on(1); + goto exit; + } + + PHY_TxPowerByRateConfiguration(adapter); + hal_data->txpwr_by_rate_loaded = 1; + + ret = _SUCCESS; + +exit: + return ret; +} + +#if CONFIG_TXPWR_LIMIT +int phy_load_tx_power_limit(_adapter *adapter, u8 chk_file) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct registry_priv *regsty = dvobj_to_regsty(adapter_to_dvobj(adapter)); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + int ret = _FAIL; + + hal_data->txpwr_limit_loaded = 0; + rtw_regd_exc_list_free(rfctl); + rtw_txpwr_lmt_list_free(rfctl); + + if (!hal_data->txpwr_by_rate_loaded && regsty->target_tx_pwr_valid != _TRUE) { + RTW_ERR("%s():Read Tx power limit before target tx power is specify\n", __func__); + goto exit; + } + +#ifdef CONFIG_LOAD_PHY_PARA_FROM_FILE + if (chk_file + && PHY_ConfigRFWithPowerLimitTableParaFile(adapter, PHY_FILE_TXPWR_LMT) == _SUCCESS + ) { + hal_data->txpwr_limit_from_file = 1; + goto post_hdl; + } +#endif + +#ifdef CONFIG_EMBEDDED_FWIMG + if (odm_config_rf_with_header_file(&hal_data->odmpriv, CONFIG_RF_TXPWR_LMT, RF_PATH_A) == HAL_STATUS_SUCCESS) { + RTW_INFO("default power limit loaded\n"); + hal_data->txpwr_limit_from_file = 0; + goto post_hdl; + } +#endif + + RTW_ERR("%s():Read Tx power limit fail\n", __func__); + goto exit; + +post_hdl: + phy_txpwr_lmt_post_hdl(adapter); + rtw_txpwr_init_regd(rfctl); + hal_data->txpwr_limit_loaded = 1; + ret = _SUCCESS; + +exit: + return ret; +} +#endif /* CONFIG_TXPWR_LIMIT */ + +void phy_load_tx_power_ext_info(_adapter *adapter, u8 chk_file) +{ + struct registry_priv *regsty = adapter_to_regsty(adapter); + + /* check registy target tx power */ + regsty->target_tx_pwr_valid = rtw_regsty_chk_target_tx_power_valid(adapter); + + /* power by rate */ + if (phy_is_tx_power_by_rate_needed(adapter) + || regsty->target_tx_pwr_valid != _TRUE /* need target tx power from by rate table */ + ) + phy_load_tx_power_by_rate(adapter, chk_file); + + /* power limit */ +#if CONFIG_TXPWR_LIMIT + if (phy_is_tx_power_limit_needed(adapter)) + phy_load_tx_power_limit(adapter, chk_file); +#endif +} + +inline void phy_reload_tx_power_ext_info(_adapter *adapter) +{ + phy_load_tx_power_ext_info(adapter, 1); + op_class_pref_apply_regulatory(adapter, REG_TXPWR_CHANGE); +} + +inline void phy_reload_default_tx_power_ext_info(_adapter *adapter) +{ + phy_load_tx_power_ext_info(adapter, 0); + op_class_pref_apply_regulatory(adapter, REG_TXPWR_CHANGE); +} + +#ifdef CONFIG_PROC_DEBUG +void dump_tx_power_ext_info(void *sel, _adapter *adapter) +{ + struct registry_priv *regsty = adapter_to_regsty(adapter); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + + RTW_PRINT_SEL(sel, "txpwr_pg_mode: %s\n", txpwr_pg_mode_str(hal_data->txpwr_pg_mode)); + + if (regsty->target_tx_pwr_valid == _TRUE) + RTW_PRINT_SEL(sel, "target_tx_power: from registry\n"); + else if (hal_data->txpwr_by_rate_loaded) + RTW_PRINT_SEL(sel, "target_tx_power: from power by rate\n"); + else + RTW_PRINT_SEL(sel, "target_tx_power: unavailable\n"); + + RTW_PRINT_SEL(sel, "tx_power_by_rate: %s, %s, %s\n" + , phy_is_tx_power_by_rate_needed(adapter) ? "enabled" : "disabled" + , hal_data->txpwr_by_rate_loaded ? "loaded" : "unloaded" + , hal_data->txpwr_by_rate_from_file ? "file" : "default" + ); + + RTW_PRINT_SEL(sel, "tx_power_limit: %s, %s, %s\n" + , phy_is_tx_power_limit_needed(adapter) ? "enabled" : "disabled" + , hal_data->txpwr_limit_loaded ? "loaded" : "unloaded" + , hal_data->txpwr_limit_from_file ? "file" : "default" + ); +} + +void dump_target_tx_power(void *sel, _adapter *adapter) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct registry_priv *regsty = adapter_to_regsty(adapter); + int path, tx_num, band, rs; + u8 target; + + for (band = BAND_ON_2_4G; band <= BAND_ON_5G; band++) { + if (!hal_is_band_support(adapter, band)) + continue; + + for (path = 0; path < RF_PATH_MAX; path++) { + if (!HAL_SPEC_CHK_RF_PATH(hal_spec, band, path)) + break; + + RTW_PRINT_SEL(sel, "[%s][%c]%s\n", band_str(band), rf_path_char(path) + , (regsty->target_tx_pwr_valid == _FALSE && hal_data->txpwr_by_rate_undefined_band_path[band][path]) ? "(dup)" : ""); + + for (rs = 0; rs < RATE_SECTION_NUM; rs++) { + tx_num = rate_section_to_tx_num(rs); + if (tx_num + 1 > hal_data->tx_nss) + continue; + + if (band == BAND_ON_5G && IS_CCK_RATE_SECTION(rs)) + continue; + + if (IS_VHT_RATE_SECTION(rs) && !IS_HARDWARE_TYPE_JAGUAR_ALL(adapter)) + continue; + + target = phy_get_target_txpwr(adapter, band, path, rs); + + if (target % hal_spec->txgi_pdbm) { + _RTW_PRINT_SEL(sel, "%7s: %2d.%d\n", rate_section_str(rs) + , target / hal_spec->txgi_pdbm, (target % hal_spec->txgi_pdbm) * 100 / hal_spec->txgi_pdbm); + } else { + _RTW_PRINT_SEL(sel, "%7s: %5d\n", rate_section_str(rs) + , target / hal_spec->txgi_pdbm); + } + } + } + } + + return; +} + +void dump_tx_power_by_rate(void *sel, _adapter *adapter) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + int path, tx_num, band, n, rs; + u8 rate_num, max_rate_num, base; + s8 by_rate; + + for (band = BAND_ON_2_4G; band <= BAND_ON_5G; band++) { + if (!hal_is_band_support(adapter, band)) + continue; + + for (path = 0; path < RF_PATH_MAX; path++) { + if (!HAL_SPEC_CHK_RF_PATH(hal_spec, band, path)) + break; + + RTW_PRINT_SEL(sel, "[%s][%c]%s\n", band_str(band), rf_path_char(path) + , hal_data->txpwr_by_rate_undefined_band_path[band][path] ? "(dup)" : ""); + + for (rs = 0; rs < RATE_SECTION_NUM; rs++) { + tx_num = rate_section_to_tx_num(rs); + if (tx_num + 1 > hal_data->tx_nss) + continue; + + if (band == BAND_ON_5G && IS_CCK_RATE_SECTION(rs)) + continue; + + if (IS_VHT_RATE_SECTION(rs) && !IS_HARDWARE_TYPE_JAGUAR_ALL(adapter)) + continue; + + if (IS_HARDWARE_TYPE_JAGUAR_ALL(adapter)) + max_rate_num = 10; + else + max_rate_num = 8; + rate_num = rate_section_rate_num(rs); + + RTW_PRINT_SEL(sel, "%7s: ", rate_section_str(rs)); + + /* dump power by rate in db */ + for (n = rate_num - 1; n >= 0; n--) { + by_rate = phy_get_txpwr_by_rate(adapter, band, path, rs, rates_by_sections[rs].rates[n]); + if (by_rate % hal_spec->txgi_pdbm) { + _RTW_PRINT_SEL(sel, "%2d.%d ", by_rate / hal_spec->txgi_pdbm + , (by_rate % hal_spec->txgi_pdbm) * 100 / hal_spec->txgi_pdbm); + } else + _RTW_PRINT_SEL(sel, "%5d ", by_rate / hal_spec->txgi_pdbm); + } + for (n = 0; n < max_rate_num - rate_num; n++) + _RTW_PRINT_SEL(sel, "%5s ", ""); + + _RTW_PRINT_SEL(sel, "|"); + + /* dump power by rate in offset */ + for (n = rate_num - 1; n >= 0; n--) { + by_rate = phy_get_txpwr_by_rate(adapter, band, path, rs, rates_by_sections[rs].rates[n]); + base = phy_get_target_txpwr(adapter, band, path, rs); + _RTW_PRINT_SEL(sel, "%3d ", by_rate - base); + } + RTW_PRINT_SEL(sel, "\n"); + + } + } + } +} +#endif +/* + * phy file path is stored in global char array rtw_phy_para_file_path + * need to care about racing + */ +int rtw_get_phy_file_path(_adapter *adapter, const char *file_name) +{ +#ifdef CONFIG_LOAD_PHY_PARA_FROM_FILE + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + int len = 0; + + if (file_name) { + len += snprintf(rtw_phy_para_file_path, PATH_LENGTH_MAX, "%s", rtw_phy_file_path); + #if defined(CONFIG_MULTIDRV) || defined(REALTEK_CONFIG_PATH_WITH_IC_NAME_FOLDER) + len += snprintf(rtw_phy_para_file_path + len, PATH_LENGTH_MAX - len, "%s/", hal_spec->ic_name); + #endif + len += snprintf(rtw_phy_para_file_path + len, PATH_LENGTH_MAX - len, "%s", file_name); + + return _TRUE; + } +#endif + return _FALSE; +} + +#ifdef CONFIG_LOAD_PHY_PARA_FROM_FILE +int +phy_ConfigMACWithParaFile( + PADAPTER Adapter, + char *pFileName +) +{ + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(Adapter); + int rlen = 0, rtStatus = _FAIL; + char *szLine, *ptmp; + u32 u4bRegOffset, u4bRegValue, u4bMove; + + if (!(Adapter->registrypriv.load_phy_file & LOAD_MAC_PARA_FILE)) + return rtStatus; + + _rtw_memset(pHalData->para_file_buf, 0, MAX_PARA_FILE_BUF_LEN); + + if ((pHalData->mac_reg_len == 0) && (pHalData->mac_reg == NULL)) { + rtw_get_phy_file_path(Adapter, pFileName); + if (rtw_readable_file_sz_chk(rtw_phy_para_file_path, + MAX_PARA_FILE_BUF_LEN) == _TRUE) { + rlen = rtw_retrieve_from_file(rtw_phy_para_file_path, pHalData->para_file_buf, MAX_PARA_FILE_BUF_LEN); + if (rlen > 0) { + rtStatus = _SUCCESS; + pHalData->mac_reg = rtw_zvmalloc(rlen); + if (pHalData->mac_reg) { + _rtw_memcpy(pHalData->mac_reg, pHalData->para_file_buf, rlen); + pHalData->mac_reg_len = rlen; + } else + RTW_INFO("%s mac_reg alloc fail !\n", __FUNCTION__); + } + } + } else { + if ((pHalData->mac_reg_len != 0) && (pHalData->mac_reg != NULL)) { + _rtw_memcpy(pHalData->para_file_buf, pHalData->mac_reg, pHalData->mac_reg_len); + rtStatus = _SUCCESS; + } else + RTW_INFO("%s(): Critical Error !!!\n", __FUNCTION__); + } + + if (rtStatus == _SUCCESS) { + ptmp = pHalData->para_file_buf; + for (szLine = GetLineFromBuffer(ptmp); szLine != NULL; szLine = GetLineFromBuffer(ptmp)) { + if (!IsCommentString(szLine)) { + /* Get 1st hex value as register offset */ + if (GetHexValueFromString(szLine, &u4bRegOffset, &u4bMove)) { + if (u4bRegOffset == 0xffff) { + /* Ending. */ + break; + } + + /* Get 2nd hex value as register value. */ + szLine += u4bMove; + if (GetHexValueFromString(szLine, &u4bRegValue, &u4bMove)) + rtw_write8(Adapter, u4bRegOffset, (u8)u4bRegValue); + } + } + } + } else + RTW_INFO("%s(): No File %s, Load from HWImg Array!\n", __FUNCTION__, pFileName); + + return rtStatus; +} + +int +phy_ConfigBBWithParaFile( + PADAPTER Adapter, + char *pFileName, + u32 ConfigType +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + int rlen = 0, rtStatus = _FAIL; + char *szLine, *ptmp; + u32 u4bRegOffset, u4bRegValue, u4bMove; + char *pBuf = NULL; + u32 *pBufLen = NULL; + + if (!(Adapter->registrypriv.load_phy_file & LOAD_BB_PARA_FILE)) + return rtStatus; + + switch (ConfigType) { + case CONFIG_BB_PHY_REG: + pBuf = pHalData->bb_phy_reg; + pBufLen = &pHalData->bb_phy_reg_len; + break; + case CONFIG_BB_AGC_TAB: + pBuf = pHalData->bb_agc_tab; + pBufLen = &pHalData->bb_agc_tab_len; + break; + default: + RTW_INFO("Unknown ConfigType!! %d\r\n", ConfigType); + break; + } + + _rtw_memset(pHalData->para_file_buf, 0, MAX_PARA_FILE_BUF_LEN); + + if ((pBufLen != NULL) && (*pBufLen == 0) && (pBuf == NULL)) { + rtw_get_phy_file_path(Adapter, pFileName); + if (rtw_readable_file_sz_chk(rtw_phy_para_file_path, + MAX_PARA_FILE_BUF_LEN) == _TRUE) { + rlen = rtw_retrieve_from_file(rtw_phy_para_file_path, pHalData->para_file_buf, MAX_PARA_FILE_BUF_LEN); + if (rlen > 0) { + rtStatus = _SUCCESS; + pBuf = rtw_zvmalloc(rlen); + if (pBuf) { + _rtw_memcpy(pBuf, pHalData->para_file_buf, rlen); + *pBufLen = rlen; + + switch (ConfigType) { + case CONFIG_BB_PHY_REG: + pHalData->bb_phy_reg = pBuf; + break; + case CONFIG_BB_AGC_TAB: + pHalData->bb_agc_tab = pBuf; + break; + } + } else + RTW_INFO("%s(): ConfigType %d alloc fail !\n", __FUNCTION__, ConfigType); + } + } + } else { + if ((pBufLen != NULL) && (*pBufLen != 0) && (pBuf != NULL)) { + _rtw_memcpy(pHalData->para_file_buf, pBuf, *pBufLen); + rtStatus = _SUCCESS; + } else + RTW_INFO("%s(): Critical Error !!!\n", __FUNCTION__); + } + + if (rtStatus == _SUCCESS) { + ptmp = pHalData->para_file_buf; + for (szLine = GetLineFromBuffer(ptmp); szLine != NULL; szLine = GetLineFromBuffer(ptmp)) { + if (!IsCommentString(szLine)) { + /* Get 1st hex value as register offset. */ + if (GetHexValueFromString(szLine, &u4bRegOffset, &u4bMove)) { + if (u4bRegOffset == 0xffff) { + /* Ending. */ + break; + } else if (u4bRegOffset == 0xfe || u4bRegOffset == 0xffe) { +#ifdef CONFIG_LONG_DELAY_ISSUE + rtw_msleep_os(50); +#else + rtw_mdelay_os(50); +#endif + } else if (u4bRegOffset == 0xfd) + rtw_mdelay_os(5); + else if (u4bRegOffset == 0xfc) + rtw_mdelay_os(1); + else if (u4bRegOffset == 0xfb) + rtw_udelay_os(50); + else if (u4bRegOffset == 0xfa) + rtw_udelay_os(5); + else if (u4bRegOffset == 0xf9) + rtw_udelay_os(1); + + /* Get 2nd hex value as register value. */ + szLine += u4bMove; + if (GetHexValueFromString(szLine, &u4bRegValue, &u4bMove)) { + /* RTW_INFO("[BB-ADDR]%03lX=%08lX\n", u4bRegOffset, u4bRegValue); */ + phy_set_bb_reg(Adapter, u4bRegOffset, bMaskDWord, u4bRegValue); + + if (u4bRegOffset == 0xa24) + pHalData->odmpriv.rf_calibrate_info.rega24 = u4bRegValue; + + /* Add 1us delay between BB/RF register setting. */ + rtw_udelay_os(1); + } + } + } + } + } else + RTW_INFO("%s(): No File %s, Load from HWImg Array!\n", __FUNCTION__, pFileName); + + return rtStatus; +} + +void +phy_DecryptBBPgParaFile( + PADAPTER Adapter, + char *buffer +) +{ + u32 i = 0, j = 0; + u8 map[95] = {0}; + u8 currentChar; + char *BufOfLines, *ptmp; + + /* RTW_INFO("=====>phy_DecryptBBPgParaFile()\n"); */ + /* 32 the ascii code of the first visable char, 126 the last one */ + for (i = 0; i < 95; ++i) + map[i] = (u8)(94 - i); + + ptmp = buffer; + i = 0; + for (BufOfLines = GetLineFromBuffer(ptmp); BufOfLines != NULL; BufOfLines = GetLineFromBuffer(ptmp)) { + /* RTW_INFO("Encrypted Line: %s\n", BufOfLines); */ + + for (j = 0; j < strlen(BufOfLines); ++j) { + currentChar = BufOfLines[j]; + + if (currentChar == '\0') + break; + + currentChar -= (u8)((((i + j) * 3) % 128)); + + BufOfLines[j] = map[currentChar - 32] + 32; + } + /* RTW_INFO("Decrypted Line: %s\n", BufOfLines ); */ + if (strlen(BufOfLines) != 0) + i++; + BufOfLines[strlen(BufOfLines)] = '\n'; + } +} + +#ifndef DBG_TXPWR_BY_RATE_FILE_PARSE +#define DBG_TXPWR_BY_RATE_FILE_PARSE 0 +#endif + +int +phy_ParseBBPgParaFile( + PADAPTER Adapter, + char *buffer +) +{ + int rtStatus = _FAIL; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(Adapter); + char *szLine, *ptmp; + u32 u4bRegOffset, u4bRegMask; + u32 u4bMove; + BOOLEAN firstLine = _TRUE; + u8 tx_num = 0; + u8 band = 0, rf_path = 0; + + if (Adapter->registrypriv.RegDecryptCustomFile == 1) + phy_DecryptBBPgParaFile(Adapter, buffer); + + ptmp = buffer; + for (szLine = GetLineFromBuffer(ptmp); szLine != NULL; szLine = GetLineFromBuffer(ptmp)) { + if (isAllSpaceOrTab(szLine, sizeof(*szLine))) + continue; + + if (!IsCommentString(szLine)) { + /* Get header info (relative value or exact value) */ + if (firstLine) { + if (strncmp(szLine, "#[v1]", 5) == 0 + || strncmp(szLine, "#[v2]", 5) == 0) + pHalData->odmpriv.phy_reg_pg_version = szLine[3] - '0'; + else { + RTW_ERR("The format in PHY_REG_PG are invalid %s\n", szLine); + goto exit; + } + + if (strncmp(szLine + 5, "[Exact]#", 8) == 0) { + pHalData->odmpriv.phy_reg_pg_value_type = PHY_REG_PG_EXACT_VALUE; + firstLine = _FALSE; + continue; + } else { + RTW_ERR("The values in PHY_REG_PG are invalid %s\n", szLine); + goto exit; + } + } + + if (pHalData->odmpriv.phy_reg_pg_version > 0) { + u32 index = 0; + + if (strncmp(szLine, "0xffff", 6) == 0) + break; + + if (strncmp(szLine, "#[END]#", 7)) { + /* load the table label info */ + if (szLine[0] == '#') { + index = 0; + if (strncmp(szLine, "#[2.4G]", 7) == 0) { + band = BAND_ON_2_4G; + index += 8; + } else if (strncmp(szLine, "#[5G]", 5) == 0) { + band = BAND_ON_5G; + index += 6; + } else { + RTW_ERR("Invalid band %s in PHY_REG_PG.txt\n", szLine); + goto exit; + } + + rf_path = szLine[index] - 'A'; + if (DBG_TXPWR_BY_RATE_FILE_PARSE) + RTW_INFO(" Table label Band %d, RfPath %d\n", band, rf_path ); + } else { /* load rows of tables */ + if (szLine[1] == '1') + tx_num = RF_1TX; + else if (szLine[1] == '2') + tx_num = RF_2TX; + else if (szLine[1] == '3') + tx_num = RF_3TX; + else if (szLine[1] == '4') + tx_num = RF_4TX; + else { + RTW_ERR("Invalid row in PHY_REG_PG.txt '%c'(%d)\n", szLine[1], szLine[1]); + goto exit; + } + + while (szLine[index] != ']') + ++index; + ++index;/* skip ] */ + + /* Get 2nd hex value as register offset. */ + szLine += index; + if (GetHexValueFromString(szLine, &u4bRegOffset, &u4bMove)) + szLine += u4bMove; + else + goto exit; + + /* Get 2nd hex value as register mask. */ + if (GetHexValueFromString(szLine, &u4bRegMask, &u4bMove)) + szLine += u4bMove; + else + goto exit; + + if (pHalData->odmpriv.phy_reg_pg_value_type == PHY_REG_PG_EXACT_VALUE) { + u32 combineValue = 0; + u8 integer = 0, fraction = 0; + + if (GetFractionValueFromString(szLine, &integer, &fraction, &u4bMove)) + szLine += u4bMove; + else + goto exit; + + integer *= hal_spec->txgi_pdbm; + integer += ((u16)fraction * (u16)hal_spec->txgi_pdbm) / 100; + if (pHalData->odmpriv.phy_reg_pg_version == 1) + combineValue |= (((integer / 10) << 4) + (integer % 10)); + else + combineValue |= integer; + + if (GetFractionValueFromString(szLine, &integer, &fraction, &u4bMove)) + szLine += u4bMove; + else + goto exit; + + integer *= hal_spec->txgi_pdbm; + integer += ((u16)fraction * (u16)hal_spec->txgi_pdbm) / 100; + combineValue <<= 8; + if (pHalData->odmpriv.phy_reg_pg_version == 1) + combineValue |= (((integer / 10) << 4) + (integer % 10)); + else + combineValue |= integer; + + if (GetFractionValueFromString(szLine, &integer, &fraction, &u4bMove)) + szLine += u4bMove; + else + goto exit; + + integer *= hal_spec->txgi_pdbm; + integer += ((u16)fraction * (u16)hal_spec->txgi_pdbm) / 100; + combineValue <<= 8; + if (pHalData->odmpriv.phy_reg_pg_version == 1) + combineValue |= (((integer / 10) << 4) + (integer % 10)); + else + combineValue |= integer; + + if (GetFractionValueFromString(szLine, &integer, &fraction, &u4bMove)) + szLine += u4bMove; + else + goto exit; + + integer *= hal_spec->txgi_pdbm; + integer += ((u16)fraction * (u16)hal_spec->txgi_pdbm) / 100; + combineValue <<= 8; + if (pHalData->odmpriv.phy_reg_pg_version == 1) + combineValue |= (((integer / 10) << 4) + (integer % 10)); + else + combineValue |= integer; + + phy_store_tx_power_by_rate(Adapter, band, rf_path, tx_num, u4bRegOffset, u4bRegMask, combineValue); + + if (DBG_TXPWR_BY_RATE_FILE_PARSE) + RTW_INFO("addr:0x%3x mask:0x%08x %dTx = 0x%08x\n", u4bRegOffset, u4bRegMask, tx_num + 1, combineValue); + } + } + } + } + } + } + + rtStatus = _SUCCESS; + +exit: + RTW_INFO("%s return %d\n", __func__, rtStatus); + return rtStatus; +} + +int +phy_ConfigBBWithPgParaFile( + PADAPTER Adapter, + const char *pFileName) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + int rlen = 0, rtStatus = _FAIL; + + if (!(Adapter->registrypriv.load_phy_file & LOAD_BB_PG_PARA_FILE)) + return rtStatus; + + _rtw_memset(pHalData->para_file_buf, 0, MAX_PARA_FILE_BUF_LEN); + + if (pHalData->bb_phy_reg_pg == NULL) { + rtw_get_phy_file_path(Adapter, pFileName); + if (rtw_readable_file_sz_chk(rtw_phy_para_file_path, + MAX_PARA_FILE_BUF_LEN) == _TRUE) { + rlen = rtw_retrieve_from_file(rtw_phy_para_file_path, pHalData->para_file_buf, MAX_PARA_FILE_BUF_LEN); + if (rlen > 0) { + rtStatus = _SUCCESS; + pHalData->bb_phy_reg_pg = rtw_zvmalloc(rlen); + if (pHalData->bb_phy_reg_pg) { + _rtw_memcpy(pHalData->bb_phy_reg_pg, pHalData->para_file_buf, rlen); + pHalData->bb_phy_reg_pg_len = rlen; + } else + RTW_INFO("%s bb_phy_reg_pg alloc fail !\n", __FUNCTION__); + } + } + } else { + if ((pHalData->bb_phy_reg_pg_len != 0) && (pHalData->bb_phy_reg_pg != NULL)) { + _rtw_memcpy(pHalData->para_file_buf, pHalData->bb_phy_reg_pg, pHalData->bb_phy_reg_pg_len); + rtStatus = _SUCCESS; + } else + RTW_INFO("%s(): Critical Error !!!\n", __FUNCTION__); + } + + if (rtStatus == _SUCCESS) { + /* RTW_INFO("phy_ConfigBBWithPgParaFile(): read %s ok\n", pFileName); */ + rtStatus = phy_ParseBBPgParaFile(Adapter, pHalData->para_file_buf); + } else + RTW_INFO("%s(): No File %s, Load from HWImg Array!\n", __FUNCTION__, pFileName); + + return rtStatus; +} + +#if (MP_DRIVER == 1) + +int +phy_ConfigBBWithMpParaFile( + PADAPTER Adapter, + char *pFileName +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + int rlen = 0, rtStatus = _FAIL; + char *szLine, *ptmp; + u32 u4bRegOffset, u4bRegValue, u4bMove; + + if (!(Adapter->registrypriv.load_phy_file & LOAD_BB_MP_PARA_FILE)) + return rtStatus; + + _rtw_memset(pHalData->para_file_buf, 0, MAX_PARA_FILE_BUF_LEN); + + if ((pHalData->bb_phy_reg_mp_len == 0) && (pHalData->bb_phy_reg_mp == NULL)) { + rtw_get_phy_file_path(Adapter, pFileName); + if (rtw_readable_file_sz_chk(rtw_phy_para_file_path, + MAX_PARA_FILE_BUF_LEN) == _TRUE) { + rlen = rtw_retrieve_from_file(rtw_phy_para_file_path, pHalData->para_file_buf, MAX_PARA_FILE_BUF_LEN); + if (rlen > 0) { + rtStatus = _SUCCESS; + pHalData->bb_phy_reg_mp = rtw_zvmalloc(rlen); + if (pHalData->bb_phy_reg_mp) { + _rtw_memcpy(pHalData->bb_phy_reg_mp, pHalData->para_file_buf, rlen); + pHalData->bb_phy_reg_mp_len = rlen; + } else + RTW_INFO("%s bb_phy_reg_mp alloc fail !\n", __FUNCTION__); + } + } + } else { + if ((pHalData->bb_phy_reg_mp_len != 0) && (pHalData->bb_phy_reg_mp != NULL)) { + _rtw_memcpy(pHalData->para_file_buf, pHalData->bb_phy_reg_mp, pHalData->bb_phy_reg_mp_len); + rtStatus = _SUCCESS; + } else + RTW_INFO("%s(): Critical Error !!!\n", __FUNCTION__); + } + + if (rtStatus == _SUCCESS) { + /* RTW_INFO("phy_ConfigBBWithMpParaFile(): read %s ok\n", pFileName); */ + + ptmp = pHalData->para_file_buf; + for (szLine = GetLineFromBuffer(ptmp); szLine != NULL; szLine = GetLineFromBuffer(ptmp)) { + if (!IsCommentString(szLine)) { + /* Get 1st hex value as register offset. */ + if (GetHexValueFromString(szLine, &u4bRegOffset, &u4bMove)) { + if (u4bRegOffset == 0xffff) { + /* Ending. */ + break; + } else if (u4bRegOffset == 0xfe || u4bRegOffset == 0xffe) { +#ifdef CONFIG_LONG_DELAY_ISSUE + rtw_msleep_os(50); +#else + rtw_mdelay_os(50); +#endif + } else if (u4bRegOffset == 0xfd) + rtw_mdelay_os(5); + else if (u4bRegOffset == 0xfc) + rtw_mdelay_os(1); + else if (u4bRegOffset == 0xfb) + rtw_udelay_os(50); + else if (u4bRegOffset == 0xfa) + rtw_udelay_os(5); + else if (u4bRegOffset == 0xf9) + rtw_udelay_os(1); + + /* Get 2nd hex value as register value. */ + szLine += u4bMove; + if (GetHexValueFromString(szLine, &u4bRegValue, &u4bMove)) { + /* RTW_INFO("[ADDR]%03lX=%08lX\n", u4bRegOffset, u4bRegValue); */ + phy_set_bb_reg(Adapter, u4bRegOffset, bMaskDWord, u4bRegValue); + + /* Add 1us delay between BB/RF register setting. */ + rtw_udelay_os(1); + } + } + } + } + } else + RTW_INFO("%s(): No File %s, Load from HWImg Array!\n", __FUNCTION__, pFileName); + + return rtStatus; +} + +#endif + +int +PHY_ConfigRFWithParaFile( + PADAPTER Adapter, + char *pFileName, + enum rf_path eRFPath +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + int rlen = 0, rtStatus = _FAIL; + char *szLine, *ptmp; + u32 u4bRegOffset, u4bRegValue, u4bMove; + u16 i; + char *pBuf = NULL; + u32 *pBufLen = NULL; + + if (!(Adapter->registrypriv.load_phy_file & LOAD_RF_PARA_FILE)) + return rtStatus; + + switch (eRFPath) { + case RF_PATH_A: + pBuf = pHalData->rf_radio_a; + pBufLen = &pHalData->rf_radio_a_len; + break; + case RF_PATH_B: + pBuf = pHalData->rf_radio_b; + pBufLen = &pHalData->rf_radio_b_len; + break; + default: + RTW_INFO("Unknown RF path!! %d\r\n", eRFPath); + break; + } + + _rtw_memset(pHalData->para_file_buf, 0, MAX_PARA_FILE_BUF_LEN); + + if ((pBufLen != NULL) && (*pBufLen == 0) && (pBuf == NULL)) { + rtw_get_phy_file_path(Adapter, pFileName); + if (rtw_readable_file_sz_chk(rtw_phy_para_file_path, + MAX_PARA_FILE_BUF_LEN) == _TRUE) { + rlen = rtw_retrieve_from_file(rtw_phy_para_file_path, pHalData->para_file_buf, MAX_PARA_FILE_BUF_LEN); + if (rlen > 0) { + rtStatus = _SUCCESS; + pBuf = rtw_zvmalloc(rlen); + if (pBuf) { + _rtw_memcpy(pBuf, pHalData->para_file_buf, rlen); + *pBufLen = rlen; + + switch (eRFPath) { + case RF_PATH_A: + pHalData->rf_radio_a = pBuf; + break; + case RF_PATH_B: + pHalData->rf_radio_b = pBuf; + break; + default: + RTW_INFO("Unknown RF path!! %d\r\n", eRFPath); + break; + } + } else + RTW_INFO("%s(): eRFPath=%d alloc fail !\n", __FUNCTION__, eRFPath); + } + } + } else { + if ((pBufLen != NULL) && (*pBufLen != 0) && (pBuf != NULL)) { + _rtw_memcpy(pHalData->para_file_buf, pBuf, *pBufLen); + rtStatus = _SUCCESS; + } else + RTW_INFO("%s(): Critical Error !!!\n", __FUNCTION__); + } + + if (rtStatus == _SUCCESS) { + /* RTW_INFO("%s(): read %s successfully\n", __FUNCTION__, pFileName); */ + + ptmp = pHalData->para_file_buf; + for (szLine = GetLineFromBuffer(ptmp); szLine != NULL; szLine = GetLineFromBuffer(ptmp)) { + if (!IsCommentString(szLine)) { + /* Get 1st hex value as register offset. */ + if (GetHexValueFromString(szLine, &u4bRegOffset, &u4bMove)) { + if (u4bRegOffset == 0xfe || u4bRegOffset == 0xffe) { + /* Deay specific ms. Only RF configuration require delay. */ +#ifdef CONFIG_LONG_DELAY_ISSUE + rtw_msleep_os(50); +#else + rtw_mdelay_os(50); +#endif + } else if (u4bRegOffset == 0xfd) { + /* delay_ms(5); */ + for (i = 0; i < 100; i++) + rtw_udelay_os(MAX_STALL_TIME); + } else if (u4bRegOffset == 0xfc) { + /* delay_ms(1); */ + for (i = 0; i < 20; i++) + rtw_udelay_os(MAX_STALL_TIME); + } else if (u4bRegOffset == 0xfb) + rtw_udelay_os(50); + else if (u4bRegOffset == 0xfa) + rtw_udelay_os(5); + else if (u4bRegOffset == 0xf9) + rtw_udelay_os(1); + else if (u4bRegOffset == 0xffff) + break; + + /* Get 2nd hex value as register value. */ + szLine += u4bMove; + if (GetHexValueFromString(szLine, &u4bRegValue, &u4bMove)) { + phy_set_rf_reg(Adapter, eRFPath, u4bRegOffset, bRFRegOffsetMask, u4bRegValue); + + /* Temp add, for frequency lock, if no delay, that may cause */ + /* frequency shift, ex: 2412MHz => 2417MHz */ + /* If frequency shift, the following action may works. */ + /* Fractional-N table in radio_a.txt */ + /* 0x2a 0x00001 */ /* channel 1 */ + /* 0x2b 0x00808 frequency divider. */ + /* 0x2b 0x53333 */ + /* 0x2c 0x0000c */ + rtw_udelay_os(1); + } + } + } + } + } else + RTW_INFO("%s(): No File %s, Load from HWImg Array!\n", __FUNCTION__, pFileName); + + return rtStatus; +} + +void +initDeltaSwingIndexTables( + PADAPTER Adapter, + char *Band, + char *Path, + char *Sign, + char *Channel, + char *Rate, + char *Data +) +{ +#define STR_EQUAL_5G(_band, _path, _sign, _rate, _chnl) \ + ((strcmp(Band, _band) == 0) && (strcmp(Path, _path) == 0) && (strcmp(Sign, _sign) == 0) &&\ + (strcmp(Rate, _rate) == 0) && (strcmp(Channel, _chnl) == 0)\ + ) +#define STR_EQUAL_2G(_band, _path, _sign, _rate) \ + ((strcmp(Band, _band) == 0) && (strcmp(Path, _path) == 0) && (strcmp(Sign, _sign) == 0) &&\ + (strcmp(Rate, _rate) == 0)\ + ) + +#define STORE_SWING_TABLE(_array, _iteratedIdx) \ + do { \ + for (token = strsep(&Data, delim); token != NULL; token = strsep(&Data, delim)) {\ + sscanf(token, "%d", &idx);\ + _array[_iteratedIdx++] = (u8)idx;\ + } } while (0)\ + + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + struct dm_rf_calibration_struct *pRFCalibrateInfo = &(pDM_Odm->rf_calibrate_info); + u32 j = 0; + char *token; + char delim[] = ","; + u32 idx = 0; + + /* RTW_INFO("===>initDeltaSwingIndexTables(): Band: %s;\nPath: %s;\nSign: %s;\nChannel: %s;\nRate: %s;\n, Data: %s;\n", */ + /* Band, Path, Sign, Channel, Rate, Data); */ + + if (STR_EQUAL_2G("2G", "A", "+", "CCK")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_2g_cck_a_p, j); + else if (STR_EQUAL_2G("2G", "A", "-", "CCK")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_2g_cck_a_n, j); + else if (STR_EQUAL_2G("2G", "B", "+", "CCK")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_2g_cck_b_p, j); + else if (STR_EQUAL_2G("2G", "B", "-", "CCK")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_2g_cck_b_n, j); + else if (STR_EQUAL_2G("2G", "A", "+", "ALL")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_2ga_p, j); + else if (STR_EQUAL_2G("2G", "A", "-", "ALL")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_2ga_n, j); + else if (STR_EQUAL_2G("2G", "B", "+", "ALL")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_2gb_p, j); + else if (STR_EQUAL_2G("2G", "B", "-", "ALL")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_2gb_n, j); + else if (STR_EQUAL_5G("5G", "A", "+", "ALL", "0")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5ga_p[0], j); + else if (STR_EQUAL_5G("5G", "A", "-", "ALL", "0")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5ga_n[0], j); + else if (STR_EQUAL_5G("5G", "B", "+", "ALL", "0")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5gb_p[0], j); + else if (STR_EQUAL_5G("5G", "B", "-", "ALL", "0")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5gb_n[0], j); + else if (STR_EQUAL_5G("5G", "A", "+", "ALL", "1")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5ga_p[1], j); + else if (STR_EQUAL_5G("5G", "A", "-", "ALL", "1")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5ga_n[1], j); + else if (STR_EQUAL_5G("5G", "B", "+", "ALL", "1")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5gb_p[1], j); + else if (STR_EQUAL_5G("5G", "B", "-", "ALL", "1")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5gb_n[1], j); + else if (STR_EQUAL_5G("5G", "A", "+", "ALL", "2")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5ga_p[2], j); + else if (STR_EQUAL_5G("5G", "A", "-", "ALL", "2")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5ga_n[2], j); + else if (STR_EQUAL_5G("5G", "B", "+", "ALL", "2")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5gb_p[2], j); + else if (STR_EQUAL_5G("5G", "B", "-", "ALL", "2")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5gb_n[2], j); + else if (STR_EQUAL_5G("5G", "A", "+", "ALL", "3")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5ga_p[3], j); + else if (STR_EQUAL_5G("5G", "A", "-", "ALL", "3")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5ga_n[3], j); + else if (STR_EQUAL_5G("5G", "B", "+", "ALL", "3")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5gb_p[3], j); + else if (STR_EQUAL_5G("5G", "B", "-", "ALL", "3")) + STORE_SWING_TABLE(pRFCalibrateInfo->delta_swing_table_idx_5gb_n[3], j); + else + RTW_INFO("===>initDeltaSwingIndexTables(): The input is invalid!!\n"); +} + +int +PHY_ConfigRFWithTxPwrTrackParaFile( + PADAPTER Adapter, + char *pFileName +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + int rlen = 0, rtStatus = _FAIL; + char *szLine, *ptmp; + u32 i = 0; + + if (!(Adapter->registrypriv.load_phy_file & LOAD_RF_TXPWR_TRACK_PARA_FILE)) + return rtStatus; + + _rtw_memset(pHalData->para_file_buf, 0, MAX_PARA_FILE_BUF_LEN); + + if ((pHalData->rf_tx_pwr_track_len == 0) && (pHalData->rf_tx_pwr_track == NULL)) { + rtw_get_phy_file_path(Adapter, pFileName); + if (rtw_readable_file_sz_chk(rtw_phy_para_file_path, + MAX_PARA_FILE_BUF_LEN) == _TRUE) { + rlen = rtw_retrieve_from_file(rtw_phy_para_file_path, pHalData->para_file_buf, MAX_PARA_FILE_BUF_LEN); + if (rlen > 0) { + rtStatus = _SUCCESS; + pHalData->rf_tx_pwr_track = rtw_zvmalloc(rlen); + if (pHalData->rf_tx_pwr_track) { + _rtw_memcpy(pHalData->rf_tx_pwr_track, pHalData->para_file_buf, rlen); + pHalData->rf_tx_pwr_track_len = rlen; + } else + RTW_INFO("%s rf_tx_pwr_track alloc fail !\n", __FUNCTION__); + } + } + } else { + if ((pHalData->rf_tx_pwr_track_len != 0) && (pHalData->rf_tx_pwr_track != NULL)) { + _rtw_memcpy(pHalData->para_file_buf, pHalData->rf_tx_pwr_track, pHalData->rf_tx_pwr_track_len); + rtStatus = _SUCCESS; + } else + RTW_INFO("%s(): Critical Error !!!\n", __FUNCTION__); + } + + if (rtStatus == _SUCCESS) { + /* RTW_INFO("%s(): read %s successfully\n", __FUNCTION__, pFileName); */ + + ptmp = pHalData->para_file_buf; + for (szLine = GetLineFromBuffer(ptmp); szLine != NULL; szLine = GetLineFromBuffer(ptmp)) { + if (!IsCommentString(szLine)) { + char band[5] = "", path[5] = "", sign[5] = ""; + char chnl[5] = "", rate[10] = ""; + char data[300] = ""; /* 100 is too small */ + + if (strlen(szLine) < 10 || szLine[0] != '[') + continue; + + strncpy(band, szLine + 1, 2); + strncpy(path, szLine + 5, 1); + strncpy(sign, szLine + 8, 1); + + i = 10; /* szLine+10 */ + if (!ParseQualifiedString(szLine, &i, rate, '[', ']')) { + /* RTW_INFO("Fail to parse rate!\n"); */ + } + if (!ParseQualifiedString(szLine, &i, chnl, '[', ']')) { + /* RTW_INFO("Fail to parse channel group!\n"); */ + } + while (szLine[i] != '{' && i < strlen(szLine)) + i++; + if (!ParseQualifiedString(szLine, &i, data, '{', '}')) { + /* RTW_INFO("Fail to parse data!\n"); */ + } + + initDeltaSwingIndexTables(Adapter, band, path, sign, chnl, rate, data); + } + } + } else + RTW_INFO("%s(): No File %s, Load from HWImg Array!\n", __FUNCTION__, pFileName); +#if 0 + for (i = 0; i < DELTA_SWINGIDX_SIZE; ++i) { + RTW_INFO("pRFCalibrateInfo->delta_swing_table_idx_2ga_p[%d] = %d\n", i, pRFCalibrateInfo->delta_swing_table_idx_2ga_p[i]); + RTW_INFO("pRFCalibrateInfo->delta_swing_table_idx_2ga_n[%d] = %d\n", i, pRFCalibrateInfo->delta_swing_table_idx_2ga_n[i]); + RTW_INFO("pRFCalibrateInfo->delta_swing_table_idx_2gb_p[%d] = %d\n", i, pRFCalibrateInfo->delta_swing_table_idx_2gb_p[i]); + RTW_INFO("pRFCalibrateInfo->delta_swing_table_idx_2gb_n[%d] = %d\n", i, pRFCalibrateInfo->delta_swing_table_idx_2gb_n[i]); + RTW_INFO("pRFCalibrateInfo->delta_swing_table_idx_2g_cck_a_p[%d] = %d\n", i, pRFCalibrateInfo->delta_swing_table_idx_2g_cck_a_p[i]); + RTW_INFO("pRFCalibrateInfo->delta_swing_table_idx_2g_cck_a_n[%d] = %d\n", i, pRFCalibrateInfo->delta_swing_table_idx_2g_cck_a_n[i]); + RTW_INFO("pRFCalibrateInfo->delta_swing_table_idx_2g_cck_b_p[%d] = %d\n", i, pRFCalibrateInfo->delta_swing_table_idx_2g_cck_b_p[i]); + RTW_INFO("pRFCalibrateInfo->delta_swing_table_idx_2g_cck_b_n[%d] = %d\n", i, pRFCalibrateInfo->delta_swing_table_idx_2g_cck_b_n[i]); + + for (j = 0; j < 3; ++j) { + RTW_INFO("pRFCalibrateInfo->delta_swing_table_idx_5ga_p[%d][%d] = %d\n", j, i, pRFCalibrateInfo->delta_swing_table_idx_5ga_p[j][i]); + RTW_INFO("pRFCalibrateInfo->delta_swing_table_idx_5ga_n[%d][%d] = %d\n", j, i, pRFCalibrateInfo->delta_swing_table_idx_5ga_n[j][i]); + RTW_INFO("pRFCalibrateInfo->delta_swing_table_idx_5gb_p[%d][%d] = %d\n", j, i, pRFCalibrateInfo->delta_swing_table_idx_5gb_p[j][i]); + RTW_INFO("pRFCalibrateInfo->delta_swing_table_idx_5gb_n[%d][%d] = %d\n", j, i, pRFCalibrateInfo->delta_swing_table_idx_5gb_n[j][i]); + } + } +#endif + return rtStatus; +} + +#if CONFIG_TXPWR_LIMIT + +#ifndef DBG_TXPWR_LMT_FILE_PARSE +#define DBG_TXPWR_LMT_FILE_PARSE 0 +#endif + +#define PARSE_RET_NO_HDL 0 +#define PARSE_RET_SUCCESS 1 +#define PARSE_RET_FAIL 2 + +/* +* @@Ver=2.0 +* or +* @@DomainCode=0x28, Regulation=C6 +* or +* @@CountryCode=GB, Regulation=C7 +*/ +static u8 parse_reg_exc_config(_adapter *adapter, char *szLine) +{ +#define VER_PREFIX "Ver=" +#define DOMAIN_PREFIX "DomainCode=0x" +#define COUNTRY_PREFIX "CountryCode=" +#define REG_PREFIX "Regulation=" + + const u8 ver_prefix_len = strlen(VER_PREFIX); + const u8 domain_prefix_len = strlen(DOMAIN_PREFIX); + const u8 country_prefix_len = strlen(COUNTRY_PREFIX); + const u8 reg_prefix_len = strlen(REG_PREFIX); + u32 i, i_val_s, i_val_e; + u32 j; + u8 domain = 0xFF; + char *country = NULL; + u8 parse_reg = 0; + + if (szLine[0] != '@' || szLine[1] != '@') + return PARSE_RET_NO_HDL; + + i = 2; + if (strncmp(szLine + i, VER_PREFIX, ver_prefix_len) == 0) + ; /* nothing to do */ + else if (strncmp(szLine + i, DOMAIN_PREFIX, domain_prefix_len) == 0) { + /* get string after domain prefix to ',' */ + i += domain_prefix_len; + i_val_s = i; + while (szLine[i] != ',') { + if (szLine[i] == '\0') + return PARSE_RET_FAIL; + i++; + } + i_val_e = i; + + /* check if all hex */ + for (j = i_val_s; j < i_val_e; j++) + if (IsHexDigit(szLine[j]) == _FALSE) + return PARSE_RET_FAIL; + + /* get value from hex string */ + if (sscanf(szLine + i_val_s, "%hhx", &domain) != 1) + return PARSE_RET_FAIL; + + parse_reg = 1; + } else if (strncmp(szLine + i, COUNTRY_PREFIX, country_prefix_len) == 0) { + /* get string after country prefix to ',' */ + i += country_prefix_len; + i_val_s = i; + while (szLine[i] != ',') { + if (szLine[i] == '\0') + return PARSE_RET_FAIL; + i++; + } + i_val_e = i; + + if (i_val_e - i_val_s != 2) + return PARSE_RET_FAIL; + + /* check if all alpha */ + for (j = i_val_s; j < i_val_e; j++) + if (is_alpha(szLine[j]) == _FALSE) + return PARSE_RET_FAIL; + + country = szLine + i_val_s; + + parse_reg = 1; + + } else + return PARSE_RET_FAIL; + + if (parse_reg) { + /* move to 'R' */ + while (szLine[i] != 'R') { + if (szLine[i] == '\0') + return PARSE_RET_FAIL; + i++; + } + + /* check if matching regulation prefix */ + if (strncmp(szLine + i, REG_PREFIX, reg_prefix_len) != 0) + return PARSE_RET_FAIL; + + /* get string after regulation prefix ending with space */ + i += reg_prefix_len; + i_val_s = i; + while (szLine[i] != ' ' && szLine[i] != '\t' && szLine[i] != '\0') + i++; + + if (i == i_val_s) + return PARSE_RET_FAIL; + + rtw_regd_exc_add_with_nlen(adapter_to_rfctl(adapter), country, domain, szLine + i_val_s, i - i_val_s); + } + + return PARSE_RET_SUCCESS; +} + +static int +phy_ParsePowerLimitTableFile( + PADAPTER Adapter, + char *buffer +) +{ +#define LD_STAGE_EXC_MAPPING 0 +#define LD_STAGE_TAB_DEFINE 1 +#define LD_STAGE_TAB_START 2 +#define LD_STAGE_COLUMN_DEFINE 3 +#define LD_STAGE_CH_ROW 4 + + int rtStatus = _FAIL; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(Adapter); + struct dm_struct *pDM_Odm = &(pHalData->odmpriv); + u8 loadingStage = LD_STAGE_EXC_MAPPING; + u32 i = 0, forCnt = 0; + char *szLine, *ptmp; + char band[10], bandwidth[10], rateSection[10], ntx[10], colNumBuf[10]; + char **regulation = NULL; + u8 colNum = 0; + + if (Adapter->registrypriv.RegDecryptCustomFile == 1) + phy_DecryptBBPgParaFile(Adapter, buffer); + + ptmp = buffer; + for (szLine = GetLineFromBuffer(ptmp); szLine != NULL; szLine = GetLineFromBuffer(ptmp)) { + if (isAllSpaceOrTab(szLine, sizeof(*szLine))) + continue; + if (IsCommentString(szLine)) + continue; + + if (loadingStage == LD_STAGE_EXC_MAPPING) { + if (szLine[0] == '#' || szLine[1] == '#') { + loadingStage = LD_STAGE_TAB_DEFINE; + if (DBG_TXPWR_LMT_FILE_PARSE) + dump_regd_exc_list(RTW_DBGDUMP, adapter_to_rfctl(Adapter)); + } else { + if (parse_reg_exc_config(Adapter, szLine) == PARSE_RET_FAIL) { + RTW_ERR("Fail to parse regulation exception ruls!\n"); + goto exit; + } + continue; + } + } + + if (loadingStage == LD_STAGE_TAB_DEFINE) { + /* read "## 2.4G, 20M, 1T, CCK" */ + if (szLine[0] != '#' || szLine[1] != '#') + continue; + + /* skip the space */ + i = 2; + while (szLine[i] == ' ' || szLine[i] == '\t') + ++i; + + szLine[--i] = ' '; /* return the space in front of the regulation info */ + + /* Parse the label of the table */ + _rtw_memset((void *) band, 0, 10); + _rtw_memset((void *) bandwidth, 0, 10); + _rtw_memset((void *) ntx, 0, 10); + _rtw_memset((void *) rateSection, 0, 10); + if (!ParseQualifiedString(szLine, &i, band, ' ', ',')) { + RTW_ERR("Fail to parse band!\n"); + goto exit; + } + if (!ParseQualifiedString(szLine, &i, bandwidth, ' ', ',')) { + RTW_ERR("Fail to parse bandwidth!\n"); + goto exit; + } + if (!ParseQualifiedString(szLine, &i, ntx, ' ', ',')) { + RTW_ERR("Fail to parse ntx!\n"); + goto exit; + } + if (!ParseQualifiedString(szLine, &i, rateSection, ' ', ',')) { + RTW_ERR("Fail to parse rate!\n"); + goto exit; + } + + loadingStage = LD_STAGE_TAB_START; + } else if (loadingStage == LD_STAGE_TAB_START) { + /* read "## START" */ + if (szLine[0] != '#' || szLine[1] != '#') + continue; + + /* skip the space */ + i = 2; + while (szLine[i] == ' ' || szLine[i] == '\t') + ++i; + + if (strncmp((u8 *)(szLine + i), "START", 5)) { + RTW_ERR("Missing \"## START\" label\n"); + goto exit; + } + + loadingStage = LD_STAGE_COLUMN_DEFINE; + } else if (loadingStage == LD_STAGE_COLUMN_DEFINE) { + /* read "## #5# FCC ETSI MKK IC KCC" */ + if (szLine[0] != '#' || szLine[1] != '#') + continue; + + /* skip the space */ + i = 2; + while (szLine[i] == ' ' || szLine[i] == '\t') + ++i; + + _rtw_memset((void *) colNumBuf, 0, 10); + if (!ParseQualifiedString(szLine, &i, colNumBuf, '#', '#')) { + RTW_ERR("Fail to parse column number!\n"); + goto exit; + } + if (!GetU1ByteIntegerFromStringInDecimal(colNumBuf, &colNum)) { + RTW_ERR("Column number \"%s\" is not unsigned decimal\n", colNumBuf); + goto exit; + } + if (colNum == 0) { + RTW_ERR("Column number is 0\n"); + goto exit; + } + + if (DBG_TXPWR_LMT_FILE_PARSE) + RTW_PRINT("[%s][%s][%s][%s] column num:%d\n", band, bandwidth, rateSection, ntx, colNum); + + regulation = (char **)rtw_zmalloc(sizeof(char *) * colNum); + if (!regulation) { + RTW_ERR("Regulation alloc fail\n"); + goto exit; + } + + for (forCnt = 0; forCnt < colNum; ++forCnt) { + u32 i_ns; + + /* skip the space */ + while (szLine[i] == ' ' || szLine[i] == '\t') + i++; + i_ns = i; + + while (szLine[i] != ' ' && szLine[i] != '\t' && szLine[i] != '\0') + i++; + + regulation[forCnt] = (char *)rtw_malloc(i - i_ns + 1); + if (!regulation[forCnt]) { + RTW_ERR("Regulation alloc fail\n"); + goto exit; + } + + _rtw_memcpy(regulation[forCnt], szLine + i_ns, i - i_ns); + regulation[forCnt][i - i_ns] = '\0'; + } + + if (DBG_TXPWR_LMT_FILE_PARSE) { + RTW_PRINT("column name:"); + for (forCnt = 0; forCnt < colNum; ++forCnt) + _RTW_PRINT(" %s", regulation[forCnt]); + _RTW_PRINT("\n"); + } + + loadingStage = LD_STAGE_CH_ROW; + } else if (loadingStage == LD_STAGE_CH_ROW) { + char channel[10] = {0}, powerLimit[10] = {0}; + u8 cnt = 0; + + /* the table ends */ + if (szLine[0] == '#' && szLine[1] == '#') { + i = 2; + while (szLine[i] == ' ' || szLine[i] == '\t') + ++i; + + if (strncmp((u8 *)(szLine + i), "END", 3) == 0) { + loadingStage = LD_STAGE_TAB_DEFINE; + if (regulation) { + for (forCnt = 0; forCnt < colNum; ++forCnt) { + if (regulation[forCnt]) { + rtw_mfree(regulation[forCnt], strlen(regulation[forCnt]) + 1); + regulation[forCnt] = NULL; + } + } + rtw_mfree((u8 *)regulation, sizeof(char *) * colNum); + regulation = NULL; + } + colNum = 0; + continue; + } else { + RTW_ERR("Missing \"## END\" label\n"); + goto exit; + } + } + + if ((szLine[0] != 'c' && szLine[0] != 'C') || + (szLine[1] != 'h' && szLine[1] != 'H') + ) { + RTW_WARN("Wrong channel prefix: '%c','%c'(%d,%d)\n", szLine[0], szLine[1], szLine[0], szLine[1]); + continue; + } + i = 2;/* move to the location behind 'h' */ + + /* load the channel number */ + cnt = 0; + while (szLine[i] >= '0' && szLine[i] <= '9') { + channel[cnt] = szLine[i]; + ++cnt; + ++i; + } + /* RTW_INFO("chnl %s!\n", channel); */ + + for (forCnt = 0; forCnt < colNum; ++forCnt) { + /* skip the space between channel number and the power limit value */ + while (szLine[i] == ' ' || szLine[i] == '\t') + ++i; + + /* load the power limit value */ + _rtw_memset((void *) powerLimit, 0, 10); + + if (szLine[i] == 'W' && szLine[i + 1] == 'W') { + /* + * case "WW" assign special ww value + * means to get minimal limit in other regulations at same channel + */ + s8 ww_value = phy_txpwr_ww_lmt_value(Adapter); + + sprintf(powerLimit, "%d", ww_value); + i += 2; + + } else if (szLine[i] == 'N' && szLine[i + 1] == 'A') { + /* + * case "NA" assign max txgi value + * means no limitation + */ + sprintf(powerLimit, "%d", hal_spec->txgi_max); + i += 2; + + } else if ((szLine[i] >= '0' && szLine[i] <= '9') || szLine[i] == '.' + || szLine[i] == '+' || szLine[i] == '-' + ){ + /* case of dBm value */ + u8 integer = 0, fraction = 0, negative = 0; + u32 u4bMove; + s8 lmt = 0; + + if (szLine[i] == '+' || szLine[i] == '-') { + if (szLine[i] == '-') + negative = 1; + i++; + } + + if (GetFractionValueFromString(&szLine[i], &integer, &fraction, &u4bMove)) + i += u4bMove; + else { + RTW_ERR("Limit \"%s\" is not valid decimal\n", &szLine[i]); + goto exit; + } + + /* transform to string of value in unit of txgi */ + lmt = integer * hal_spec->txgi_pdbm + ((u16)fraction * (u16)hal_spec->txgi_pdbm) / 100; + if (negative) + lmt = -lmt; + sprintf(powerLimit, "%d", lmt); + + } else { + RTW_ERR("Wrong limit expression \"%c%c\"(%d, %d)\n" + , szLine[i], szLine[i + 1], szLine[i], szLine[i + 1]); + goto exit; + } + + /* store the power limit value */ + phy_set_tx_power_limit(pDM_Odm, (u8 *)regulation[forCnt], (u8 *)band, + (u8 *)bandwidth, (u8 *)rateSection, (u8 *)ntx, (u8 *)channel, (u8 *)powerLimit); + + } + } + } + + rtStatus = _SUCCESS; + +exit: + if (regulation) { + for (forCnt = 0; forCnt < colNum; ++forCnt) { + if (regulation[forCnt]) { + rtw_mfree(regulation[forCnt], strlen(regulation[forCnt]) + 1); + regulation[forCnt] = NULL; + } + } + rtw_mfree((u8 *)regulation, sizeof(char *) * colNum); + regulation = NULL; + } + + RTW_INFO("%s return %d\n", __func__, rtStatus); + return rtStatus; +} + +int +PHY_ConfigRFWithPowerLimitTableParaFile( + PADAPTER Adapter, + const char *pFileName +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + int rlen = 0, rtStatus = _FAIL; + + if (!(Adapter->registrypriv.load_phy_file & LOAD_RF_TXPWR_LMT_PARA_FILE)) + return rtStatus; + + _rtw_memset(pHalData->para_file_buf, 0, MAX_PARA_FILE_BUF_LEN); + + if (pHalData->rf_tx_pwr_lmt == NULL) { + rtw_get_phy_file_path(Adapter, pFileName); + if (rtw_readable_file_sz_chk(rtw_phy_para_file_path, + MAX_PARA_FILE_BUF_LEN) == _TRUE) { + rlen = rtw_retrieve_from_file(rtw_phy_para_file_path, pHalData->para_file_buf, MAX_PARA_FILE_BUF_LEN); + if (rlen > 0) { + rtStatus = _SUCCESS; + pHalData->rf_tx_pwr_lmt = rtw_zvmalloc(rlen); + if (pHalData->rf_tx_pwr_lmt) { + _rtw_memcpy(pHalData->rf_tx_pwr_lmt, pHalData->para_file_buf, rlen); + pHalData->rf_tx_pwr_lmt_len = rlen; + } else + RTW_INFO("%s rf_tx_pwr_lmt alloc fail !\n", __FUNCTION__); + } + } + } else { + if ((pHalData->rf_tx_pwr_lmt_len != 0) && (pHalData->rf_tx_pwr_lmt != NULL)) { + _rtw_memcpy(pHalData->para_file_buf, pHalData->rf_tx_pwr_lmt, pHalData->rf_tx_pwr_lmt_len); + rtStatus = _SUCCESS; + } else + RTW_INFO("%s(): Critical Error !!!\n", __FUNCTION__); + } + + if (rtStatus == _SUCCESS) { + /* RTW_INFO("%s(): read %s ok\n", __FUNCTION__, pFileName); */ + rtStatus = phy_ParsePowerLimitTableFile(Adapter, pHalData->para_file_buf); + } else + RTW_INFO("%s(): No File %s, Load from HWImg Array!\n", __FUNCTION__, pFileName); + + return rtStatus; +} +#endif /* CONFIG_TXPWR_LIMIT */ + +void phy_free_filebuf_mask(_adapter *padapter, u8 mask) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + + if (pHalData->mac_reg && (mask & LOAD_MAC_PARA_FILE)) { + rtw_vmfree(pHalData->mac_reg, pHalData->mac_reg_len); + pHalData->mac_reg = NULL; + } + if (mask & LOAD_BB_PARA_FILE) { + if (pHalData->bb_phy_reg) { + rtw_vmfree(pHalData->bb_phy_reg, pHalData->bb_phy_reg_len); + pHalData->bb_phy_reg = NULL; + } + if (pHalData->bb_agc_tab) { + rtw_vmfree(pHalData->bb_agc_tab, pHalData->bb_agc_tab_len); + pHalData->bb_agc_tab = NULL; + } + } + if (pHalData->bb_phy_reg_pg && (mask & LOAD_BB_PG_PARA_FILE)) { + rtw_vmfree(pHalData->bb_phy_reg_pg, pHalData->bb_phy_reg_pg_len); + pHalData->bb_phy_reg_pg = NULL; + } + if (pHalData->bb_phy_reg_mp && (mask & LOAD_BB_MP_PARA_FILE)) { + rtw_vmfree(pHalData->bb_phy_reg_mp, pHalData->bb_phy_reg_mp_len); + pHalData->bb_phy_reg_mp = NULL; + } + if (mask & LOAD_RF_PARA_FILE) { + if (pHalData->rf_radio_a) { + rtw_vmfree(pHalData->rf_radio_a, pHalData->rf_radio_a_len); + pHalData->rf_radio_a = NULL; + } + if (pHalData->rf_radio_b) { + rtw_vmfree(pHalData->rf_radio_b, pHalData->rf_radio_b_len); + pHalData->rf_radio_b = NULL; + } + } + if (pHalData->rf_tx_pwr_track && (mask & LOAD_RF_TXPWR_TRACK_PARA_FILE)) { + rtw_vmfree(pHalData->rf_tx_pwr_track, pHalData->rf_tx_pwr_track_len); + pHalData->rf_tx_pwr_track = NULL; + } + if (pHalData->rf_tx_pwr_lmt && (mask & LOAD_RF_TXPWR_LMT_PARA_FILE)) { + rtw_vmfree(pHalData->rf_tx_pwr_lmt, pHalData->rf_tx_pwr_lmt_len); + pHalData->rf_tx_pwr_lmt = NULL; + } +} + +inline void phy_free_filebuf(_adapter *padapter) +{ + phy_free_filebuf_mask(padapter, 0xFF); +} + +#endif + +/* +* TX power limit of regulatory without HAL consideration +* Return value in unit of TX Gain Index +* hal_spec.txgi_max means unspecified +*/ +s8 phy_get_txpwr_regd_lmt(_adapter *adapter, struct hal_spec_t *hal_spec, u8 cch, enum channel_width bw, u8 ntx_idx) +{ + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + s16 total_mbm = UNSPECIFIED_MBM; + s8 lmt; + + if ((adapter->registrypriv.RegEnableTxPowerLimit == 2 && hal_data->EEPROMRegulatory != 1) || + adapter->registrypriv.RegEnableTxPowerLimit == 0) + goto exit; + +#ifdef CONFIG_REGD_SRC_FROM_OS + if (rfctl->regd_src == REGD_SRC_OS) + total_mbm = rtw_os_get_total_txpwr_regd_lmt_mbm(adapter, cch, bw); +#endif + +exit: + if (total_mbm != UNSPECIFIED_MBM) + lmt = (total_mbm - mb_of_ntx(ntx_idx + 1) - rfctl->antenna_gain) * hal_spec->txgi_pdbm / MBM_PDBM; + else + lmt = hal_spec->txgi_max; + + return lmt; +} + +/* +* check if user specified mbm is valid +*/ +bool phy_is_txpwr_user_mbm_valid(_adapter *adapter, s16 mbm) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + + /* 1T upper bound check */ + if (hal_spec->txgi_max <= mbm * hal_spec->txgi_pdbm / MBM_PDBM) + return 0; + + return 1; +} + +bool phy_is_txpwr_user_target_specified(_adapter *adapter) +{ + s16 total_mbm = UNSPECIFIED_MBM; + +#ifdef CONFIG_IOCTL_CFG80211 + total_mbm = rtw_cfg80211_dev_get_total_txpwr_target_mbm(adapter_to_dvobj(adapter)); +#endif + + return total_mbm != UNSPECIFIED_MBM; +} + +/* +* Return value in unit of TX Gain Index +* hal_spec.txgi_max means unspecified +*/ +s8 phy_get_txpwr_user_target(_adapter *adapter, struct hal_spec_t *hal_spec, u8 ntx_idx) +{ + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + s16 total_mbm = UNSPECIFIED_MBM; + s8 target; + +#ifdef CONFIG_IOCTL_CFG80211 + total_mbm = rtw_cfg80211_dev_get_total_txpwr_target_mbm(adapter_to_dvobj(adapter)); +#endif + if (total_mbm != UNSPECIFIED_MBM) + target = (total_mbm - mb_of_ntx(ntx_idx + 1) - rfctl->antenna_gain) * hal_spec->txgi_pdbm / MBM_PDBM; + else + target = hal_spec->txgi_max; + + return target; +} + +/* +* Return value in unit of TX Gain Index +* hal_spec.txgi_max means unspecified +*/ +s8 phy_get_txpwr_user_lmt(_adapter *adapter, struct hal_spec_t *hal_spec, u8 ntx_idx) +{ + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + s16 total_mbm = UNSPECIFIED_MBM; + s8 lmt; + +#ifdef CONFIG_IOCTL_CFG80211 + total_mbm = rtw_cfg80211_dev_get_total_txpwr_lmt_mbm(adapter_to_dvobj(adapter)); +#endif + if (total_mbm != UNSPECIFIED_MBM) + lmt = (total_mbm - mb_of_ntx(ntx_idx + 1) - rfctl->antenna_gain) * hal_spec->txgi_pdbm / MBM_PDBM; + else + lmt = hal_spec->txgi_max; + + return lmt; +} + +/* +* Return value in unit of TX Gain Index +* 0 means unspecified +*/ +s8 phy_get_txpwr_tpc(_adapter *adapter, struct hal_spec_t *hal_spec) +{ + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + u16 cnst = 0; + + if (rfctl->tpc_mode == TPC_MODE_MANUAL) + cnst = rfctl->tpc_manual_constraint * hal_spec->txgi_pdbm / MBM_PDBM; + + return -cnst; +} + +void dump_txpwr_tpc_settings(void *sel, _adapter *adapter) +{ + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + + if (rfctl->tpc_mode == TPC_MODE_DISABLE) + RTW_PRINT_SEL(sel, "mode:DISABLE(%d)\n", rfctl->tpc_mode); + else if (rfctl->tpc_mode == TPC_MODE_MANUAL) { + RTW_PRINT_SEL(sel, "mode:MANUAL(%d)\n", rfctl->tpc_mode); + RTW_PRINT_SEL(sel, "constraint:%d (mB)\n", rfctl->tpc_manual_constraint); + } +} + +void dump_txpwr_antenna_gain(void *sel, _adapter *adapter) +{ + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + + RTW_PRINT_SEL(sel, "%d (mBi)\n", rfctl->antenna_gain); +} + +/* +* Return value in unit of TX Gain Index +*/ +s8 phy_get_txpwr_target(_adapter *adapter, u8 rfpath, RATE_SECTION rs, u8 rate, u8 ntx_idx + , enum channel_width bw, BAND_TYPE band, u8 cch, u8 opch, bool reg_max, struct txpwr_idx_comp *tic) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + s8 target, by_rate = 0, btc_diff = 0, extra = 0; + s8 lmt, rlmt, utgt, ulmt; + s8 tpc = 0; + + rlmt = lmt = utgt = ulmt = hal_spec->txgi_max; + + if (band != BAND_ON_2_4G && IS_CCK_RATE(rate)) + goto exit; + + if (!reg_max) { + utgt = phy_get_txpwr_user_target(adapter, hal_spec, ntx_idx); + if (utgt != hal_spec->txgi_max) + goto get_lmt; + } + +#ifdef CONFIG_RTL8812A + if (IS_HARDWARE_TYPE_8812(adapter) + && phy_get_txpwr_target_skip_by_rate_8812a(adapter, rate)) + by_rate = phy_get_target_txpwr(adapter, band, rfpath, rs); + else +#endif + by_rate = phy_get_txpwr_by_rate(adapter, band, rfpath, rs, rate); + if (by_rate == hal_spec->txgi_max) + by_rate = 0; + +#ifdef CONFIG_BT_COEXIST + if (!reg_max) { + if (hal_data->EEPROMBluetoothCoexist == _TRUE) + btc_diff = -(rtw_btcoex_query_reduced_wl_pwr_lvl(adapter) * hal_spec->txgi_pdbm); + } +#endif + + extra = rtw_hal_get_txpwr_target_extra_bias(adapter, rfpath, rs, rate, bw, band, cch); + +get_lmt: + rlmt = phy_get_txpwr_regd_lmt(adapter, hal_spec, cch, bw, ntx_idx); + lmt = phy_get_txpwr_lmt_sub_chs(adapter, NULL, band, bw, rfpath, rate, ntx_idx, cch, opch, reg_max); + if (!reg_max) + ulmt = phy_get_txpwr_user_lmt(adapter, hal_spec, ntx_idx); + /* TODO: limit from outer source, ex: 11d */ + + if (!reg_max) + tpc = phy_get_txpwr_tpc(adapter, hal_spec); + +exit: + if (utgt != hal_spec->txgi_max) + target = utgt; + else + target = by_rate + btc_diff + extra; + + if (target > rlmt) + target = rlmt; + if (target > lmt) + target = lmt; + if (target > ulmt) + target = ulmt; + + target += tpc; + + if (tic) { + tic->target = target; + if (utgt == hal_spec->txgi_max) { + tic->by_rate = by_rate; + tic->btc = btc_diff; + tic->extra = extra; + } + tic->utarget = utgt; + tic->rlimit = rlmt; + tic->limit = lmt; + tic->ulimit = ulmt; + tic->tpc = tpc; + } + + return target; +} + +/* TODO: common dpd_diff getting API from phydm */ +#ifdef CONFIG_RTL8822C +#include "./rtl8822c/rtl8822c.h" +#endif + +/* +* Return in unit of TX Gain Index +*/ +s8 phy_get_txpwr_amends(_adapter *adapter, u8 rfpath, RATE_SECTION rs, u8 rate, u8 ntx_idx + , enum channel_width bw, BAND_TYPE band, u8 cch, struct txpwr_idx_comp *tic) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + s8 tpt_diff = 0, dpd_diff = 0, val = 0; + + if (band != BAND_ON_2_4G && IS_CCK_RATE(rate)) + goto exit; + + if (IS_HARDWARE_TYPE_8188E(adapter) || IS_HARDWARE_TYPE_8188F(adapter) || IS_HARDWARE_TYPE_8188GTV(adapter) + || IS_HARDWARE_TYPE_8192E(adapter) || IS_HARDWARE_TYPE_8192F(adapter) + || IS_HARDWARE_TYPE_8723B(adapter) || IS_HARDWARE_TYPE_8703B(adapter) || IS_HARDWARE_TYPE_8723D(adapter) + || IS_HARDWARE_TYPE_8710B(adapter) + || IS_HARDWARE_TYPE_8821(adapter) || IS_HARDWARE_TYPE_8812(adapter) + ) + tpt_diff = PHY_GetTxPowerTrackingOffset(adapter, rfpath, rate); + +#ifdef CONFIG_RTL8822C + if (IS_HARDWARE_TYPE_8822C(adapter)) + dpd_diff = -(rtl8822c_get_dis_dpd_by_rate_diff(adapter, rate) * hal_spec->txgi_pdbm); +#endif + +exit: + if (tic) { + tic->tpt = tpt_diff; + tic->dpd = dpd_diff; + } + + return tpt_diff + dpd_diff; +} + +#ifdef CONFIG_TXPWR_PG_WITH_TSSI_OFFSET +s8 phy_get_tssi_txpwr_by_rate_ref(_adapter *adapter, enum rf_path path + , enum channel_width bw, u8 cch, u8 opch) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u8 ntx_idx = phy_get_current_tx_num(adapter, MGN_MCS7); + BAND_TYPE band = cch > 14 ? BAND_ON_5G : BAND_ON_2_4G; + s8 pwr_idx; + + pwr_idx = phy_get_txpwr_target(adapter, path, HT_1SS, MGN_MCS7 + , ntx_idx, bw, band, cch, opch, 0, NULL); + pwr_idx += phy_get_txpwr_amends(adapter, path, HT_1SS, MGN_MCS7 + , ntx_idx, bw, band, cch, NULL); + + return pwr_idx; +} +#endif + +/* + * Rteurn tx power index for rate + */ +u8 hal_com_get_txpwr_idx(_adapter *adapter, enum rf_path rfpath + , RATE_SECTION rs, enum MGN_RATE rate, enum channel_width bw, BAND_TYPE band, u8 cch, u8 opch + , struct txpwr_idx_comp *tic) +{ + PHAL_DATA_TYPE hal = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + s16 power_idx = 0; + s8 base = 0; + s8 rate_target, rate_amends; + u8 ntx_idx = phy_get_current_tx_num(adapter, rate); + + /* target */ + rate_target = phy_get_txpwr_target(adapter, rfpath, rs, rate, ntx_idx, bw, band, cch, opch, 0, tic); + + /* amends */ + rate_amends = phy_get_txpwr_amends(adapter, rfpath, rs, rate, ntx_idx, bw, band, cch, tic); + + switch (hal->txpwr_pg_mode) { +#ifdef CONFIG_TXPWR_PG_WITH_PWR_IDX + case TXPWR_PG_WITH_PWR_IDX: { + /* + * power index = + * 1. pg base (per rate section) + + * 2. target diff (per rate) to target of its rate section + + * 3. amends diff (per rate) + */ + u8 rs_target; + + base = phy_get_pg_txpwr_idx(adapter, rfpath, rs, ntx_idx, bw, band, cch); + rs_target = phy_get_target_txpwr(adapter, band, rfpath, rs); + power_idx = base + (rate_target - rs_target) + (rate_amends); + + if (tic) { + if (tic->utarget == hal_spec->txgi_max) + tic->by_rate -= rs_target; + else + tic->utarget -= rs_target; + if (tic->rlimit != hal_spec->txgi_max) + tic->rlimit -= rs_target; + if (tic->limit != hal_spec->txgi_max) + tic->limit -= rs_target; + if (tic->ulimit != hal_spec->txgi_max) + tic->ulimit -= rs_target; + } + } + break; +#endif +#ifdef CONFIG_TXPWR_PG_WITH_TSSI_OFFSET + case TXPWR_PG_WITH_TSSI_OFFSET: { + /* + * power index = + * 1. base (fixed) + + * 2. target (per rate) + + * 3. amends diff (per rate) + * base is selected that power index of MCS7 == halrf_get_tssi_codeword_for_txindex() + */ +#if defined(CONFIG_RTL8822C) || defined(CONFIG_RTL8814B) + s8 mcs7_idx; + + mcs7_idx = phy_get_tssi_txpwr_by_rate_ref(adapter, rfpath, bw, cch, opch); + base = halrf_get_tssi_codeword_for_txindex(adapter_to_phydm(adapter)) - mcs7_idx; + power_idx = base + rate_target + rate_amends; +#else + base = 0; + power_idx = rate_target + rate_amends; +#endif + } + break; +#endif + } + + if (tic) { + tic->ntx_idx = ntx_idx; + tic->base = base; + } + + if (power_idx < 0) + power_idx = 0; + else if (power_idx > hal_spec->txgi_max) + power_idx = hal_spec->txgi_max; + +#if defined(CONFIG_RTL8821A) || defined(CONFIG_RTL8812A) + if ((IS_HARDWARE_TYPE_8821(adapter) || IS_HARDWARE_TYPE_8812(adapter)) + && power_idx % 2 == 1 && !IS_NORMAL_CHIP(hal->version_id)) + --power_idx; +#endif + + return power_idx; +} + +static s16 phy_get_txpwr_mbm(_adapter *adapter, u8 rfpath, RATE_SECTION rs, u8 rate + , enum channel_width bw, u8 cch, u8 opch, bool total, bool reg_max, bool eirp, struct txpwr_idx_comp *tic) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + BAND_TYPE band = cch <= 14 ? BAND_ON_2_4G : BAND_ON_5G; + u8 ntx_idx_max, ntx_idx, i; + s16 val, max = UNSPECIFIED_MBM; + + if (reg_max) { + ntx_idx_max = phy_get_capable_tx_num(adapter, rate); + ntx_idx = rate_section_to_tx_num(rs); + if (ntx_idx > ntx_idx_max) { + rtw_warn_on(1); + return 0; + } + } else + ntx_idx_max = ntx_idx = phy_get_current_tx_num(adapter, rate); + + for (i = 0; ntx_idx + i <= ntx_idx_max; i++) { + val = phy_get_txpwr_target(adapter, rfpath, rs, rate, ntx_idx, bw, band, cch, opch, reg_max, tic); + val = (val * MBM_PDBM) / hal_spec->txgi_pdbm; + if (total) + val += mb_of_ntx(ntx_idx + 1); + if (eirp) + val += rfctl->antenna_gain; + + if (max == UNSPECIFIED_MBM || max < val) + max = val; + } + + if (tic) + tic->ntx_idx = ntx_idx; + + if (max == UNSPECIFIED_MBM) { + rtw_warn_on(1); + max = 0; + } + return max; +} + +/* get txpowr in mBm for single path */ +s16 phy_get_txpwr_single_mbm(_adapter *adapter, u8 rfpath, RATE_SECTION rs, u8 rate + , enum channel_width bw, u8 cch, u8 opch, bool reg_max, bool eirp, struct txpwr_idx_comp *tic) +{ + return phy_get_txpwr_mbm(adapter, rfpath, rs, rate, bw, cch, opch, 0, reg_max, eirp, tic); +} + +/* get txpowr in mBm with effect of N-TX */ +s16 phy_get_txpwr_total_mbm(_adapter *adapter, RATE_SECTION rs, u8 rate + , enum channel_width bw, u8 cch, u8 opch, bool reg_max, bool eirp, struct txpwr_idx_comp *tic) +{ + /* assume all path have same txpower target */ + return phy_get_txpwr_mbm(adapter, RF_PATH_A, rs, rate, bw, cch, opch, 1, reg_max, eirp, tic); +} + +static s16 _phy_get_txpwr_max_mbm(_adapter *adapter, s8 rfpath + , enum channel_width bw, u8 cch, u8 opch, u16 bmp_cck_ofdm, u32 bmp_ht, u64 bmp_vht, bool reg_max, bool eirp) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + BAND_TYPE band = cch <= 14 ? BAND_ON_2_4G : BAND_ON_5G; + u8 tx_num; + RATE_SECTION rs; + u8 hw_rate; + int i; + s16 max = UNSPECIFIED_MBM, mbm; + + if (0) + RTW_INFO("cck_ofdm:0x%04x, ht:0x%08x, vht:0x%016llx\n", bmp_cck_ofdm, bmp_ht, bmp_vht); + + for (rs = 0; rs < RATE_SECTION_NUM; rs++) { + tx_num = rate_section_to_tx_num(rs); + if (tx_num + 1 > hal_data->tx_nss) + continue; + + if (band == BAND_ON_5G && IS_CCK_RATE_SECTION(rs)) + continue; + + if (IS_VHT_RATE_SECTION(rs) && !IS_HARDWARE_TYPE_JAGUAR_ALL(adapter)) + continue; + + for (i = 0; i < rates_by_sections[rs].rate_num; i++) { + hw_rate = MRateToHwRate(rates_by_sections[rs].rates[i]); + if (IS_LEGACY_HRATE(hw_rate)) { + if (!(bmp_cck_ofdm & BIT(hw_rate))) + continue; + } else if (IS_HT_HRATE(hw_rate)) { + if (!(bmp_ht & BIT(hw_rate - DESC_RATEMCS0))) + continue; + } else if (IS_VHT_HRATE(hw_rate)) { + if (!(bmp_vht & BIT(hw_rate - DESC_RATEVHTSS1MCS0))) + continue; + } + + if (rfpath < 0) /* total */ + mbm = phy_get_txpwr_total_mbm(adapter, rs, rates_by_sections[rs].rates[i], bw, cch, opch, reg_max, eirp, NULL); + else + mbm = phy_get_txpwr_single_mbm(adapter, rfpath, rs, rates_by_sections[rs].rates[i], bw, cch, opch, reg_max, eirp, NULL); + + if (max == UNSPECIFIED_MBM || mbm > max) + max = mbm; + } + } + + return max; +} + +s16 phy_get_txpwr_single_max_mbm(_adapter *adapter, u8 rfpath + , enum channel_width bw, u8 cch, u8 opch, u16 bmp_cck_ofdm, u32 bmp_ht, u64 bmp_vht, bool reg_max, bool eirp) +{ + return _phy_get_txpwr_max_mbm(adapter, rfpath, bw, cch, opch, bmp_cck_ofdm, bmp_ht, bmp_vht, reg_max, eirp); +} + +s16 phy_get_txpwr_total_max_mbm(_adapter *adapter + , enum channel_width bw, u8 cch, u8 opch, u16 bmp_cck_ofdm, u32 bmp_ht, u64 bmp_vht, bool reg_max, bool eirp) +{ + return _phy_get_txpwr_max_mbm(adapter, -1, bw, cch, opch, bmp_cck_ofdm, bmp_ht, bmp_vht, reg_max, eirp); +} + +s8 +phy_get_tx_power_final_absolute_value(_adapter *adapter, u8 rfpath, u8 rate, + enum channel_width bw, u8 cch) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + RATE_SECTION rs = mgn_rate_to_rs(rate); + BAND_TYPE band = cch <= 14 ? BAND_ON_2_4G : BAND_ON_5G; + s8 val; + + val = phy_get_txpwr_target(adapter, rfpath + , rs, rate, phy_get_current_tx_num(adapter, rate), bw, band, cch, 0, 0, NULL); + + val /= hal_spec->txgi_pdbm; + + return val; +} diff --git a/hal/hal_dm.c b/hal/hal_dm.c new file mode 100644 index 0000000..83bf523 --- /dev/null +++ b/hal/hal_dm.c @@ -0,0 +1,1937 @@ +/****************************************************************************** + * + * Copyright(c) 2014 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ + +#include +#include + +/* A mapping from HalData to ODM. */ +enum odm_board_type boardType(u8 InterfaceSel) +{ + enum odm_board_type board = ODM_BOARD_DEFAULT; + +#ifdef CONFIG_PCI_HCI + INTERFACE_SELECT_PCIE pcie = (INTERFACE_SELECT_PCIE)InterfaceSel; + switch (pcie) { + case INTF_SEL0_SOLO_MINICARD: + board |= ODM_BOARD_MINICARD; + break; + case INTF_SEL1_BT_COMBO_MINICARD: + board |= ODM_BOARD_BT; + board |= ODM_BOARD_MINICARD; + break; + default: + board = ODM_BOARD_DEFAULT; + break; + } + +#elif defined(CONFIG_USB_HCI) + INTERFACE_SELECT_USB usb = (INTERFACE_SELECT_USB)InterfaceSel; + switch (usb) { + case INTF_SEL1_USB_High_Power: + board |= ODM_BOARD_EXT_LNA; + board |= ODM_BOARD_EXT_PA; + break; + case INTF_SEL2_MINICARD: + board |= ODM_BOARD_MINICARD; + break; + case INTF_SEL4_USB_Combo: + board |= ODM_BOARD_BT; + break; + case INTF_SEL5_USB_Combo_MF: + board |= ODM_BOARD_BT; + break; + case INTF_SEL0_USB: + case INTF_SEL3_USB_Solo: + default: + board = ODM_BOARD_DEFAULT; + break; + } + +#endif + /* RTW_INFO("===> boardType(): (pHalData->InterfaceSel, pDM_Odm->BoardType) = (%d, %d)\n", InterfaceSel, board); */ + + return board; +} + +void rtw_hal_update_iqk_fw_offload_cap(_adapter *adapter) +{ + PHAL_DATA_TYPE hal = GET_HAL_DATA(adapter); + struct dm_struct *p_dm_odm = adapter_to_phydm(adapter); + + if (hal->RegIQKFWOffload) { + rtw_sctx_init(&hal->iqk_sctx, 0); + phydm_fwoffload_ability_init(p_dm_odm, PHYDM_RF_IQK_OFFLOAD); + } else + phydm_fwoffload_ability_clear(p_dm_odm, PHYDM_RF_IQK_OFFLOAD); + + RTW_INFO("IQK FW offload:%s\n", hal->RegIQKFWOffload ? "enable" : "disable"); + + if (rtw_mi_check_status(adapter, MI_LINKED)) { + #ifdef CONFIG_LPS + LPS_Leave(adapter, "SWITCH_IQK_OFFLOAD"); + #endif + halrf_iqk_trigger(p_dm_odm, _FALSE); + } +} + +#if ((RTL8822B_SUPPORT == 1) || (RTL8821C_SUPPORT == 1) || (RTL8814B_SUPPORT == 1) || (RTL8822C_SUPPORT == 1) \ + || (RTL8723F_SUPPORT == 1)) +void rtw_phydm_iqk_trigger(_adapter *adapter) +{ + struct dm_struct *p_dm_odm = adapter_to_phydm(adapter); + u8 clear = _TRUE; + u8 segment = _FALSE; + u8 rfk_forbidden = _FALSE; + + halrf_cmn_info_set(p_dm_odm, HALRF_CMNINFO_RFK_FORBIDDEN, rfk_forbidden); +#if (RTL8822C_SUPPORT == 1) || (RTL8814B_SUPPORT == 1) || (RTL8723F_SUPPORT == 1) + /* halrf_cmn_info_set(p_dm_odm, HALRF_CMNINFO_IQK_SEGMENT, segment); to do */ + halrf_rf_k_connect_trigger(p_dm_odm, _TRUE, SEGMENT_FREE); +#else + /*segment = _rtw_phydm_iqk_segment_chk(adapter);*/ + halrf_cmn_info_set(p_dm_odm, HALRF_CMNINFO_IQK_SEGMENT, segment); + halrf_segment_iqk_trigger(p_dm_odm, clear, segment); +#endif +} +#endif + +void rtw_phydm_iqk_trigger_all(_adapter *adapter) +{ + struct dm_struct *p_dm_odm = adapter_to_phydm(adapter); + u8 clear = _TRUE; + u8 segment = _FALSE; + u8 rfk_forbidden = _FALSE; + +#if ((RTL8822B_SUPPORT == 1) || (RTL8821C_SUPPORT == 1) || (RTL8814B_SUPPORT == 1) || (RTL8822C_SUPPORT == 1) \ + || (RTL8723F_SUPPORT == 1)) + halrf_cmn_info_set(p_dm_odm, HALRF_CMNINFO_RFK_FORBIDDEN, rfk_forbidden); +#if (RTL8822C_SUPPORT == 1) || (RTL8814B_SUPPORT == 1) || (RTL8723F_SUPPORT == 1) + /* halrf_cmn_info_set(p_dm_odm, HALRF_CMNINFO_IQK_SEGMENT, segment); to do */ + halrf_rf_k_connect_trigger(p_dm_odm, _TRUE, SEGMENT_FREE); +#else + /*segment = _rtw_phydm_iqk_segment_chk(adapter);*/ + halrf_cmn_info_set(p_dm_odm, HALRF_CMNINFO_IQK_SEGMENT, segment); + halrf_segment_iqk_trigger(p_dm_odm, clear, segment); +#endif /* (RTL8822C_SUPPORT == 1) || (RTL8814B_SUPPORT == 1) (RTL8723F_SUPPORT == 1) */ +#else + halrf_iqk_trigger(p_dm_odm, _FALSE); +#endif /* ((RTL8822B_SUPPORT == 1) || (RTL8821C_SUPPORT == 1) || (RTL8814B_SUPPORT == 1) || (RTL8822C_SUPPORT == 1)) + (RTL8723F_SUPPORT == 1) */ +} + +void rtw_phydm_iqk_trigger_dbg(_adapter *adapter, bool recovery, bool clear, bool segment) +{ + struct dm_struct *p_dm_odm = adapter_to_phydm(adapter); + +#if ((RTL8822B_SUPPORT == 1) || (RTL8821C_SUPPORT == 1) || (RTL8814B_SUPPORT == 1) || (RTL8822C_SUPPORT == 1)) + halrf_segment_iqk_trigger(p_dm_odm, clear, segment); +#else + halrf_iqk_trigger(p_dm_odm, recovery); +#endif +} +void rtw_phydm_lck_trigger(_adapter *adapter) +{ + struct dm_struct *p_dm_odm = adapter_to_phydm(adapter); + + halrf_lck_trigger(p_dm_odm); +} + +void rtw_hal_phydm_cal_trigger(_adapter *adapter) +{ + struct dm_struct *p_dm_odm = adapter_to_phydm(adapter); + + rtw_ps_deny(adapter, PS_DENY_IOCTL); + LeaveAllPowerSaveModeDirect(adapter); + + rtw_phydm_iqk_trigger_all(adapter); + + rtw_ps_deny_cancel(adapter, PS_DENY_IOCTL); +} + +#ifdef CONFIG_DBG_RF_CAL +void rtw_hal_iqk_test(_adapter *adapter, bool recovery, bool clear, bool segment) +{ + struct dm_struct *p_dm_odm = adapter_to_phydm(adapter); + + rtw_ps_deny(adapter, PS_DENY_IOCTL); + LeaveAllPowerSaveModeDirect(adapter); + + rtw_phydm_ability_backup(adapter); + rtw_phydm_func_disable_all(adapter); + + halrf_cmn_info_set(p_dm_odm, HALRF_CMNINFO_ABILITY, HAL_RF_IQK); + + rtw_phydm_iqk_trigger_dbg(adapter, recovery, clear, segment); + rtw_phydm_ability_restore(adapter); + + rtw_ps_deny_cancel(adapter, PS_DENY_IOCTL); +} + +void rtw_hal_lck_test(_adapter *adapter) +{ + struct dm_struct *p_dm_odm = adapter_to_phydm(adapter); + + rtw_ps_deny(adapter, PS_DENY_IOCTL); + LeaveAllPowerSaveModeDirect(adapter); + + rtw_phydm_ability_backup(adapter); + rtw_phydm_func_disable_all(adapter); + + halrf_cmn_info_set(p_dm_odm, HALRF_CMNINFO_ABILITY, HAL_RF_LCK); + + rtw_phydm_lck_trigger(adapter); + + rtw_phydm_ability_restore(adapter); + rtw_ps_deny_cancel(adapter, PS_DENY_IOCTL); +} +#endif + +#ifdef CONFIG_FW_OFFLOAD_PARAM_INIT +void rtw_hal_update_param_init_fw_offload_cap(_adapter *adapter) +{ + struct dm_struct *p_dm_odm = adapter_to_phydm(adapter); + + if (adapter->registrypriv.fw_param_init) + phydm_fwoffload_ability_init(p_dm_odm, PHYDM_PHY_PARAM_OFFLOAD); + else + phydm_fwoffload_ability_clear(p_dm_odm, PHYDM_PHY_PARAM_OFFLOAD); + + RTW_INFO("Init-Parameter FW offload:%s\n", adapter->registrypriv.fw_param_init ? "enable" : "disable"); +} +#endif + +void record_ra_info(void *p_dm_void, u8 macid, struct cmn_sta_info *p_sta, u64 ra_mask) +{ + struct dm_struct *p_dm = (struct dm_struct *)p_dm_void; + _adapter *adapter = p_dm->adapter; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct macid_ctl_t *macid_ctl = dvobj_to_macidctl(dvobj); + + if (p_sta) { + rtw_macid_ctl_set_bw(macid_ctl, macid, p_sta->ra_info.ra_bw_mode); + rtw_macid_ctl_set_vht_en(macid_ctl, macid, p_sta->ra_info.is_vht_enable); + rtw_macid_ctl_set_rate_bmp0(macid_ctl, macid, ra_mask); + rtw_macid_ctl_set_rate_bmp1(macid_ctl, macid, ra_mask >> 32); + + rtw_update_tx_rate_bmp(adapter_to_dvobj(adapter)); + } +} + +#ifdef CONFIG_SUPPORT_DYNAMIC_TXPWR +void rtw_phydm_fill_desc_dpt(void *dm, u8 *desc, u8 dpt_lv) +{ + struct dm_struct *p_dm = (struct dm_struct *)dm; + _adapter *adapter = p_dm->adapter; + + switch (rtw_get_chip_type(adapter)) { +/* + #ifdef CONFIG_RTL8188F + case RTL8188F: + break; + #endif + + #ifdef CONFIG_RTL8723B + case RTL8723B : + break; + #endif + + #ifdef CONFIG_RTL8703B + case RTL8703B : + break; + #endif + + #ifdef CONFIG_RTL8812A + case RTL8812 : + break; + #endif + + #ifdef CONFIG_RTL8821A + case RTL8821: + break; + #endif + + #ifdef CONFIG_RTL8814A + case RTL8814A : + break; + #endif + + #ifdef CONFIG_RTL8192F + case RTL8192F : + break; + #endif +*/ +/* + #ifdef CONFIG_RTL8192E + case RTL8192E : + SET_TX_DESC_TX_POWER_0_PSET_92E(desc, dpt_lv); + break; + #endif +*/ + #ifdef CONFIG_RTL8822B + case RTL8822B : + SET_TX_DESC_TXPWR_OFSET_8822B(desc, dpt_lv); + break; + #endif + + #ifdef CONFIG_RTL8821C + case RTL8821C : + SET_TX_DESC_TXPWR_OFSET_8821C(desc, dpt_lv); + break; + #endif + + default : + RTW_ERR("%s IC not support dynamic tx power\n", __func__); + break; + } +} +void rtw_phydm_set_dyntxpwr(_adapter *adapter, u8 *desc, u8 mac_id) +{ + struct dm_struct *dm = adapter_to_phydm(adapter); + + odm_set_dyntxpwr(dm, desc, mac_id); +} +#endif + +#ifdef CONFIG_TDMADIG +void rtw_phydm_tdmadig(_adapter *adapter, u8 state) +{ + struct registry_priv *pregistrypriv = &adapter->registrypriv; + struct mlme_priv *pmlmepriv = &(adapter->mlmepriv); + struct dm_struct *dm = adapter_to_phydm(adapter); + u8 tdma_dig_en; + + switch (state) { + case TDMADIG_INIT: + phydm_tdma_dig_para_upd(dm, ENABLE_TDMA, pregistrypriv->tdmadig_en); + phydm_tdma_dig_para_upd(dm, MODE_DECISION, pregistrypriv->tdmadig_mode); + break; + case TDMADIG_NON_INIT: + if(pregistrypriv->tdmadig_dynamic) { + if(pmlmepriv->LinkDetectInfo.bBusyTraffic == _TRUE) + tdma_dig_en = 0; + else + tdma_dig_en = pregistrypriv->tdmadig_en; + phydm_tdma_dig_para_upd(dm, ENABLE_TDMA, tdma_dig_en); + } + break; + default: + break; + + } +} +#endif/*CONFIG_TDMADIG*/ +void rtw_phydm_ops_func_init(struct dm_struct *p_phydm) +{ + struct ra_table *p_ra_t = &p_phydm->dm_ra_table; + + p_ra_t->record_ra_info = record_ra_info; + #ifdef CONFIG_SUPPORT_DYNAMIC_TXPWR + p_phydm->fill_desc_dyntxpwr = rtw_phydm_fill_desc_dpt; + #endif +} +void rtw_phydm_priv_init(_adapter *adapter) +{ + PHAL_DATA_TYPE hal = GET_HAL_DATA(adapter); + struct dm_struct *phydm = &(hal->odmpriv); + + phydm->adapter = adapter; + odm_cmn_info_init(phydm, ODM_CMNINFO_PLATFORM, ODM_CE); +} + +void Init_ODM_ComInfo(_adapter *adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(adapter); + struct dm_struct *pDM_Odm = &(pHalData->odmpriv); + struct pwrctrl_priv *pwrctl = adapter_to_pwrctl(adapter); + int i; + + /*phydm_op_mode could be change for different scenarios: ex: SoftAP - PHYDM_BALANCE_MODE*/ + pHalData->phydm_op_mode = PHYDM_PERFORMANCE_MODE;/*Service one device*/ + rtw_odm_init_ic_type(adapter); + + if (rtw_get_intf_type(adapter) == RTW_GSPI) + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_INTERFACE, ODM_ITRF_SDIO); + else + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_INTERFACE, rtw_get_intf_type(adapter)); + + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_MP_TEST_CHIP, IS_NORMAL_CHIP(pHalData->version_id)); + + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_PATCH_ID, pHalData->CustomerID); + + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_BWIFI_TEST, adapter->registrypriv.wifi_spec); + +#ifdef CONFIG_ADVANCE_OTA + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_ADVANCE_OTA, adapter->registrypriv.adv_ota); +#endif + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_RF_TYPE, pHalData->rf_type); + + { + /* 1 ======= BoardType: ODM_CMNINFO_BOARD_TYPE ======= */ + u8 odm_board_type = ODM_BOARD_DEFAULT; + + if (pHalData->ExternalLNA_2G != 0) { + odm_board_type |= ODM_BOARD_EXT_LNA; + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_EXT_LNA, 1); + } + if (pHalData->external_lna_5g != 0) { + odm_board_type |= ODM_BOARD_EXT_LNA_5G; + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_5G_EXT_LNA, 1); + } + if (pHalData->ExternalPA_2G != 0) { + odm_board_type |= ODM_BOARD_EXT_PA; + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_EXT_PA, 1); + } + if (pHalData->external_pa_5g != 0) { + odm_board_type |= ODM_BOARD_EXT_PA_5G; + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_5G_EXT_PA, 1); + } + if (pHalData->EEPROMBluetoothCoexist) + odm_board_type |= ODM_BOARD_BT; + + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_BOARD_TYPE, odm_board_type); + /* 1 ============== End of BoardType ============== */ + } + + rtw_hal_set_odm_var(adapter, HAL_ODM_REGULATION, NULL, _TRUE); + +#ifdef CONFIG_DFS_MASTER + rtw_odm_update_dfs_region(dvobj); + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_DFS_MASTER_ENABLE, &(adapter_to_rfctl(adapter)->radar_detect_enabled)); +#endif + + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_GPA, pHalData->TypeGPA); + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_APA, pHalData->TypeAPA); + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_GLNA, pHalData->TypeGLNA); + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_ALNA, pHalData->TypeALNA); + + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_RFE_TYPE, pHalData->rfe_type); + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_X_CAP_SETTING, pHalData->crystal_cap); + + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_EXT_TRSW, 0); + + /*Add by YuChen for kfree init*/ + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_REGRFKFREEENABLE, adapter->registrypriv.RegPwrTrimEnable); + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_RFKFREEENABLE, pHalData->RfKFreeEnable); + + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_RF_ANTENNA_TYPE, pHalData->TRxAntDivType); + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_BE_FIX_TX_ANT, pHalData->b_fix_tx_ant); + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_WITH_EXT_ANTENNA_SWITCH, pHalData->with_extenal_ant_switch); + + /* (8822B) efuse 0x3D7 & 0x3D8 for TX PA bias */ + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_EFUSE0X3D7, pHalData->efuse0x3d7); + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_EFUSE0X3D8, pHalData->efuse0x3d8); + + /* waiting for PhyDMV034 support*/ + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_MANUAL_SUPPORTABILITY, &(adapter->registrypriv.phydm_ability)); + /*Add by YuChen for adaptivity init*/ + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_ADAPTIVITY, &(adapter->registrypriv.adaptivity_en)); + phydm_adaptivity_info_init(pDM_Odm, PHYDM_ADAPINFO_CARRIER_SENSE_ENABLE, (adapter->registrypriv.adaptivity_mode != 0) ? TRUE : FALSE); + phydm_adaptivity_info_init(pDM_Odm, PHYDM_ADAPINFO_TH_L2H_INI, adapter->registrypriv.adaptivity_th_l2h_ini); + phydm_adaptivity_info_init(pDM_Odm, PHYDM_ADAPINFO_TH_EDCCA_HL_DIFF, adapter->registrypriv.adaptivity_th_edcca_hl_diff); + + /*halrf info init*/ + halrf_cmn_info_init(pDM_Odm, HALRF_CMNINFO_EEPROM_THERMAL_VALUE, pHalData->eeprom_thermal_meter); + halrf_cmn_info_init(pDM_Odm, HALRF_CMNINFO_PWT_TYPE, 0); + halrf_cmn_info_init(pDM_Odm, HALRF_CMNINFO_MP_POWER_TRACKING_TYPE, pHalData->txpwr_pg_mode); + + if (rtw_odm_adaptivity_needed(adapter) == _TRUE) + rtw_odm_adaptivity_config_msg(RTW_DBGDUMP, adapter); + +#ifdef CONFIG_IQK_PA_OFF + odm_cmn_info_init(pDM_Odm, ODM_CMNINFO_IQKPAOFF, 1); +#endif + rtw_hal_update_iqk_fw_offload_cap(adapter); + #ifdef CONFIG_FW_OFFLOAD_PARAM_INIT + rtw_hal_update_param_init_fw_offload_cap(adapter); + #endif + + /* Pointer reference */ + /*Antenna diversity relative parameters*/ + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_ANT_DIV, &(pHalData->AntDivCfg)); + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_MP_MODE, &(adapter->registrypriv.mp_mode)); + + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_BB_OPERATION_MODE, &(pHalData->phydm_op_mode)); + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_TX_UNI, &(dvobj->traffic_stat.tx_bytes)); + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_RX_UNI, &(dvobj->traffic_stat.rx_bytes)); + + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_BAND, &(pHalData->current_band_type)); + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_FORCED_RATE, &(pHalData->ForcedDataRate)); + + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_SEC_CHNL_OFFSET, &(pHalData->nCur40MhzPrimeSC)); + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_SEC_MODE, &(adapter->securitypriv.dot11PrivacyAlgrthm)); +#ifdef CONFIG_NARROWBAND_SUPPORTING + if ((adapter->registrypriv.rtw_nb_config == RTW_NB_CONFIG_WIDTH_10) + || (adapter->registrypriv.rtw_nb_config == RTW_NB_CONFIG_WIDTH_5)) { + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_BW, &(adapter->registrypriv.rtw_nb_config)); + } + else +#endif + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_BW, &(pHalData->current_channel_bw)); + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_CHNL, &(pHalData->current_channel)); + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_NET_CLOSED, &(adapter->net_closed)); + + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_SCAN, &(pHalData->bScanInProcess)); + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_POWER_SAVING, &(pwrctl->bpower_saving)); + /*Add by Yuchen for phydm beamforming*/ + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_TX_TP, &(dvobj->traffic_stat.cur_tx_tp)); + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_RX_TP, &(dvobj->traffic_stat.cur_rx_tp)); + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_ANT_TEST, &(pHalData->antenna_test)); +#ifdef CONFIG_RTL8723B + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_IS1ANTENNA, &pHalData->EEPROMBluetoothAntNum); + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_RFDEFAULTPATH, &pHalData->ant_path); +#endif /*CONFIG_RTL8723B*/ +#ifdef CONFIG_USB_HCI + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_HUBUSBMODE, &(dvobj->usb_speed)); +#endif + +#ifdef CONFIG_DYNAMIC_SOML + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_ADAPTIVE_SOML, &(adapter->registrypriv.dyn_soml_en)); +#endif +#ifdef CONFIG_RTW_PATH_DIV + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_PATH_DIV, &(adapter->registrypriv.path_div)); +#endif + odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_FCS_MODE, &(pHalData->multi_ch_switch_mode)); + + /*halrf info hook*/ + /* waiting for PhyDMV034 support*/ + halrf_cmn_info_hook(pDM_Odm, HALRF_CMNINFO_MANUAL_RF_SUPPORTABILITY, &(adapter->registrypriv.halrf_ability)); +#ifdef CONFIG_MP_INCLUDED + halrf_cmn_info_hook(pDM_Odm, HALRF_CMNINFO_CON_TX, &(adapter->mppriv.mpt_ctx.is_start_cont_tx)); + halrf_cmn_info_hook(pDM_Odm, HALRF_CMNINFO_SINGLE_TONE, &(adapter->mppriv.mpt_ctx.is_single_tone)); + halrf_cmn_info_hook(pDM_Odm, HALRF_CMNINFO_CARRIER_SUPPRESSION, &(adapter->mppriv.mpt_ctx.is_carrier_suppression)); + halrf_cmn_info_hook(pDM_Odm, HALRF_CMNINFO_MP_RATE_INDEX, &(adapter->mppriv.mpt_ctx.mpt_rate_index)); +#endif/*CONFIG_MP_INCLUDED*/ + for (i = 0; i < ODM_ASSOCIATE_ENTRY_NUM; i++) + phydm_cmn_sta_info_hook(pDM_Odm, i, NULL); + + rtw_phydm_ops_func_init(pDM_Odm); + phydm_dm_early_init(pDM_Odm); + /* TODO */ + /* odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_BT_OPERATION, _FALSE); */ + /* odm_cmn_info_hook(pDM_Odm, ODM_CMNINFO_BT_DISABLE_EDCA, _FALSE); */ +} + + +static u32 edca_setting_UL[HT_IOT_PEER_MAX] = +/*UNKNOWN, REALTEK_90, REALTEK_92SE, BROADCOM,*/ +/*RALINK, ATHEROS, CISCO, MERU, MARVELL, 92U_AP, SELF_AP(DownLink/Tx) */ +{ 0x5e4322, 0xa44f, 0x5e4322, 0x5ea32b, 0x5ea422, 0x5ea322, 0x3ea430, 0x5ea42b, 0x5ea44f, 0x5e4322, 0x5e4322}; + +static u32 edca_setting_DL[HT_IOT_PEER_MAX] = +/*UNKNOWN, REALTEK_90, REALTEK_92SE, BROADCOM,*/ +/*RALINK, ATHEROS, CISCO, MERU, MARVELL, 92U_AP, SELF_AP(UpLink/Rx)*/ +{ 0xa44f, 0x5ea44f, 0x5e4322, 0x5ea42b, 0xa44f, 0xa630, 0x5ea630, 0x5ea42b, 0xa44f, 0xa42b, 0xa42b}; + +static u32 edca_setting_dl_g_mode[HT_IOT_PEER_MAX] = +/*UNKNOWN, REALTEK_90, REALTEK_92SE, BROADCOM,*/ +/*RALINK, ATHEROS, CISCO, MERU, MARVELL, 92U_AP, SELF_AP */ +{ 0x4322, 0xa44f, 0x5e4322, 0xa42b, 0x5e4322, 0x4322, 0xa42b, 0x5ea42b, 0xa44f, 0x5e4322, 0x5ea42b}; + + +struct turbo_edca_setting{ + u32 edca_ul; /* uplink, tx */ + u32 edca_dl; /* downlink, rx */ +}; + +#define TURBO_EDCA_ENT(UL, DL) {UL, DL} + +#if 0 +#define TURBO_EDCA_MODE_NUM 18 +static struct turbo_edca_setting rtw_turbo_edca[TURBO_EDCA_MODE_NUM] = { + TURBO_EDCA_ENT(0xa42b, 0xa42b), /* mode 0 */ + TURBO_EDCA_ENT(0x431c, 0x431c), /* mode 1 */ + TURBO_EDCA_ENT(0x4319, 0x4319), /* mode 2 */ + + TURBO_EDCA_ENT(0x5ea42b, 0x5ea42b), /* mode 3 */ + TURBO_EDCA_ENT(0x5e431c, 0x5e431c), /* mode 4 */ + TURBO_EDCA_ENT(0x5e4319, 0x5e4319), /* mode 5 */ + + TURBO_EDCA_ENT(0x6ea42b, 0x6ea42b), /* mode 6 */ + TURBO_EDCA_ENT(0x6e431c, 0x6e431c), /* mode 7 */ + TURBO_EDCA_ENT(0x6e4319, 0x6e4319), /* mode 8 */ + + TURBO_EDCA_ENT(0x5ea42b, 0xa42b), /* mode 9 */ + TURBO_EDCA_ENT(0x5e431c, 0x431c), /* mode 10 */ + TURBO_EDCA_ENT(0x5e4319, 0x4319), /* mode 11 */ + + TURBO_EDCA_ENT(0x6ea42b, 0xa42b), /* mode 12 */ + TURBO_EDCA_ENT(0x6e431c, 0x431c), /* mode 13 */ + TURBO_EDCA_ENT(0x6e4319, 0x4319), /* mode 14 */ + + TURBO_EDCA_ENT(0x431c, 0x5e431c), /* mode 15 */ + + TURBO_EDCA_ENT(0xa42b, 0x5ea42b), /* mode 16 */ + + TURBO_EDCA_ENT(0x138642b, 0x431c), /* mode 17 */ +}; +#else +#define TURBO_EDCA_MODE_NUM 8 +static struct turbo_edca_setting rtw_turbo_edca[TURBO_EDCA_MODE_NUM] = { + /* { UL, DL } */ + TURBO_EDCA_ENT(0x5e431c, 0x431c), /* mode 0 */ + + TURBO_EDCA_ENT(0x431c, 0x431c), /* mode 1 */ + + TURBO_EDCA_ENT(0x5e431c, 0x5e431c), /* mode 2 */ + + TURBO_EDCA_ENT(0x5ea42b, 0x5ea42b), /* mode 3 */ + + TURBO_EDCA_ENT(0x5ea42b, 0x431c), /* mode 4 */ + + TURBO_EDCA_ENT(0x6ea42b, 0x6ea42b), /* mode 5 */ + + TURBO_EDCA_ENT(0xa42b, 0xa42b), /* mode 6 */ + + TURBO_EDCA_ENT(0x5e431c, 0xa42b), /* mode 7 */ +}; +#endif + +void rtw_hal_turbo_edca(_adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct recv_priv *precvpriv = &(adapter->recvpriv); + struct registry_priv *pregpriv = &adapter->registrypriv; + struct mlme_ext_priv *pmlmeext = &(adapter->mlmeextpriv); + struct mlme_ext_info *pmlmeinfo = &(pmlmeext->mlmext_info); + + /* Parameter suggested by Scott */ +#if 0 + u32 EDCA_BE_UL = edca_setting_UL[p_mgnt_info->iot_peer]; + u32 EDCA_BE_DL = edca_setting_DL[p_mgnt_info->iot_peer]; +#endif + u32 EDCA_BE_UL = 0x5ea42b; + u32 EDCA_BE_DL = 0x00a42b; + u8 ic_type = rtw_get_chip_type(adapter); + + u8 iot_peer = 0; + u8 wireless_mode = 0xFF; /* invalid value */ + u8 traffic_index; + u32 edca_param; + u64 cur_tx_bytes = 0; + u64 cur_rx_bytes = 0; + u8 bbtchange = _TRUE; + u8 is_bias_on_rx = _FALSE; + u8 is_linked = _FALSE; + u8 interface_type; + + if (hal_data->dis_turboedca == 1) + return; + + if (rtw_mi_check_status(adapter, MI_ASSOC)) + is_linked = _TRUE; + + if (is_linked != _TRUE) { + precvpriv->is_any_non_be_pkts = _FALSE; + return; + } + + if ((pregpriv->wifi_spec == 1)) { /* || (pmlmeinfo->HT_enable == 0)) */ + precvpriv->is_any_non_be_pkts = _FALSE; + return; + } + + interface_type = rtw_get_intf_type(adapter); + wireless_mode = pmlmeext->cur_wireless_mode; + + iot_peer = pmlmeinfo->assoc_AP_vendor; + + if (iot_peer >= HT_IOT_PEER_MAX) { + precvpriv->is_any_non_be_pkts = _FALSE; + return; + } + + if (ic_type == RTL8188E) { + if ((iot_peer == HT_IOT_PEER_RALINK) || (iot_peer == HT_IOT_PEER_ATHEROS)) + is_bias_on_rx = _TRUE; + } + + /* Check if the status needs to be changed. */ + if ((bbtchange) || (!precvpriv->is_any_non_be_pkts)) { + cur_tx_bytes = dvobj->traffic_stat.cur_tx_bytes; + cur_rx_bytes = dvobj->traffic_stat.cur_rx_bytes; + + /* traffic, TX or RX */ + if (is_bias_on_rx) { + if (cur_tx_bytes > (cur_rx_bytes << 2)) { + /* Uplink TP is present. */ + traffic_index = UP_LINK; + } else { + /* Balance TP is present. */ + traffic_index = DOWN_LINK; + } + } else { + if (cur_rx_bytes > (cur_tx_bytes << 2)) { + /* Downlink TP is present. */ + traffic_index = DOWN_LINK; + } else { + /* Balance TP is present. */ + traffic_index = UP_LINK; + } + } +#if 0 + if ((p_dm_odm->dm_edca_table.prv_traffic_idx != traffic_index) + || (!p_dm_odm->dm_edca_table.is_current_turbo_edca)) +#endif + { + if (interface_type == RTW_PCIE) { + EDCA_BE_UL = 0x6ea42b; + EDCA_BE_DL = 0x6ea42b; + } + + /* 92D txop can't be set to 0x3e for cisco1250 */ + if ((iot_peer == HT_IOT_PEER_CISCO) && (wireless_mode == ODM_WM_N24G)) { + EDCA_BE_DL = edca_setting_DL[iot_peer]; + EDCA_BE_UL = edca_setting_UL[iot_peer]; + } + /* merge from 92s_92c_merge temp*/ + else if ((iot_peer == HT_IOT_PEER_CISCO) && ((wireless_mode == ODM_WM_G) || (wireless_mode == (ODM_WM_B | ODM_WM_G)) || (wireless_mode == ODM_WM_A) || (wireless_mode == ODM_WM_B))) + EDCA_BE_DL = edca_setting_dl_g_mode[iot_peer]; + else if ((iot_peer == HT_IOT_PEER_AIRGO) && ((wireless_mode == ODM_WM_G) || (wireless_mode == ODM_WM_A))) + EDCA_BE_DL = 0xa630; + else if (iot_peer == HT_IOT_PEER_MARVELL) { + EDCA_BE_DL = edca_setting_DL[iot_peer]; + EDCA_BE_UL = edca_setting_UL[iot_peer]; + } else if (iot_peer == HT_IOT_PEER_ATHEROS) { + /* Set DL EDCA for Atheros peer to 0x3ea42b.*/ + /* Suggested by SD3 Wilson for ASUS TP issue.*/ + EDCA_BE_DL = edca_setting_DL[iot_peer]; + } + + if ((ic_type == RTL8812) || (ic_type == RTL8821) || (ic_type == RTL8192E) || (ic_type == RTL8192F)) { /* add 8812AU/8812AE */ + EDCA_BE_UL = 0x5ea42b; + EDCA_BE_DL = 0x5ea42b; + + RTW_DBG("8812A: EDCA_BE_UL=0x%x EDCA_BE_DL =0x%x\n", EDCA_BE_UL, EDCA_BE_DL); + } + + if (interface_type == RTW_PCIE && + ((ic_type == RTL8822B) + || (ic_type == RTL8822C) + || (ic_type == RTL8814A) || (ic_type == RTL8814B))) { + EDCA_BE_UL = 0x6ea42b; + EDCA_BE_DL = 0x6ea42b; + } + + if ((ic_type == RTL8822B) + && (interface_type == RTW_SDIO)) + EDCA_BE_DL = 0x00431c; + +#ifdef CONFIG_RTW_TPT_MODE + if ( dvobj->tpt_mode > 0 ) { + EDCA_BE_UL = dvobj->edca_be_ul; + EDCA_BE_DL = dvobj->edca_be_dl; + } +#endif /* CONFIG_RTW_TPT_MODE */ + + /* keep this condition at last check */ + if (hal_data->dis_turboedca == 2) { + + if (hal_data->edca_param_mode < TURBO_EDCA_MODE_NUM) { + + struct turbo_edca_setting param; + + param = rtw_turbo_edca[hal_data->edca_param_mode]; + + EDCA_BE_UL = param.edca_ul; + EDCA_BE_DL = param.edca_dl; + + } else { + + EDCA_BE_UL = hal_data->edca_param_mode; + EDCA_BE_DL = hal_data->edca_param_mode; + } + } + + if (traffic_index == DOWN_LINK) + edca_param = EDCA_BE_DL; + else + edca_param = EDCA_BE_UL; + +#ifdef CONFIG_EXTEND_LOWRATE_TXOP +#define TXOP_CCK1M 0x01A6 +#define TXOP_CCK2M 0x00E6 +#define TXOP_CCK5M 0x006B +#define TXOP_OFD6M 0x0066 +#define TXOP_MCS6M 0x0061 +{ + struct sta_info *psta; + struct macid_ctl_t *macid_ctl = dvobj_to_macidctl(dvobj); + u8 mac_id, role, current_rate_id; + + /* search all used & connect2AP macid */ + for (mac_id = 0; mac_id < macid_ctl->num; mac_id++) { + if (rtw_macid_is_used(macid_ctl, mac_id)) { + role = GET_H2CCMD_MSRRPT_PARM_ROLE(&(macid_ctl->h2c_msr[mac_id])); + if (role != H2C_MSR_ROLE_AP) + continue; + + psta = macid_ctl->sta[mac_id]; + current_rate_id = rtw_get_current_tx_rate(adapter, psta); + /* Check init tx_rate==1M and set 0x508[31:16]==0x019B(unit 32us) if it is */ + switch (current_rate_id) { + case DESC_RATE1M: + edca_param &= 0x0000FFFF; + edca_param |= (TXOP_CCK1M<<16); + break; + case DESC_RATE2M: + edca_param &= 0x0000FFFF; + edca_param |= (TXOP_CCK2M<<16); + break; + case DESC_RATE5_5M: + edca_param &= 0x0000FFFF; + edca_param |= (TXOP_CCK5M<<16); + break; + case DESC_RATE6M: + edca_param &= 0x0000FFFF; + edca_param |= (TXOP_OFD6M<<16); + break; + case DESC_RATEMCS0: + edca_param &= 0x0000FFFF; + edca_param |= (TXOP_MCS6M<<16); + break; + default: + break; + } + } + } +} +#endif /* CONFIG_EXTEND_LOWRATE_TXOP */ + +#ifdef CONFIG_RTW_CUSTOMIZE_BEEDCA + edca_param = CONFIG_RTW_CUSTOMIZE_BEEDCA; +#endif + + if ( edca_param != hal_data->ac_param_be) { + + rtw_hal_set_hwreg(adapter, HW_VAR_AC_PARAM_BE, (u8 *)(&edca_param)); + + RTW_INFO("Turbo EDCA =0x%x\n", edca_param); + } + + hal_data->prv_traffic_idx = traffic_index; + } + + hal_data->is_turbo_edca = _TRUE; + } else { + /* */ + /* Turn Off EDCA turbo here. */ + /* Restore original EDCA according to the declaration of AP. */ + /* */ + if (hal_data->is_turbo_edca) { + edca_param = hal_data->ac_param_be; + rtw_hal_set_hwreg(adapter, HW_VAR_AC_PARAM_BE, (u8 *)(&edca_param)); + hal_data->is_turbo_edca = _FALSE; + } + } + +} + +s8 rtw_dm_get_min_rssi(_adapter *adapter) +{ + struct macid_ctl_t *macid_ctl = adapter_to_macidctl(adapter); + struct sta_info *sta; + s8 min_rssi = 127, rssi; + int i; + + for (i = 0; i < MACID_NUM_SW_LIMIT; i++) { + sta = macid_ctl->sta[i]; + if (!sta || !GET_H2CCMD_MSRRPT_PARM_OPMODE(macid_ctl->h2c_msr + i) + || is_broadcast_mac_addr(sta->cmn.mac_addr)) + continue; + rssi = sta->cmn.rssi_stat.rssi; + if (rssi >= 0 && min_rssi > rssi) + min_rssi = rssi; + } + + return min_rssi == 127 ? 0 : min_rssi; +} + +s8 rtw_phydm_get_min_rssi(_adapter *adapter) +{ + struct dm_struct *phydm = adapter_to_phydm(adapter); + s8 rssi_min = 0; + + rssi_min = phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_RSSI_MIN); + return rssi_min; +} + +u8 rtw_phydm_get_cur_igi(_adapter *adapter) +{ + struct dm_struct *phydm = adapter_to_phydm(adapter); + u8 cur_igi = 0; + + cur_igi = phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_CURR_IGI); + return cur_igi; +} + +bool rtw_phydm_get_edcca_flag(_adapter *adapter) +{ + struct dm_struct *phydm = adapter_to_phydm(adapter); + bool cur_edcca_flag = 0; + + cur_edcca_flag = phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_EDCCA_FLAG); + return cur_edcca_flag; +} + +u32 rtw_phydm_get_phy_cnt(_adapter *adapter, enum phy_cnt cnt) +{ + struct dm_struct *phydm = adapter_to_phydm(adapter); + + if (cnt == FA_OFDM) + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_FA_OFDM); + else if (cnt == FA_CCK) + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_FA_CCK); + else if (cnt == FA_TOTAL) + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_FA_TOTAL); + else if (cnt == CCA_OFDM) + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_CCA_OFDM); + else if (cnt == CCA_CCK) + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_CCA_CCK); + else if (cnt == CCA_ALL) + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_CCA_ALL); + else if (cnt == CRC32_OK_VHT) + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_CRC32_OK_VHT); + else if (cnt == CRC32_OK_HT) + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_CRC32_OK_HT); + else if (cnt == CRC32_OK_LEGACY) + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_CRC32_OK_LEGACY); + else if (cnt == CRC32_OK_CCK) + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_CRC32_OK_CCK); + else if (cnt == CRC32_ERROR_VHT) + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_CRC32_ERROR_VHT); + else if (cnt == CRC32_ERROR_HT) + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_CRC32_ERROR_HT); + else if (cnt == CRC32_ERROR_LEGACY) + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_CRC32_ERROR_LEGACY); + else if (cnt == CRC32_ERROR_CCK) + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_CRC32_ERROR_CCK); + else + return 0; +} + +u8 rtw_phydm_is_iqk_in_progress(_adapter *adapter) +{ + u8 rts = _FALSE; + struct dm_struct *podmpriv = adapter_to_phydm(adapter); + + odm_acquire_spin_lock(podmpriv, RT_IQK_SPINLOCK); + if (podmpriv->rf_calibrate_info.is_iqk_in_progress == _TRUE) { + RTW_ERR("IQK InProgress\n"); + rts = _TRUE; + } + odm_release_spin_lock(podmpriv, RT_IQK_SPINLOCK); + + return rts; +} + +void SetHalODMVar( + PADAPTER Adapter, + HAL_ODM_VARIABLE eVariable, + void *pValue1, + BOOLEAN bSet) +{ + struct dm_struct *podmpriv = adapter_to_phydm(Adapter); + /* _irqL irqL; */ + switch (eVariable) { + case HAL_ODM_STA_INFO: { + struct sta_info *psta = (struct sta_info *)pValue1; + + if (bSet) { + RTW_INFO("### Set STA_(%d) info ###\n", psta->cmn.mac_id); + psta->cmn.dm_ctrl = STA_DM_CTRL_ACTIVE; + phydm_cmn_sta_info_hook(podmpriv, psta->cmn.mac_id, &(psta->cmn)); + } else { + RTW_INFO("### Clean STA_(%d) info ###\n", psta->cmn.mac_id); + /* _enter_critical_bh(&pHalData->odm_stainfo_lock, &irqL); */ + psta->cmn.dm_ctrl = 0; + phydm_cmn_sta_info_hook(podmpriv, psta->cmn.mac_id, NULL); + + /* _exit_critical_bh(&pHalData->odm_stainfo_lock, &irqL); */ + } + } + break; + case HAL_ODM_P2P_STATE: + odm_cmn_info_update(podmpriv, ODM_CMNINFO_WIFI_DIRECT, bSet); + break; + case HAL_ODM_WIFI_DISPLAY_STATE: + odm_cmn_info_update(podmpriv, ODM_CMNINFO_WIFI_DISPLAY, bSet); + break; + case HAL_ODM_REGULATION: + /* used to auto enable/disable adaptivity by SD7 */ + phydm_adaptivity_info_update(podmpriv, PHYDM_ADAPINFO_DOMAIN_CODE_2G, 0); + phydm_adaptivity_info_update(podmpriv, PHYDM_ADAPINFO_DOMAIN_CODE_5G, 0); + break; + case HAL_ODM_INITIAL_GAIN: { + u8 rx_gain = *((u8 *)(pValue1)); + /*printk("rx_gain:%x\n",rx_gain);*/ + if (rx_gain == 0xff) {/*restore rx gain*/ + /*odm_write_dig(podmpriv,pDigTable->backup_ig_value);*/ + odm_pause_dig(podmpriv, PHYDM_RESUME, PHYDM_PAUSE_LEVEL_0, rx_gain); + } else { + /*pDigTable->backup_ig_value = pDigTable->cur_ig_value;*/ + /*odm_write_dig(podmpriv,rx_gain);*/ + odm_pause_dig(podmpriv, PHYDM_PAUSE, PHYDM_PAUSE_LEVEL_0, rx_gain); + } + } + break; + case HAL_ODM_RX_INFO_DUMP: { + u8 cur_igi = 0; + s8 rssi_min; + void *sel; + + sel = pValue1; + cur_igi = rtw_phydm_get_cur_igi(Adapter); + rssi_min = rtw_phydm_get_min_rssi(Adapter); + + _RTW_PRINT_SEL(sel, "============ Rx Info dump ===================\n"); + _RTW_PRINT_SEL(sel, "is_linked = %d, rssi_min = %d(%%)(%d(%%)), current_igi = 0x%x\n" + , podmpriv->is_linked, rssi_min, rtw_dm_get_min_rssi(Adapter), cur_igi); + _RTW_PRINT_SEL(sel, "cnt_cck_fail = %d, cnt_ofdm_fail = %d, Total False Alarm = %d\n", + rtw_phydm_get_phy_cnt(Adapter, FA_CCK), + rtw_phydm_get_phy_cnt(Adapter, FA_OFDM), + rtw_phydm_get_phy_cnt(Adapter, FA_TOTAL)); + + if (podmpriv->is_linked) { + _RTW_PRINT_SEL(sel, "rx_rate = %s", HDATA_RATE(podmpriv->rx_rate)); + if (IS_HARDWARE_TYPE_8814A(Adapter)) + _RTW_PRINT_SEL(sel, " rssi_a = %d(%%), rssi_b = %d(%%), rssi_c = %d(%%), rssi_d = %d(%%)\n", + podmpriv->rssi_a, podmpriv->rssi_b, podmpriv->rssi_c, podmpriv->rssi_d); + else + _RTW_PRINT_SEL(sel, " rssi_a = %d(%%), rssi_b = %d(%%)\n", podmpriv->rssi_a, podmpriv->rssi_b); +#ifdef DBG_RX_SIGNAL_DISPLAY_RAW_DATA + rtw_dump_raw_rssi_info(Adapter, sel); +#endif + } + } + break; + case HAL_ODM_RX_Dframe_INFO: { + void *sel; + + sel = pValue1; + + /*_RTW_PRINT_SEL(sel , "HAL_ODM_RX_Dframe_INFO\n");*/ +#ifdef DBG_RX_DFRAME_RAW_DATA + rtw_dump_rx_dframe_info(Adapter, sel); +#endif + } + break; + +#ifdef CONFIG_ANTENNA_DIVERSITY + case HAL_ODM_ANTDIV_SELECT: { + u8 antenna = (*(u8 *)pValue1); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + /*switch antenna*/ + odm_update_rx_idle_ant(&pHalData->odmpriv, antenna); + /*RTW_INFO("==> HAL_ODM_ANTDIV_SELECT, Ant_(%s)\n", (antenna == MAIN_ANT) ? "MAIN_ANT" : "AUX_ANT");*/ + + } + break; +#endif + + default: + break; + } +} + +void GetHalODMVar( + PADAPTER Adapter, + HAL_ODM_VARIABLE eVariable, + void *pValue1, + void *pValue2) +{ + struct dm_struct *podmpriv = adapter_to_phydm(Adapter); + + switch (eVariable) { +#ifdef CONFIG_ANTENNA_DIVERSITY + case HAL_ODM_ANTDIV_SELECT: { + struct phydm_fat_struct *pDM_FatTable = &podmpriv->dm_fat_table; + *((u8 *)pValue1) = pDM_FatTable->rx_idle_ant; + } + break; +#endif + case HAL_ODM_INITIAL_GAIN: + *((u8 *)pValue1) = rtw_phydm_get_cur_igi(Adapter); + break; + default: + break; + } +} + +#ifdef RTW_HALMAC +#include "../hal_halmac.h" +#endif +bool rtw_phydm_rfe_ctrl_gpio( + _adapter *adapter, + u8 gpio_num +) +{ + #ifdef RTW_HALMAC + if(rtw_halmac_rfe_ctrl_cfg(adapter_to_dvobj(adapter), gpio_num)) + return _TRUE; + else + #endif/*RTW_HALMAC*/ + return _FALSE; +} + +enum hal_status +rtw_phydm_fw_iqk( + struct dm_struct *p_dm_odm, + u8 clear, + u8 segment +) +{ + #ifdef RTW_HALMAC + struct _ADAPTER *adapter = p_dm_odm->adapter; + + if (rtw_halmac_iqk(adapter_to_dvobj(adapter), clear, segment) == 0) + return HAL_STATUS_SUCCESS; + #endif + return HAL_STATUS_FAILURE; +} + +enum hal_status +rtw_phydm_cfg_phy_para( + struct dm_struct *p_dm_odm, + enum phydm_halmac_param config_type, + u32 offset, + u32 data, + u32 mask, + enum rf_path e_rf_path, + u32 delay_time) +{ + #ifdef RTW_HALMAC + struct _ADAPTER *adapter = p_dm_odm->adapter; + struct rtw_phy_parameter para; + + switch (config_type) { + case PHYDM_HALMAC_CMD_MAC_W8: + para.cmd = 0; /* MAC register */ + para.data.mac.offset = offset; + para.data.mac.value = data; + para.data.mac.msk = mask; + para.data.mac.msk_en = (mask) ? 1 : 0; + para.data.mac.size = 1; + break; + case PHYDM_HALMAC_CMD_MAC_W16: + para.cmd = 0; /* MAC register */ + para.data.mac.offset = offset; + para.data.mac.value = data; + para.data.mac.msk = mask; + para.data.mac.msk_en = (mask) ? 1 : 0; + para.data.mac.size = 2; + break; + case PHYDM_HALMAC_CMD_MAC_W32: + para.cmd = 0; /* MAC register */ + para.data.mac.offset = offset; + para.data.mac.value = data; + para.data.mac.msk = mask; + para.data.mac.msk_en = (mask) ? 1 : 0; + para.data.mac.size = 4; + break; + case PHYDM_HALMAC_CMD_BB_W8: + para.cmd = 1; /* BB register */ + para.data.bb.offset = offset; + para.data.bb.value = data; + para.data.bb.msk = mask; + para.data.bb.msk_en = (mask) ? 1 : 0; + para.data.bb.size = 1; + break; + case PHYDM_HALMAC_CMD_BB_W16: + para.cmd = 1; /* BB register */ + para.data.bb.offset = offset; + para.data.bb.value = data; + para.data.bb.msk = mask; + para.data.bb.msk_en = (mask) ? 1 : 0; + para.data.bb.size = 2; + break; + case PHYDM_HALMAC_CMD_BB_W32: + para.cmd = 1; /* BB register */ + para.data.bb.offset = offset; + para.data.bb.value = data; + para.data.bb.msk = mask; + para.data.bb.msk_en = (mask) ? 1 : 0; + para.data.bb.size = 4; + break; + case PHYDM_HALMAC_CMD_RF_W: + para.cmd = 2; /* RF register */ + para.data.rf.offset = offset; + para.data.rf.value = data; + para.data.rf.msk = mask; + para.data.rf.msk_en = (mask) ? 1 : 0; + if (e_rf_path == RF_PATH_A) + para.data.rf.path = 0; + else if (e_rf_path == RF_PATH_B) + para.data.rf.path = 1; + else if (e_rf_path == RF_PATH_C) + para.data.rf.path = 2; + else if (e_rf_path == RF_PATH_D) + para.data.rf.path = 3; + else + para.data.rf.path = 0; + break; + case PHYDM_HALMAC_CMD_DELAY_US: + para.cmd = 3; /* Delay */ + para.data.delay.unit = 0; /* microsecond */ + para.data.delay.value = delay_time; + break; + case PHYDM_HALMAC_CMD_DELAY_MS: + para.cmd = 3; /* Delay */ + para.data.delay.unit = 1; /* millisecond */ + para.data.delay.value = delay_time; + break; + case PHYDM_HALMAC_CMD_END: + para.cmd = 0xFF; /* End command */ + break; + default: + return HAL_STATUS_FAILURE; + } + + if (rtw_halmac_cfg_phy_para(adapter_to_dvobj(adapter), ¶)) + return HAL_STATUS_FAILURE; + #endif /*RTW_HALMAC*/ + return HAL_STATUS_SUCCESS; +} + + +#ifdef CONFIG_LPS_LCLK_WD_TIMER +void rtw_phydm_wd_lps_lclk_hdl(_adapter *adapter) +{ + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(adapter); + struct sta_priv *pstapriv = &adapter->stapriv; + struct sta_info *psta = NULL; + bool is_linked = _FALSE; + + if (!rtw_is_hw_init_completed(adapter)) + return; + + if (rtw_mi_check_status(adapter, MI_ASSOC)) + is_linked = _TRUE; + + if (is_linked == _FALSE) + return; + + psta = rtw_get_stainfo(pstapriv, get_bssid(pmlmepriv)); + if (psta == NULL) + return; + + odm_cmn_info_update(&pHalData->odmpriv, ODM_CMNINFO_LINK, is_linked); + + phydm_watchdog_lps_32k(&pHalData->odmpriv); + +#ifdef CONFIG_LPS_PG + if (pwrpriv->lps_level == LPS_PG) { + if (rtw_hal_set_lps_pg_info_cmd(adapter) == _FAIL) + RTW_INFO(FUNC_ADPT_FMT": Send PG H2C command Fail! \n", + FUNC_ADPT_ARG(adapter)); + } +#endif /* CONFIG_LPS_PG */ +} + +void rtw_phydm_watchdog_in_lps_lclk(_adapter *adapter) +{ + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + struct sta_priv *pstapriv = &adapter->stapriv; + u8 cur_igi = 0; + s8 min_rssi = 0; + + if (!rtw_is_hw_init_completed(adapter)) + return; + + cur_igi = rtw_phydm_get_cur_igi(adapter); + min_rssi = rtw_dm_get_min_rssi(adapter); + /*RTW_INFO("%s "ADPT_FMT" cur_ig_value=%d, min_rssi = %d\n", __func__, ADPT_ARG(adapter), cur_igi, min_rssi);*/ + + if (min_rssi <= 0) + return; + + if ((cur_igi > min_rssi + 5) || + (cur_igi < min_rssi - 5)) { +#ifdef CONFIG_LPS + rtw_dm_in_lps_wk_cmd(adapter); +#endif + } +} +#endif /*CONFIG_LPS_LCLK_WD_TIMER*/ + +void dump_sta_traffic(void *sel, _adapter *adapter, struct sta_info *psta) +{ + struct ra_sta_info *ra_info; + u8 curr_sgi = _FALSE; + u32 tx_tp_mbips, rx_tp_mbips, bi_tp_mbips; + + if (!psta) + return; + RTW_PRINT_SEL(sel, "\n"); + RTW_PRINT_SEL(sel, "====== mac_id : %d [" MAC_FMT "] ======\n", + psta->cmn.mac_id, MAC_ARG(psta->cmn.mac_addr)); + + if (is_client_associated_to_ap(psta->padapter)) + RTW_PRINT_SEL(sel, "BCN counts : %d (per-%d second), DTIM Period:%d\n", + rtw_get_bcn_cnt(psta->padapter) / 2, 1, rtw_get_bcn_dtim_period(psta->padapter)); + + ra_info = &psta->cmn.ra_info; + curr_sgi = rtw_get_current_tx_sgi(adapter, psta); + RTW_PRINT_SEL(sel, "tx_rate : %s(%s) rx_rate : %s, rx_rate_bmc : %s, rssi : %d %%\n" + , HDATA_RATE(rtw_get_current_tx_rate(adapter, psta)), (curr_sgi) ? "S" : "L" + , HDATA_RATE((psta->curr_rx_rate & 0x7F)), HDATA_RATE((psta->curr_rx_rate_bmc & 0x7F)), psta->cmn.rssi_stat.rssi + ); + + if (0) { + RTW_PRINT_SEL(sel, "tx_bytes:%llu(%llu - %llu)\n" + , psta->sta_stats.tx_bytes - psta->sta_stats.last_tx_bytes + , psta->sta_stats.tx_bytes, psta->sta_stats.last_tx_bytes + ); + RTW_PRINT_SEL(sel, "rx_uc_bytes:%llu(%llu - %llu)\n" + , sta_rx_uc_bytes(psta) - sta_last_rx_uc_bytes(psta) + , sta_rx_uc_bytes(psta), sta_last_rx_uc_bytes(psta) + ); + RTW_PRINT_SEL(sel, "rx_mc_bytes:%llu(%llu - %llu)\n" + , psta->sta_stats.rx_mc_bytes - psta->sta_stats.last_rx_mc_bytes + , psta->sta_stats.rx_mc_bytes, psta->sta_stats.last_rx_mc_bytes + ); + RTW_PRINT_SEL(sel, "rx_bc_bytes:%llu(%llu - %llu)\n" + , psta->sta_stats.rx_bc_bytes - psta->sta_stats.last_rx_bc_bytes + , psta->sta_stats.rx_bc_bytes, psta->sta_stats.last_rx_bc_bytes + ); + } + + _RTW_PRINT_SEL(sel, "RTW: [TP] "); + tx_tp_mbips = psta->sta_stats.tx_tp_kbits >> 10; + rx_tp_mbips = psta->sta_stats.rx_tp_kbits >> 10; + bi_tp_mbips = tx_tp_mbips + rx_tp_mbips; + + if (tx_tp_mbips) + _RTW_PRINT_SEL(sel, "Tx : %d(Mbps) ", tx_tp_mbips); + else + _RTW_PRINT_SEL(sel, "Tx : %d(Kbps) ", psta->sta_stats.tx_tp_kbits); + + if (rx_tp_mbips) + _RTW_PRINT_SEL(sel, "Rx : %d(Mbps) ", rx_tp_mbips); + else + _RTW_PRINT_SEL(sel, "Rx : %d(Kbps) ", psta->sta_stats.rx_tp_kbits); + + if (bi_tp_mbips) + _RTW_PRINT_SEL(sel, "Total : %d(Mbps)\n", bi_tp_mbips); + else + _RTW_PRINT_SEL(sel, "Total : %d(Kbps)\n", psta->sta_stats.tx_tp_kbits + psta->sta_stats.rx_tp_kbits); + + + _RTW_PRINT_SEL(sel, "RTW: [Smooth TP] "); + tx_tp_mbips = psta->sta_stats.smooth_tx_tp_kbits >> 10; + rx_tp_mbips = psta->sta_stats.smooth_rx_tp_kbits >> 10; + bi_tp_mbips = tx_tp_mbips + rx_tp_mbips; + if (tx_tp_mbips) + _RTW_PRINT_SEL(sel, "Tx : %d(Mbps) ", tx_tp_mbips); + else + _RTW_PRINT_SEL(sel, "Tx : %d(Kbps) ", psta->sta_stats.smooth_tx_tp_kbits); + + if (rx_tp_mbips) + _RTW_PRINT_SEL(sel, "Rx : %d(Mbps) ", rx_tp_mbips); + else + _RTW_PRINT_SEL(sel, "Rx : %d(Kbps) ", psta->sta_stats.smooth_rx_tp_kbits); + + if (bi_tp_mbips) + _RTW_PRINT_SEL(sel, "Total : %d(Mbps)\n", bi_tp_mbips); + else + _RTW_PRINT_SEL(sel, "Total : %d(Kbps)\n", psta->sta_stats.smooth_tx_tp_kbits + psta->sta_stats.rx_tp_kbits); + + #if 0 + RTW_PRINT_SEL(sel, "Moving-AVG TP {Tx,Rx,Total} = { %d , %d , %d } Mbps\n\n", + (psta->cmn.tx_moving_average_tp << 3), (psta->cmn.rx_moving_average_tp << 3), + (psta->cmn.tx_moving_average_tp + psta->cmn.rx_moving_average_tp) << 3); + #endif +} + +void dump_sta_info(void *sel, struct sta_info *psta) +{ + struct ra_sta_info *ra_info; + u8 curr_tx_sgi = _FALSE; + u8 curr_tx_rate = 0; + + if (!psta) + return; + + ra_info = &psta->cmn.ra_info; + + RTW_PRINT_SEL(sel, "============ STA [" MAC_FMT "] ===================\n", + MAC_ARG(psta->cmn.mac_addr)); + RTW_PRINT_SEL(sel, "mac_id : %d\n", psta->cmn.mac_id); + RTW_PRINT_SEL(sel, "wireless_mode : 0x%02x\n", psta->wireless_mode); + RTW_PRINT_SEL(sel, "mimo_type : %d\n", psta->cmn.mimo_type); + RTW_PRINT_SEL(sel, "static smps : %s\n", (psta->cmn.sm_ps == SM_PS_STATIC) ? "Y" : "N"); + RTW_PRINT_SEL(sel, "bw_mode : %s, ra_bw_mode : %s\n", + ch_width_str(psta->cmn.bw_mode), ch_width_str(ra_info->ra_bw_mode)); + RTW_PRINT_SEL(sel, "rate_id : %d\n", ra_info->rate_id); + RTW_PRINT_SEL(sel, "rssi : %d (%%), rssi_level : %d\n", psta->cmn.rssi_stat.rssi, ra_info->rssi_level); + RTW_PRINT_SEL(sel, "is_support_sgi : %s, is_vht_enable : %s\n", + (ra_info->is_support_sgi) ? "Y" : "N", (ra_info->is_vht_enable) ? "Y" : "N"); + RTW_PRINT_SEL(sel, "disable_ra : %s, disable_pt : %s\n", + (ra_info->disable_ra) ? "Y" : "N", (ra_info->disable_pt) ? "Y" : "N"); + RTW_PRINT_SEL(sel, "is_noisy : %s\n", (ra_info->is_noisy) ? "Y" : "N"); + RTW_PRINT_SEL(sel, "txrx_state : %d\n", ra_info->txrx_state);/*0: uplink, 1:downlink, 2:bi-direction*/ + + curr_tx_sgi = rtw_get_current_tx_sgi(psta->padapter, psta); + curr_tx_rate = rtw_get_current_tx_rate(psta->padapter, psta); + RTW_PRINT_SEL(sel, "curr_tx_rate : %s (%s)\n", + HDATA_RATE(curr_tx_rate), (curr_tx_sgi) ? "S" : "L"); + RTW_PRINT_SEL(sel, "curr_tx_bw : %s\n", ch_width_str(ra_info->curr_tx_bw)); + RTW_PRINT_SEL(sel, "curr_retry_ratio : %d\n", ra_info->curr_retry_ratio); + RTW_PRINT_SEL(sel, "ra_mask : 0x%016llx\n\n", ra_info->ramask); +} + +void rtw_phydm_ra_registed(_adapter *adapter, struct sta_info *psta) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct macid_ctl_t *macid_ctl = dvobj_to_macidctl(dvobj); + + if (psta == NULL) { + RTW_ERR(FUNC_ADPT_FMT" sta is NULL\n", FUNC_ADPT_ARG(adapter)); + rtw_warn_on(1); + return; + } + + if (psta->cmn.mac_id >= macid_ctl->num) + return; + + phydm_ra_registed(&hal_data->odmpriv, psta->cmn.mac_id, psta->cmn.rssi_stat.rssi); + dump_sta_info(RTW_DBGDUMP, psta); +} + +static void init_phydm_info(_adapter *adapter) +{ + PHAL_DATA_TYPE hal_data = GET_HAL_DATA(adapter); + struct dm_struct *phydm = &(hal_data->odmpriv); + + odm_cmn_info_init(phydm, ODM_CMNINFO_FW_VER, hal_data->firmware_version); + odm_cmn_info_init(phydm, ODM_CMNINFO_FW_SUB_VER, hal_data->firmware_sub_version); +} + +#ifdef CONFIG_CTRL_TXSS_BY_TP +void rtw_phydm_trx_cfg(_adapter *adapter, bool tx_1ss) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + enum bb_path txpath = BB_PATH_AB; + enum bb_path rxpath = BB_PATH_AB; + /*is_2tx = _FALSE for 8822B, or BB_PATH_AUTO for PATH_DIVERSITY for 8822B*/ + enum bb_path txpath_1ss = BB_PATH_A; + + rtw_hal_get_trx_path(adapter_to_dvobj(adapter), NULL, &txpath, &rxpath); + txpath = (tx_1ss) ? BB_PATH_A : txpath; + + if (phydm_api_trx_mode(adapter_to_phydm(adapter), txpath, rxpath, txpath_1ss) == FALSE) + RTW_ERR("%s failed\n", __func__); +} +#endif + +u8 rtw_hal_runtime_trx_path_decision(_adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + enum bb_path txpath; + enum bb_path rxpath; + int i; + u8 rst = _FAIL; + + rtw_hal_get_trx_path(adapter_to_dvobj(adapter), NULL, &txpath, &rxpath); + if (!txpath) { + RTW_ERR("%s tx_path_bmp is empty\n", __func__); + rtw_warn_on(1); + goto exit; + } + if (!rxpath) { + RTW_ERR("%s rx_path_bmp is empty\n", __func__); + rtw_warn_on(1); + goto exit; + } + + tx_path_nss_set_default(hal_data->txpath_nss, hal_data->txpath_num_nss + , GET_HAL_TX_PATH_BMP(adapter)); + +#if defined(CONFIG_RTL8192F) || defined(CONFIG_RTL8822B) ||defined(CONFIG_RTL8822C) +{ + enum bb_path txpath_1ss; + + if (txpath == BB_PATH_AB) { + switch (hal_data->max_tx_cnt) { + case 2: + #ifdef CONFIG_RTW_TX_NPATH_EN + if (adapter->registrypriv.tx_npath == 1) + txpath_1ss = BB_PATH_AB; + else + #endif + #ifdef CONFIG_RTW_PATH_DIV + if (adapter->registrypriv.path_div == 1) /* path diversity, support 2sts TX */ + txpath_1ss = BB_PATH_AUTO; + else + #endif + txpath_1ss = BB_PATH_A; + break; + case 1: + #ifdef CONFIG_RTW_PATH_DIV + if (adapter->registrypriv.path_div == 1) /* path diversity, no support 2sts TX */ + txpath = txpath_1ss = BB_PATH_AUTO; + else + #endif + txpath = txpath_1ss = BB_PATH_A; + break; + default: + RTW_ERR("%s invalid max_tx_cnt:%u\n", __func__ + , hal_data->max_tx_cnt); + rtw_warn_on(1); + goto exit; + } + } else + txpath_1ss = txpath; + + if (hal_data->txpath_nss[0] != txpath_1ss) { + hal_data->txpath_nss[0] = txpath_1ss; + if (txpath_1ss == BB_PATH_AUTO) + hal_data->txpath_num_nss[0] = 1; + else { + hal_data->txpath_num_nss[0] = 0; + for (i = 0; i < RF_PATH_MAX; i++) { + if (txpath_1ss & BIT(i)) + hal_data->txpath_num_nss[0]++; + } + } + } +} +#elif defined(CONFIG_RTL8814B) +{ + /* 8814B is always full-TX */ + tx_path_nss_set_full_tx(hal_data->txpath_nss, hal_data->txpath_num_nss, txpath); +} +#elif defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8192E) +{ + #ifdef CONFIG_RTW_TX_NPATH_EN + if (adapter->registrypriv.tx_npath == 1) + tx_path_nss_set_full_tx(hal_data->txpath_nss, hal_data->txpath_num_nss, txpath); + #endif +} +#endif + + hal_data->txpath = txpath; + hal_data->rxpath = rxpath; + dump_hal_runtime_trx_mode(RTW_DBGDUMP, adapter); + rst = _SUCCESS; + +exit: + return rst; +} + +/* +* trx_mode init - 8822B / 8822C / 8192F +* 1ssNTx - 8192E / 8812A / 8822B / 8822C / 8192F +* Path-diversity - 8822B / 8822C / 8192F +* PHYDM API - phydm_api_trx_mode +*/ +static u8 rtw_phydm_config_trx_path(_adapter *adapter) +{ + u8 rst = _SUCCESS; + +#if defined(CONFIG_RTL8192F) || defined(CONFIG_RTL8822B) ||defined(CONFIG_RTL8822C) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + enum bb_path txpath = hal_data->txpath; + enum bb_path rxpath = hal_data->rxpath; + enum bb_path txpath_1ss = hal_data->txpath_nss[0]; + + if (phydm_api_trx_mode(adapter_to_phydm(adapter), txpath, rxpath, txpath_1ss) == FALSE) { + RTW_ERR("%s txpath=0x%x, rxpath=0x%x, txpath_1ss=0x%x fail\n", __func__ + , txpath, rxpath, txpath_1ss); + rtw_warn_on(1); + rst = _FAIL; + } +} +#elif defined(CONFIG_RTL8814B) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + enum bb_path txpath = hal_data->txpath; + enum bb_path rxpath = hal_data->rxpath; + + if (txpath == BB_PATH_ABCD && rxpath == BB_PATH_ABCD) + rst = config_phydm_trx_mode_8814b(adapter_to_phydm(adapter), txpath, rxpath); + else + rst = config_phydm_trx_mode_ext_8814b(adapter_to_phydm(adapter), txpath, + rxpath, + txpath, txpath, txpath); + if (rst == FALSE) { + RTW_ERR("%s txpath=0x%x, rxpath=0x%x fail\n", __func__ + , txpath, rxpath); + rtw_warn_on(1); + rst = _FAIL; + } +} +#elif defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8192E) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + + if (hal_data->txpath_num_nss[0] == 2) + phydm_tx_2path(adapter_to_phydm(adapter)); +} +#endif + + return rst; +} + +void rtw_phydm_init(_adapter *adapter) +{ + PHAL_DATA_TYPE hal_data = GET_HAL_DATA(adapter); + struct dm_struct *phydm = &(hal_data->odmpriv); + + rtw_phydm_config_trx_path(adapter); + init_phydm_info(adapter); + hal_data->phydm_init_result = odm_dm_init(phydm); + +#ifdef CONFIG_CUSTOMER01_SMART_ANTENNA + phydm_pathb_q_matrix_rotate_en(phydm); +#endif +} + +bool rtw_phydm_set_crystal_cap(_adapter *adapter, u8 crystal_cap) +{ + PHAL_DATA_TYPE hal_data = GET_HAL_DATA(adapter); + struct dm_struct *phydm = &(hal_data->odmpriv); + + return phydm_set_crystal_cap_reg(phydm, crystal_cap); +} + +#ifdef CONFIG_LPS_PG +/* +static void _lps_pg_state_update(_adapter *adapter) +{ + u8 is_in_lpspg = _FALSE; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(adapter); + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(adapter); + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + struct sta_priv *pstapriv = &adapter->stapriv; + struct sta_info *psta = NULL; + + if ((pwrpriv->lps_level == LPS_PG) && (pwrpriv->pwr_mode != PS_MODE_ACTIVE) && (pwrpriv->rpwm <= PS_STATE_S2)) + is_in_lpspg = _TRUE; + psta = rtw_get_stainfo(pstapriv, get_bssid(pmlmepriv)); + + if (psta) + psta->cmn.ra_info.disable_ra = (is_in_lpspg) ? _TRUE : _FALSE; +} +*/ +void rtw_phydm_lps_pg_hdl(_adapter *adapter, struct sta_info *sta, bool in_lpspg) +{ + struct dm_struct *phydm = adapter_to_phydm(adapter); + /*u8 rate_id;*/ + + if(sta == NULL) { + RTW_ERR("%s sta is null\n", __func__); + rtw_warn_on(1); + return; + } + + if (in_lpspg) { + sta->cmn.ra_info.disable_ra = _TRUE; + sta->cmn.ra_info.disable_pt = _TRUE; + /*TODO : DRV fix tx rate*/ + /*rate_id = phydm_get_rate_from_rssi_lv(phydm, sta->cmn.mac_id);*/ + } else { + sta->cmn.ra_info.disable_ra = _FALSE; + sta->cmn.ra_info.disable_pt = _FALSE; + } + + rtw_phydm_ra_registed(adapter, sta); +} +#endif + +/*#define DBG_PHYDM_STATE_CHK*/ + + +static u8 _rtw_phydm_rfk_condition_check(_adapter *adapter, u8 is_scaning, u8 ifs_linked) +{ + u8 rfk_allowed = _TRUE; + + #ifdef CONFIG_SKIP_RFK_IN_DM + rfk_allowed = _FALSE; + if (0) + RTW_ERR("[RFK-CHK] RF-K not allowed due to CONFIG_SKIP_RFK_IN_DM\n"); + return rfk_allowed; + #endif + + #ifdef CONFIG_MCC_MODE + /*not in MCC State*/ + if (MCC_EN(adapter) && + rtw_hal_check_mcc_status(adapter, MCC_STATUS_DOING_MCC)) { + rfk_allowed = _FALSE; + if (0) + RTW_INFO("[RFK-CHK] RF-K not allowed due to doing MCC\n"); + return rfk_allowed; + } + #endif + + #if defined(CONFIG_TDLS) && defined(CONFIG_TDLS_CH_SW) + + #endif + + if (ifs_linked) { + if (is_scaning) { + rfk_allowed = _FALSE; + RTW_DBG("[RFK-CHK] RF-K not allowed due to ifaces under site-survey\n"); + } + else { + rfk_allowed = rtw_mi_stayin_union_ch_chk(adapter) ? _TRUE : _FALSE; + if (rfk_allowed == _FALSE) + RTW_ERR("[RFK-CHK] RF-K not allowed due to ld_iface not stayin union ch\n"); + } + } + + return rfk_allowed; +} + +#if ((RTL8822B_SUPPORT == 1) || (RTL8821C_SUPPORT == 1) || (RTL8814B_SUPPORT == 1) || (RTL8822C_SUPPORT == 1)) +static u8 _rtw_phydm_iqk_segment_chk(_adapter *adapter, u8 ifs_linked) +{ + u8 iqk_sgt = _FALSE; + +#if 0 + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + if (ifs_linked && (dvobj->traffic_stat.cur_tx_tp > 2 || dvobj->traffic_stat.cur_rx_tp > 2)) + rst = _TRUE; +#else + if (ifs_linked) + iqk_sgt = _TRUE; +#endif + return iqk_sgt; +} +#endif + +/*check the tx low rate while unlinked to any AP;for pwr tracking */ +static u8 _rtw_phydm_pwr_tracking_rate_check(_adapter *adapter) +{ + int i; + _adapter *iface; + u8 if_tx_rate = 0xFF; + u8 tx_rate = 0xFF; + struct mlme_ext_priv *pmlmeext = NULL; + struct mlme_priv *pmlmepriv = &adapter->mlmepriv; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(adapter); + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + pmlmeext = &(iface->mlmeextpriv); + if ((iface) && rtw_is_adapter_up(iface)) { +#ifdef CONFIG_P2P + if (!rtw_p2p_chk_role(&(iface)->wdinfo, P2P_ROLE_DISABLE)) + if_tx_rate = IEEE80211_OFDM_RATE_6MB; + else +#endif + if_tx_rate = pmlmeext->tx_rate; + + if (if_tx_rate < tx_rate) { + /*5G limit ofdm rate*/ + if (pHalData->current_channel > 14) { + if (!IS_CCK_RATE(if_tx_rate)) + tx_rate = if_tx_rate; + } else { + tx_rate = if_tx_rate; + } + } + RTW_DBG("%s i=%d if_tx_rate =0x%x\n", __func__, i, if_tx_rate); + } + } + + /*suggest by RF James,unlinked setting ofdm rate*/ + if (tx_rate == 0xFF) + tx_rate = IEEE80211_OFDM_RATE_6MB; + + RTW_DBG("%s tx_low_rate (unlinked to any AP)=0x%x\n", __func__, tx_rate); + return tx_rate; +} + +#ifdef CONFIG_DYNAMIC_SOML +void rtw_dyn_soml_byte_update(_adapter *adapter, u8 data_rate, u32 size) +{ + struct dm_struct *phydm = adapter_to_phydm(adapter); + + phydm_soml_bytes_acq(phydm, data_rate, size); +} + +void rtw_dyn_soml_para_set(_adapter *adapter, u8 train_num, u8 intvl, + u8 period, u8 delay) +{ + struct dm_struct *phydm = adapter_to_phydm(adapter); + + phydm_adaptive_soml_para_set(phydm, train_num, intvl, period, delay); + RTW_INFO("%s.\n", __func__); +} + +void rtw_dyn_soml_config(_adapter *adapter) +{ + RTW_INFO("%s.\n", __func__); + + if (adapter->registrypriv.dyn_soml_en == 1) { + /* Must after phydm_adaptive_soml_init() */ + rtw_hal_set_hwreg(adapter , HW_VAR_SET_SOML_PARAM , NULL); + RTW_INFO("dyn_soml_en = 1\n"); + } else { + if (adapter->registrypriv.dyn_soml_en == 2) { + rtw_dyn_soml_para_set(adapter, + adapter->registrypriv.dyn_soml_train_num, + adapter->registrypriv.dyn_soml_interval, + adapter->registrypriv.dyn_soml_period, + adapter->registrypriv.dyn_soml_delay); + RTW_INFO("dyn_soml_en = 2\n"); + RTW_INFO("dyn_soml_en, param = %d, %d, %d, %d\n", + adapter->registrypriv.dyn_soml_train_num, + adapter->registrypriv.dyn_soml_interval, + adapter->registrypriv.dyn_soml_period, + adapter->registrypriv.dyn_soml_delay); + } else if (adapter->registrypriv.dyn_soml_en == 0) { + RTW_INFO("dyn_soml_en = 0\n"); + } else + RTW_ERR("%s, wrong setting: dyn_soml_en = %d\n", __func__, + adapter->registrypriv.dyn_soml_en); + } +} +#endif + +void rtw_phydm_set_rrsr(_adapter *adapter, u32 rrsr_value, bool write_rrsr) +{ + struct dm_struct *phydm = adapter_to_phydm(adapter); + u32 temp_rrsr =0xFFFFFFFF; + + if (adapter->registrypriv.set_rrsr_value != 0xFFFFFFFF) + temp_rrsr = adapter->registrypriv.set_rrsr_value; + else + temp_rrsr = rrsr_value; + + odm_cmn_info_update(phydm, ODM_CMNINFO_RRSR_VAL, temp_rrsr); + if(write_rrsr) + phydm_rrsr_set_register(phydm, temp_rrsr); +} +void rtw_phydm_dyn_rrsr_en(_adapter *adapter, bool en_rrsr) +{ + struct dm_struct *phydm = adapter_to_phydm(adapter); + + phydm_rrsr_en(phydm, en_rrsr); +} +void rtw_phydm_read_efuse(_adapter *adapter) +{ + PHAL_DATA_TYPE hal_data = GET_HAL_DATA(adapter); + struct dm_struct *phydm = &(hal_data->odmpriv); + + /*PHYDM API - thermal trim*/ + phydm_get_thermal_trim_offset(phydm); + /*PHYDM API - power trim*/ + phydm_get_power_trim_offset(phydm); +} + +#ifdef CONFIG_LPS_PWR_TRACKING +void rtw_phydm_pwr_tracking_directly(_adapter *adapter) +{ + PHAL_DATA_TYPE hal_data = GET_HAL_DATA(adapter); + u8 rfk_forbidden = _TRUE; + u8 is_linked = _FALSE; + + if (rtw_mi_check_status(adapter, MI_ASSOC)) + is_linked = _TRUE; + + rfk_forbidden = (_rtw_phydm_rfk_condition_check(adapter, hal_data->bScanInProcess, is_linked) == _TRUE) ? _FALSE : _TRUE; + halrf_cmn_info_set(&hal_data->odmpriv, HALRF_CMNINFO_RFK_FORBIDDEN, rfk_forbidden); + + odm_txpowertracking_direct_ce(&hal_data->odmpriv); +} +#endif + +void rtw_phydm_watchdog(_adapter *adapter, bool in_lps) +{ + u8 bLinked = _FALSE; + u8 bsta_state = _FALSE; + u8 bBtDisabled = _TRUE; + u8 rfk_forbidden = _FALSE; + #if ((RTL8822B_SUPPORT == 1) || (RTL8821C_SUPPORT == 1) || (RTL8814B_SUPPORT == 1) || (RTL8822C_SUPPORT == 1)) + u8 segment_iqk = _FALSE; + #endif + u8 tx_unlinked_low_rate = 0xFF; + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(adapter); + + if (!rtw_is_hw_init_completed(adapter)) { + RTW_DBG("%s skip due to hw_init_completed == FALSE\n", __func__); + return; + } + if (rtw_mi_check_fwstate(adapter, WIFI_UNDER_SURVEY)) + pHalData->bScanInProcess = _TRUE; + else + pHalData->bScanInProcess = _FALSE; + + if (rtw_mi_check_status(adapter, MI_ASSOC)) { + bLinked = _TRUE; + if (rtw_mi_check_status(adapter, MI_STA_LINKED)) + bsta_state = _TRUE; + } + + odm_cmn_info_update(&pHalData->odmpriv, ODM_CMNINFO_LINK, bLinked); + odm_cmn_info_update(&pHalData->odmpriv, ODM_CMNINFO_STATION_STATE, bsta_state); + + #ifdef CONFIG_BT_COEXIST + bBtDisabled = rtw_btcoex_IsBtDisabled(adapter); + odm_cmn_info_update(&pHalData->odmpriv, ODM_CMNINFO_BT_ENABLED, + (bBtDisabled == _TRUE) ? _FALSE : _TRUE); + #else + odm_cmn_info_update(&pHalData->odmpriv, ODM_CMNINFO_BT_ENABLED, _FALSE); + #endif /* CONFIG_BT_COEXIST */ + + rfk_forbidden = (_rtw_phydm_rfk_condition_check(adapter, pHalData->bScanInProcess, bLinked) == _TRUE) ? _FALSE : _TRUE; + halrf_cmn_info_set(&pHalData->odmpriv, HALRF_CMNINFO_RFK_FORBIDDEN, rfk_forbidden); + + #if ((RTL8822B_SUPPORT == 1) || (RTL8821C_SUPPORT == 1) || (RTL8814B_SUPPORT == 1) || (RTL8822C_SUPPORT == 1)) + segment_iqk = _rtw_phydm_iqk_segment_chk(adapter, bLinked); + halrf_cmn_info_set(&pHalData->odmpriv, HALRF_CMNINFO_IQK_SEGMENT, segment_iqk); + #endif + #ifdef DBG_PHYDM_STATE_CHK + RTW_INFO("%s rfk_forbidden = %s, segment_iqk = %s\n", + __func__, (rfk_forbidden) ? "Y" : "N", (segment_iqk) ? "Y" : "N"); + #endif + + if (bLinked == _FALSE) { + tx_unlinked_low_rate = _rtw_phydm_pwr_tracking_rate_check(adapter); + halrf_cmn_info_set(&pHalData->odmpriv, HALRF_CMNINFO_RATE_INDEX, tx_unlinked_low_rate); + } + + /*if (!rtw_mi_stayin_union_band_chk(adapter)) { + #ifdef DBG_PHYDM_STATE_CHK + RTW_ERR("Not stay in union band, skip phydm\n"); + #endif + goto _exit; + }*/ + + #ifdef CONFIG_TDMADIG + rtw_phydm_tdmadig(adapter, TDMADIG_NON_INIT); + #endif/*CONFIG_TDMADIG*/ + + if (in_lps) + phydm_watchdog_lps(&pHalData->odmpriv); + else + phydm_watchdog(&pHalData->odmpriv); + + #ifdef CONFIG_RTW_ACS + rtw_acs_update_current_info(adapter); + #endif + + return; +} + + diff --git a/hal/hal_dm.h b/hal/hal_dm.h new file mode 100644 index 0000000..863ffb7 --- /dev/null +++ b/hal/hal_dm.h @@ -0,0 +1,121 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#ifndef __HAL_DM_H__ +#define __HAL_DM_H__ + +#define adapter_to_phydm(adapter) (&(GET_HAL_DATA(adapter)->odmpriv)) +#define dvobj_to_phydm(dvobj) adapter_to_phydm(dvobj_get_primary_adapter(dvobj)) +#ifdef CONFIG_TDMADIG +void rtw_phydm_tdmadig(_adapter *adapter, u8 state); +#endif +void rtw_phydm_priv_init(_adapter *adapter); +void Init_ODM_ComInfo(_adapter *adapter); +void rtw_phydm_init(_adapter *adapter); + +void rtw_hal_turbo_edca(_adapter *adapter); +u8 rtw_phydm_is_iqk_in_progress(_adapter *adapter); + +void GetHalODMVar( + PADAPTER Adapter, + HAL_ODM_VARIABLE eVariable, + void *pValue1, + void *pValue2); +void SetHalODMVar( + PADAPTER Adapter, + HAL_ODM_VARIABLE eVariable, + void *pValue1, + BOOLEAN bSet); + +void rtw_phydm_ra_registed(_adapter *adapter, struct sta_info *psta); + +#ifdef CONFIG_DYNAMIC_SOML +void rtw_dyn_soml_byte_update(_adapter *adapter, u8 data_rate, u32 size); +void rtw_dyn_soml_para_set(_adapter *adapter, u8 train_num, u8 intvl, + u8 period, u8 delay); +void rtw_dyn_soml_config(_adapter *adapter); +#endif +void rtw_phydm_set_rrsr(_adapter *adapter, u32 rrsr_value, bool write_rrsr); +void rtw_phydm_dyn_rrsr_en(_adapter *adapter, bool en_rrsr); +void rtw_phydm_watchdog(_adapter *adapter, bool in_lps); + +void rtw_hal_update_iqk_fw_offload_cap(_adapter *adapter); +void dump_sta_info(void *sel, struct sta_info *psta); +void dump_sta_traffic(void *sel, _adapter *adapter, struct sta_info *psta); + +void rtw_hal_phydm_cal_trigger(_adapter *adapter); +#ifdef CONFIG_DBG_RF_CAL +void rtw_hal_iqk_test(_adapter *adapter, bool recovery, bool clear, bool segment); +void rtw_hal_lck_test(_adapter *adapter); +#endif + +s8 rtw_dm_get_min_rssi(_adapter *adapter); +s8 rtw_phydm_get_min_rssi(_adapter *adapter); +u8 rtw_phydm_get_cur_igi(_adapter *adapter); +bool rtw_phydm_get_edcca_flag(_adapter *adapter); + + +#ifdef CONFIG_LPS_LCLK_WD_TIMER +extern void phydm_rssi_monitor_check(void *p_dm_void); + +void rtw_phydm_wd_lps_lclk_hdl(_adapter *adapter); +void rtw_phydm_watchdog_in_lps_lclk(_adapter *adapter); +#endif +#ifdef CONFIG_TDMADIG +enum rtw_tdmadig_state{ + TDMADIG_INIT, + TDMADIG_NON_INIT, +}; +#endif +enum phy_cnt { + FA_OFDM, + FA_CCK, + FA_TOTAL, + CCA_OFDM, + CCA_CCK, + CCA_ALL, + CRC32_OK_VHT, + CRC32_OK_HT, + CRC32_OK_LEGACY, + CRC32_OK_CCK, + CRC32_ERROR_VHT, + CRC32_ERROR_HT, + CRC32_ERROR_LEGACY, + CRC32_ERROR_CCK, +}; +u32 rtw_phydm_get_phy_cnt(_adapter *adapter, enum phy_cnt cnt); +#if ((RTL8822B_SUPPORT == 1) || (RTL8821C_SUPPORT == 1) || (RTL8814B_SUPPORT == 1) || (RTL8822C_SUPPORT == 1) \ + || (RTL8723F_SUPPORT == 1)) +void rtw_phydm_iqk_trigger(_adapter *adapter); +#endif +void rtw_phydm_read_efuse(_adapter *adapter); +bool rtw_phydm_set_crystal_cap(_adapter *adapter, u8 crystal_cap); + +#ifdef CONFIG_SUPPORT_DYNAMIC_TXPWR +void rtw_phydm_set_dyntxpwr(_adapter *adapter, u8 *desc, u8 mac_id); +#endif + +#ifdef CONFIG_LPS_PG +void rtw_phydm_lps_pg_hdl(_adapter *adapter, struct sta_info *sta, bool in_lpspg); +#endif +#ifdef CONFIG_LPS_PWR_TRACKING +void rtw_phydm_pwr_tracking_directly(_adapter *adapter); +#endif + +#ifdef CONFIG_CTRL_TXSS_BY_TP +void rtw_phydm_trx_cfg(_adapter *adapter, bool tx_1ss); +#endif +u8 rtw_hal_runtime_trx_path_decision(_adapter *adapter); +bool rtw_phydm_rfe_ctrl_gpio(_adapter *adapter, u8 gpio_num); +#endif /* __HAL_DM_H__ */ diff --git a/hal/hal_dm_acs.c b/hal/hal_dm_acs.c new file mode 100644 index 0000000..0fa3414 --- /dev/null +++ b/hal/hal_dm_acs.c @@ -0,0 +1,607 @@ +/****************************************************************************** + * + * Copyright(c) 2014 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#include +#include + + +#if defined(CONFIG_RTW_ACS) || defined(CONFIG_BACKGROUND_NOISE_MONITOR) +static void _rtw_bss_nums_count(_adapter *adapter, u8 *pbss_nums) +{ + struct mlme_priv *pmlmepriv = &(adapter->mlmepriv); + _queue *queue = &(pmlmepriv->scanned_queue); + struct wlan_network *pnetwork = NULL; + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + + _list *plist, *phead; + _irqL irqL; + int chan_idx = -1; + + if (pbss_nums == NULL) { + RTW_ERR("%s pbss_nums is null pointer\n", __func__); + return; + } + _rtw_memset(pbss_nums, 0, MAX_CHANNEL_NUM); + + _enter_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); + phead = get_list_head(queue); + plist = get_next(phead); + while (1) { + if (rtw_end_of_queue_search(phead, plist) == _TRUE) + break; + + pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list); + if (!pnetwork) + break; + chan_idx = rtw_chset_search_ch(adapter_to_chset(adapter), pnetwork->network.Configuration.DSConfig); + if ((chan_idx == -1) || (chan_idx >= MAX_CHANNEL_NUM)) { + RTW_ERR("%s can't get chan_idx(CH:%d)\n", + __func__, pnetwork->network.Configuration.DSConfig); + chan_idx = 0; + } + /*if (pnetwork->network.Reserved[0] != BSS_TYPE_PROB_REQ)*/ + + pbss_nums[chan_idx]++; + + plist = get_next(plist); + } + _exit_critical_bh(&(pmlmepriv->scanned_queue.lock), &irqL); +} + +u8 rtw_get_ch_num_by_idx(_adapter *adapter, u8 idx) +{ + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + RT_CHANNEL_INFO *pch_set = rfctl->channel_set; + u8 max_chan_nums = rfctl->max_chan_nums; + + if (idx >= max_chan_nums) + return 0; + return pch_set[idx].ChannelNum; +} +#endif /*defined(CONFIG_RTW_ACS) || defined(CONFIG_BACKGROUND_NOISE_MONITOR)*/ + + +#ifdef CONFIG_RTW_ACS +void rtw_acs_version_dump(void *sel, _adapter *adapter) +{ + _RTW_PRINT_SEL(sel, "RTK_ACS VER_%d\n", RTK_ACS_VERSION); +} +u8 rtw_phydm_clm_ratio(_adapter *adapter) +{ + struct dm_struct *phydm = adapter_to_phydm(adapter); + + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_CLM_RATIO); +} +u8 rtw_phydm_nhm_ratio(_adapter *adapter) +{ + struct dm_struct *phydm = adapter_to_phydm(adapter); + + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_NHM_ENV_RATIO); +} + +u8 rtw_phydm_nhm_noise_pwr(_adapter *adapter) +{ + struct dm_struct *phydm = adapter_to_phydm(adapter); + + return phydm_cmn_info_query(phydm, (enum phydm_info_query) PHYDM_INFO_NHM_PWR); +} + +void rtw_acs_reset(_adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct auto_chan_sel *pacs = &hal_data->acs; + + _rtw_memset(pacs, 0, sizeof(struct auto_chan_sel)); + #ifdef CONFIG_RTW_ACS_DBG + rtw_acs_adv_reset(adapter); + #endif /*CONFIG_RTW_ACS_DBG*/ +} + +#ifdef CONFIG_RTW_ACS_DBG +u8 rtw_is_acs_igi_valid(_adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct auto_chan_sel *pacs = &hal_data->acs; + + if ((pacs->igi) && ((pacs->igi >= 0x1E) || (pacs->igi < 0x60))) + return _TRUE; + + return _FALSE; +} +void rtw_acs_adv_setting(_adapter *adapter, RT_SCAN_TYPE scan_type, u16 scan_time, u8 igi, u8 bw) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct auto_chan_sel *pacs = &hal_data->acs; + struct mlme_ext_priv *pmlmeext = &adapter->mlmeextpriv; + struct mlme_ext_info *pmlmeinfo = &pmlmeext->mlmext_info; + + pacs->scan_type = scan_type; + pacs->scan_time = scan_time; + pacs->igi = igi; + pacs->bw = bw; + RTW_INFO("[ACS] ADV setting - scan_type:%c, ch_ms:%d(ms), igi:0x%02x, bw:%d\n", + pacs->scan_type ? 'A' : 'P', pacs->scan_time, pacs->igi, pacs->bw); +} +void rtw_acs_adv_reset(_adapter *adapter) +{ + rtw_acs_adv_setting(adapter, SCAN_ACTIVE, 0, 0, 0); +} +#endif /*CONFIG_RTW_ACS_DBG*/ + +void rtw_acs_trigger(_adapter *adapter, u16 scan_time_ms, u8 scan_chan, enum NHM_PID pid) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct dm_struct *phydm = adapter_to_phydm(adapter); +#if (RTK_ACS_VERSION == 3) + struct clm_para_info clm_para; + struct nhm_para_info nhm_para; + struct env_trig_rpt trig_rpt; + + scan_time_ms -= 10; + + init_acs_clm(clm_para, scan_time_ms); + + if (pid == NHM_PID_IEEE_11K_HIGH) + init_11K_high_nhm(nhm_para, scan_time_ms); + else if (pid == NHM_PID_IEEE_11K_LOW) + init_11K_low_nhm(nhm_para, scan_time_ms); + else + init_acs_nhm(nhm_para, scan_time_ms); + + hal_data->acs.trig_rst = phydm_env_mntr_trigger(phydm, &nhm_para, &clm_para, &trig_rpt); + if (hal_data->acs.trig_rst == (NHM_SUCCESS | CLM_SUCCESS)) { + hal_data->acs.trig_rpt.clm_rpt_stamp = trig_rpt.clm_rpt_stamp; + hal_data->acs.trig_rpt.nhm_rpt_stamp = trig_rpt.nhm_rpt_stamp; + /*RTW_INFO("[ACS] trigger success (rst = 0x%02x, clm_stamp:%d, nhm_stamp:%d)\n", + hal_data->acs.trig_rst, hal_data->acs.trig_rpt.clm_rpt_stamp, hal_data->acs.trig_rpt.nhm_rpt_stamp);*/ + } else + RTW_ERR("[ACS] trigger failed (rst = 0x%02x)\n", hal_data->acs.trig_rst); +#else + phydm_ccx_monitor_trigger(phydm, scan_time_ms); +#endif + + hal_data->acs.trigger_ch = scan_chan; + hal_data->acs.triggered = _TRUE; + + #ifdef CONFIG_RTW_ACS_DBG + RTW_INFO("[ACS] Trigger CH:%d, Times:%d\n", hal_data->acs.trigger_ch, scan_time_ms); + #endif +} +void rtw_acs_get_rst(_adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct dm_struct *phydm = adapter_to_phydm(adapter); + int chan_idx = -1; + u8 cur_chan = hal_data->acs.trigger_ch; + + if (cur_chan == 0) + return; + + if (!hal_data->acs.triggered) + return; + + chan_idx = rtw_chset_search_ch(adapter_to_chset(adapter), cur_chan); + if ((chan_idx == -1) || (chan_idx >= MAX_CHANNEL_NUM)) { + RTW_ERR("[ACS] %s can't get chan_idx(CH:%d)\n", __func__, cur_chan); + return; + } +#if (RTK_ACS_VERSION == 3) + if (!(hal_data->acs.trig_rst == (NHM_SUCCESS | CLM_SUCCESS))) { + RTW_ERR("[ACS] get_rst return, due to acs trigger failed\n"); + return; + } + + { + struct env_mntr_rpt rpt = {0}; + u8 rst; + + rst = phydm_env_mntr_result(phydm, &rpt); + if ((rst == (NHM_SUCCESS | CLM_SUCCESS)) && + (rpt.clm_rpt_stamp == hal_data->acs.trig_rpt.clm_rpt_stamp) && + (rpt.nhm_rpt_stamp == hal_data->acs.trig_rpt.nhm_rpt_stamp)){ + hal_data->acs.clm_ratio[chan_idx] = rpt.clm_ratio; + hal_data->acs.nhm_ratio[chan_idx] = rpt.nhm_env_ratio; + hal_data->acs.env_mntr_rpt[chan_idx] = (rpt.nhm_noise_pwr -100); + _rtw_memcpy(&hal_data->acs.nhm[chan_idx][0], rpt.nhm_result, NHM_RPT_NUM); + + /*RTW_INFO("[ACS] get_rst success (rst = 0x%02x, clm_stamp:%d:%d, nhm_stamp:%d:%d)\n", + rst, + hal_data->acs.trig_rpt.clm_rpt_stamp, rpt.clm_rpt_stamp, + hal_data->acs.trig_rpt.nhm_rpt_stamp, rpt.nhm_rpt_stamp);*/ + } else { + RTW_ERR("[ACS] get_rst failed (rst = 0x%02x, clm_stamp:%d:%d, nhm_stamp:%d:%d)\n", + rst, + hal_data->acs.trig_rpt.clm_rpt_stamp, rpt.clm_rpt_stamp, + hal_data->acs.trig_rpt.nhm_rpt_stamp, rpt.nhm_rpt_stamp); + } + } + +#else + phydm_ccx_monitor_result(phydm); + + hal_data->acs.clm_ratio[chan_idx] = rtw_phydm_clm_ratio(adapter); + hal_data->acs.nhm_ratio[chan_idx] = rtw_phydm_nhm_ratio(adapter); +#endif + hal_data->acs.triggered = _FALSE; + #ifdef CONFIG_RTW_ACS_DBG + RTW_INFO("[ACS] Result CH:%d, CLM:%d NHM:%d\n", + cur_chan, hal_data->acs.clm_ratio[chan_idx], hal_data->acs.nhm_ratio[chan_idx]); + RTW_INFO("[ACS] Result NHM(dBm):%d\n", + hal_data->acs.env_mntr_rpt[chan_idx] ); + #endif +} + +void _rtw_phydm_acs_select_best_chan(_adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + u8 ch_idx; + u8 ch_idx_24g = 0xFF, ch_idx_5g = 0xFF; + u8 min_itf_24g = 0xFF, min_itf_5g = 0xFF; + u8 *pbss_nums = hal_data->acs.bss_nums; + u8 *pclm_ratio = hal_data->acs.clm_ratio; + u8 *pnhm_ratio = hal_data->acs.nhm_ratio; + u8 *pinterference_time = hal_data->acs.interference_time; + u8 max_chan_nums = rfctl->max_chan_nums; + + for (ch_idx = 0; ch_idx < max_chan_nums; ch_idx++) { + if (pbss_nums[ch_idx]) + pinterference_time[ch_idx] = (pclm_ratio[ch_idx] / 2) + (pnhm_ratio[ch_idx] / 2); + else + pinterference_time[ch_idx] = (pclm_ratio[ch_idx] / 3) + ((pnhm_ratio[ch_idx] * 2) / 3); + + if (rtw_get_ch_num_by_idx(adapter, ch_idx) < 14) { + if (pinterference_time[ch_idx] < min_itf_24g) { + min_itf_24g = pinterference_time[ch_idx]; + ch_idx_24g = ch_idx; + } + } else { + if (pinterference_time[ch_idx] < min_itf_5g) { + min_itf_5g = pinterference_time[ch_idx]; + ch_idx_5g = ch_idx; + } + } + } + if (ch_idx_24g != 0xFF) + hal_data->acs.best_chan_24g = rtw_get_ch_num_by_idx(adapter, ch_idx_24g); + + if (ch_idx_5g != 0xFF) + hal_data->acs.best_chan_5g = rtw_get_ch_num_by_idx(adapter, ch_idx_5g); + + hal_data->acs.trigger_ch = 0; +} + +void rtw_acs_info_dump(void *sel, _adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + u8 max_chan_nums = rfctl->max_chan_nums; + u8 ch_idx, ch_num; + + _RTW_PRINT_SEL(sel, "========== ACS (VER-%d) ==========\n", RTK_ACS_VERSION); + _RTW_PRINT_SEL(sel, "Best 24G Channel:%d\n", hal_data->acs.best_chan_24g); + _RTW_PRINT_SEL(sel, "Best 5G Channel:%d\n\n", hal_data->acs.best_chan_5g); + + #ifdef CONFIG_RTW_ACS_DBG + _RTW_PRINT_SEL(sel, "Advanced setting - scan_type:%c, ch_ms:%d(ms), igi:0x%02x, bw:%d\n", + hal_data->acs.scan_type ? 'A' : 'P', hal_data->acs.scan_time, hal_data->acs.igi, hal_data->acs.bw); + + _RTW_PRINT_SEL(sel, "BW 20MHz\n"); + _RTW_PRINT_SEL(sel, "%5s %3s %3s %3s(%%) %3s(%%) %3s\n", + "Index", "CH", "BSS", "CLM", "NHM", "ITF"); + + for (ch_idx = 0; ch_idx < max_chan_nums; ch_idx++) { + ch_num = rtw_get_ch_num_by_idx(adapter, ch_idx); + _RTW_PRINT_SEL(sel, "%5d %3d %3d %6d %6d %3d\n", + ch_idx, ch_num, hal_data->acs.bss_nums[ch_idx], + hal_data->acs.clm_ratio[ch_idx], + hal_data->acs.nhm_ratio[ch_idx], + hal_data->acs.interference_time[ch_idx]); + } + #endif +} +void rtw_acs_select_best_chan(_adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + + _rtw_bss_nums_count(adapter, hal_data->acs.bss_nums); + _rtw_phydm_acs_select_best_chan(adapter); + rtw_acs_info_dump(RTW_DBGDUMP, adapter); +} + +void rtw_acs_start(_adapter *adapter) +{ + rtw_acs_reset(adapter); + if (GET_ACS_STATE(adapter) != ACS_ENABLE) + SET_ACS_STATE(adapter, ACS_ENABLE); +} +void rtw_acs_stop(_adapter *adapter) +{ + SET_ACS_STATE(adapter, ACS_DISABLE); +} + + +u8 rtw_acs_get_clm_ratio_by_ch_num(_adapter *adapter, u8 chan) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + int chan_idx = -1; + + chan_idx = rtw_chset_search_ch(adapter_to_chset(adapter), chan); + if ((chan_idx == -1) || (chan_idx >= MAX_CHANNEL_NUM)) { + RTW_ERR("[ACS] Get CLM fail, can't get chan_idx(CH:%d)\n", chan); + return 0; + } + + return hal_data->acs.clm_ratio[chan_idx]; +} +u8 rtw_acs_get_clm_ratio_by_ch_idx(_adapter *adapter, u8 ch_idx) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + + if (ch_idx >= MAX_CHANNEL_NUM) { + RTW_ERR("%s [ACS] ch_idx(%d) is invalid\n", __func__, ch_idx); + return 0; + } + + return hal_data->acs.clm_ratio[ch_idx]; +} +u8 rtw_acs_get_nhm_ratio_by_ch_num(_adapter *adapter, u8 chan) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + int chan_idx = -1; + + chan_idx = rtw_chset_search_ch(adapter_to_chset(adapter), chan); + if ((chan_idx == -1) || (chan_idx >= MAX_CHANNEL_NUM)) { + RTW_ERR("[ACS] Get NHM fail, can't get chan_idx(CH:%d)\n", chan); + return 0; + } + + return hal_data->acs.nhm_ratio[chan_idx]; +} +u8 rtw_acs_get_nhm_noise_pwr_by_ch_idx(_adapter *adapter, u8 ch_idx) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + + if (ch_idx >= MAX_CHANNEL_NUM) { + RTW_ERR("%s [ACS] ch_idx(%d) is invalid\n", __func__, ch_idx); + return 0; + } + + return hal_data->acs.env_mntr_rpt[ch_idx]; +} + +u8 rtw_acs_get_num_ratio_by_ch_idx(_adapter *adapter, u8 ch_idx) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + + if (ch_idx >= MAX_CHANNEL_NUM) { + RTW_ERR("%s [ACS] ch_idx(%d) is invalid\n", __func__, ch_idx); + return 0; + } + + return hal_data->acs.nhm_ratio[ch_idx]; +} +void rtw_acs_chan_info_dump(void *sel, _adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + u8 max_chan_nums = rfctl->max_chan_nums; + u8 ch_idx, ch_num; + u8 utilization; + + _RTW_PRINT_SEL(sel, "BW 20MHz\n"); + _RTW_PRINT_SEL(sel, "%5s %3s %7s(%%) %12s(%%) %11s(%%) %9s(%%) %8s(%%)\n", + "Index", "CH", "Quality", "Availability", "Utilization", + "WIFI Util", "Interference Util"); + + for (ch_idx = 0; ch_idx < max_chan_nums; ch_idx++) { + ch_num = rtw_get_ch_num_by_idx(adapter, ch_idx); + utilization = hal_data->acs.clm_ratio[ch_idx] + hal_data->acs.nhm_ratio[ch_idx]; + _RTW_PRINT_SEL(sel, "%5d %3d %7d %12d %12d %12d %12d\n", + ch_idx, ch_num, + (100-hal_data->acs.interference_time[ch_idx]), + (100-utilization), + utilization, + hal_data->acs.clm_ratio[ch_idx], + hal_data->acs.nhm_ratio[ch_idx]); + } +} +void rtw_acs_current_info_dump(void *sel, _adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u8 ch, cen_ch, bw, offset; + + _RTW_PRINT_SEL(sel, "========== ACS (VER-%d) ==========\n", RTK_ACS_VERSION); + + ch = rtw_get_oper_ch(adapter); + bw = rtw_get_oper_bw(adapter); + offset = rtw_get_oper_choffset(adapter); + + _RTW_PRINT_SEL(sel, "Current Channel:%d\n", ch); + if ((bw == CHANNEL_WIDTH_80) ||(bw == CHANNEL_WIDTH_40)) { + cen_ch = rtw_get_center_ch(ch, bw, offset); + _RTW_PRINT_SEL(sel, "Center Channel:%d\n", cen_ch); + } + + _RTW_PRINT_SEL(sel, "Current BW %s\n", ch_width_str(bw)); + if (0) + _RTW_PRINT_SEL(sel, "Current IGI 0x%02x\n", rtw_phydm_get_cur_igi(adapter)); + _RTW_PRINT_SEL(sel, "CLM:%d, NHM:%d\n\n", + hal_data->acs.cur_ch_clm_ratio, hal_data->acs.cur_ch_nhm_ratio); +} + +void rtw_acs_update_current_info(_adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + + hal_data->acs.cur_ch_clm_ratio = rtw_phydm_clm_ratio(adapter); + hal_data->acs.cur_ch_nhm_ratio = rtw_phydm_nhm_ratio(adapter); + + #ifdef CONFIG_RTW_ACS_DBG + rtw_acs_current_info_dump(RTW_DBGDUMP, adapter); + #endif +} +/* +rsni +para1:rcpi=>RSSI in dbm +para2:anpi=>nhm in dbm +range:0~255 +255: is not available (defined by 802.11k spec) + +*/ +u8 rtw_acs_get_rsni(_adapter *adapter, s8 rcpi, u8 ch) +{ + struct dm_struct *phydm = adapter_to_phydm(adapter); + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + u8 rsni = 255; + s8 anpi = 0; + int chan_idx = -1; + + if(ch == 0) + goto exit; + + chan_idx = rtw_chset_search_ch(adapter_to_chset(adapter), ch); + if(chan_idx == -1) + goto exit; + + anpi = rtw_acs_get_nhm_noise_pwr_by_ch_idx(adapter, chan_idx); + if((rcpi != 0) && (anpi != 0)) + rsni = phydm_env_mntr_get_802_11_k_rsni(phydm, rcpi, anpi); + RTW_DBG("[ACS][RSNI]ch=%d chan_idx=%d RSNI=%u RSSI=%d NHM=%d\n", ch, chan_idx, rsni,rcpi, anpi); +exit: + return rsni; +} +#endif /*CONFIG_RTW_ACS*/ + +#ifdef CONFIG_BACKGROUND_NOISE_MONITOR +void rtw_noise_monitor_version_dump(void *sel, _adapter *adapter) +{ + _RTW_PRINT_SEL(sel, "RTK_NOISE_MONITOR VER_%d\n", RTK_NOISE_MONITOR_VERSION); +} +void rtw_nm_enable(_adapter *adapter) +{ + SET_NM_STATE(adapter, NM_ENABLE); +} +void rtw_nm_disable(_adapter *adapter) +{ + SET_NM_STATE(adapter, NM_DISABLE); +} +void rtw_noise_info_dump(void *sel, _adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct rf_ctl_t *rfctl = adapter_to_rfctl(adapter); + u8 max_chan_nums = rfctl->max_chan_nums; + u8 ch_idx, ch_num; + + _RTW_PRINT_SEL(sel, "========== NM (VER-%d) ==========\n", RTK_NOISE_MONITOR_VERSION); + + _RTW_PRINT_SEL(sel, "%5s %3s %3s %10s", "Index", "CH", "BSS", "Noise(dBm)\n"); + + _rtw_bss_nums_count(adapter, hal_data->nm.bss_nums); + + for (ch_idx = 0; ch_idx < max_chan_nums; ch_idx++) { + ch_num = rtw_get_ch_num_by_idx(adapter, ch_idx); + _RTW_PRINT_SEL(sel, "%5d %3d %3d %10d\n", + ch_idx, ch_num, hal_data->nm.bss_nums[ch_idx], + hal_data->nm.noise[ch_idx]); + } +} + +void rtw_noise_measure(_adapter *adapter, u8 chan, u8 is_pause_dig, u8 igi_value, u32 max_time) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct dm_struct *phydm = &hal_data->odmpriv; + int chan_idx = -1; + s16 noise = 0; + + #ifdef DBG_NOISE_MONITOR + RTW_INFO("[NM] chan(%d)-PauseDIG:%s, IGIValue:0x%02x, max_time:%d (ms)\n", + chan, (is_pause_dig) ? "Y" : "N", igi_value, max_time); + #endif + + chan_idx = rtw_chset_search_ch(adapter_to_chset(adapter), chan); + if ((chan_idx == -1) || (chan_idx >= MAX_CHANNEL_NUM)) { + RTW_ERR("[NM] Get noise fail, can't get chan_idx(CH:%d)\n", chan); + return; + } + noise = odm_inband_noise_monitor(phydm, is_pause_dig, igi_value, max_time); /*dBm*/ + + hal_data->nm.noise[chan_idx] = noise; + + #ifdef DBG_NOISE_MONITOR + RTW_INFO("[NM] %s chan_%d, noise = %d (dBm)\n", __func__, chan, hal_data->nm.noise[chan_idx]); + + RTW_INFO("[NM] noise_a = %d, noise_b = %d noise_all:%d\n", + phydm->noise_level.noise[RF_PATH_A], + phydm->noise_level.noise[RF_PATH_B], + phydm->noise_level.noise_all); + #endif /*DBG_NOISE_MONITOR*/ +} + +s16 rtw_noise_query_by_chan_num(_adapter *adapter, u8 chan) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + s16 noise = 0; + int chan_idx = -1; + + chan_idx = rtw_chset_search_ch(adapter_to_chset(adapter), chan); + if ((chan_idx == -1) || (chan_idx >= MAX_CHANNEL_NUM)) { + RTW_ERR("[NM] Get noise fail, can't get chan_idx(CH:%d)\n", chan); + return noise; + } + noise = hal_data->nm.noise[chan_idx]; + + #ifdef DBG_NOISE_MONITOR + RTW_INFO("[NM] %s chan_%d, noise = %d (dBm)\n", __func__, chan, noise); + #endif/*DBG_NOISE_MONITOR*/ + return noise; +} +s16 rtw_noise_query_by_chan_idx(_adapter *adapter, u8 ch_idx) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + s16 noise = 0; + + if (ch_idx >= MAX_CHANNEL_NUM) { + RTW_ERR("[NM] %s ch_idx(%d) is invalid\n", __func__, ch_idx); + return noise; + } + noise = hal_data->nm.noise[ch_idx]; + + #ifdef DBG_NOISE_MONITOR + RTW_INFO("[NM] %s ch_idx %d, noise = %d (dBm)\n", __func__, ch_idx, noise); + #endif/*DBG_NOISE_MONITOR*/ + return noise; +} + +s16 rtw_noise_measure_curchan(_adapter *padapter) +{ + s16 noise = 0; + u8 igi_value = 0x1E; + u32 max_time = 100;/* ms */ + u8 is_pause_dig = _TRUE; + u8 cur_chan = rtw_get_oper_ch(padapter); + + if (rtw_linked_check(padapter) == _FALSE) + return noise; + + rtw_ps_deny(padapter, PS_DENY_IOCTL); + LeaveAllPowerSaveModeDirect(padapter); + rtw_noise_measure(padapter, cur_chan, is_pause_dig, igi_value, max_time); + noise = rtw_noise_query_by_chan_num(padapter, cur_chan); + rtw_ps_deny_cancel(padapter, PS_DENY_IOCTL); + + return noise; +} +#endif /*CONFIG_BACKGROUND_NOISE_MONITOR*/ + diff --git a/hal/hal_dm_acs.h b/hal/hal_dm_acs.h new file mode 100644 index 0000000..f962001 --- /dev/null +++ b/hal/hal_dm_acs.h @@ -0,0 +1,173 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#ifndef __HAL_DM_ACS_H__ +#define __HAL_DM_ACS_H__ +#ifdef CONFIG_RTW_ACS +#define RTK_ACS_VERSION 3 + +#if (RTK_ACS_VERSION == 3) +enum NHM_PID { + NHM_PID_ACS, + NHM_PID_IEEE_11K_HIGH, + NHM_PID_IEEE_11K_LOW, +}; + +#define init_clm_param(clm, app, lv, time) \ + do {\ + clm.clm_app = app;\ + clm.clm_lv = lv;\ + clm.mntr_time = time;\ + } while (0) + +#define init_nhm_param(nhm, txon, cca, cnt_opt, app, lv, time) \ + do {\ + nhm.incld_txon = txon;\ + nhm.incld_cca = cca;\ + nhm.div_opt = cnt_opt;\ + nhm.nhm_app = app;\ + nhm.nhm_lv = lv;\ + nhm.mntr_time = time;\ + } while (0) + + +#define init_acs_clm(clm, time) \ + init_clm_param(clm, CLM_ACS, CLM_LV_2, time) + +#define init_acs_nhm(nhm, time) \ + init_nhm_param(nhm, NHM_EXCLUDE_TXON, NHM_EXCLUDE_CCA, NHM_CNT_ALL, NHM_ACS, NHM_LV_2, time) + +#define init_11K_high_nhm(nhm, time) \ + init_nhm_param(nhm, NHM_EXCLUDE_TXON, NHM_EXCLUDE_CCA, NHM_CNT_ALL, IEEE_11K_HIGH, NHM_LV_2, time) + +#define init_11K_low_nhm(nhm, time) \ + init_nhm_param(nhm, NHM_EXCLUDE_TXON, NHM_EXCLUDE_CCA, NHM_CNT_ALL, IEEE_11K_LOW, NHM_LV_2, time) + + +#endif /*(RTK_ACS_VERSION == 3)*/ +void rtw_acs_version_dump(void *sel, _adapter *adapter); +extern void phydm_ccx_monitor_trigger(void *p_dm_void, u16 monitor_time); +extern void phydm_ccx_monitor_result(void *p_dm_void); + +#define GET_ACS_STATE(padapter) (ATOMIC_READ(&GET_HAL_DATA(padapter)->acs.state)) +#define SET_ACS_STATE(padapter, set_state) (ATOMIC_SET(&GET_HAL_DATA(padapter)->acs.state, set_state)) +#define IS_ACS_ENABLE(padapter) ((GET_ACS_STATE(padapter) == ACS_ENABLE) ? _TRUE : _FALSE) + +enum ACS_STATE { + ACS_DISABLE, + ACS_ENABLE, +}; + +#define ACS_BW_20M BIT(0) +#define ACS_BW_40M BIT(1) +#define ACS_BW_80M BIT(2) +#define ACS_BW_160M BIT(3) + +struct auto_chan_sel { + ATOMIC_T state; + u8 trigger_ch; + bool triggered; + u8 clm_ratio[MAX_CHANNEL_NUM]; + u8 nhm_ratio[MAX_CHANNEL_NUM]; + s8 env_mntr_rpt[MAX_CHANNEL_NUM]; /*unit:dbm*/ + #if (RTK_ACS_VERSION == 3) + u8 nhm[MAX_CHANNEL_NUM][NHM_RPT_NUM]; + #endif + u8 bss_nums[MAX_CHANNEL_NUM]; + u8 interference_time[MAX_CHANNEL_NUM]; + u8 cur_ch_clm_ratio; + u8 cur_ch_nhm_ratio; + u8 best_chan_5g; + u8 best_chan_24g; + + #if (RTK_ACS_VERSION == 3) + u8 trig_rst; + struct env_trig_rpt trig_rpt; + #endif + + #ifdef CONFIG_RTW_ACS_DBG + RT_SCAN_TYPE scan_type; + u16 scan_time; + u8 igi; + u8 bw; + #endif +}; + +#define rtw_acs_get_best_chan_24g(adapter) (GET_HAL_DATA(adapter)->acs.best_chan_24g) +#define rtw_acs_get_best_chan_5g(adapter) (GET_HAL_DATA(adapter)->acs.best_chan_5g) + +#ifdef CONFIG_RTW_ACS_DBG +#define rtw_is_acs_passiv_scan(adapter) (((GET_HAL_DATA(adapter)->acs.scan_type) == SCAN_PASSIVE) ? _TRUE : _FALSE) + +#define rtw_acs_get_adv_st(adapter) (GET_HAL_DATA(adapter)->acs.scan_time) +#define rtw_is_acs_st_valid(adapter) ((GET_HAL_DATA(adapter)->acs.scan_time) ? _TRUE : _FALSE) + +#define rtw_acs_get_adv_igi(adapter) (GET_HAL_DATA(adapter)->acs.igi) +u8 rtw_is_acs_igi_valid(_adapter *adapter); + +#define rtw_acs_get_adv_bw(adapter) (GET_HAL_DATA(adapter)->acs.bw) + +void rtw_acs_adv_setting(_adapter *adapter, RT_SCAN_TYPE scan_type, u16 scan_time, u8 igi, u8 bw); +void rtw_acs_adv_reset(_adapter *adapter); +#endif + +u8 rtw_acs_get_clm_ratio_by_ch_num(_adapter *adapter, u8 chan); +u8 rtw_acs_get_clm_ratio_by_ch_idx(_adapter *adapter, u8 ch_idx); +u8 rtw_acs_get_nhm_ratio_by_ch_num(_adapter *adapter, u8 chan); +u8 rtw_acs_get_nhm_noise_pwr_by_ch_idx(_adapter *adapter, u8 ch_idx); +u8 rtw_acs_get_num_ratio_by_ch_idx(_adapter *adapter, u8 ch_idx); + +u8 rtw_phydm_clm_ratio(_adapter *adapter); +u8 rtw_phydm_nhm_ratio(_adapter *adapter); +u8 rtw_phydm_nhm_noise_pwr(_adapter *adapter); + +void rtw_acs_reset(_adapter *adapter); +void rtw_acs_trigger(_adapter *adapter, u16 scan_time_ms, u8 scan_chan, enum NHM_PID pid); +void rtw_acs_get_rst(_adapter *adapter); +void rtw_acs_select_best_chan(_adapter *adapter); +void rtw_acs_info_dump(void *sel, _adapter *adapter); +void rtw_acs_update_current_info(_adapter *adapter); +void rtw_acs_chan_info_dump(void *sel, _adapter *adapter); +void rtw_acs_current_info_dump(void *sel, _adapter *adapter); + +void rtw_acs_start(_adapter *adapter); +void rtw_acs_stop(_adapter *adapter); +u8 rtw_acs_get_rsni(_adapter *adapter, s8 rcpi, u8 ch); +#endif /*CONFIG_RTW_ACS*/ + +#ifdef CONFIG_BACKGROUND_NOISE_MONITOR +#define RTK_NOISE_MONITOR_VERSION 3 +#define GET_NM_STATE(padapter) (ATOMIC_READ(&GET_HAL_DATA(padapter)->nm.state)) +#define SET_NM_STATE(padapter, set_state) (ATOMIC_SET(&GET_HAL_DATA(padapter)->nm.state, set_state)) +#define IS_NM_ENABLE(padapter) ((GET_NM_STATE(padapter) == NM_ENABLE) ? _TRUE : _FALSE) + +enum NM_STATE { + NM_DISABLE, + NM_ENABLE, +}; + +struct noise_monitor { + ATOMIC_T state; + s16 noise[MAX_CHANNEL_NUM]; + u8 bss_nums[MAX_CHANNEL_NUM]; +}; +void rtw_nm_enable(_adapter *adapter); +void rtw_nm_disable(_adapter *adapter); +void rtw_noise_measure(_adapter *adapter, u8 chan, u8 is_pause_dig, u8 igi_value, u32 max_time); +s16 rtw_noise_query_by_chan_num(_adapter *adapter, u8 chan); +s16 rtw_noise_query_by_chan_idx(_adapter *adapter, u8 ch_idx); +s16 rtw_noise_measure_curchan(_adapter *padapter); +void rtw_noise_info_dump(void *sel, _adapter *adapter); +#endif +#endif /* __HAL_DM_ACS_H__ */ diff --git a/hal/hal_halmac.c b/hal/hal_halmac.c new file mode 100644 index 0000000..aa1bbf9 --- /dev/null +++ b/hal/hal_halmac.c @@ -0,0 +1,5927 @@ +/****************************************************************************** + * + * Copyright(c) 2015 - 2019 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#define _HAL_HALMAC_C_ + +#include /* PADAPTER, struct dvobj_priv, SDIO_ERR_VAL8 and etc. */ +#include /* efuse, PHAL_DATA_TYPE and etc. */ +#include "hal_halmac.h" /* dvobj_to_halmac() and ect. */ + +/* + * HALMAC take return value 0 for fail and 1 for success to replace + * _FALSE/_TRUE after V1_04_09 + */ +#define RTW_HALMAC_FAIL 0 +#define RTW_HALMAC_SUCCESS 1 + +#define DEFAULT_INDICATOR_TIMELMT 1000 /* ms */ +#define MSG_PREFIX "[HALMAC]" + +#define RTW_HALMAC_DLFW_MEM_NO_STOP_TX +#define RTW_HALMAC_FILTER_DRV_C2H /* Block C2H owner=driver */ + +/* + * Driver API for HALMAC operations + */ + +#ifdef CONFIG_SDIO_HCI +#include + +static u8 _halmac_mac_reg_page0_chk(const char *func, struct dvobj_priv *dvobj, u32 offset) +{ +#if defined(CONFIG_IO_CHECK_IN_ANA_LOW_CLK) && defined(CONFIG_LPS_LCLK) + struct pwrctrl_priv *pwrpriv = &dvobj->pwrctl_priv; + u32 mac_reg_offset = 0; + + if (pwrpriv->pwr_mode == PS_MODE_ACTIVE) + return _TRUE; + + if (pwrpriv->lps_level == LPS_NORMAL) + return _TRUE; + + if (pwrpriv->rpwm >= PS_STATE_S2) + return _TRUE; + + if (offset & (WLAN_IOREG_DEVICE_ID << 13)) { /*WLAN_IOREG_OFFSET*/ + mac_reg_offset = offset & HALMAC_WLAN_MAC_REG_MSK; + if (mac_reg_offset < 0x100) { + RTW_ERR(FUNC_ADPT_FMT + "access MAC REG -0x%04x in PS-mode:0x%02x (rpwm:0x%02x, lps_level:0x%02x)\n", + FUNC_ADPT_ARG(dvobj_get_primary_adapter(dvobj)), mac_reg_offset, + pwrpriv->pwr_mode, pwrpriv->rpwm, pwrpriv->lps_level); + rtw_warn_on(1); + return _FALSE; + } + } +#endif + return _TRUE; +} + +static u8 _halmac_sdio_cmd52_read(void *p, u32 offset) +{ + struct dvobj_priv *d; + u8 val; + u8 ret; + + + d = (struct dvobj_priv *)p; + _halmac_mac_reg_page0_chk(__func__, d, offset); + ret = rtw_sdio_read_cmd52(d, offset, &val, 1); + if (_FAIL == ret) { + RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__); + return SDIO_ERR_VAL8; + } + + return val; +} + +static void _halmac_sdio_cmd52_write(void *p, u32 offset, u8 val) +{ + struct dvobj_priv *d; + u8 ret; + + + d = (struct dvobj_priv *)p; + _halmac_mac_reg_page0_chk(__func__, d, offset); + ret = rtw_sdio_write_cmd52(d, offset, &val, 1); + if (_FAIL == ret) + RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__); +} + +static u8 _halmac_sdio_reg_read_8(void *p, u32 offset) +{ + struct dvobj_priv *d; + u8 *pbuf; + u8 val; + u8 ret; + + + d = (struct dvobj_priv *)p; + val = SDIO_ERR_VAL8; + _halmac_mac_reg_page0_chk(__func__, d, offset); + pbuf = rtw_zmalloc(1); + if (!pbuf) + return val; + + ret = rtw_sdio_read_cmd53(d, offset, pbuf, 1); + if (ret == _FAIL) { + RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__); + goto exit; + } + + val = *pbuf; + +exit: + rtw_mfree(pbuf, 1); + + return val; +} + +static u16 _halmac_sdio_reg_read_16(void *p, u32 offset) +{ + struct dvobj_priv *d; + u8 *pbuf; + u16 val; + u8 ret; + + + d = (struct dvobj_priv *)p; + val = SDIO_ERR_VAL16; + _halmac_mac_reg_page0_chk(__func__, d, offset); + pbuf = rtw_zmalloc(2); + if (!pbuf) + return val; + + ret = rtw_sdio_read_cmd53(d, offset, pbuf, 2); + if (ret == _FAIL) { + RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__); + goto exit; + } + + val = le16_to_cpu(*(u16 *)pbuf); + +exit: + rtw_mfree(pbuf, 2); + + return val; +} + +static u32 _halmac_sdio_reg_read_32(void *p, u32 offset) +{ + struct dvobj_priv *d; + u8 *pbuf; + u32 val; + u8 ret; + + + d = (struct dvobj_priv *)p; + val = SDIO_ERR_VAL32; + _halmac_mac_reg_page0_chk(__func__, d, offset); + pbuf = rtw_zmalloc(4); + if (!pbuf) + return val; + + ret = rtw_sdio_read_cmd53(d, offset, pbuf, 4); + if (ret == _FAIL) { + RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__); + goto exit; + } + + val = le32_to_cpu(*(u32 *)pbuf); + +exit: + rtw_mfree(pbuf, 4); + + return val; +} + +static u8 _halmac_sdio_reg_read_n(void *p, u32 offset, u32 size, u8 *data) +{ + struct dvobj_priv *d = (struct dvobj_priv *)p; + u8 *pbuf; + u8 ret; + u8 rst = RTW_HALMAC_FAIL; + u32 sdio_read_size; + + + if (!data) + return rst; + + sdio_read_size = RND4(size); + sdio_read_size = rtw_sdio_cmd53_align_size(d, sdio_read_size); + + pbuf = rtw_zmalloc(sdio_read_size); + if (!pbuf) + return rst; + + ret = rtw_sdio_read_cmd53(d, offset, pbuf, sdio_read_size); + if (ret == _FAIL) { + RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__); + goto exit; + } + + _rtw_memcpy(data, pbuf, size); + rst = RTW_HALMAC_SUCCESS; +exit: + rtw_mfree(pbuf, sdio_read_size); + + return rst; +} + +static void _halmac_sdio_reg_write_8(void *p, u32 offset, u8 val) +{ + struct dvobj_priv *d; + u8 *pbuf; + u8 ret; + + + d = (struct dvobj_priv *)p; + _halmac_mac_reg_page0_chk(__func__, d, offset); + pbuf = rtw_zmalloc(1); + if (!pbuf) + return; + _rtw_memcpy(pbuf, &val, 1); + + ret = rtw_sdio_write_cmd53(d, offset, pbuf, 1); + if (ret == _FAIL) + RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__); + + rtw_mfree(pbuf, 1); +} + +static void _halmac_sdio_reg_write_16(void *p, u32 offset, u16 val) +{ + struct dvobj_priv *d; + u8 *pbuf; + u8 ret; + + + d = (struct dvobj_priv *)p; + _halmac_mac_reg_page0_chk(__func__, d, offset); + val = cpu_to_le16(val); + pbuf = rtw_zmalloc(2); + if (!pbuf) + return; + _rtw_memcpy(pbuf, &val, 2); + + ret = rtw_sdio_write_cmd53(d, offset, pbuf, 2); + if (ret == _FAIL) + RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__); + + rtw_mfree(pbuf, 2); +} + +static void _halmac_sdio_reg_write_32(void *p, u32 offset, u32 val) +{ + struct dvobj_priv *d; + u8 *pbuf; + u8 ret; + + + d = (struct dvobj_priv *)p; + _halmac_mac_reg_page0_chk(__func__, d, offset); + val = cpu_to_le32(val); + pbuf = rtw_zmalloc(4); + if (!pbuf) + return; + _rtw_memcpy(pbuf, &val, 4); + + ret = rtw_sdio_write_cmd53(d, offset, pbuf, 4); + if (ret == _FAIL) + RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__); + + rtw_mfree(pbuf, 4); +} + +static u8 _halmac_sdio_read_cia(void *p, u32 offset) +{ + struct dvobj_priv *d; + u8 data = 0; + u8 ret; + + + d = (struct dvobj_priv *)p; + + ret = rtw_sdio_f0_read(d, offset, &data, 1); + if (ret == _FAIL) + RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__); + + return data; +} + +#else /* !CONFIG_SDIO_HCI */ + +static u8 _halmac_reg_read_8(void *p, u32 offset) +{ + struct dvobj_priv *d; + PADAPTER adapter; + + + d = (struct dvobj_priv *)p; + adapter = dvobj_get_primary_adapter(d); + + return _rtw_read8(adapter, offset); +} + +static u16 _halmac_reg_read_16(void *p, u32 offset) +{ + struct dvobj_priv *d; + PADAPTER adapter; + + + d = (struct dvobj_priv *)p; + adapter = dvobj_get_primary_adapter(d); + + return _rtw_read16(adapter, offset); +} + +static u32 _halmac_reg_read_32(void *p, u32 offset) +{ + struct dvobj_priv *d; + PADAPTER adapter; + + + d = (struct dvobj_priv *)p; + adapter = dvobj_get_primary_adapter(d); + + return _rtw_read32(adapter, offset); +} + +static void _halmac_reg_write_8(void *p, u32 offset, u8 val) +{ + struct dvobj_priv *d; + PADAPTER adapter; + int err; + + + d = (struct dvobj_priv *)p; + adapter = dvobj_get_primary_adapter(d); + + err = _rtw_write8(adapter, offset, val); + if (err == _FAIL) + RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__); +} + +static void _halmac_reg_write_16(void *p, u32 offset, u16 val) +{ + struct dvobj_priv *d; + PADAPTER adapter; + int err; + + + d = (struct dvobj_priv *)p; + adapter = dvobj_get_primary_adapter(d); + + err = _rtw_write16(adapter, offset, val); + if (err == _FAIL) + RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__); +} + +static void _halmac_reg_write_32(void *p, u32 offset, u32 val) +{ + struct dvobj_priv *d; + PADAPTER adapter; + int err; + + + d = (struct dvobj_priv *)p; + adapter = dvobj_get_primary_adapter(d); + + err = _rtw_write32(adapter, offset, val); + if (err == _FAIL) + RTW_ERR("%s: I/O FAIL!\n", __FUNCTION__); +} +#endif /* !CONFIG_SDIO_HCI */ + +#ifdef DBG_IO +static void _halmac_reg_read_monitor(void *p, u32 addr, u32 len, u32 val + , const char *caller, const u32 line) +{ + struct dvobj_priv *d = (struct dvobj_priv *)p; + _adapter *adapter = dvobj_get_primary_adapter(d); + + dbg_rtw_reg_read_monitor(adapter, addr, len, val, caller, line); +} + +static void _halmac_reg_write_monitor(void *p, u32 addr, u32 len, u32 val + , const char *caller, const u32 line) +{ + struct dvobj_priv *d = (struct dvobj_priv *)p; + _adapter *adapter = dvobj_get_primary_adapter(d); + + dbg_rtw_reg_write_monitor(adapter, addr, len, val, caller, line); +} +#endif + +static u8 _halmac_mfree(void *p, void *buffer, u32 size) +{ + rtw_mfree(buffer, size); + return RTW_HALMAC_SUCCESS; +} + +static void *_halmac_malloc(void *p, u32 size) +{ + return rtw_zmalloc(size); +} + +static u8 _halmac_memcpy(void *p, void *dest, void *src, u32 size) +{ + _rtw_memcpy(dest, src, size); + return RTW_HALMAC_SUCCESS; +} + +static u8 _halmac_memset(void *p, void *addr, u8 value, u32 size) +{ + _rtw_memset(addr, value, size); + return RTW_HALMAC_SUCCESS; +} + +static void _halmac_udelay(void *p, u32 us) +{ + /* Most hardware polling wait time < 50us) */ + if (us <= 50) + rtw_udelay_os(us); + else if (us <= 1000) + rtw_usleep_os(us); + else + rtw_msleep_os(RTW_DIV_ROUND_UP(us, 1000)); +} + +static u8 _halmac_mutex_init(void *p, HALMAC_MUTEX *pMutex) +{ + _rtw_mutex_init(pMutex); + return RTW_HALMAC_SUCCESS; +} + +static u8 _halmac_mutex_deinit(void *p, HALMAC_MUTEX *pMutex) +{ + _rtw_mutex_free(pMutex); + return RTW_HALMAC_SUCCESS; +} + +static u8 _halmac_mutex_lock(void *p, HALMAC_MUTEX *pMutex) +{ + int err; + + err = _enter_critical_mutex(pMutex, NULL); + if (err) + return RTW_HALMAC_FAIL; + + return RTW_HALMAC_SUCCESS; +} + +static u8 _halmac_mutex_unlock(void *p, HALMAC_MUTEX *pMutex) +{ + _exit_critical_mutex(pMutex, NULL); + return RTW_HALMAC_SUCCESS; +} + +#ifndef CONFIG_SDIO_HCI +#define DBG_MSG_FILTER +#endif + +#ifdef DBG_MSG_FILTER +static u8 is_msg_allowed(uint drv_lv, u8 msg_lv) +{ + switch (drv_lv) { + case _DRV_NONE_: + return _FALSE; + + case _DRV_ALWAYS_: + if (msg_lv > HALMAC_DBG_ALWAYS) + return _FALSE; + break; + case _DRV_ERR_: + if (msg_lv > HALMAC_DBG_ERR) + return _FALSE; + break; + case _DRV_WARNING_: + if (msg_lv > HALMAC_DBG_WARN) + return _FALSE; + break; + case _DRV_INFO_: + if (msg_lv >= HALMAC_DBG_TRACE) + return _FALSE; + break; + } + + return _TRUE; +} +#endif /* DBG_MSG_FILTER */ + +static u8 _halmac_msg_print(void *p, u32 msg_type, u8 msg_level, s8 *fmt, ...) +{ +#define MSG_LEN 100 + va_list args; + u8 str[MSG_LEN] = {0}; +#ifdef DBG_MSG_FILTER + uint drv_level = _DRV_NONE_; +#endif + int err; + u8 ret = RTW_HALMAC_SUCCESS; + + +#ifdef DBG_MSG_FILTER +#ifdef CONFIG_RTW_DEBUG + drv_level = rtw_drv_log_level; +#endif + if (is_msg_allowed(drv_level, msg_level) == _FALSE) + return ret; +#endif + + str[0] = '\n'; + va_start(args, fmt); + err = vsnprintf(str, MSG_LEN, fmt, args); + va_end(args); + + /* An output error is encountered */ + if (err < 0) + return RTW_HALMAC_FAIL; + /* Output may be truncated due to size limit */ + if ((err == (MSG_LEN - 1)) && (str[MSG_LEN - 2] != '\n')) + ret = RTW_HALMAC_FAIL; + + if (msg_level == HALMAC_DBG_ALWAYS) + RTW_PRINT(MSG_PREFIX "%s", str); + else if (msg_level <= HALMAC_DBG_ERR) + RTW_ERR(MSG_PREFIX "%s", str); + else if (msg_level <= HALMAC_DBG_WARN) + RTW_WARN(MSG_PREFIX "%s", str); + else + RTW_DBG(MSG_PREFIX "%s", str); + + return ret; +} + +static u8 _halmac_buff_print(void *p, u32 msg_type, u8 msg_level, s8 *buf, u32 size) +{ + if (msg_level <= HALMAC_DBG_WARN) + RTW_INFO_DUMP(MSG_PREFIX, buf, size); + else + RTW_DBG_DUMP(MSG_PREFIX, buf, size); + + return RTW_HALMAC_SUCCESS; +} + + +const char *const RTW_HALMAC_FEATURE_NAME[] = { + "HALMAC_FEATURE_CFG_PARA", + "HALMAC_FEATURE_DUMP_PHYSICAL_EFUSE", + "HALMAC_FEATURE_DUMP_LOGICAL_EFUSE", + "HALMAC_FEATURE_DUMP_LOGICAL_EFUSE_MASK", + "HALMAC_FEATURE_UPDATE_PACKET", + "HALMAC_FEATURE_SEND_SCAN_PACKET", + "HALMAC_FEATURE_DROP_SCAN_PACKET", + "HALMAC_FEATURE_UPDATE_DATAPACK", + "HALMAC_FEATURE_RUN_DATAPACK", + "HALMAC_FEATURE_CHANNEL_SWITCH", + "HALMAC_FEATURE_IQK", + "HALMAC_FEATURE_POWER_TRACKING", + "HALMAC_FEATURE_PSD", + "HALMAC_FEATURE_FW_SNDING", + "HALMAC_FEATURE_DPK", + "HALMAC_FEATURE_ALL" +}; + +static inline u8 is_valid_id_status(enum halmac_feature_id id, enum halmac_cmd_process_status status) +{ + switch (id) { + case HALMAC_FEATURE_CFG_PARA: + RTW_DBG("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]); + break; + case HALMAC_FEATURE_DUMP_PHYSICAL_EFUSE: + RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]); + if (HALMAC_CMD_PROCESS_DONE != status) + RTW_INFO("%s: id(%d) unspecified status(%d)!\n", + __FUNCTION__, id, status); + break; + case HALMAC_FEATURE_DUMP_LOGICAL_EFUSE: + RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]); + if (HALMAC_CMD_PROCESS_DONE != status) + RTW_INFO("%s: id(%d) unspecified status(%d)!\n", + __FUNCTION__, id, status); + break; + case HALMAC_FEATURE_UPDATE_PACKET: + RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]); + if (status != HALMAC_CMD_PROCESS_DONE) + RTW_INFO("%s: id(%d) unspecified status(%d)!\n", + __FUNCTION__, id, status); + break; + case HALMAC_FEATURE_UPDATE_DATAPACK: + RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]); + break; + case HALMAC_FEATURE_RUN_DATAPACK: + RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]); + break; + case HALMAC_FEATURE_CHANNEL_SWITCH: + RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]); + if ((status != HALMAC_CMD_PROCESS_DONE) && (status != HALMAC_CMD_PROCESS_RCVD)) + RTW_INFO("%s: id(%d) unspecified status(%d)!\n", + __FUNCTION__, id, status); + if (status == HALMAC_CMD_PROCESS_DONE) + return _FALSE; + break; + case HALMAC_FEATURE_IQK: + RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]); + break; + case HALMAC_FEATURE_POWER_TRACKING: + RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]); + break; + case HALMAC_FEATURE_PSD: + RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]); + break; + case HALMAC_FEATURE_FW_SNDING: + RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]); + break; + case HALMAC_FEATURE_DPK: + if (status == HALMAC_CMD_PROCESS_RCVD) + return _FALSE; + if ((status != HALMAC_CMD_PROCESS_DONE) + || (status != HALMAC_CMD_PROCESS_ERROR)) + RTW_WARN("%s: %s unexpected status(0x%x)!\n", + __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id], + status); + break; + case HALMAC_FEATURE_ALL: + RTW_INFO("%s: %s\n", __FUNCTION__, RTW_HALMAC_FEATURE_NAME[id]); + break; + default: + RTW_ERR("%s: unknown feature id(%d)\n", __FUNCTION__, id); + return _FALSE; + } + + return _TRUE; +} + +static int init_halmac_event_with_waittime(struct dvobj_priv *d, enum halmac_feature_id id, u8 *buf, u32 size, u32 time) +{ + struct submit_ctx *sctx; + + + if (!d->hmpriv.indicator[id].sctx) { + sctx = (struct submit_ctx *)rtw_zmalloc(sizeof(*sctx)); + if (!sctx) + return -1; + } else { + RTW_WARN("%s: id(%d) sctx is not NULL!!\n", __FUNCTION__, id); + sctx = d->hmpriv.indicator[id].sctx; + d->hmpriv.indicator[id].sctx = NULL; + } + + rtw_sctx_init(sctx, time); + d->hmpriv.indicator[id].buffer = buf; + d->hmpriv.indicator[id].buf_size = size; + d->hmpriv.indicator[id].ret_size = 0; + d->hmpriv.indicator[id].status = 0; + /* fill sctx at least to sure other variables are all ready! */ + d->hmpriv.indicator[id].sctx = sctx; + + return 0; +} + +static inline int init_halmac_event(struct dvobj_priv *d, enum halmac_feature_id id, u8 *buf, u32 size) +{ + return init_halmac_event_with_waittime(d, id, buf, size, DEFAULT_INDICATOR_TIMELMT); +} + +static void free_halmac_event(struct dvobj_priv *d, enum halmac_feature_id id) +{ + struct submit_ctx *sctx; + + + if (!d->hmpriv.indicator[id].sctx) + return; + + sctx = d->hmpriv.indicator[id].sctx; + d->hmpriv.indicator[id].sctx = NULL; + rtw_mfree((u8 *)sctx, sizeof(*sctx)); +} + +static int wait_halmac_event(struct dvobj_priv *d, enum halmac_feature_id id) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + struct submit_ctx *sctx; + int status; + int ret; + + + sctx = d->hmpriv.indicator[id].sctx; + if (!sctx) + return -1; + + ret = rtw_sctx_wait(sctx, RTW_HALMAC_FEATURE_NAME[id]); + status = sctx->status; + free_halmac_event(d, id); + if (_SUCCESS == ret) + return 0; + + /* If no one change sctx->status, it is timeout case */ + if (status == 0) + status = RTW_SCTX_DONE_TIMEOUT; + RTW_ERR("%s: id(%d, %s) status=0x%x ! Reset HALMAC state!\n", + __FUNCTION__, id, RTW_HALMAC_FEATURE_NAME[id], status); + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + api->halmac_reset_feature(mac, id); + + return -1; +} + +/* + * Return: + * Always return RTW_HALMAC_SUCCESS, HALMAC don't care the return value. + */ +static u8 _halmac_event_indication(void *p, enum halmac_feature_id feature_id, + enum halmac_cmd_process_status process_status, + u8 *buf, u32 size) +{ + struct dvobj_priv *d; + PADAPTER adapter; + PHAL_DATA_TYPE hal; + struct halmac_indicator *tbl, *indicator; + struct submit_ctx *sctx; + u32 cpsz; + u8 ret; + + + d = (struct dvobj_priv *)p; + adapter = dvobj_get_primary_adapter(d); + hal = GET_HAL_DATA(adapter); + tbl = d->hmpriv.indicator; + + /* Filter(Skip) middle status indication */ + ret = is_valid_id_status(feature_id, process_status); + if (_FALSE == ret) + goto exit; + + indicator = &tbl[feature_id]; + indicator->status = process_status; + indicator->ret_size = size; + if (!indicator->sctx) { + RTW_WARN("%s: id(%d, %s) is not waiting!!\n", __FUNCTION__, + feature_id, RTW_HALMAC_FEATURE_NAME[feature_id]); + goto exit; + } + sctx = indicator->sctx; + + if (HALMAC_CMD_PROCESS_ERROR == process_status) { + RTW_ERR("%s: id(%d, %s) Something wrong!!\n", __FUNCTION__, + feature_id, RTW_HALMAC_FEATURE_NAME[feature_id]); + if ((size == 1) && buf) + RTW_ERR("%s: error code=0x%x\n", __FUNCTION__, *buf); + rtw_sctx_done_err(&sctx, RTW_SCTX_DONE_UNKNOWN); + goto exit; + } + + if (size > indicator->buf_size) { + RTW_WARN("%s: id(%d, %s) buffer is not enough(%d<%d), " + "and data will be truncated!\n", + __FUNCTION__, + feature_id, RTW_HALMAC_FEATURE_NAME[feature_id], + indicator->buf_size, size); + cpsz = indicator->buf_size; + } else { + cpsz = size; + } + if (cpsz && indicator->buffer) + _rtw_memcpy(indicator->buffer, buf, cpsz); + + rtw_sctx_done(&sctx); + +exit: + return RTW_HALMAC_SUCCESS; +} + +struct halmac_platform_api rtw_halmac_platform_api = { + /* R/W register */ +#ifdef CONFIG_SDIO_HCI + .SDIO_CMD52_READ = _halmac_sdio_cmd52_read, + .SDIO_CMD53_READ_8 = _halmac_sdio_reg_read_8, + .SDIO_CMD53_READ_16 = _halmac_sdio_reg_read_16, + .SDIO_CMD53_READ_32 = _halmac_sdio_reg_read_32, + .SDIO_CMD53_READ_N = _halmac_sdio_reg_read_n, + .SDIO_CMD52_WRITE = _halmac_sdio_cmd52_write, + .SDIO_CMD53_WRITE_8 = _halmac_sdio_reg_write_8, + .SDIO_CMD53_WRITE_16 = _halmac_sdio_reg_write_16, + .SDIO_CMD53_WRITE_32 = _halmac_sdio_reg_write_32, + .SDIO_CMD52_CIA_READ = _halmac_sdio_read_cia, +#endif /* CONFIG_SDIO_HCI */ +#if defined(CONFIG_USB_HCI) || defined(CONFIG_PCI_HCI) + .REG_READ_8 = _halmac_reg_read_8, + .REG_READ_16 = _halmac_reg_read_16, + .REG_READ_32 = _halmac_reg_read_32, + .REG_WRITE_8 = _halmac_reg_write_8, + .REG_WRITE_16 = _halmac_reg_write_16, + .REG_WRITE_32 = _halmac_reg_write_32, +#endif /* CONFIG_USB_HCI || CONFIG_PCI_HCI */ + +#ifdef DBG_IO + .READ_MONITOR = _halmac_reg_read_monitor, + .WRITE_MONITOR = _halmac_reg_write_monitor, +#endif + + /* Write data */ +#if 0 + /* impletement in HAL-IC level */ + .SEND_RSVD_PAGE = sdio_write_data_rsvd_page, + .SEND_H2C_PKT = sdio_write_data_h2c, +#endif + /* Memory allocate */ + .RTL_FREE = _halmac_mfree, + .RTL_MALLOC = _halmac_malloc, + .RTL_MEMCPY = _halmac_memcpy, + .RTL_MEMSET = _halmac_memset, + + /* Sleep */ + .RTL_DELAY_US = _halmac_udelay, + + /* Process Synchronization */ + .MUTEX_INIT = _halmac_mutex_init, + .MUTEX_DEINIT = _halmac_mutex_deinit, + .MUTEX_LOCK = _halmac_mutex_lock, + .MUTEX_UNLOCK = _halmac_mutex_unlock, + + .MSG_PRINT = _halmac_msg_print, + .BUFF_PRINT = _halmac_buff_print, + .EVENT_INDICATION = _halmac_event_indication, +}; + +u8 rtw_halmac_read8(struct intf_hdl *pintfhdl, u32 addr) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + + + /* WARNING: pintf_dev should not be null! */ + mac = dvobj_to_halmac(pintfhdl->pintf_dev); + api = HALMAC_GET_API(mac); + + return api->halmac_reg_read_8(mac, addr); +} + +u16 rtw_halmac_read16(struct intf_hdl *pintfhdl, u32 addr) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + + + /* WARNING: pintf_dev should not be null! */ + mac = dvobj_to_halmac(pintfhdl->pintf_dev); + api = HALMAC_GET_API(mac); + + return api->halmac_reg_read_16(mac, addr); +} + +u32 rtw_halmac_read32(struct intf_hdl *pintfhdl, u32 addr) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + + + /* WARNING: pintf_dev should not be null! */ + mac = dvobj_to_halmac(pintfhdl->pintf_dev); + api = HALMAC_GET_API(mac); + + return api->halmac_reg_read_32(mac, addr); +} + +static void _read_register(struct dvobj_priv *d, u32 addr, u32 cnt, u8 *buf) +{ +#if 1 + struct _ADAPTER *a; + u32 i, n; + u16 val16; + u32 val32; + + + a = dvobj_get_primary_adapter(d); + + i = addr & 0x3; + /* Handle address not start from 4 bytes alignment case */ + if (i) { + val32 = cpu_to_le32(rtw_read32(a, addr & ~0x3)); + n = 4 - i; + _rtw_memcpy(buf, ((u8 *)&val32) + i, n); + i = n; + cnt -= n; + } + + while (cnt) { + if (cnt >= 4) + n = 4; + else if (cnt >= 2) + n = 2; + else + n = 1; + cnt -= n; + + switch (n) { + case 1: + buf[i] = rtw_read8(a, addr+i); + i++; + break; + case 2: + val16 = cpu_to_le16(rtw_read16(a, addr+i)); + _rtw_memcpy(&buf[i], &val16, 2); + i += 2; + break; + case 4: + val32 = cpu_to_le32(rtw_read32(a, addr+i)); + _rtw_memcpy(&buf[i], &val32, 4); + i += 4; + break; + } + } +#else + struct _ADAPTER *a; + u32 i; + + + a = dvobj_get_primary_adapter(d); + for (i = 0; i < cnt; i++) + buf[i] = rtw_read8(a, addr + i); +#endif +} + +#ifdef CONFIG_SDIO_HCI +static int _sdio_read_local(struct dvobj_priv *d, u32 addr, u32 cnt, u8 *buf) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + + + if (buf == NULL) + return -1; + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_reg_sdio_cmd53_read_n(mac, addr, cnt, buf); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: addr=0x%08x cnt=%d err=%d\n", + __FUNCTION__, addr, cnt, status); + return -1; + } + + return 0; +} +#endif /* CONFIG_SDIO_HCI */ + +void rtw_halmac_read_mem(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem) +{ + struct dvobj_priv *d; + + + if (pmem == NULL) { + RTW_ERR("pmem is NULL\n"); + return; + } + + d = pintfhdl->pintf_dev; + +#ifdef CONFIG_SDIO_HCI + if (addr & 0xFFFF0000) { + int err = 0; + + err = _sdio_read_local(d, addr, cnt, pmem); + if (!err) + return; + } +#endif /* CONFIG_SDIO_HCI */ + + _read_register(d, addr, cnt, pmem); +} + +#ifdef CONFIG_SDIO_INDIRECT_ACCESS +u8 rtw_halmac_iread8(struct intf_hdl *pintfhdl, u32 addr) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + + /* WARNING: pintf_dev should not be null! */ + mac = dvobj_to_halmac(pintfhdl->pintf_dev); + api = HALMAC_GET_API(mac); + + /*return api->halmac_reg_read_indirect_8(mac, addr);*/ + return api->halmac_reg_read_8(mac, addr); +} + +u16 rtw_halmac_iread16(struct intf_hdl *pintfhdl, u32 addr) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + u16 val16 = 0; + + /* WARNING: pintf_dev should not be null! */ + mac = dvobj_to_halmac(pintfhdl->pintf_dev); + api = HALMAC_GET_API(mac); + + /*return api->halmac_reg_read_indirect_16(mac, addr);*/ + return api->halmac_reg_read_16(mac, addr); +} + +u32 rtw_halmac_iread32(struct intf_hdl *pintfhdl, u32 addr) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + + + /* WARNING: pintf_dev should not be null! */ + mac = dvobj_to_halmac(pintfhdl->pintf_dev); + api = HALMAC_GET_API(mac); + + return api->halmac_reg_read_indirect_32(mac, addr); +} +#endif /* CONFIG_SDIO_INDIRECT_ACCESS */ + +int rtw_halmac_write8(struct intf_hdl *pintfhdl, u32 addr, u8 value) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + + + /* WARNING: pintf_dev should not be null! */ + mac = dvobj_to_halmac(pintfhdl->pintf_dev); + api = HALMAC_GET_API(mac); + + status = api->halmac_reg_write_8(mac, addr, value); + + if (status == HALMAC_RET_SUCCESS) + return 0; + + return -1; +} + +int rtw_halmac_write16(struct intf_hdl *pintfhdl, u32 addr, u16 value) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + + + /* WARNING: pintf_dev should not be null! */ + mac = dvobj_to_halmac(pintfhdl->pintf_dev); + api = HALMAC_GET_API(mac); + + status = api->halmac_reg_write_16(mac, addr, value); + + if (status == HALMAC_RET_SUCCESS) + return 0; + + return -1; +} + +int rtw_halmac_write32(struct intf_hdl *pintfhdl, u32 addr, u32 value) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + + + /* WARNING: pintf_dev should not be null! */ + mac = dvobj_to_halmac(pintfhdl->pintf_dev); + api = HALMAC_GET_API(mac); + + status = api->halmac_reg_write_32(mac, addr, value); + + if (status == HALMAC_RET_SUCCESS) + return 0; + + return -1; +} + +static int init_write_rsvd_page_size(struct dvobj_priv *d) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + u32 size = 0; + struct halmac_ofld_func_info ofld_info; + enum halmac_ret_status status; + int err = 0; + + +#ifdef CONFIG_USB_HCI + /* for USB do not exceed MAX_CMDBUF_SZ */ + size = 0x1000; +#elif defined(CONFIG_PCI_HCI) + size = MAX_CMDBUF_SZ - TXDESC_OFFSET; +#elif defined(CONFIG_SDIO_HCI) + size = 0x7000; /* 28KB */ +#else + /* Use HALMAC default setting and don't call any function */ + return 0; +#endif +#if 0 /* Fail to pass coverity DEADCODE check */ + /* If size==0, use HALMAC default setting and don't call any function */ + if (!size) + return 0; +#endif + err = rtw_halmac_set_max_dl_fw_size(d, size); + if (err) { + RTW_ERR("%s: Fail to set max download fw size!\n", __FUNCTION__); + return -1; + } + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + _rtw_memset(&ofld_info, 0, sizeof(ofld_info)); + ofld_info.halmac_malloc_max_sz = 0xFFFFFFFF; + ofld_info.rsvd_pg_drv_buf_max_sz = size; + status = api->halmac_ofld_func_cfg(mac, &ofld_info); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: Fail to config offload parameters!\n", __FUNCTION__); + return -1; + } + + return 0; +} + +static int init_priv(struct halmacpriv *priv) +{ + struct halmac_indicator *indicator; + u32 count, size; + + + if (priv->indicator) + RTW_WARN("%s: HALMAC private data is not CLEAR!\n", __FUNCTION__); + count = HALMAC_FEATURE_ALL + 1; + size = sizeof(*indicator) * count; + indicator = (struct halmac_indicator *)rtw_zmalloc(size); + if (!indicator) + return -1; + priv->indicator = indicator; + + return 0; +} + +static void deinit_priv(struct halmacpriv *priv) +{ + struct halmac_indicator *indicator; + + + indicator = priv->indicator; + priv->indicator = NULL; + if (indicator) { + u32 count, size; + + count = HALMAC_FEATURE_ALL + 1; +#ifdef CONFIG_RTW_DEBUG + { + struct submit_ctx *sctx; + u32 i; + + for (i = 0; i < count; i++) { + if (!indicator[i].sctx) + continue; + + RTW_WARN("%s: %s id(%d) sctx still exist!!\n", + __FUNCTION__, RTW_HALMAC_FEATURE_NAME[i], i); + sctx = indicator[i].sctx; + indicator[i].sctx = NULL; + rtw_mfree((u8 *)sctx, sizeof(*sctx)); + } + } +#endif /* !CONFIG_RTW_DEBUG */ + size = sizeof(*indicator) * count; + rtw_mfree((u8 *)indicator, size); + } +} + +#ifdef CONFIG_SDIO_HCI +static enum halmac_sdio_spec_ver _sdio_ver_drv2halmac(struct dvobj_priv *d) +{ + bool v3; + enum halmac_sdio_spec_ver ver; + + + v3 = rtw_is_sdio30(dvobj_get_primary_adapter(d)); + if (v3) + ver = HALMAC_SDIO_SPEC_VER_3_00; + else + ver = HALMAC_SDIO_SPEC_VER_2_00; + + return ver; +} +#endif /* CONFIG_SDIO_HCI */ + +void rtw_halmac_get_version(char *str, u32 len) +{ + enum halmac_ret_status status; + struct halmac_ver ver; + + + status = halmac_get_version(&ver); + if (status != HALMAC_RET_SUCCESS) + return; + + rtw_sprintf(str, len, "V%d_%02d_%02d", + ver.major_ver, ver.prototype_ver, ver.minor_ver); +} + +int rtw_halmac_init_adapter(struct dvobj_priv *d, struct halmac_platform_api *pf_api) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_interface intf; + enum halmac_intf_phy_platform pltfm = HALMAC_INTF_PHY_PLATFORM_ALL; + enum halmac_ret_status status; + int err = 0; +#ifdef CONFIG_SDIO_HCI + struct halmac_sdio_hw_info info; +#endif /* CONFIG_SDIO_HCI */ + + + halmac = dvobj_to_halmac(d); + if (halmac) { + RTW_WARN("%s: initialize already completed!\n", __FUNCTION__); + goto error; + } + + err = init_priv(&d->hmpriv); + if (err) + goto error; + +#ifdef CONFIG_SDIO_HCI + intf = HALMAC_INTERFACE_SDIO; +#elif defined(CONFIG_USB_HCI) + intf = HALMAC_INTERFACE_USB; +#elif defined(CONFIG_PCI_HCI) + intf = HALMAC_INTERFACE_PCIE; +#else +#warning "INTERFACE(CONFIG_XXX_HCI) not be defined!!" + intf = HALMAC_INTERFACE_UNDEFINE; +#endif + status = halmac_init_adapter(d, pf_api, intf, &halmac, &api); + if (HALMAC_RET_SUCCESS != status) { + RTW_ERR("%s: halmac_init_adapter fail!(status=%d)\n", __FUNCTION__, status); + err = -1; + if (halmac) + goto deinit; + goto free; + } + + dvobj_set_halmac(d, halmac); + + status = api->halmac_interface_integration_tuning(halmac); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: halmac_interface_integration_tuning fail!(status=%d)\n", __FUNCTION__, status); + err = -1; + goto deinit; + } + +#ifdef CONFIG_PLATFORM_RTK1319 + pltfm = HALMAC_INTF_PHY_PLATFORM_DHC; +#endif /* CONFIG_PLATFORM_RTK1319 */ + status = api->halmac_phy_cfg(halmac, pltfm); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: halmac_phy_cfg fail! (platform=%d, status=%d)\n", + __FUNCTION__, pltfm, status); + err = -1; + goto deinit; + } + + init_write_rsvd_page_size(d); + +#ifdef CONFIG_SDIO_HCI + _rtw_memset(&info, 0, sizeof(info)); + info.spec_ver = _sdio_ver_drv2halmac(d); + /* Convert clock speed unit to MHz from Hz */ + info.clock_speed = RTW_DIV_ROUND_UP(rtw_sdio_get_clock(d), 1000000); + info.block_size = rtw_sdio_get_block_size(d); + if (d->hmpriv.sdio_io_indir == 2) + info.io_indir_flag = 0; + else + info.io_indir_flag = 1; /* Default enable indirect I/O */ + RTW_DBG("%s: SDIO ver=%u clock=%uMHz blk_size=%u bytes, io_indir=%u\n", + __FUNCTION__, info.spec_ver+2, info.clock_speed, + info.block_size, info.io_indir_flag); + status = api->halmac_sdio_hw_info(halmac, &info); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: halmac_sdio_hw_info fail!(status=%d)\n", + __FUNCTION__, status); + err = -1; + goto deinit; + } +#endif /* CONFIG_SDIO_HCI */ + + return 0; + +deinit: + status = halmac_deinit_adapter(halmac); + dvobj_set_halmac(d, NULL); + if (status != HALMAC_RET_SUCCESS) + RTW_ERR("%s: halmac_deinit_adapter fail!(status=%d)\n", + __FUNCTION__, status); + +free: + deinit_priv(&d->hmpriv); + +error: + return err; +} + +int rtw_halmac_deinit_adapter(struct dvobj_priv *d) +{ + struct halmac_adapter *halmac; + enum halmac_ret_status status; + int err = 0; + + + halmac = dvobj_to_halmac(d); + if (halmac) { + status = halmac_deinit_adapter(halmac); + dvobj_set_halmac(d, NULL); + if (status != HALMAC_RET_SUCCESS) + err = -1; + } + + deinit_priv(&d->hmpriv); + + return err; +} + +static inline enum halmac_portid _hw_port_drv2halmac(enum _hw_port hwport) +{ + enum halmac_portid port = HALMAC_PORTID_NUM; + + + switch (hwport) { + case HW_PORT0: + port = HALMAC_PORTID0; + break; + case HW_PORT1: + port = HALMAC_PORTID1; + break; + case HW_PORT2: + port = HALMAC_PORTID2; + break; + case HW_PORT3: + port = HALMAC_PORTID3; + break; + case HW_PORT4: + port = HALMAC_PORTID4; + break; + default: + break; + } + + return port; +} + +static enum halmac_network_type_select _network_type_drv2halmac(u8 type) +{ + enum halmac_network_type_select network = HALMAC_NETWORK_UNDEFINE; + + + switch (type) { + case _HW_STATE_NOLINK_: + case _HW_STATE_MONITOR_: + network = HALMAC_NETWORK_NO_LINK; + break; + + case _HW_STATE_ADHOC_: + network = HALMAC_NETWORK_ADHOC; + break; + + case _HW_STATE_STATION_: + network = HALMAC_NETWORK_INFRASTRUCTURE; + break; + + case _HW_STATE_AP_: + network = HALMAC_NETWORK_AP; + break; + } + + return network; +} + +static u8 _network_type_halmac2drv(enum halmac_network_type_select network) +{ + u8 type = _HW_STATE_NOLINK_; + + + switch (network) { + case HALMAC_NETWORK_NO_LINK: + case HALMAC_NETWORK_UNDEFINE: + type = _HW_STATE_NOLINK_; + break; + + case HALMAC_NETWORK_ADHOC: + type = _HW_STATE_ADHOC_; + break; + + case HALMAC_NETWORK_INFRASTRUCTURE: + type = _HW_STATE_STATION_; + break; + + case HALMAC_NETWORK_AP: + type = _HW_STATE_AP_; + break; + } + + return type; +} + +static void _beacon_ctrl_halmac2drv(struct halmac_bcn_ctrl *ctrl, + struct rtw_halmac_bcn_ctrl *drv_ctrl) +{ + drv_ctrl->rx_bssid_fit = ctrl->dis_rx_bssid_fit ? 0 : 1; + drv_ctrl->txbcn_rpt = ctrl->en_txbcn_rpt ? 1 : 0; + drv_ctrl->tsf_update = ctrl->dis_tsf_udt ? 0 : 1; + drv_ctrl->enable_bcn = ctrl->en_bcn ? 1 : 0; + drv_ctrl->rxbcn_rpt = ctrl->en_rxbcn_rpt ? 1 : 0; + drv_ctrl->p2p_ctwin = ctrl->en_p2p_ctwin ? 1 : 0; + drv_ctrl->p2p_bcn_area = ctrl->en_p2p_bcn_area ? 1 : 0; +} + +static void _beacon_ctrl_drv2halmac(struct rtw_halmac_bcn_ctrl *drv_ctrl, + struct halmac_bcn_ctrl *ctrl) +{ + ctrl->dis_rx_bssid_fit = drv_ctrl->rx_bssid_fit ? 0 : 1; + ctrl->en_txbcn_rpt = drv_ctrl->txbcn_rpt ? 1 : 0; + ctrl->dis_tsf_udt = drv_ctrl->tsf_update ? 0 : 1; + ctrl->en_bcn = drv_ctrl->enable_bcn ? 1 : 0; + ctrl->en_rxbcn_rpt = drv_ctrl->rxbcn_rpt ? 1 : 0; + ctrl->en_p2p_ctwin = drv_ctrl->p2p_ctwin ? 1 : 0; + ctrl->en_p2p_bcn_area = drv_ctrl->p2p_bcn_area ? 1 : 0; +} + +int rtw_halmac_get_hw_value(struct dvobj_priv *d, enum halmac_hw_id hw_id, void *pvalue) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_get_hw_value(mac, hw_id, pvalue); + if (HALMAC_RET_SUCCESS != status) + return -1; + + return 0; +} + +/** + * rtw_halmac_get_tx_fifo_size() - TX FIFO size + * @d: struct dvobj_priv* + * @size: TX FIFO size, unit is byte. + * + * Get TX FIFO size(byte) from HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_get_tx_fifo_size(struct dvobj_priv *d, u32 *size) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 val = 0; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_TXFIFO_SIZE, &val); + if (status != HALMAC_RET_SUCCESS) + return -1; + + *size = val; + + return 0; +} + +/** + * rtw_halmac_get_rx_fifo_size() - RX FIFO size + * @d: struct dvobj_priv* + * @size: RX FIFO size, unit is byte + * + * Get RX FIFO size(byte) from HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_get_rx_fifo_size(struct dvobj_priv *d, u32 *size) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 val = 0; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_RXFIFO_SIZE, &val); + if (status != HALMAC_RET_SUCCESS) + return -1; + + *size = val; + + return 0; +} + +/** + * rtw_halmac_get_rsvd_drv_pg_bndy() - Reserve page boundary of driver + * @d: struct dvobj_priv* + * @size: Page size, unit is byte + * + * Get reserve page boundary of driver from HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_get_rsvd_drv_pg_bndy(struct dvobj_priv *d, u16 *bndy) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + u16 val = 0; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_RSVD_PG_BNDY, &val); + if (status != HALMAC_RET_SUCCESS) + return -1; + + *bndy = val; + + return 0; +} + +/** + * rtw_halmac_get_page_size() - Page size + * @d: struct dvobj_priv* + * @size: Page size, unit is byte + * + * Get TX/RX page size(byte) from HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_get_page_size(struct dvobj_priv *d, u32 *size) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 val = 0; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_PAGE_SIZE, &val); + if (status != HALMAC_RET_SUCCESS) + return -1; + + *size = val; + + return 0; +} + +/** + * rtw_halmac_get_tx_agg_align_size() - TX aggregation align size + * @d: struct dvobj_priv* + * @size: TX aggregation align size, unit is byte + * + * Get TX aggregation align size(byte) from HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_get_tx_agg_align_size(struct dvobj_priv *d, u16 *size) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + u16 val = 0; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_TX_AGG_ALIGN_SIZE, &val); + if (status != HALMAC_RET_SUCCESS) + return -1; + + *size = val; + + return 0; +} + +/** + * rtw_halmac_get_rx_agg_align_size() - RX aggregation align size + * @d: struct dvobj_priv* + * @size: RX aggregation align size, unit is byte + * + * Get RX aggregation align size(byte) from HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_get_rx_agg_align_size(struct dvobj_priv *d, u8 *size) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + u8 val = 0; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_RX_AGG_ALIGN_SIZE, &val); + if (status != HALMAC_RET_SUCCESS) + return -1; + + *size = val; + + return 0; +} + +/* + * Description: + * Get RX driver info size. RX driver info is a small memory space between + * scriptor and RX payload. + * + * +-------------------------+ + * | RX descriptor | + * | usually 24 bytes | + * +-------------------------+ + * | RX driver info | + * | depends on driver cfg | + * +-------------------------+ + * | RX paylad | + * | | + * +-------------------------+ + * + * Parameter: + * d pointer to struct dvobj_priv of driver + * sz rx driver info size in bytes. + * + * Return: + * 0 Success + * other Fail + */ +int rtw_halmac_get_rx_drv_info_sz(struct dvobj_priv *d, u8 *sz) +{ + enum halmac_ret_status status; + struct halmac_adapter *halmac = dvobj_to_halmac(d); + struct halmac_api *api = HALMAC_GET_API(halmac); + u8 dw = 0; + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_DRV_INFO_SIZE, &dw); + if (status != HALMAC_RET_SUCCESS) + return -1; + + *sz = dw * 8; + return 0; +} + +/** + * rtw_halmac_get_tx_desc_size() - TX descriptor size + * @d: struct dvobj_priv* + * @size: TX descriptor size, unit is byte. + * + * Get TX descriptor size(byte) from HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_get_tx_desc_size(struct dvobj_priv *d, u32 *size) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 val = 0; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_TX_DESC_SIZE, &val); + if (status != HALMAC_RET_SUCCESS) + return -1; + + *size = val; + + return 0; +} + +/** + * rtw_halmac_get_rx_desc_size() - RX descriptor size + * @d: struct dvobj_priv* + * @size: RX descriptor size, unit is byte. + * + * Get RX descriptor size(byte) from HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_get_rx_desc_size(struct dvobj_priv *d, u32 *size) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 val = 0; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_RX_DESC_SIZE, &val); + if (status != HALMAC_RET_SUCCESS) + return -1; + + *size = val; + + return 0; +} + +/** + * rtw_halmac_get_tx_dma_ch_map() - Get TX DMA channel Map for tx desc + * @d: struct dvobj_priv* + * @dma_ch_map: return map of QSEL to DMA channel + * @map_size: size of dma_ch_map + * Suggest size to be last valid QSEL(QSLT_CMD)+1 or full QSLT + * size(0x20) + * + * 8814B would need this to get mapping of QSEL to DMA channel for TX desc. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_get_tx_dma_ch_map(struct dvobj_priv *d, u8 *dma_ch_map, u8 map_size) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + struct halmac_rqpn_ch_map map; + enum halmac_dma_ch channel = HALMAC_DMA_CH_UNDEFINE; + u8 qsel; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_RQPN_CH_MAPPING, &map); + if (status != HALMAC_RET_SUCCESS) + return -1; + + for (qsel = 0; qsel < map_size; qsel++) { + switch (qsel) { + /*case QSLT_VO:*/ + case 0x06: + case 0x07: + channel = map.dma_map_vo; + break; + /*case QSLT_VI:*/ + case 0x04: + case 0x05: + channel = map.dma_map_vi; + break; + /*case QSLT_BE:*/ + case 0x00: + case 0x03: + channel = map.dma_map_be; + break; + /*case QSLT_BK:*/ + case 0x01: + case 0x02: + channel = map.dma_map_bk; + break; + /*case QSLT_BEACON:*/ + case 0x10: + channel = HALMAC_DMA_CH_BCN; + break; + /*case QSLT_HIGH:*/ + case 0x11: + channel = map.dma_map_hi; + break; + /*case QSLT_MGNT:*/ + case 0x12: + channel = map.dma_map_mg; + break; + /*case QSLT_CMD:*/ + case 0x13: + channel = HALMAC_DMA_CH_H2C; + break; + default: + /*RTW_ERR("%s: invalid qsel=0x%x\n", __FUNCTION__, qsel);*/ + channel = HALMAC_DMA_CH_UNDEFINE; + break; + } + dma_ch_map[qsel] = (u8)channel; + } + + return 0; +} + +/** + * rtw_halmac_get_fw_max_size() - Firmware MAX size + * @d: struct dvobj_priv* + * @size: MAX Firmware size, unit is byte. + * + * Get Firmware MAX size(byte) from HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +static int rtw_halmac_get_fw_max_size(struct dvobj_priv *d, u32 *size) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 val = 0; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_FW_MAX_SIZE, &val); + if (status != HALMAC_RET_SUCCESS) + return -1; + + *size = val; + + return 0; +} + +/** + * rtw_halmac_get_ori_h2c_size() - Original H2C MAX size + * @d: struct dvobj_priv* + * @size: H2C MAX size, unit is byte. + * + * Get original H2C MAX size(byte) from HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_get_ori_h2c_size(struct dvobj_priv *d, u32 *size) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 val = 0; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_ORI_H2C_SIZE, &val); + if (status != HALMAC_RET_SUCCESS) + return -1; + + *size = val; + + return 0; +} + +int rtw_halmac_get_oqt_size(struct dvobj_priv *d, u8 *size) +{ + enum halmac_ret_status status; + struct halmac_adapter *halmac; + struct halmac_api *api; + u8 val; + + + if (!size) + return -1; + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_AC_OQT_SIZE, &val); + if (status != HALMAC_RET_SUCCESS) + return -1; + + *size = val; + return 0; +} + +int rtw_halmac_get_ac_queue_number(struct dvobj_priv *d, u8 *num) +{ + enum halmac_ret_status status; + struct halmac_adapter *halmac; + struct halmac_api *api; + u8 val; + + + if (!num) + return -1; + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_AC_QUEUE_NUM, &val); + if (status != HALMAC_RET_SUCCESS) + return -1; + + *num = val; + return 0; +} + +/** + * rtw_halmac_get_mac_address() - Get MAC address of specific port + * @d: struct dvobj_priv* + * @hwport: port + * @addr: buffer for storing MAC address + * + * Get MAC address of specific port from HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_get_mac_address(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_portid port; + union halmac_wlan_addr hwa; + enum halmac_ret_status status; + int err = -1; + + + if (!addr) + goto out; + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + port = _hw_port_drv2halmac(hwport); + _rtw_memset(&hwa, 0, sizeof(hwa)); + + status = api->halmac_get_mac_addr(halmac, port, &hwa); + if (status != HALMAC_RET_SUCCESS) + goto out; + + _rtw_memcpy(addr, hwa.addr, 6); + + err = 0; +out: + return err; +} + +/** + * rtw_halmac_get_network_type() - Get network type of specific port + * @d: struct dvobj_priv* + * @hwport: port + * @type: buffer to put network type (_HW_STATE_*) + * + * Get network type of specific port from HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_get_network_type(struct dvobj_priv *d, enum _hw_port hwport, u8 *type) +{ +#if 0 + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_portid port; + enum halmac_network_type_select network; + enum halmac_ret_status status; + int err = -1; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + port = _hw_port_drv2halmac(hwport); + network = HALMAC_NETWORK_UNDEFINE; + + status = api->halmac_get_net_type(halmac, port, &network); + if (status != HALMAC_RET_SUCCESS) + goto out; + + *type = _network_type_halmac2drv(network); + + err = 0; +out: + return err; +#else + struct _ADAPTER *a; + enum halmac_portid port; + enum halmac_network_type_select network; + u32 val; + int err = -1; + + + a = dvobj_get_primary_adapter(d); + port = _hw_port_drv2halmac(hwport); + network = HALMAC_NETWORK_UNDEFINE; + + switch (port) { + case HALMAC_PORTID0: + val = rtw_read32(a, REG_CR); + network = BIT_GET_NETYPE0(val); + break; + + case HALMAC_PORTID1: + val = rtw_read32(a, REG_CR); + network = BIT_GET_NETYPE1(val); + break; + + case HALMAC_PORTID2: + val = rtw_read32(a, REG_CR_EXT); + network = BIT_GET_NETYPE2(val); + break; + + case HALMAC_PORTID3: + val = rtw_read32(a, REG_CR_EXT); + network = BIT_GET_NETYPE3(val); + break; + + case HALMAC_PORTID4: + val = rtw_read32(a, REG_CR_EXT); + network = BIT_GET_NETYPE4(val); + break; + + default: + goto out; + } + + *type = _network_type_halmac2drv(network); + + err = 0; +out: + return err; +#endif +} + +/** + * rtw_halmac_get_bcn_ctrl() - Get beacon control setting of specific port + * @d: struct dvobj_priv* + * @hwport: port + * @bcn_ctrl: setting of beacon control + * + * Get beacon control setting of specific port from HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_get_bcn_ctrl(struct dvobj_priv *d, enum _hw_port hwport, + struct rtw_halmac_bcn_ctrl *bcn_ctrl) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_portid port; + struct halmac_bcn_ctrl ctrl; + enum halmac_ret_status status; + int err = -1; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + port = _hw_port_drv2halmac(hwport); + _rtw_memset(&ctrl, 0, sizeof(ctrl)); + + status = api->halmac_rw_bcn_ctrl(halmac, port, 0, &ctrl); + if (status != HALMAC_RET_SUCCESS) + goto out; + _beacon_ctrl_halmac2drv(&ctrl, bcn_ctrl); + + err = 0; +out: + return err; +} + +/* + * Note: + * When this function return, the register REG_RCR may be changed. + */ +int rtw_halmac_config_rx_info(struct dvobj_priv *d, enum halmac_drv_info info) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + int err = -1; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_cfg_drv_info(halmac, info); + if (status != HALMAC_RET_SUCCESS) + goto out; + + err = 0; +out: + /* Sync driver RCR cache with register setting */ + rtw_hal_get_hwreg(dvobj_get_primary_adapter(d), HW_VAR_RCR, NULL); + + return err; +} + +/** + * rtw_halmac_set_max_dl_fw_size() - Set the MAX download firmware size + * @d: struct dvobj_priv* + * @size: the max download firmware size in one I/O + * + * Set the max download firmware size in one I/O. + * Please also consider the max size of the callback function "SEND_RSVD_PAGE" + * could accept, because download firmware would call "SEND_RSVD_PAGE" to send + * firmware to IC. + * + * If the value of "size" is not even, it would be rounded down to nearest + * even, and 0 and 1 are both invalid value. + * + * Return 0 for setting OK, otherwise fail. + */ +int rtw_halmac_set_max_dl_fw_size(struct dvobj_priv *d, u32 size) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + + + if (!size || (size == 1)) + return -1; + + mac = dvobj_to_halmac(d); + if (!mac) { + RTW_ERR("%s: HALMAC is not ready!!\n", __FUNCTION__); + return -1; + } + api = HALMAC_GET_API(mac); + + size &= ~1; /* round down to even */ + status = api->halmac_cfg_max_dl_size(mac, size); + if (status != HALMAC_RET_SUCCESS) { + RTW_WARN("%s: Fail to cfg_max_dl_size(%d), err=%d!!\n", + __FUNCTION__, size, status); + return -1; + } + + return 0; +} + +/** + * rtw_halmac_set_mac_address() - Set mac address of specific port + * @d: struct dvobj_priv* + * @hwport: port + * @addr: mac address + * + * Set self mac address of specific port to HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_set_mac_address(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_portid port; + union halmac_wlan_addr hwa; + enum halmac_ret_status status; + int err = -1; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + port = _hw_port_drv2halmac(hwport); + _rtw_memset(&hwa, 0, sizeof(hwa)); + _rtw_memcpy(hwa.addr, addr, 6); + + status = api->halmac_cfg_mac_addr(halmac, port, &hwa); + if (status != HALMAC_RET_SUCCESS) + goto out; + + err = 0; +out: + return err; +} + +/** + * rtw_halmac_set_bssid() - Set BSSID of specific port + * @d: struct dvobj_priv* + * @hwport: port + * @addr: BSSID, mac address of AP + * + * Set BSSID of specific port to HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_set_bssid(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_portid port; + union halmac_wlan_addr hwa; + enum halmac_ret_status status; + int err = -1; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + port = _hw_port_drv2halmac(hwport); + + _rtw_memset(&hwa, 0, sizeof(hwa)); + _rtw_memcpy(hwa.addr, addr, 6); + status = api->halmac_cfg_bssid(halmac, port, &hwa); + if (status != HALMAC_RET_SUCCESS) + goto out; + + err = 0; +out: + return err; +} + +/** + * rtw_halmac_set_tx_address() - Set transmitter address of specific port + * @d: struct dvobj_priv* + * @hwport: port + * @addr: transmitter address + * + * Set transmitter address of specific port to HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_set_tx_address(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_portid port; + union halmac_wlan_addr hwa; + enum halmac_ret_status status; + int err = -1; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + port = _hw_port_drv2halmac(hwport); + _rtw_memset(&hwa, 0, sizeof(hwa)); + _rtw_memcpy(hwa.addr, addr, 6); + + status = api->halmac_cfg_transmitter_addr(halmac, port, &hwa); + if (status != HALMAC_RET_SUCCESS) + goto out; + + err = 0; +out: + return err; +} + +/** + * rtw_halmac_set_network_type() - Set network type of specific port + * @d: struct dvobj_priv* + * @hwport: port + * @type: network type (_HW_STATE_*) + * + * Set network type of specific port to HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_set_network_type(struct dvobj_priv *d, enum _hw_port hwport, u8 type) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_portid port; + enum halmac_network_type_select network; + enum halmac_ret_status status; + int err = -1; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + port = _hw_port_drv2halmac(hwport); + network = _network_type_drv2halmac(type); + + status = api->halmac_cfg_net_type(halmac, port, network); + if (status != HALMAC_RET_SUCCESS) + goto out; + + err = 0; +out: + return err; +} + +/** + * rtw_halmac_reset_tsf() - Reset TSF timer of specific port + * @d: struct dvobj_priv* + * @hwport: port + * + * Notice HALMAC to reset timing synchronization function(TSF) timer of + * specific port. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_reset_tsf(struct dvobj_priv *d, enum _hw_port hwport) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_portid port; + enum halmac_ret_status status; + int err = -1; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + port = _hw_port_drv2halmac(hwport); + + status = api->halmac_cfg_tsf_rst(halmac, port); + if (status != HALMAC_RET_SUCCESS) + goto out; + + err = 0; +out: + return err; +} + +/** + * rtw_halmac_set_bcn_interval() - Set beacon interval of each port + * @d: struct dvobj_priv* + * @hwport: port + * @space: beacon interval, unit is ms + * + * Set beacon interval of specific port to HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_set_bcn_interval(struct dvobj_priv *d, enum _hw_port hwport, + u32 interval) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_portid port; + enum halmac_ret_status status; + int err = -1; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + port = _hw_port_drv2halmac(hwport); + + status = api->halmac_cfg_bcn_space(halmac, port, interval); + if (status != HALMAC_RET_SUCCESS) + goto out; + + err = 0; +out: + return err; +} + +/** + * rtw_halmac_set_bcn_ctrl() - Set beacon control setting of each port + * @d: struct dvobj_priv* + * @hwport: port + * @bcn_ctrl: setting of beacon control + * + * Set beacon control setting of specific port to HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_set_bcn_ctrl(struct dvobj_priv *d, enum _hw_port hwport, + struct rtw_halmac_bcn_ctrl *bcn_ctrl) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_portid port; + struct halmac_bcn_ctrl ctrl; + enum halmac_ret_status status; + int err = -1; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + port = _hw_port_drv2halmac(hwport); + _rtw_memset(&ctrl, 0, sizeof(ctrl)); + _beacon_ctrl_drv2halmac(bcn_ctrl, &ctrl); + + status = api->halmac_rw_bcn_ctrl(halmac, port, 1, &ctrl); + if (status != HALMAC_RET_SUCCESS) + goto out; + + err = 0; +out: + return err; +} + +/** + * rtw_halmac_set_aid() - Set association identifier(AID) of specific port + * @d: struct dvobj_priv* + * @hwport: port + * @aid: Association identifier + * + * Set association identifier(AID) of specific port to HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_set_aid(struct dvobj_priv *d, enum _hw_port hwport, u16 aid) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_portid port; + enum halmac_ret_status status; + int err = -1; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + port = _hw_port_drv2halmac(hwport); + +#if 0 + status = api->halmac_cfg_aid(halmac, port, aid); + if (status != HALMAC_RET_SUCCESS) + goto out; +#else +{ + struct _ADAPTER *a; + u32 addr; + u16 val; + + a = dvobj_get_primary_adapter(d); + + switch (port) { + case 0: + addr = REG_BCN_PSR_RPT; + val = rtw_read16(a, addr); + val = BIT_SET_PS_AID_0(val, aid); + rtw_write16(a, addr, val); + break; + + case 1: + addr = REG_BCN_PSR_RPT1; + val = rtw_read16(a, addr); + val = BIT_SET_PS_AID_1(val, aid); + rtw_write16(a, addr, val); + break; + + case 2: + addr = REG_BCN_PSR_RPT2; + val = rtw_read16(a, addr); + val = BIT_SET_PS_AID_2(val, aid); + rtw_write16(a, addr, val); + break; + + case 3: + addr = REG_BCN_PSR_RPT3; + val = rtw_read16(a, addr); + val = BIT_SET_PS_AID_3(val, aid); + rtw_write16(a, addr, val); + break; + + case 4: + addr = REG_BCN_PSR_RPT4; + val = rtw_read16(a, addr); + val = BIT_SET_PS_AID_4(val, aid); + rtw_write16(a, addr, val); + break; + + default: + goto out; + } +} +#endif + + err = 0; +out: + return err; +} + +int rtw_halmac_set_bandwidth(struct dvobj_priv *d, u8 channel, u8 pri_ch_idx, u8 bw) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_cfg_ch_bw(mac, channel, pri_ch_idx, bw); + if (HALMAC_RET_SUCCESS != status) + return -1; + + return 0; +} + +/** + * rtw_halmac_set_edca() - config edca parameter + * @d: struct dvobj_priv* + * @queue: XMIT_[VO/VI/BE/BK]_QUEUE + * @aifs: Arbitration inter-frame space(AIFS) + * @cw: Contention window(CW) + * @txop: MAX Transmit Opportunity(TXOP) + * + * Return: 0 if process OK, otherwise -1. + */ +int rtw_halmac_set_edca(struct dvobj_priv *d, u8 queue, u8 aifs, u8 cw, u16 txop) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_acq_id ac; + struct halmac_edca_para edca; + enum halmac_ret_status status; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + switch (queue) { + case XMIT_VO_QUEUE: + ac = HALMAC_ACQ_ID_VO; + break; + case XMIT_VI_QUEUE: + ac = HALMAC_ACQ_ID_VI; + break; + case XMIT_BE_QUEUE: + ac = HALMAC_ACQ_ID_BE; + break; + case XMIT_BK_QUEUE: + ac = HALMAC_ACQ_ID_BK; + break; + default: + return -1; + } + + edca.aifs = aifs; + edca.cw = cw; + edca.txop_limit = txop; + + status = api->halmac_cfg_edca_para(mac, ac, &edca); + if (status != HALMAC_RET_SUCCESS) + return -1; + + return 0; +} + +/** + * rtw_halmac_set_rts_full_bw() - Send RTS to all covered channels + * @d: struct dvobj_priv* + * @enable: _TRUE(enable), _FALSE(disable) + * + * Hradware will duplicate RTS packet to all channels which are covered in used + * bandwidth. + * + * Return 0 if process OK, otherwise -1. + */ +int rtw_halmac_set_rts_full_bw(struct dvobj_priv *d, u8 enable) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + u8 full; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + full = (enable == _TRUE) ? 1 : 0; + + status = api->halmac_set_hw_value(mac, HALMAC_HW_RTS_FULL_BW, &full); + if (HALMAC_RET_SUCCESS != status) + return -1; + + return 0; +} + +#ifdef RTW_HALMAC_DBG_POWER_SWITCH +static void _dump_mac_reg(struct dvobj_priv *d, u32 start, u32 end) +{ + struct _ADAPTER *adapter; + int i, j = 1; + + + adapter = dvobj_get_primary_adapter(d); + for (i = start; i < end; i += 4) { + if (j % 4 == 1) + RTW_PRINT("0x%04x", i); + _RTW_PRINT(" 0x%08x ", rtw_read32(adapter, i)); + if ((j++) % 4 == 0) + _RTW_PRINT("\n"); + } +} + +void dump_dbg_val(struct _ADAPTER *a, u32 reg) +{ + u32 v32; + + + rtw_write8(a, 0x3A, reg); + v32 = rtw_read32(a, 0xC0); + RTW_PRINT("0x3A = %02x, 0xC0 = 0x%08x\n",reg, v32); +} + +#ifdef CONFIG_PCI_HCI +static void _dump_pcie_cfg_space(struct dvobj_priv *d) +{ + struct _ADAPTER *padapter = dvobj_get_primary_adapter(d); + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter); + struct pci_dev *pdev = pdvobjpriv->ppcidev; + struct pci_dev *bridge_pdev = pdev->bus->self; + + u32 tmp[4] = { 0 }; + u32 i, j; + + RTW_PRINT("\n***** PCI Device Configuration Space *****\n\n"); + + for(i = 0; i < 0x100; i += 0x10) + { + for (j = 0 ; j < 4 ; j++) + pci_read_config_dword(pdev, i + j * 4, tmp+j); + + RTW_PRINT("%03x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", + i, tmp[0] & 0xFF, (tmp[0] >> 8) & 0xFF, (tmp[0] >> 16) & 0xFF, (tmp[0] >> 24) & 0xFF, + tmp[1] & 0xFF, (tmp[1] >> 8) & 0xFF, (tmp[1] >> 16) & 0xFF, (tmp[1] >> 24) & 0xFF, + tmp[2] & 0xFF, (tmp[2] >> 8) & 0xFF, (tmp[2] >> 16) & 0xFF, (tmp[2] >> 24) & 0xFF, + tmp[3] & 0xFF, (tmp[3] >> 8) & 0xFF, (tmp[3] >> 16) & 0xFF, (tmp[3] >> 24) & 0xFF); + } + + RTW_PRINT("\n***** PCI Host Device Configuration Space*****\n\n"); + + for(i = 0; i < 0x100; i += 0x10) + { + for (j = 0 ; j < 4 ; j++) + pci_read_config_dword(bridge_pdev, i + j * 4, tmp+j); + + RTW_PRINT("%03x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", + i, tmp[0] & 0xFF, (tmp[0] >> 8) & 0xFF, (tmp[0] >> 16) & 0xFF, (tmp[0] >> 24) & 0xFF, + tmp[1] & 0xFF, (tmp[1] >> 8) & 0xFF, (tmp[1] >> 16) & 0xFF, (tmp[1] >> 24) & 0xFF, + tmp[2] & 0xFF, (tmp[2] >> 8) & 0xFF, (tmp[2] >> 16) & 0xFF, (tmp[2] >> 24) & 0xFF, + tmp[3] & 0xFF, (tmp[3] >> 8) & 0xFF, (tmp[3] >> 16) & 0xFF, (tmp[3] >> 24) & 0xFF); + } +} +#endif + +static void _dump_mac_reg_for_power_switch(struct dvobj_priv *d, + const char* caller, char* desc) +{ + struct _ADAPTER *a; + u8 v8; + + + RTW_PRINT("%s: %s\n", caller, desc); + RTW_PRINT("======= MAC REG =======\n"); + /* page 0/1 */ + _dump_mac_reg(d, 0x0, 0x200); + _dump_mac_reg(d, 0x300, 0x400); /* also dump page 3 */ + + /* dump debug register */ + a = dvobj_get_primary_adapter(d); + +#ifdef CONFIG_PCI_HCI + _dump_pcie_cfg_space(d); + + v8 = rtw_read8(a, 0xF6) | 0x01; + rtw_write8(a, 0xF6, v8); + RTW_PRINT("0xF6 = %02x\n", v8); + + dump_dbg_val(a, 0x63); + dump_dbg_val(a, 0x64); + dump_dbg_val(a, 0x68); + dump_dbg_val(a, 0x69); + dump_dbg_val(a, 0x6a); + dump_dbg_val(a, 0x6b); + dump_dbg_val(a, 0x71); + dump_dbg_val(a, 0x72); +#endif +} + +static enum halmac_ret_status _power_switch(struct halmac_adapter *halmac, + struct halmac_api *api, + enum halmac_mac_power pwr) +{ + enum halmac_ret_status status; + char desc[80] = {0}; + + + rtw_sprintf(desc, 80, "before calling power %s", + (pwr==HALMAC_MAC_POWER_ON)?"on":"off"); + _dump_mac_reg_for_power_switch((struct dvobj_priv *)halmac->drv_adapter, + __FUNCTION__, desc); + + status = api->halmac_mac_power_switch(halmac, pwr); + RTW_PRINT("%s: status=%d\n", __FUNCTION__, status); + + rtw_sprintf(desc, 80, "after calling power %s", + (pwr==HALMAC_MAC_POWER_ON)?"on":"off"); + _dump_mac_reg_for_power_switch((struct dvobj_priv *)halmac->drv_adapter, + __FUNCTION__, desc); + + return status; +} +#else /* !RTW_HALMAC_DBG_POWER_SWITCH */ +#define _power_switch(mac, api, pwr) (api)->halmac_mac_power_switch(mac, pwr) +#endif /* !RTW_HALMAC_DBG_POWER_SWITCH */ + +/* + * Description: + * Power on device hardware. + * [Notice!] If device's power state is on before, + * it would be power off first and turn on power again. + * + * Return: + * 0 power on success + * -1 power on fail + * -2 power state unchange + */ +int rtw_halmac_poweron(struct dvobj_priv *d) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + int err = -1; +#if defined(CONFIG_PCI_HCI) && defined(CONFIG_RTL8822B) + struct _ADAPTER *a; + u8 v8; + u32 addr; + + a = dvobj_get_primary_adapter(d); +#endif + + halmac = dvobj_to_halmac(d); + if (!halmac) + goto out; + + api = HALMAC_GET_API(halmac); + + status = api->halmac_pre_init_system_cfg(halmac); + if (status != HALMAC_RET_SUCCESS) + goto out; + +#ifdef CONFIG_SDIO_HCI + status = api->halmac_sdio_cmd53_4byte(halmac, HALMAC_SDIO_CMD53_4BYTE_MODE_RW); + if (status != HALMAC_RET_SUCCESS) + goto out; +#endif /* CONFIG_SDIO_HCI */ + +#if defined(CONFIG_PCI_HCI) && defined(CONFIG_RTL8822B) + addr = 0x3F3; + v8 = rtw_read8(a, addr); + RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v8); + /* are we in pcie debug mode? */ + if (!(v8 & BIT(2))) { + RTW_PRINT("%s: Enable pcie debug mode\n", __FUNCTION__); + v8 |= BIT(2); + v8 = rtw_write8(a, addr, v8); + } +#endif + + status = _power_switch(halmac, api, HALMAC_MAC_POWER_ON); + if (HALMAC_RET_PWR_UNCHANGE == status) { + +#if defined(CONFIG_PCI_HCI) && defined(CONFIG_RTL8822B) + addr = 0x3F3; + v8 = rtw_read8(a, addr); + RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v8); + + /* are we in pcie debug mode? */ + if (!(v8 & BIT(2))) { + RTW_PRINT("%s: Enable pcie debug mode\n", __FUNCTION__); + v8 |= BIT(2); + v8 = rtw_write8(a, addr, v8); + } else if (v8 & BIT(0)) { + /* DMA stuck */ + addr = 0x1350; + v8 = rtw_read8(a, addr); + RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v8); + RTW_PRINT("%s: recover DMA stuck\n", __FUNCTION__); + v8 |= BIT(6); + v8 = rtw_write8(a, addr, v8); + RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v8); + } +#endif + /* + * Work around for warm reboot but device not power off, + * but it would also fall into this case when auto power on is enabled. + */ + _power_switch(halmac, api, HALMAC_MAC_POWER_OFF); + status = _power_switch(halmac, api, HALMAC_MAC_POWER_ON); + RTW_WARN("%s: Power state abnormal, try to recover...%s\n", + __FUNCTION__, (HALMAC_RET_SUCCESS == status)?"OK":"FAIL!"); + } + if (HALMAC_RET_SUCCESS != status) { + if (HALMAC_RET_PWR_UNCHANGE == status) + err = -2; + goto out; + } + + status = api->halmac_init_system_cfg(halmac); + if (status != HALMAC_RET_SUCCESS) + goto out; + + err = 0; +out: + return err; +} + +/* + * Description: + * Power off device hardware. + * + * Return: + * 0 Power off success + * -1 Power off fail + */ +int rtw_halmac_poweroff(struct dvobj_priv *d) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + int err = -1; + + + halmac = dvobj_to_halmac(d); + if (!halmac) + goto out; + + api = HALMAC_GET_API(halmac); + + status = _power_switch(halmac, api, HALMAC_MAC_POWER_OFF); + if ((HALMAC_RET_SUCCESS != status) + && (HALMAC_RET_PWR_UNCHANGE != status)) + goto out; + + err = 0; +out: + return err; +} + +#ifdef CONFIG_SUPPORT_TRX_SHARED +static inline enum halmac_rx_fifo_expanding_mode _trx_share_mode_drv2halmac(u8 trx_share_mode) +{ + if (0 == trx_share_mode) + return HALMAC_RX_FIFO_EXPANDING_MODE_DISABLE; + else if (1 == trx_share_mode) + return HALMAC_RX_FIFO_EXPANDING_MODE_1_BLOCK; + else if (2 == trx_share_mode) + return HALMAC_RX_FIFO_EXPANDING_MODE_2_BLOCK; + else if (3 == trx_share_mode) + return HALMAC_RX_FIFO_EXPANDING_MODE_3_BLOCK; + else + return HALMAC_RX_FIFO_EXPANDING_MODE_DISABLE; +} + +static enum halmac_rx_fifo_expanding_mode _rtw_get_trx_share_mode(struct _ADAPTER *adapter) +{ + struct registry_priv *registry_par = &adapter->registrypriv; + + return _trx_share_mode_drv2halmac(registry_par->trx_share_mode); +} + +void dump_trx_share_mode(void *sel, struct _ADAPTER *adapter) +{ + struct registry_priv *registry_par = &adapter->registrypriv; + u8 mode = _trx_share_mode_drv2halmac(registry_par->trx_share_mode); + + if (HALMAC_RX_FIFO_EXPANDING_MODE_1_BLOCK == mode) + RTW_PRINT_SEL(sel, "TRx share mode : %s\n", "RX_FIFO_EXPANDING_MODE_1"); + else if (HALMAC_RX_FIFO_EXPANDING_MODE_2_BLOCK == mode) + RTW_PRINT_SEL(sel, "TRx share mode : %s\n", "RX_FIFO_EXPANDING_MODE_2"); + else if (HALMAC_RX_FIFO_EXPANDING_MODE_3_BLOCK == mode) + RTW_PRINT_SEL(sel, "TRx share mode : %s\n", "RX_FIFO_EXPANDING_MODE_3"); + else + RTW_PRINT_SEL(sel, "TRx share mode : %s\n", "DISABLE"); +} +#endif + +static enum halmac_drv_rsvd_pg_num _rsvd_page_num_drv2halmac(u16 num) +{ + if (num <= 8) + return HALMAC_RSVD_PG_NUM8; + if (num <= 16) + return HALMAC_RSVD_PG_NUM16; + if (num <= 24) + return HALMAC_RSVD_PG_NUM24; + if (num <= 32) + return HALMAC_RSVD_PG_NUM32; + if (num <= 64) + return HALMAC_RSVD_PG_NUM64; + if (num <= 128) + return HALMAC_RSVD_PG_NUM128; + + if (num > 256) + RTW_WARN("%s: Fail to allocate RSVD page(%d)!!" + " The MAX RSVD page number is 256...\n", + __FUNCTION__, num); + + return HALMAC_RSVD_PG_NUM256; +} + +static u16 _rsvd_page_num_halmac2drv(enum halmac_drv_rsvd_pg_num rsvd_page_number) +{ + u16 num = 0; + + + switch (rsvd_page_number) { + case HALMAC_RSVD_PG_NUM8: + num = 8; + break; + + case HALMAC_RSVD_PG_NUM16: + num = 16; + break; + + case HALMAC_RSVD_PG_NUM24: + num = 24; + break; + + case HALMAC_RSVD_PG_NUM32: + num = 32; + break; + + case HALMAC_RSVD_PG_NUM64: + num = 64; + break; + + case HALMAC_RSVD_PG_NUM128: + num = 128; + break; + + case HALMAC_RSVD_PG_NUM256: + num = 256; + break; + } + + return num; +} + +static enum halmac_trx_mode _choose_trx_mode(struct dvobj_priv *d) +{ + PADAPTER p; + + + p = dvobj_get_primary_adapter(d); + + if (p->registrypriv.wifi_spec) + return HALMAC_TRX_MODE_WMM; + +#ifdef CONFIG_SUPPORT_TRX_SHARED + if (_rtw_get_trx_share_mode(p)) + return HALMAC_TRX_MODE_TRXSHARE; +#endif + + return HALMAC_TRX_MODE_NORMAL; +} + +static inline enum halmac_rf_type _rf_type_drv2halmac(enum rf_type rf_drv) +{ + enum halmac_rf_type rf_mac; + + + switch (rf_drv) { + case RF_1T1R: + rf_mac = HALMAC_RF_1T1R; + break; + case RF_1T2R: + rf_mac = HALMAC_RF_1T2R; + break; + case RF_2T2R: + rf_mac = HALMAC_RF_2T2R; + break; + case RF_2T3R: + rf_mac = HALMAC_RF_2T3R; + break; + case RF_2T4R: + rf_mac = HALMAC_RF_2T4R; + break; + case RF_3T3R: + rf_mac = HALMAC_RF_3T3R; + break; + case RF_3T4R: + rf_mac = HALMAC_RF_3T4R; + break; + case RF_4T4R: + rf_mac = HALMAC_RF_4T4R; + break; + default: + rf_mac = HALMAC_RF_MAX_TYPE; + RTW_ERR("%s: Invalid RF type(0x%x)!\n", __FUNCTION__, rf_drv); + break; + } + + return rf_mac; +} + +static inline enum rf_type _rf_type_halmac2drv(enum halmac_rf_type rf_mac) +{ + enum rf_type rf_drv; + + + switch (rf_mac) { + case HALMAC_RF_1T2R: + rf_drv = RF_1T2R; + break; + case HALMAC_RF_2T4R: + rf_drv = RF_2T4R; + break; + case HALMAC_RF_2T2R: + case HALMAC_RF_2T2R_GREEN: + rf_drv = RF_2T2R; + break; + case HALMAC_RF_2T3R: + rf_drv = RF_2T3R; + break; + case HALMAC_RF_1T1R: + rf_drv = RF_1T1R; + break; + case HALMAC_RF_3T3R: + rf_drv = RF_3T3R; + break; + case HALMAC_RF_3T4R: + rf_drv = RF_3T4R; + break; + case HALMAC_RF_4T4R: + rf_drv = RF_4T4R; + break; + default: + rf_drv = RF_TYPE_MAX; + RTW_ERR("%s: Invalid RF type(0x%x)!\n", __FUNCTION__, rf_mac); + break; + } + + return rf_drv; +} + +static enum odm_cut_version _cut_version_drv2phydm( + enum tag_HAL_Cut_Version_Definition cut_drv) +{ + enum odm_cut_version cut_phydm = ODM_CUT_A; + u32 diff; + + + if (cut_drv > K_CUT_VERSION) + RTW_WARN("%s: unknown cut_ver=%d !!\n", __FUNCTION__, cut_drv); + + diff = cut_drv - A_CUT_VERSION; + cut_phydm += diff; + + return cut_phydm; +} + +static int _send_general_info_by_reg(struct dvobj_priv *d, + struct halmac_general_info *info) +{ + struct _ADAPTER *a; + struct hal_com_data *hal; + enum tag_HAL_Cut_Version_Definition cut_drv; + enum rf_type rftype; + enum odm_cut_version cut_phydm; + u8 h2c[RTW_HALMAC_H2C_MAX_SIZE] = {0}; + + + a = dvobj_get_primary_adapter(d); + hal = GET_HAL_DATA(a); + rftype = _rf_type_halmac2drv(info->rf_type); + cut_drv = GET_CVID_CUT_VERSION(hal->version_id); + cut_phydm = _cut_version_drv2phydm(cut_drv); + +#define CLASS_GENERAL_INFO_REG 0x02 +#define CMD_ID_GENERAL_INFO_REG 0x0C +#define GENERAL_INFO_REG_SET_CMD_ID(buf, v) SET_BITS_TO_LE_4BYTE(buf, 0, 5, v) +#define GENERAL_INFO_REG_SET_CLASS(buf, v) SET_BITS_TO_LE_4BYTE(buf, 5, 3, v) +#define GENERAL_INFO_REG_SET_RFE_TYPE(buf, v) SET_BITS_TO_LE_4BYTE(buf, 8, 8, v) +#define GENERAL_INFO_REG_SET_RF_TYPE(buf, v) SET_BITS_TO_LE_4BYTE(buf, 16, 8, v) +#define GENERAL_INFO_REG_SET_CUT_VERSION(buf, v) SET_BITS_TO_LE_4BYTE(buf, 24, 8, v) +#define GENERAL_INFO_REG_SET_RX_ANT_STATUS(buf, v) SET_BITS_TO_LE_1BYTE(buf+4, 0, 4, v) +#define GENERAL_INFO_REG_SET_TX_ANT_STATUS(buf, v) SET_BITS_TO_LE_1BYTE(buf+4, 4, 4, v) + + GENERAL_INFO_REG_SET_CMD_ID(h2c, CMD_ID_GENERAL_INFO_REG); + GENERAL_INFO_REG_SET_CLASS(h2c, CLASS_GENERAL_INFO_REG); + GENERAL_INFO_REG_SET_RFE_TYPE(h2c, info->rfe_type); + GENERAL_INFO_REG_SET_RF_TYPE(h2c, rftype); + GENERAL_INFO_REG_SET_CUT_VERSION(h2c, cut_phydm); + GENERAL_INFO_REG_SET_RX_ANT_STATUS(h2c, info->rx_ant_status); + GENERAL_INFO_REG_SET_TX_ANT_STATUS(h2c, info->tx_ant_status); + + return rtw_halmac_send_h2c(d, h2c); +} + +static int _send_general_info(struct dvobj_priv *d) +{ + struct _ADAPTER *adapter; + struct hal_com_data *hal; + struct halmac_adapter *halmac; + struct halmac_api *api; + struct halmac_general_info info; + enum halmac_ret_status status; + enum rf_type rf = RF_1T1R; + enum bb_path txpath = BB_PATH_A; + enum bb_path rxpath = BB_PATH_A; + int err; + + + adapter = dvobj_get_primary_adapter(d); + hal = GET_HAL_DATA(adapter); + halmac = dvobj_to_halmac(d); + if (!halmac) + return -1; + api = HALMAC_GET_API(halmac); + + _rtw_memset(&info, 0, sizeof(info)); + info.rfe_type = (u8)hal->rfe_type; + rtw_hal_get_trx_path(d, &rf, &txpath, &rxpath); + info.rf_type = _rf_type_drv2halmac(rf); + info.tx_ant_status = (u8)txpath; + info.rx_ant_status = (u8)rxpath; + info.ext_pa = 0; /* 2.4G or 5G? format not known */ + info.package_type = hal->PackageType; + info.mp_mode = adapter->registrypriv.mp_mode; + + status = api->halmac_send_general_info(halmac, &info); + switch (status) { + case HALMAC_RET_SUCCESS: + break; + case HALMAC_RET_NO_DLFW: + RTW_WARN("%s: halmac_send_general_info() fail because fw not dl!\n", + __FUNCTION__); + /* fall through */ + default: + return -1; + } + + err = _send_general_info_by_reg(d, &info); + if (err) { + RTW_ERR("%s: Fail to send general info by register!\n", + __FUNCTION__); + return -1; + } + + return 0; +} + +static int _cfg_drv_rsvd_pg_num(struct dvobj_priv *d) +{ + struct _ADAPTER *a; + struct hal_com_data *hal; + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_drv_rsvd_pg_num rsvd_page_number; + enum halmac_ret_status status; + u16 drv_rsvd_num; + int ret = 0; + + + a = dvobj_get_primary_adapter(d); + hal = GET_HAL_DATA(a); + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + drv_rsvd_num = rtw_hal_get_rsvd_page_num(a); + rsvd_page_number = _rsvd_page_num_drv2halmac(drv_rsvd_num); + status = api->halmac_cfg_drv_rsvd_pg_num(halmac, rsvd_page_number); + if (status != HALMAC_RET_SUCCESS) { + ret = -1; + goto exit; + } + hal->drv_rsvd_page_number = _rsvd_page_num_halmac2drv(rsvd_page_number); + +exit: +#ifndef DBG_RSVD_PAGE_CFG + if (drv_rsvd_num != _rsvd_page_num_halmac2drv(rsvd_page_number)) +#endif + RTW_INFO("%s: request %d pages => halmac %d pages %s\n" + , __FUNCTION__, drv_rsvd_num, _rsvd_page_num_halmac2drv(rsvd_page_number) + , ret ? "fail" : "success"); + + return ret; +} + +static void _debug_dlfw_fail(struct dvobj_priv *d) +{ + struct _ADAPTER *a; + u32 addr; + u32 v32, i, n; + + + a = dvobj_get_primary_adapter(d); + + /* read 0x80[15:0], 0x10F8[31:0] once */ + addr = 0x80; + v32 = rtw_read16(a, addr); + RTW_PRINT("%s: 0x%X = 0x%04x\n", __FUNCTION__, addr, v32); + + addr = 0x10F8; + v32 = rtw_read32(a, addr); + RTW_PRINT("%s: 0x%X = 0x%08x\n", __FUNCTION__, addr, v32); + + /* read 0x10FC[31:0], 5 times */ + addr = 0x10FC; + n = 5; + for (i = 0; i < n; i++) { + v32 = rtw_read32(a, addr); + RTW_PRINT("%s: 0x%X = 0x%08x (%u/%u)\n", + __FUNCTION__, addr, v32, i, n); + } + + /* + * write 0x3A[7:0]=0x28 and 0xF6[7:0]=0x01 + * and then read 0xC0[31:0] 5 times + */ + addr = 0x3A; + v32 = 0x28; + rtw_write8(a, addr, (u8)v32); + v32 = rtw_read8(a, addr); + RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v32); + + addr = 0xF6; + v32 = 0x1; + rtw_write8(a, addr, (u8)v32); + v32 = rtw_read8(a, addr); + RTW_PRINT("%s: 0x%X = 0x%02x\n", __FUNCTION__, addr, v32); + + addr = 0xC0; + n = 5; + for (i = 0; i < n; i++) { + v32 = rtw_read32(a, addr); + RTW_PRINT("%s: 0x%X = 0x%08x (%u/%u)\n", + __FUNCTION__, addr, v32, i, n); + } + + mac_reg_dump(NULL, a); +#ifdef CONFIG_SDIO_HCI + RTW_PRINT("======= SDIO Local REG =======\n"); + sdio_local_reg_dump(NULL, a); + RTW_PRINT("======= SDIO CCCR REG =======\n"); + sd_f0_reg_dump(NULL, a); +#endif /* CONFIG_SDIO_HCI */ + + /* read 0x80 after 10 secs */ + rtw_msleep_os(10000); + addr = 0x80; + v32 = rtw_read16(a, addr); + RTW_PRINT("%s: 0x%X = 0x%04x (after 10 secs)\n", + __FUNCTION__, addr, v32); +} + +static enum halmac_ret_status _enter_cpu_sleep_mode(struct dvobj_priv *d) +{ + struct hal_com_data *hal; + struct halmac_adapter *mac; + struct halmac_api *api; + + + hal = GET_HAL_DATA(dvobj_get_primary_adapter(d)); + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + +#ifdef CONFIG_RTL8822B + /* Support after firmware version 21 */ + if (hal->firmware_version < 21) + return HALMAC_RET_NOT_SUPPORT; +#elif defined(CONFIG_RTL8821C) + /* Support after firmware version 13.6 or 16 */ + if (hal->firmware_version == 13) { + if (hal->firmware_sub_version < 6) + return HALMAC_RET_NOT_SUPPORT; + } else if (hal->firmware_version < 16) { + return HALMAC_RET_NOT_SUPPORT; + } +#endif + + return api->halmac_enter_cpu_sleep_mode(mac); +} + +/* + * _cpu_sleep() - Let IC CPU enter sleep mode + * @d: struct dvobj_priv* + * @timeout: time limit of wait, unit is ms + * 0 for no limit + * + * Return 0 for CPU in sleep mode, otherwise fail to enter sleep mode. + * Error codes definition are as follow: + * -1 HALMAC enter sleep return fail + * -2 HALMAC get CPU mode return fail + * -110 timeout + */ +static int _cpu_sleep(struct dvobj_priv *d, u32 timeout) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + enum halmac_wlcpu_mode mode = HALMAC_WLCPU_UNDEFINE; + systime start_t; + s32 period = 0; + u32 cnt = 0; + int err = 0; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + start_t = rtw_get_current_time(); + + status = _enter_cpu_sleep_mode(d); + if (status != HALMAC_RET_SUCCESS) { + if (status != HALMAC_RET_NOT_SUPPORT) + err = -1; + goto exit; + } + + do { + cnt++; + + mode = HALMAC_WLCPU_UNDEFINE; + status = api->halmac_get_cpu_mode(mac, &mode); + + period = rtw_get_passing_time_ms(start_t); + + if (status != HALMAC_RET_SUCCESS) { + err = -2; + break; + } + if (mode == HALMAC_WLCPU_SLEEP) + break; + if (period > timeout) { + err = -110; + break; + } + + rtw_msleep_os(1); + } while (1); + +exit: + if (err) + RTW_ERR("%s: Fail to enter sleep mode! (%d, %d)\n", + __FUNCTION__, status, mode); + + RTW_INFO("%s: Cost %dms to polling %u times. (err=%d)\n", + __FUNCTION__, period, cnt, err); + + return err; +} + +static void _init_trx_cfg_drv(struct dvobj_priv *d) +{ +#ifdef CONFIG_PCI_HCI + rtw_hal_irp_reset(dvobj_get_primary_adapter(d)); +#endif +} + +/* + * Description: + * Downlaod Firmware Flow + * + * Parameters: + * d pointer of struct dvobj_priv + * fw firmware array + * fwsize firmware size + * re_dl re-download firmware or not + * 0: run in init hal flow, not re-download + * 1: it is a stand alone operation, not in init hal flow + * + * Return: + * 0 Success + * others Fail + */ +static int download_fw(struct dvobj_priv *d, u8 *fw, u32 fwsize, u8 re_dl) +{ + PHAL_DATA_TYPE hal; + struct halmac_adapter *mac; + struct halmac_api *api; + struct halmac_fw_version fw_vesion; + enum halmac_ret_status status; + int err = 0; + + + hal = GET_HAL_DATA(dvobj_get_primary_adapter(d)); + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + if ((!fw) || (!fwsize)) + return -1; + + /* 1. Driver Stop Tx */ + /* ToDo */ + + /* 2. Driver Check Tx FIFO is empty */ + err = rtw_halmac_txfifo_wait_empty(d, 2000); /* wait 2s */ + if (err) { + err = -1; + goto resume_tx; + } + + /* 3. Config MAX download size */ + /* + * Already done in rtw_halmac_init_adapter() or + * somewhere calling rtw_halmac_set_max_dl_fw_size(). + */ + + if (re_dl) { + /* 4. Enter IC CPU sleep mode */ + err = _cpu_sleep(d, 2000); + if (err) { + RTW_ERR("%s: IC CPU fail to enter sleep mode!(%d)\n", + __FUNCTION__, err); + /* skip this error */ + err = 0; + } + } + + /* 5. Download Firmware */ + status = api->halmac_download_firmware(mac, fw, fwsize); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: download firmware FAIL! status=0x%02x\n", + __FUNCTION__, status); + _debug_dlfw_fail(d); + err = -1; + goto resume_tx; + } + + /* 5.1. (Driver) Reset driver variables if needed */ + hal->LastHMEBoxNum = 0; + + /* 5.2. (Driver) Get FW version */ + status = api->halmac_get_fw_version(mac, &fw_vesion); + if (status == HALMAC_RET_SUCCESS) { + hal->firmware_version = fw_vesion.version; + hal->firmware_sub_version = fw_vesion.sub_version; + hal->firmware_size = fwsize; + } + +resume_tx: + /* 6. Driver resume TX if needed */ + /* ToDo */ + + if (err) + goto exit; + + if (re_dl) { + enum halmac_trx_mode mode; + + /* 7. Change reserved page size */ + err = _cfg_drv_rsvd_pg_num(d); + if (err) + return -1; + + /* 8. Init TRX Configuration */ + mode = _choose_trx_mode(d); + status = api->halmac_init_trx_cfg(mac, mode); + if (HALMAC_RET_SUCCESS != status) + return -1; + _init_trx_cfg_drv(d); + + /* 9. Config RX Aggregation */ + err = rtw_halmac_rx_agg_switch(d, _TRUE); + if (err) + return -1; + + /* 10. Send General Info */ + err = _send_general_info(d); + if (err) + return -1; + } + +exit: + return err; +} + +static int init_mac_flow(struct dvobj_priv *d) +{ + PADAPTER p; + struct hal_com_data *hal; + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_drv_rsvd_pg_num rsvd_page_number; + union halmac_wlan_addr hwa; + enum halmac_trx_mode trx_mode; + enum halmac_ret_status status; + u8 drv_rsvd_num; + u8 nettype; + int err, err_ret = -1; + + + p = dvobj_get_primary_adapter(d); + hal = GET_HAL_DATA(p); + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + +#ifdef CONFIG_SUPPORT_TRX_SHARED + status = api->halmac_cfg_rxff_expand_mode(halmac, + _rtw_get_trx_share_mode(p)); + if (status != HALMAC_RET_SUCCESS) + goto out; +#endif + +#ifdef DBG_LA_MODE + if (dvobj_to_regsty(d)->la_mode_en) { + status = api->halmac_cfg_la_mode(halmac, HALMAC_LA_MODE_PARTIAL); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: Fail to enable LA mode!\n", __FUNCTION__); + goto out; + } + RTW_PRINT("%s: Enable LA mode OK.\n", __FUNCTION__); + } +#endif + + err = _cfg_drv_rsvd_pg_num(d); + if (err) + goto out; + +#ifdef CONFIG_USB_HCI + status = api->halmac_set_bulkout_num(halmac, d->RtNumOutPipes); + if (status != HALMAC_RET_SUCCESS) + goto out; +#endif /* CONFIG_USB_HCI */ + + trx_mode = _choose_trx_mode(d); + status = api->halmac_init_mac_cfg(halmac, trx_mode); + if (status != HALMAC_RET_SUCCESS) + goto out; + + /* Driver insert flow: Sync driver setting with register */ + /* Sync driver RCR cache with register setting */ + rtw_hal_get_hwreg(dvobj_get_primary_adapter(d), HW_VAR_RCR, NULL); + +#ifdef CONFIG_RTS_FULL_BW + err = rtw_halmac_set_rts_full_bw(d, _TRUE); + if (err) + RTW_WARN("%s: Fail to set RTS FULL BW mode\n", __FUNCTION__); +#else + err = rtw_halmac_set_rts_full_bw(d, _FALSE); + if (err) + RTW_WARN("%s: Fail to disable RTS FULL BW mode\n", __FUNCTION__); +#endif /* CONFIG_RTS_FULL_BW */ + + _init_trx_cfg_drv(d); + /* Driver inser flow end */ + + err = rtw_halmac_rx_agg_switch(d, _TRUE); + if (err) + goto out; + + nettype = dvobj_to_regsty(d)->wireless_mode; + if (is_supported_vht(nettype) == _TRUE) + status = api->halmac_cfg_operation_mode(halmac, HALMAC_WIRELESS_MODE_AC); + else if (is_supported_ht(nettype) == _TRUE) + status = api->halmac_cfg_operation_mode(halmac, HALMAC_WIRELESS_MODE_N); + else if (IsSupportedTxOFDM(nettype) == _TRUE) + status = api->halmac_cfg_operation_mode(halmac, HALMAC_WIRELESS_MODE_G); + else + status = api->halmac_cfg_operation_mode(halmac, HALMAC_WIRELESS_MODE_B); + if (status != HALMAC_RET_SUCCESS) + goto out; + + err_ret = 0; +out: + return err_ret; +} + +static int _drv_enable_trx(struct dvobj_priv *d) +{ + struct _ADAPTER *adapter; + u32 status; + + + adapter = dvobj_get_primary_adapter(d); + if (adapter->bup == _FALSE) { +#ifdef CONFIG_NEW_NETDEV_HDL + status = rtw_mi_start_drv_threads(adapter); +#else + status = rtw_start_drv_threads(adapter); +#endif + if (status == _FAIL) { + RTW_ERR("%s: Start threads Failed!\n", __FUNCTION__); + return -1; + } + } + + rtw_intf_start(adapter); + + return 0; +} + +/* + * Notices: + * Make sure following information + * 1. GET_HAL_RFPATH + * 2. GET_HAL_DATA(dvobj_get_primary_adapter(d))->rfe_type + * 3. GET_HAL_DATA(dvobj_get_primary_adapter(d))->PackageType + * 4. dvobj_get_primary_adapter(d)->registrypriv.mp_mode + * are all ready before calling this function. + */ +static int _halmac_init_hal(struct dvobj_priv *d, u8 *fw, u32 fwsize) +{ + PADAPTER adapter; + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 ok; + u8 fw_ok = _FALSE; + int err, err_ret = -1; + + + adapter = dvobj_get_primary_adapter(d); + halmac = dvobj_to_halmac(d); + if (!halmac) + goto out; + api = HALMAC_GET_API(halmac); + + /* StatePowerOff */ + + /* SKIP: halmac_init_adapter (Already done before) */ + + /* halmac_pre_Init_system_cfg */ + /* halmac_mac_power_switch(on) */ + /* halmac_Init_system_cfg */ + ok = rtw_hal_power_on(adapter); + if (_FAIL == ok) + goto out; + + /* StatePowerOn */ + + /* DownloadFW */ + if (fw && fwsize) { + err = download_fw(d, fw, fwsize, 0); + if (err) + goto out; + fw_ok = _TRUE; + } + + /* InitMACFlow */ + err = init_mac_flow(d); + if (err) + goto out; + + /* Driver insert flow: Enable TR/RX */ + err = _drv_enable_trx(d); + if (err) + goto out; + + /* halmac_send_general_info */ + if (_TRUE == fw_ok) { + err = _send_general_info(d); + if (err) + goto out; + } + + /* Init Phy parameter-MAC */ + ok = rtw_hal_init_mac_register(adapter); + if (_FALSE == ok) + goto out; + + /* StateMacInitialized */ + + /* halmac_cfg_drv_info */ + err = rtw_halmac_config_rx_info(d, HALMAC_DRV_INFO_PHY_STATUS); + if (err) + goto out; + + /* halmac_set_hw_value(HALMAC_HW_EN_BB_RF) */ + /* Init BB, RF */ + ok = rtw_hal_init_phy(adapter); + if (_FALSE == ok) + goto out; + + status = api->halmac_init_interface_cfg(halmac); + if (status != HALMAC_RET_SUCCESS) + goto out; + + /* SKIP: halmac_verify_platform_api */ + /* SKIP: halmac_h2c_lb */ + + /* StateRxIdle */ + + err_ret = 0; +out: + return err_ret; +} + +int rtw_halmac_init_hal(struct dvobj_priv *d) +{ + return _halmac_init_hal(d, NULL, 0); +} + +/* + * Notices: + * Make sure following information + * 1. GET_HAL_RFPATH + * 2. GET_HAL_DATA(dvobj_get_primary_adapter(d))->rfe_type + * 3. GET_HAL_DATA(dvobj_get_primary_adapter(d))->PackageType + * 4. dvobj_get_primary_adapter(d)->registrypriv.mp_mode + * are all ready before calling this function. + */ +int rtw_halmac_init_hal_fw(struct dvobj_priv *d, u8 *fw, u32 fwsize) +{ + return _halmac_init_hal(d, fw, fwsize); +} + +/* + * Notices: + * Make sure following information + * 1. GET_HAL_RFPATH + * 2. GET_HAL_DATA(dvobj_get_primary_adapter(d))->rfe_type + * 3. GET_HAL_DATA(dvobj_get_primary_adapter(d))->PackageType + * 4. dvobj_get_primary_adapter(d)->registrypriv.mp_mode + * are all ready before calling this function. + */ +int rtw_halmac_init_hal_fw_file(struct dvobj_priv *d, u8 *fwpath) +{ + u8 *fw = NULL; + u32 fwmaxsize = 0, size = 0; + int err = 0; + + + err = rtw_halmac_get_fw_max_size(d, &fwmaxsize); + if (err) { + RTW_ERR("%s: Fail to get Firmware MAX size(err=%d)\n", __FUNCTION__, err); + return -1; + } + + fw = rtw_zmalloc(fwmaxsize); + if (!fw) + return -1; + + size = rtw_retrieve_from_file(fwpath, fw, fwmaxsize); + if (!size) { + err = -1; + goto exit; + } + + err = _halmac_init_hal(d, fw, size); + +exit: + rtw_mfree(fw, fwmaxsize); + /*fw = NULL;*/ + + return err; +} + +int rtw_halmac_deinit_hal(struct dvobj_priv *d) +{ + PADAPTER adapter; + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + int err = -1; + + + adapter = dvobj_get_primary_adapter(d); + halmac = dvobj_to_halmac(d); + if (!halmac) + goto out; + api = HALMAC_GET_API(halmac); + + status = api->halmac_deinit_interface_cfg(halmac); + if (status != HALMAC_RET_SUCCESS) + goto out; + + rtw_hal_power_off(adapter); + + err = 0; +out: + return err; +} + +int rtw_halmac_self_verify(struct dvobj_priv *d) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + int err = -1; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_verify_platform_api(mac); + if (status != HALMAC_RET_SUCCESS) + goto out; + + status = api->halmac_h2c_lb(mac); + if (status != HALMAC_RET_SUCCESS) + goto out; + + err = 0; +out: + return err; +} + +static u8 rtw_halmac_txfifo_is_empty(struct dvobj_priv *d) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 chk_num = 10; + u8 rst = _FALSE; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_txfifo_is_empty(mac, chk_num); + if (status == HALMAC_RET_SUCCESS) + rst = _TRUE; + + return rst; +} + +/** + * rtw_halmac_txfifo_wait_empty() - Wait TX FIFO to be emtpy + * @d: struct dvobj_priv* + * @timeout: time limit of wait, unit is ms + * 0 for no limit + * + * Wait TX FIFO to be emtpy. + * + * Return 0 for TX FIFO is empty, otherwise not empty. + */ +int rtw_halmac_txfifo_wait_empty(struct dvobj_priv *d, u32 timeout) +{ + struct _ADAPTER *a; + u8 empty = _FALSE; + u32 cnt = 0; + systime start_time = 0; + u32 pass_time; /* ms */ + + + a = dvobj_get_primary_adapter(d); + start_time = rtw_get_current_time(); + + do { + cnt++; + empty = rtw_halmac_txfifo_is_empty(d); + if (empty == _TRUE) + break; + + if (timeout) { + pass_time = rtw_get_passing_time_ms(start_time); + if (pass_time > timeout) + break; + } + if (RTW_CANNOT_IO(a)) { + RTW_WARN("%s: Interrupted by I/O forbiden!\n", __FUNCTION__); + break; + } + + rtw_msleep_os(2); + } while (1); + + if (empty == _FALSE) { +#ifdef CONFIG_RTW_DEBUG + u16 dbg_reg[] = {0x210, 0x230, 0x234, 0x238, 0x23C, 0x240, + 0x418, 0x10FC, 0x10F8, 0x11F4, 0x11F8}; + u8 i; + u32 val; + + if (!RTW_CANNOT_IO(a)) { + for (i = 0; i < ARRAY_SIZE(dbg_reg); i++) { + val = rtw_read32(a, dbg_reg[i]); + RTW_ERR("REG_%X:0x%08x\n", dbg_reg[i], val); + } + } +#endif /* CONFIG_RTW_DEBUG */ + + RTW_ERR("%s: Fail to wait txfifo empty!(cnt=%d)\n", + __FUNCTION__, cnt); + return -1; + } + + return 0; +} + +static enum halmac_dlfw_mem _fw_mem_drv2halmac(enum fw_mem mem, u8 tx_stop) +{ + enum halmac_dlfw_mem mem_halmac = HALMAC_DLFW_MEM_UNDEFINE; + + + switch (mem) { + case FW_EMEM: + if (tx_stop == _FALSE) + mem_halmac = HALMAC_DLFW_MEM_EMEM_RSVD_PG; + else + mem_halmac = HALMAC_DLFW_MEM_EMEM; + break; + + case FW_IMEM: + case FW_DMEM: + mem_halmac = HALMAC_DLFW_MEM_UNDEFINE; + break; + } + + return mem_halmac; +} + +int rtw_halmac_dlfw_mem(struct dvobj_priv *d, u8 *fw, u32 fwsize, enum fw_mem mem) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + enum halmac_dlfw_mem dlfw_mem; + u8 tx_stop = _FALSE; + u32 chk_timeout = 2000; /* unit: ms */ + int err = 0; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + if ((!fw) || (!fwsize)) + return -1; + +#ifndef RTW_HALMAC_DLFW_MEM_NO_STOP_TX + /* 1. Driver Stop Tx */ + /* ToDo */ + + /* 2. Driver Check Tx FIFO is empty */ + err = rtw_halmac_txfifo_wait_empty(d, chk_timeout); + if (err) + tx_stop = _FALSE; + else + tx_stop = _TRUE; +#endif /* !RTW_HALMAC_DLFW_MEM_NO_STOP_TX */ + + /* 3. Download Firmware MEM */ + dlfw_mem = _fw_mem_drv2halmac(mem, tx_stop); + if (dlfw_mem == HALMAC_DLFW_MEM_UNDEFINE) { + err = -1; + goto resume_tx; + } + status = api->halmac_free_download_firmware(mac, dlfw_mem, fw, fwsize); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: halmac_free_download_firmware fail(err=0x%x)\n", + __FUNCTION__, status); + err = -1; + goto resume_tx; + } + +resume_tx: +#ifndef RTW_HALMAC_DLFW_MEM_NO_STOP_TX + /* 4. Driver resume TX if needed */ + /* ToDo */ +#endif /* !RTW_HALMAC_DLFW_MEM_NO_STOP_TX */ + + return err; +} + +int rtw_halmac_dlfw_mem_from_file(struct dvobj_priv *d, u8 *fwpath, enum fw_mem mem) +{ + u8 *fw = NULL; + u32 fwmaxsize = 0, size = 0; + int err = 0; + + + err = rtw_halmac_get_fw_max_size(d, &fwmaxsize); + if (err) { + RTW_ERR("%s: Fail to get Firmware MAX size(err=%d)\n", __FUNCTION__, err); + return -1; + } + + fw = rtw_zmalloc(fwmaxsize); + if (!fw) + return -1; + + size = rtw_retrieve_from_file(fwpath, fw, fwmaxsize); + if (size) + err = rtw_halmac_dlfw_mem(d, fw, size, mem); + else + err = -1; + + rtw_mfree(fw, fwmaxsize); + /*fw = NULL;*/ + + return err; +} + +/* + * Return: + * 0 Success + * -22 Invalid arguemnt + */ +int rtw_halmac_dlfw(struct dvobj_priv *d, u8 *fw, u32 fwsize) +{ + PADAPTER adapter; + enum halmac_ret_status status; + u32 ok; + int err, err_ret = -1; + + + if (!fw || !fwsize) + return -22; + + adapter = dvobj_get_primary_adapter(d); + + /* re-download firmware */ + if (rtw_is_hw_init_completed(adapter)) + return download_fw(d, fw, fwsize, 1); + + /* Download firmware before hal init */ + /* Power on, download firmware and init mac */ + ok = rtw_hal_power_on(adapter); + if (_FAIL == ok) + goto out; + + err = download_fw(d, fw, fwsize, 0); + if (err) { + err_ret = err; + goto out; + } + + err = init_mac_flow(d); + if (err) + goto out; + + err = _send_general_info(d); + if (err) + goto out; + + err_ret = 0; + +out: + return err_ret; +} + +int rtw_halmac_dlfw_from_file(struct dvobj_priv *d, u8 *fwpath) +{ + u8 *fw = NULL; + u32 fwmaxsize = 0, size = 0; + int err = 0; + + + err = rtw_halmac_get_fw_max_size(d, &fwmaxsize); + if (err) { + RTW_ERR("%s: Fail to get Firmware MAX size(err=%d)\n", __FUNCTION__, err); + return -1; + } + + fw = rtw_zmalloc(fwmaxsize); + if (!fw) + return -1; + + size = rtw_retrieve_from_file(fwpath, fw, fwmaxsize); + if (size) + err = rtw_halmac_dlfw(d, fw, size); + else + err = -1; + + rtw_mfree(fw, fwmaxsize); + /*fw = NULL;*/ + + return err; +} + +/* + * Description: + * Power on/off BB/RF domain. + * + * Parameters: + * enable _TRUE/_FALSE for power on/off + * + * Return: + * 0 Success + * others Fail + */ +int rtw_halmac_phy_power_switch(struct dvobj_priv *d, u8 enable) +{ + PADAPTER adapter; + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + u8 on; + + + adapter = dvobj_get_primary_adapter(d); + halmac = dvobj_to_halmac(d); + if (!halmac) + return -1; + api = HALMAC_GET_API(halmac); + on = (enable == _TRUE) ? 1 : 0; + + status = api->halmac_set_hw_value(halmac, HALMAC_HW_EN_BB_RF, &on); + if (status != HALMAC_RET_SUCCESS) + return -1; + + return 0; +} + +static u8 _is_fw_read_cmd_down(PADAPTER adapter, u8 msgbox_num) +{ + u8 read_down = _FALSE; + int retry_cnts = 100; + u8 valid; + + do { + valid = rtw_read8(adapter, REG_HMETFR) & BIT(msgbox_num); + if (0 == valid) + read_down = _TRUE; + else + rtw_msleep_os(1); + } while ((!read_down) && (retry_cnts--)); + + if (_FALSE == read_down) + RTW_WARN("%s, reg_1cc(%x), msg_box(%d)...\n", __func__, rtw_read8(adapter, REG_HMETFR), msgbox_num); + + return read_down; +} + +/** + * rtw_halmac_send_h2c() - Send H2C to firmware + * @d: struct dvobj_priv* + * @h2c: H2C data buffer, suppose to be 8 bytes + * + * Send H2C to firmware by message box register(0x1D0~0x1D3 & 0x1F0~0x1F3). + * + * Assume firmware be ready to accept H2C here, please check + * (hal->bFWReady == _TRUE) before call this function or make sure firmware is + * ready. + * + * Return: 0 if process OK, otherwise fail to send this H2C. + */ +int rtw_halmac_send_h2c(struct dvobj_priv *d, u8 *h2c) +{ + PADAPTER adapter = dvobj_get_primary_adapter(d); + PHAL_DATA_TYPE hal = GET_HAL_DATA(adapter); + u8 h2c_box_num = 0; + u32 msgbox_addr = 0; + u32 msgbox_ex_addr = 0; + u32 h2c_cmd = 0; + u32 h2c_cmd_ex = 0; + int err = -1; + + + if (!h2c) { + RTW_WARN("%s: pbuf is NULL\n", __FUNCTION__); + return err; + } + + if (rtw_is_surprise_removed(adapter)) { + RTW_WARN("%s: surprise removed\n", __FUNCTION__); + return err; + } + + _enter_critical_mutex(&d->h2c_fwcmd_mutex, NULL); + + /* pay attention to if race condition happened in H2C cmd setting */ + h2c_box_num = hal->LastHMEBoxNum; + + if (!_is_fw_read_cmd_down(adapter, h2c_box_num)) { + RTW_WARN(" fw read cmd failed...\n"); +#ifdef DBG_CONFIG_ERROR_DETECT + hal->srestpriv.self_dect_fw = _TRUE; + hal->srestpriv.self_dect_fw_cnt++; +#endif /* DBG_CONFIG_ERROR_DETECT */ + goto exit; + } + + /* Write Ext command (byte 4~7) */ + msgbox_ex_addr = REG_HMEBOX_E0 + (h2c_box_num * EX_MESSAGE_BOX_SIZE); + _rtw_memcpy((u8 *)(&h2c_cmd_ex), h2c + 4, EX_MESSAGE_BOX_SIZE); + h2c_cmd_ex = le32_to_cpu(h2c_cmd_ex); + rtw_write32(adapter, msgbox_ex_addr, h2c_cmd_ex); + + /* Write command (byte 0~3) */ + msgbox_addr = REG_HMEBOX0 + (h2c_box_num * MESSAGE_BOX_SIZE); + _rtw_memcpy((u8 *)(&h2c_cmd), h2c, 4); + h2c_cmd = le32_to_cpu(h2c_cmd); + rtw_write32(adapter, msgbox_addr, h2c_cmd); + + /* update last msg box number */ + hal->LastHMEBoxNum = (h2c_box_num + 1) % MAX_H2C_BOX_NUMS; + err = 0; + +#ifdef DBG_H2C_CONTENT + RTW_INFO_DUMP("[H2C] - ", h2c, RTW_HALMAC_H2C_MAX_SIZE); +#endif +exit: + _exit_critical_mutex(&d->h2c_fwcmd_mutex, NULL); + return err; +} + +/** + * rtw_halmac_c2h_handle() - Handle C2H for HALMAC + * @d: struct dvobj_priv* + * @c2h: Full C2H packet, including RX description and payload + * @size: Size(byte) of c2h + * + * Send C2H packet to HALMAC to process C2H packets, and the expected C2H ID is + * 0xFF. This function won't have any I/O, so caller doesn't have to call it in + * I/O safe place(ex. command thread). + * + * Please sure doesn't call this function in the same thread as someone is + * waiting HALMAC C2H ack, otherwise there is a deadlock happen. + * + * Return: 0 if process OK, otherwise no action for this C2H. + */ +int rtw_halmac_c2h_handle(struct dvobj_priv *d, u8 *c2h, u32 size) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; +#ifdef RTW_HALMAC_FILTER_DRV_C2H + u32 desc_size = 0; + u8 *c2h_data; + u8 sub; +#endif /* RTW_HALMAC_FILTER_DRV_C2H */ + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + +#ifdef RTW_HALMAC_FILTER_DRV_C2H + status = api->halmac_get_hw_value(mac, HALMAC_HW_RX_DESC_SIZE, + &desc_size); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: fail to get rx desc size!\n", __FUNCTION__); + goto skip_filter; + } + + c2h_data = c2h + desc_size; + sub = C2H_HDR_GET_C2H_SUB_CMD_ID(c2h_data); + switch (sub) { + case C2H_SUB_CMD_ID_C2H_PKT_FTM_DBG: + case C2H_SUB_CMD_ID_C2H_PKT_FTM_2_DBG: + case C2H_SUB_CMD_ID_C2H_PKT_FTM_3_DBG: + case C2H_SUB_CMD_ID_C2H_PKT_FTM_4_DBG: + case C2H_SUB_CMD_ID_FTMACKRPT_HDL_DBG: + case C2H_SUB_CMD_ID_FTMC2H_RPT: + case C2H_SUB_CMD_ID_DRVFTMC2H_RPT: + case C2H_SUB_CMD_ID_C2H_PKT_FTM_5_DBG: + case C2H_SUB_CMD_ID_CCX_RPT: + case C2H_SUB_CMD_ID_C2H_PKT_NAN_RPT: + case C2H_SUB_CMD_ID_C2H_PKT_ATM_RPT: + case C2H_SUB_CMD_ID_C2H_PKT_SCC_CSA_RPT: + case C2H_SUB_CMD_ID_C2H_PKT_FW_STATUS_NOTIFY: + case C2H_SUB_CMD_ID_C2H_PKT_FTMSESSION_END: + case C2H_SUB_CMD_ID_C2H_PKT_DETECT_THERMAL: + case C2H_SUB_CMD_ID_FW_FWCTRL_RPT: + case C2H_SUB_CMD_ID_SCAN_CH_NOTIFY: + case C2H_SUB_CMD_ID_FW_TBTT_RPT: + case C2H_SUB_CMD_ID_BCN_OFFLOAD: + case C2H_SUB_CMD_ID_FW_DBG_MSG: + RTW_PRINT("%s: unhandled C2H, id=0xFF subid=0x%x len=%u\n", + __FUNCTION__, sub, C2H_HDR_GET_LEN(c2h_data)); + RTW_PRINT_DUMP("C2H: ", c2h_data, size - desc_size); + return 0; + } + +skip_filter: +#endif /* RTW_HALMAC_FILTER_DRV_C2H */ + + status = api->halmac_get_c2h_info(mac, c2h, size); + if (HALMAC_RET_SUCCESS != status) + return -1; + + return 0; +} + +int rtw_halmac_get_available_efuse_size(struct dvobj_priv *d, u32 *size) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 val; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_get_efuse_available_size(mac, &val); + if (HALMAC_RET_SUCCESS != status) + return -1; + + *size = val; + return 0; +} + +int rtw_halmac_get_physical_efuse_size(struct dvobj_priv *d, u32 *size) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 val; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_get_efuse_size(mac, &val); + if (HALMAC_RET_SUCCESS != status) + return -1; + + *size = val; + return 0; +} + +int rtw_halmac_read_physical_efuse_map(struct dvobj_priv *d, u8 *map, u32 size) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + enum halmac_feature_id id; + int ret; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + id = HALMAC_FEATURE_DUMP_PHYSICAL_EFUSE; + + ret = init_halmac_event(d, id, map, size); + if (ret) + return -1; + + status = api->halmac_dump_efuse_map(mac, HALMAC_EFUSE_R_DRV); + if (HALMAC_RET_SUCCESS != status) { + free_halmac_event(d, id); + return -1; + } + + ret = wait_halmac_event(d, id); + if (ret) + return -1; + + return 0; +} + +int rtw_halmac_read_physical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + u8 v; + u32 i; + u8 *efuse = NULL; + u32 size = 0; + int err = 0; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + if (api->halmac_read_efuse) { + for (i = 0; i < cnt; i++) { + status = api->halmac_read_efuse(mac, offset + i, &v); + if (HALMAC_RET_SUCCESS != status) + return -1; + data[i] = v; + } + } else { + err = rtw_halmac_get_physical_efuse_size(d, &size); + if (err) + return -1; + + efuse = rtw_zmalloc(size); + if (!efuse) + return -1; + + err = rtw_halmac_read_physical_efuse_map(d, efuse, size); + if (err) + err = -1; + else + _rtw_memcpy(data, efuse + offset, cnt); + + rtw_mfree(efuse, size); + } + + return err; +} + +int rtw_halmac_write_physical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 i; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + if (api->halmac_write_efuse == NULL) + return -1; + + for (i = 0; i < cnt; i++) { + status = api->halmac_write_efuse(mac, offset + i, data[i]); + if (HALMAC_RET_SUCCESS != status) + return -1; + } + + return 0; +} + +int rtw_halmac_get_logical_efuse_size(struct dvobj_priv *d, u32 *size) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 val; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_get_logical_efuse_size(mac, &val); + if (HALMAC_RET_SUCCESS != status) + return -1; + + *size = val; + return 0; +} + +int rtw_halmac_read_logical_efuse_map(struct dvobj_priv *d, u8 *map, u32 size, u8 *maskmap, u32 masksize) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + enum halmac_feature_id id; + int ret; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + id = HALMAC_FEATURE_DUMP_LOGICAL_EFUSE; + + ret = init_halmac_event(d, id, map, size); + if (ret) + return -1; + + status = api->halmac_dump_logical_efuse_map(mac, HALMAC_EFUSE_R_DRV); + if (HALMAC_RET_SUCCESS != status) { + free_halmac_event(d, id); + return -1; + } + + ret = wait_halmac_event(d, id); + if (ret) + return -1; + + if (maskmap && masksize) { + struct halmac_pg_efuse_info pginfo; + + pginfo.efuse_map = map; + pginfo.efuse_map_size = size; + pginfo.efuse_mask = maskmap; + pginfo.efuse_mask_size = masksize; + + status = api->halmac_mask_logical_efuse(mac, &pginfo); + if (status != HALMAC_RET_SUCCESS) + RTW_WARN("%s: mask logical efuse FAIL!\n", __FUNCTION__); + } + + return 0; +} + +int rtw_halmac_write_logical_efuse_map(struct dvobj_priv *d, u8 *map, u32 size, u8 *maskmap, u32 masksize) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + struct halmac_pg_efuse_info pginfo; + enum halmac_ret_status status; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + pginfo.efuse_map = map; + pginfo.efuse_map_size = size; + pginfo.efuse_mask = maskmap; + pginfo.efuse_mask_size = masksize; + + status = api->halmac_pg_efuse_by_map(mac, &pginfo, HALMAC_EFUSE_R_AUTO); + if (HALMAC_RET_SUCCESS != status) + return -1; + + return 0; +} + +int rtw_halmac_read_logical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + u8 v; + u32 i; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + for (i = 0; i < cnt; i++) { + status = api->halmac_read_logical_efuse(mac, offset + i, &v); + if (HALMAC_RET_SUCCESS != status) + return -1; + data[i] = v; + } + + return 0; +} + +int rtw_halmac_write_logical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 i; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + for (i = 0; i < cnt; i++) { + status = api->halmac_write_logical_efuse(mac, offset + i, data[i]); + if (HALMAC_RET_SUCCESS != status) + return -1; + } + + return 0; +} + +int rtw_halmac_write_bt_physical_efuse(struct dvobj_priv *d, u32 offset, u32 cnt, u8 *data) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 i; + u8 bank = 1; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + for (i = 0; i < cnt; i++) { + status = api->halmac_write_efuse_bt(mac, offset + i, data[i], bank); + if (HALMAC_RET_SUCCESS != status) { + printk("%s: halmac_write_efuse_bt status = %d\n", __FUNCTION__, status); + return -1; + } + } + printk("%s: halmac_write_efuse_bt status = HALMAC_RET_SUCCESS %d\n", __FUNCTION__, status); + return 0; +} + + +int rtw_halmac_read_bt_physical_efuse_map(struct dvobj_priv *d, u8 *map, u32 size) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + int bank = 1; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_dump_efuse_map_bt(mac, bank, size, map); + if (HALMAC_RET_SUCCESS != status) { + printk("%s: halmac_dump_efuse_map_bt fail!\n", __FUNCTION__); + return -1; + } + + printk("%s: OK!\n", __FUNCTION__); + + return 0; +} + +static enum hal_fifo_sel _fifo_sel_drv2halmac(u8 fifo_sel) +{ + switch (fifo_sel) { + case 0: + return HAL_FIFO_SEL_TX; + case 1: + return HAL_FIFO_SEL_RX; + case 2: + return HAL_FIFO_SEL_RSVD_PAGE; + case 3: + return HAL_FIFO_SEL_REPORT; + case 4: + return HAL_FIFO_SEL_LLT; + case 5: + return HAL_FIFO_SEL_RXBUF_FW; + } + + return HAL_FIFO_SEL_RSVD_PAGE; +} + +/*#define CONFIG_HALMAC_FIFO_DUMP*/ +int rtw_halmac_dump_fifo(struct dvobj_priv *d, u8 fifo_sel, u32 addr, u32 size, u8 *buffer) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum hal_fifo_sel halmac_fifo_sel; + enum halmac_ret_status status; + u8 *pfifo_map = NULL; + u32 fifo_size = 0; + s8 ret = 0;/* 0:success, -1:error */ + u8 mem_created = _FALSE; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + if ((size != 0) && (buffer == NULL)) + return -1; + + halmac_fifo_sel = _fifo_sel_drv2halmac(fifo_sel); + + if ((size) && (buffer)) { + pfifo_map = buffer; + fifo_size = size; + } else { + fifo_size = api->halmac_get_fifo_size(mac, halmac_fifo_sel); + + if (fifo_size) + pfifo_map = rtw_zvmalloc(fifo_size); + if (pfifo_map == NULL) + return -1; + mem_created = _TRUE; + } + + status = api->halmac_dump_fifo(mac, halmac_fifo_sel, addr, fifo_size, pfifo_map); + if (HALMAC_RET_SUCCESS != status) { + ret = -1; + goto _exit; + } + +#ifdef CONFIG_HALMAC_FIFO_DUMP + { + static const char * const fifo_sel_str[] = { + "TX", "RX", "RSVD_PAGE", "REPORT", "LLT", "RXBUF_FW" + }; + + RTW_INFO("%s FIFO DUMP [start_addr:0x%04x , size:%d]\n", fifo_sel_str[halmac_fifo_sel], addr, fifo_size); + RTW_INFO_DUMP("\n", pfifo_map, fifo_size); + RTW_INFO(" ==================================================\n"); + } +#endif /* CONFIG_HALMAC_FIFO_DUMP */ + +_exit: + if ((mem_created == _TRUE) && pfifo_map) + rtw_vmfree(pfifo_map, fifo_size); + + return ret; +} + +/* + * rtw_halmac_rx_agg_switch() - Switch RX aggregation function and setting + * @d struct dvobj_priv * + * @enable _FALSE/_TRUE for disable/enable RX aggregation function + * + * This function could help to on/off bus RX aggregation function, and is only + * useful for SDIO and USB interface. Although only "enable" flag is brough in, + * some setting would be taken from other places, and they are from: + * [DMA aggregation] + * struct hal_com_data.rxagg_dma_size + * struct hal_com_data.rxagg_dma_timeout + * [USB aggregation] (only use for USB interface) + * struct hal_com_data.rxagg_usb_size + * struct hal_com_data.rxagg_usb_timeout + * If above values of size and timeout are both 0 means driver would not + * control the threshold setting and leave it to HALMAC handle. + * + * From HALMAC V1_04_04, driver force the size threshold be hard limit, and the + * rx size can not exceed the setting. + * + * Return 0 for success, otherwise fail. + */ +int rtw_halmac_rx_agg_switch(struct dvobj_priv *d, u8 enable) +{ + struct _ADAPTER *adapter; + struct hal_com_data *hal; + struct halmac_adapter *halmac; + struct halmac_api *api; + struct halmac_rxagg_cfg rxaggcfg; + enum halmac_ret_status status; + + + adapter = dvobj_get_primary_adapter(d); + hal = GET_HAL_DATA(adapter); + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + _rtw_memset((void *)&rxaggcfg, 0, sizeof(rxaggcfg)); + rxaggcfg.mode = HALMAC_RX_AGG_MODE_NONE; + /* + * Always enable size limit to avoid rx size exceed + * driver defined size. + */ + rxaggcfg.threshold.size_limit_en = 1; + +#ifdef RTW_RX_AGGREGATION + if (_TRUE == enable) { +#ifdef CONFIG_SDIO_HCI + rxaggcfg.mode = HALMAC_RX_AGG_MODE_DMA; + rxaggcfg.threshold.drv_define = 0; + if (hal->rxagg_dma_size || hal->rxagg_dma_timeout) { + rxaggcfg.threshold.drv_define = 1; + rxaggcfg.threshold.timeout = hal->rxagg_dma_timeout; + rxaggcfg.threshold.size = hal->rxagg_dma_size; + RTW_INFO("%s: RX aggregation threshold: " + "timeout=%u size=%u\n", + __FUNCTION__, + hal->rxagg_dma_timeout, + hal->rxagg_dma_size); + } +#elif defined(CONFIG_USB_HCI) + switch (hal->rxagg_mode) { + case RX_AGG_DISABLE: + rxaggcfg.mode = HALMAC_RX_AGG_MODE_NONE; + break; + + case RX_AGG_DMA: + rxaggcfg.mode = HALMAC_RX_AGG_MODE_DMA; + if (hal->rxagg_dma_size || hal->rxagg_dma_timeout) { + rxaggcfg.threshold.drv_define = 1; + rxaggcfg.threshold.timeout = hal->rxagg_dma_timeout; + rxaggcfg.threshold.size = hal->rxagg_dma_size; + } + break; + + case RX_AGG_USB: + case RX_AGG_MIX: + rxaggcfg.mode = HALMAC_RX_AGG_MODE_USB; + if (hal->rxagg_usb_size || hal->rxagg_usb_timeout) { + rxaggcfg.threshold.drv_define = 1; + rxaggcfg.threshold.timeout = hal->rxagg_usb_timeout; + rxaggcfg.threshold.size = hal->rxagg_usb_size; + } + break; + } +#endif /* CONFIG_USB_HCI */ + } +#endif /* RTW_RX_AGGREGATION */ + + status = api->halmac_cfg_rx_aggregation(halmac, &rxaggcfg); + if (status != HALMAC_RET_SUCCESS) + return -1; + + return 0; +} + +int rtw_halmac_download_rsvd_page(struct dvobj_priv *dvobj, u8 pg_offset, u8 *pbuf, u32 size) +{ + enum halmac_ret_status status = HALMAC_RET_SUCCESS; + struct halmac_adapter *halmac = dvobj_to_halmac(dvobj); + struct halmac_api *api = HALMAC_GET_API(halmac); + + status = api->halmac_dl_drv_rsvd_page(halmac, pg_offset, pbuf, size); + if (status != HALMAC_RET_SUCCESS) + return -1; + + return 0; +} + +/* + * Description + * Fill following spec info from HALMAC API: + * sec_cam_ent_num + * + * Return + * 0 Success + * others Fail + */ +int rtw_halmac_fill_hal_spec(struct dvobj_priv *dvobj, struct hal_spec_t *spec) +{ + enum halmac_ret_status status; + struct halmac_adapter *halmac; + struct halmac_api *api; + u8 cam = 0; /* Security Cam Entry Number */ + + + halmac = dvobj_to_halmac(dvobj); + api = HALMAC_GET_API(halmac); + + /* Prepare data from HALMAC */ + status = api->halmac_get_hw_value(halmac, HALMAC_HW_CAM_ENTRY_NUM, &cam); + if (status != HALMAC_RET_SUCCESS) + return -1; + + /* Fill data to hal_spec_t */ + spec->sec_cam_ent_num = cam; + + return 0; +} + +int rtw_halmac_p2pps(struct dvobj_priv *dvobj, struct hal_p2p_ps_para *pp2p_ps_para) +{ + enum halmac_ret_status status = HALMAC_RET_SUCCESS; + struct halmac_adapter *halmac = dvobj_to_halmac(dvobj); + struct halmac_api *api = HALMAC_GET_API(halmac); + struct halmac_p2pps halmac_p2p_ps; + + (&halmac_p2p_ps)->offload_en = pp2p_ps_para->offload_en; + (&halmac_p2p_ps)->role = pp2p_ps_para->role; + (&halmac_p2p_ps)->ctwindow_en = pp2p_ps_para->ctwindow_en; + (&halmac_p2p_ps)->noa_en = pp2p_ps_para->noa_en; + (&halmac_p2p_ps)->noa_sel = pp2p_ps_para->noa_sel; + (&halmac_p2p_ps)->all_sta_sleep = pp2p_ps_para->all_sta_sleep; + (&halmac_p2p_ps)->discovery = pp2p_ps_para->discovery; + (&halmac_p2p_ps)->disable_close_rf = pp2p_ps_para->disable_close_rf; + (&halmac_p2p_ps)->p2p_port_id = _hw_port_drv2halmac(pp2p_ps_para->p2p_port_id); + (&halmac_p2p_ps)->p2p_group = pp2p_ps_para->p2p_group; + (&halmac_p2p_ps)->p2p_macid = pp2p_ps_para->p2p_macid; + (&halmac_p2p_ps)->ctwindow_length = pp2p_ps_para->ctwindow_length; + (&halmac_p2p_ps)->noa_duration_para = pp2p_ps_para->noa_duration_para; + (&halmac_p2p_ps)->noa_interval_para = pp2p_ps_para->noa_interval_para; + (&halmac_p2p_ps)->noa_start_time_para = pp2p_ps_para->noa_start_time_para; + (&halmac_p2p_ps)->noa_count_para = pp2p_ps_para->noa_count_para; + + status = api->halmac_p2pps(halmac, (&halmac_p2p_ps)); + if (status != HALMAC_RET_SUCCESS) + return -1; + + return 0; + +} + +/** + * rtw_halmac_iqk() - Run IQ Calibration + * @d: struct dvobj_priv* + * @clear: IQK parameters + * @segment: IQK parameters + * + * Process IQ Calibration(IQK). + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_iqk(struct dvobj_priv *d, u8 clear, u8 segment) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + enum halmac_feature_id id; + struct halmac_iqk_para para; + int ret; + u8 retry = 3; + u8 delay = 1; /* ms */ + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + id = HALMAC_FEATURE_IQK; + + ret = init_halmac_event(d, id, NULL, 0); + if (ret) + return -1; + + para.clear = clear; + para.segment_iqk = segment; + + do { + status = api->halmac_start_iqk(mac, ¶); + if (status != HALMAC_RET_BUSY_STATE) + break; + RTW_WARN("%s: Fail to start IQK, status is BUSY! retry=%d\n", __FUNCTION__, retry); + if (!retry) + break; + retry--; + rtw_msleep_os(delay); + } while (1); + if (status != HALMAC_RET_SUCCESS) { + free_halmac_event(d, id); + return -1; + } + + ret = wait_halmac_event(d, id); + if (ret) + return -1; + + return 0; +} + +/** + * rtw_halmac_dpk() - Run DP Calibration + * @d: struct dvobj_priv* + * @buf: buffer for store return value + * @bufsz: size of buffer + * + * Process DP Calibration(DPK). + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_dpk(struct dvobj_priv *d, u8 *buf, u32 bufsz) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + enum halmac_feature_id id; + int ret; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + id = HALMAC_FEATURE_DPK; + + ret = init_halmac_event(d, id, buf, bufsz); + if (ret) + return -1; + + status = api->halmac_start_dpk(mac); + if (status != HALMAC_RET_SUCCESS) { + free_halmac_event(d, id); + RTW_ERR("%s: Fail to start DPK (0x%x)!\n", + __FUNCTION__, status); + return -1; + } + + ret = wait_halmac_event(d, id); + if (ret) + return -1; + + return 0; +} + +static inline u32 _phy_parameter_val_drv2halmac(u32 val, u8 msk_en, u32 msk) +{ + if (!msk_en) + return val; + + return (val << bitshift(msk)); +} + +static int _phy_parameter_drv2halmac(struct rtw_phy_parameter *para, struct halmac_phy_parameter_info *info) +{ + if (!para || !info) + return -1; + + _rtw_memset(info, 0, sizeof(*info)); + + switch (para->cmd) { + case 0: + /* MAC register */ + switch (para->data.mac.size) { + case 1: + info->cmd_id = HALMAC_PARAMETER_CMD_MAC_W8; + break; + case 2: + info->cmd_id = HALMAC_PARAMETER_CMD_MAC_W16; + break; + default: + info->cmd_id = HALMAC_PARAMETER_CMD_MAC_W32; + break; + } + info->content.MAC_REG_W.value = _phy_parameter_val_drv2halmac( + para->data.mac.value, + para->data.mac.msk_en, + para->data.mac.msk); + info->content.MAC_REG_W.msk = para->data.mac.msk; + info->content.MAC_REG_W.offset = para->data.mac.offset; + info->content.MAC_REG_W.msk_en = para->data.mac.msk_en; + break; + + case 1: + /* BB register */ + switch (para->data.bb.size) { + case 1: + info->cmd_id = HALMAC_PARAMETER_CMD_BB_W8; + break; + case 2: + info->cmd_id = HALMAC_PARAMETER_CMD_BB_W16; + break; + default: + info->cmd_id = HALMAC_PARAMETER_CMD_BB_W32; + break; + } + info->content.BB_REG_W.value = _phy_parameter_val_drv2halmac( + para->data.bb.value, + para->data.bb.msk_en, + para->data.bb.msk); + info->content.BB_REG_W.msk = para->data.bb.msk; + info->content.BB_REG_W.offset = para->data.bb.offset; + info->content.BB_REG_W.msk_en = para->data.bb.msk_en; + break; + + case 2: + /* RF register */ + info->cmd_id = HALMAC_PARAMETER_CMD_RF_W; + info->content.RF_REG_W.value = _phy_parameter_val_drv2halmac( + para->data.rf.value, + para->data.rf.msk_en, + para->data.rf.msk); + info->content.RF_REG_W.msk = para->data.rf.msk; + info->content.RF_REG_W.offset = para->data.rf.offset; + info->content.RF_REG_W.msk_en = para->data.rf.msk_en; + info->content.RF_REG_W.rf_path = para->data.rf.path; + break; + + case 3: + /* Delay register */ + if (para->data.delay.unit == 0) + info->cmd_id = HALMAC_PARAMETER_CMD_DELAY_US; + else + info->cmd_id = HALMAC_PARAMETER_CMD_DELAY_MS; + info->content.DELAY_TIME.delay_time = para->data.delay.value; + break; + + case 0xFF: + /* Latest(End) command */ + info->cmd_id = HALMAC_PARAMETER_CMD_END; + break; + + default: + return -1; + } + + return 0; +} + +/** + * rtw_halmac_cfg_phy_para() - Register(Phy parameter) configuration + * @d: struct dvobj_priv* + * @para: phy parameter + * + * Configure registers by firmware using H2C/C2H mechanism. + * The latest command should be para->cmd==0xFF(End command) to finish all + * processes. + * + * Return: 0 for OK, otherwise fail. + */ +int rtw_halmac_cfg_phy_para(struct dvobj_priv *d, struct rtw_phy_parameter *para) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + enum halmac_feature_id id; + struct halmac_phy_parameter_info info; + u8 full_fifo; + int err, ret; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + id = HALMAC_FEATURE_CFG_PARA; + full_fifo = 1; /* ToDo: How to deciede? */ + ret = 0; + + err = _phy_parameter_drv2halmac(para, &info); + if (err) + return -1; + + err = init_halmac_event(d, id, NULL, 0); + if (err) + return -1; + + status = api->halmac_cfg_parameter(mac, &info, full_fifo); + if (info.cmd_id == HALMAC_PARAMETER_CMD_END) { + if (status == HALMAC_RET_SUCCESS) { + err = wait_halmac_event(d, id); + if (err) + ret = -1; + } else { + free_halmac_event(d, id); + ret = -1; + RTW_ERR("%s: Fail to send END of cfg parameter, status is 0x%x!\n", __FUNCTION__, status); + } + } else { + if (status == HALMAC_RET_PARA_SENDING) { + err = wait_halmac_event(d, id); + if (err) + ret = -1; + } else { + free_halmac_event(d, id); + if (status != HALMAC_RET_SUCCESS) { + ret = -1; + RTW_ERR("%s: Fail to cfg parameter, status is 0x%x!\n", __FUNCTION__, status); + } + } + } + + return ret; +} + +static enum halmac_wlled_mode _led_mode_drv2halmac(u8 drv_mode) +{ + enum halmac_wlled_mode halmac_mode; + + + switch (drv_mode) { + case 1: + halmac_mode = HALMAC_WLLED_MODE_TX; + break; + case 2: + halmac_mode = HALMAC_WLLED_MODE_RX; + break; + case 3: + halmac_mode = HALMAC_WLLED_MODE_SW_CTRL; + break; + case 0: + default: + halmac_mode = HALMAC_WLLED_MODE_TRX; + break; + } + + return halmac_mode; +} + +/** + * rtw_halmac_led_cfg() - Configure Hardware LED Mode + * @d: struct dvobj_priv* + * @enable: enable or disable LED function + * 0: disable + * 1: enable + * @mode: WLan LED mode (valid when enable==1) + * 0: Blink when TX(transmit packet) and RX(receive packet) + * 1: Blink when TX only + * 2: Blink when RX only + * 3: Software control + * + * Configure hardware WLan LED mode. + * If want to change LED mode after enabled, need to disable LED first and + * enable again to set new mode. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_led_cfg(struct dvobj_priv *d, u8 enable, u8 mode) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_wlled_mode led_mode; + enum halmac_ret_status status; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + if (enable) { + status = api->halmac_pinmux_set_func(halmac, + HALMAC_GPIO_FUNC_WL_LED); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: pinmux set fail!(0x%x)\n", + __FUNCTION__, status); + return -1; + } + + led_mode = _led_mode_drv2halmac(mode); + status = api->halmac_pinmux_wl_led_mode(halmac, led_mode); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: mode set fail!(0x%x)\n", + __FUNCTION__, status); + return -1; + } + } else { + /* Change LED to software control and turn off */ + api->halmac_pinmux_wl_led_mode(halmac, + HALMAC_WLLED_MODE_SW_CTRL); + api->halmac_pinmux_wl_led_sw_ctrl(halmac, 0); + + status = api->halmac_pinmux_free_func(halmac, + HALMAC_GPIO_FUNC_WL_LED); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: pinmux free fail!(0x%x)\n", + __FUNCTION__, status); + return -1; + } + } + + return 0; +} + +/** + * rtw_halmac_led_switch() - Turn Hardware LED on/off + * @d: struct dvobj_priv* + * @on: LED light or not + * 0: Off + * 1: On(Light) + * + * Turn Hardware WLan LED On/Off. + * Before use this function, user should call rtw_halmac_led_ctrl() to switch + * mode to "software control(3)" first, otherwise control would fail. + * The interval between on and off must be longer than 1 ms, or the LED would + * keep light or dark only. + * Ex. Turn off LED at first, turn on after 0.5ms and turn off again after + * 0.5ms. The LED during this flow will only keep dark, and miss the turn on + * operation between two turn off operations. + */ +void rtw_halmac_led_switch(struct dvobj_priv *d, u8 on) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + api->halmac_pinmux_wl_led_sw_ctrl(halmac, on); +} + +static int _gpio_cfg(struct dvobj_priv *d, enum halmac_gpio_func gpio, u8 enable) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + if (enable) { + status = api->halmac_pinmux_set_func(halmac, gpio); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: pinmux set GPIO(%d) fail!(0x%x)\n", + __FUNCTION__, gpio, status); + return -1; + } + } else { + status = api->halmac_pinmux_free_func(halmac, gpio); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: pinmux free GPIO(%d) fail!(0x%x)\n", + __FUNCTION__, gpio, status); + return -1; + } + } + + return 0; +} + +/** + * rtw_halmac_bt_wake_cfg() - Configure BT wake host function + * @d: struct dvobj_priv* + * @enable: enable or disable BT wake host function + * 0: disable + * 1: enable + * + * Configure pinmux to allow BT to control BT wake host pin. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_bt_wake_cfg(struct dvobj_priv *d, u8 enable) +{ + return _gpio_cfg(d, HALMAC_GPIO_FUNC_BT_HOST_WAKE1, enable); +} + +static enum halmac_gpio_func _gpio_to_func_for_rfe_ctrl(u8 gpio) +{ + enum halmac_gpio_func f = HALMAC_GPIO_FUNC_UNDEFINE; + + +#ifdef CONFIG_RTL8822C + switch (gpio) { + case 1: + f = HALMAC_GPIO_FUNC_ANTSWB; + break; + case 2: + f = HALMAC_GPIO_FUNC_S1_TRSW; + break; + case 3: + f = HALMAC_GPIO_FUNC_S0_TRSW; + break; + case 6: + f = HALMAC_GPIO_FUNC_S0_PAPE; + break; + case 7: + f = HALMAC_GPIO_FUNC_S0_TRSWB; + break; + case 13: + f = HALMAC_GPIO_FUNC_ANTSW; + break; + } +#endif /* CONFIG_RTL8822C */ + + return f; +} + +/** + * rtw_halmac_rfe_ctrl_cfg() - Configure RFE control GPIO + * @d: struct dvobj_priv* + * @gpio: gpio number + * + * Configure pinmux to enable RFE control GPIO. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_rfe_ctrl_cfg(struct dvobj_priv *d, u8 gpio) +{ + enum halmac_gpio_func f; + + + f = _gpio_to_func_for_rfe_ctrl(gpio); + if (f == HALMAC_GPIO_FUNC_UNDEFINE) + return -1; + return _gpio_cfg(d, f, 1); +} + +#ifdef CONFIG_PNO_SUPPORT +/** + * _halmac_scanoffload() - Switch channel by firmware during scanning + * @d: struct dvobj_priv* + * @enable: 1: enable, 0: disable + * @nlo: 1: nlo mode (no c2h event), 0: normal mode + * @ssid: ssid of probe request + * @ssid_len: ssid length + * + * Switch Channel and Send Porbe Request Offloaded by FW + * + * Return 0 for OK, otherwise fail. + */ +static int _halmac_scanoffload(struct dvobj_priv *d, u32 enable, u8 nlo, + u8 *ssid, u8 ssid_len) +{ + struct _ADAPTER *adapter; + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + struct halmac_ch_info ch_info; + struct halmac_ch_switch_option cs_option; + struct mlme_ext_priv *pmlmeext; + enum halmac_feature_id id_update, id_ch_sw; + struct halmac_indicator *indicator, *tbl; + + int err = 0; + u8 probereq[64]; + u32 len = 0; + int i = 0; + struct pno_ssid pnossid; + struct rf_ctl_t *rfctl = NULL; + struct _RT_CHANNEL_INFO *ch_set; + + + tbl = d->hmpriv.indicator; + adapter = dvobj_get_primary_adapter(d); + mac = dvobj_to_halmac(d); + if (!mac) + return -1; + api = HALMAC_GET_API(mac); + id_update = HALMAC_FEATURE_UPDATE_PACKET; + id_ch_sw = HALMAC_FEATURE_CHANNEL_SWITCH; + pmlmeext = &(adapter->mlmeextpriv); + rfctl = adapter_to_rfctl(adapter); + ch_set = rfctl->channel_set; + + RTW_INFO("%s: %s scanoffload, mode: %s\n", + __FUNCTION__, enable?"Enable":"Disable", + nlo?"PNO/NLO":"Normal"); + + if (enable) { + _rtw_memset(probereq, 0, sizeof(probereq)); + + _rtw_memset(&pnossid, 0, sizeof(pnossid)); + if (ssid) { + if (ssid_len > sizeof(pnossid.SSID)) { + RTW_ERR("%s: SSID length(%d) is too long(>%d)!!\n", + __FUNCTION__, ssid_len, sizeof(pnossid.SSID)); + return -1; + } + + pnossid.SSID_len = ssid_len; + _rtw_memcpy(pnossid.SSID, ssid, ssid_len); + } + + rtw_hal_construct_ProbeReq(adapter, probereq, &len, &pnossid); + + if (!nlo) { + err = init_halmac_event(d, id_update, NULL, 0); + if (err) + return -1; + } + + status = api->halmac_update_packet(mac, HALMAC_PACKET_PROBE_REQ, + probereq, len); + if (status != HALMAC_RET_SUCCESS) { + if (!nlo) + free_halmac_event(d, id_update); + RTW_ERR("%s: halmac_update_packet FAIL(%d)!!\n", + __FUNCTION__, status); + return -1; + } + + if (!nlo) { + err = wait_halmac_event(d, id_update); + if (err) + RTW_ERR("%s: wait update packet FAIL(%d)!!\n", + __FUNCTION__, err); + } + + api->halmac_clear_ch_info(mac); + + for (i = 0; i < rfctl->max_chan_nums && ch_set[i].ChannelNum != 0; i++) { + _rtw_memset(&ch_info, 0, sizeof(ch_info)); + ch_info.extra_info = 0; + ch_info.channel = ch_set[i].ChannelNum; + ch_info.bw = HALMAC_BW_20; + ch_info.pri_ch_idx = HALMAC_CH_IDX_1; + ch_info.action_id = HALMAC_CS_ACTIVE_SCAN; + ch_info.timeout = 1; + status = api->halmac_add_ch_info(mac, &ch_info); + if (status != HALMAC_RET_SUCCESS) { + RTW_ERR("%s: add_ch_info FAIL(%d)!!\n", + __FUNCTION__, status); + return -1; + } + } + + /* set channel switch option */ + _rtw_memset(&cs_option, 0, sizeof(cs_option)); + cs_option.dest_bw = HALMAC_BW_20; + cs_option.periodic_option = HALMAC_CS_PERIODIC_2_PHASE; + cs_option.dest_pri_ch_idx = HALMAC_CH_IDX_UNDEFINE; + cs_option.tsf_low = 0; + cs_option.switch_en = 1; + cs_option.dest_ch_en = 1; + cs_option.absolute_time_en = 0; + cs_option.dest_ch = 1; + + cs_option.normal_period = 5; + cs_option.normal_period_sel = 0; + cs_option.normal_cycle = 10; + + cs_option.phase_2_period = 1; + cs_option.phase_2_period_sel = 1; + + /* nlo is for wow fw, 1: no c2h response */ + cs_option.nlo_en = nlo; + + if (!nlo) { + err = init_halmac_event(d, id_ch_sw, NULL, 0); + if (err) + return -1; + } + + status = api->halmac_ctrl_ch_switch(mac, &cs_option); + if (status != HALMAC_RET_SUCCESS) { + if (!nlo) + free_halmac_event(d, id_ch_sw); + RTW_ERR("%s: halmac_ctrl_ch_switch FAIL(%d)!!\n", + __FUNCTION__, status); + return -1; + } + + if (!nlo) { + err = wait_halmac_event(d, id_ch_sw); + if (err) + RTW_ERR("%s: wait ctrl_ch_switch FAIL(%d)!!\n", + __FUNCTION__, err); + } + } else { + api->halmac_clear_ch_info(mac); + + _rtw_memset(&cs_option, 0, sizeof(cs_option)); + cs_option.switch_en = 0; + + if (!nlo) { + err = init_halmac_event(d, id_ch_sw, NULL, 0); + if (err) + return -1; + } + + status = api->halmac_ctrl_ch_switch(mac, &cs_option); + if (status != HALMAC_RET_SUCCESS) { + if (!nlo) + free_halmac_event(d, id_ch_sw); + RTW_ERR("%s: halmac_ctrl_ch_switch FAIL(%d)!!\n", + __FUNCTION__, status); + return -1; + } + + if (!nlo) { + err = wait_halmac_event(d, id_ch_sw); + if (err) + RTW_ERR("%s: wait ctrl_ch_switch FAIL(%d)!!\n", + __FUNCTION__, err); + } + } + + return 0; +} + +/** + * rtw_halmac_pno_scanoffload() - Control firmware scan AP function for PNO + * @d: struct dvobj_priv* + * @enable: 1: enable, 0: disable + * + * Switch firmware scan AP function for PNO(prefer network offload) or + * NLO(network list offload). + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_pno_scanoffload(struct dvobj_priv *d, u32 enable) +{ + return _halmac_scanoffload(d, enable, 1, NULL, 0); +} +#endif /* CONFIG_PNO_SUPPORT */ + +#ifdef CONFIG_SDIO_HCI + +/** + * rtw_halmac_preinit_sdio_io_indirect() - Enable indirect I/O or not + * @d: struct dvobj_priv* + * @enable: true: enable, false: disable + * + * Enable register access using direct I/O or indirect. This function should be + * called before rtw_halmac_init_adapter(), and the life cycle is the same as + * driver until removing driver. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_preinit_sdio_io_indirect(struct dvobj_priv *d, bool enable) +{ + struct halmac_adapter *halmac; + struct halmacpriv *priv; + + + halmac = dvobj_to_halmac(d); + if (halmac) { + RTW_WARN("%s: illegal operation! " + "preinit function only could be called before init!\n", + __FUNCTION__); + return -1; + } + + priv = &d->hmpriv; + priv->sdio_io_indir = (enable ? 1 : 2); + + return 0; +} + +/* + * Description: + * Update queue allocated page number to driver + * + * Parameter: + * d pointer to struct dvobj_priv of driver + * + * Return: + * 0 Success, "page" is valid. + * others Fail, "page" is invalid. + */ +int rtw_halmac_query_tx_page_num(struct dvobj_priv *d) +{ + PADAPTER adapter; + struct halmacpriv *hmpriv; + struct halmac_adapter *halmac; + struct halmac_api *api; + struct halmac_rqpn_map rqpn; + enum halmac_dma_mapping dmaqueue; + struct halmac_txff_allocation fifosize; + enum halmac_ret_status status; + u8 i; + + + adapter = dvobj_get_primary_adapter(d); + hmpriv = &d->hmpriv; + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + _rtw_memset((void *)&rqpn, 0, sizeof(rqpn)); + _rtw_memset((void *)&fifosize, 0, sizeof(fifosize)); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_RQPN_MAPPING, &rqpn); + if (status != HALMAC_RET_SUCCESS) + return -1; + status = api->halmac_get_hw_value(halmac, HALMAC_HW_TXFF_ALLOCATION, &fifosize); + if (status != HALMAC_RET_SUCCESS) + return -1; + + for (i = 0; i < HW_QUEUE_ENTRY; i++) { + hmpriv->txpage[i] = 0; + + /* Driver index mapping to HALMAC DMA queue */ + dmaqueue = HALMAC_DMA_MAPPING_UNDEFINE; + switch (i) { + case VO_QUEUE_INX: + dmaqueue = rqpn.dma_map_vo; + break; + case VI_QUEUE_INX: + dmaqueue = rqpn.dma_map_vi; + break; + case BE_QUEUE_INX: + dmaqueue = rqpn.dma_map_be; + break; + case BK_QUEUE_INX: + dmaqueue = rqpn.dma_map_bk; + break; + case MGT_QUEUE_INX: + dmaqueue = rqpn.dma_map_mg; + break; + case HIGH_QUEUE_INX: + dmaqueue = rqpn.dma_map_hi; + break; + case BCN_QUEUE_INX: + case TXCMD_QUEUE_INX: + /* Unlimited */ + hmpriv->txpage[i] = 0xFFFF; + continue; + } + + switch (dmaqueue) { + case HALMAC_DMA_MAPPING_EXTRA: + hmpriv->txpage[i] = fifosize.extra_queue_pg_num; + break; + case HALMAC_DMA_MAPPING_LOW: + hmpriv->txpage[i] = fifosize.low_queue_pg_num; + break; + case HALMAC_DMA_MAPPING_NORMAL: + hmpriv->txpage[i] = fifosize.normal_queue_pg_num; + break; + case HALMAC_DMA_MAPPING_HIGH: + hmpriv->txpage[i] = fifosize.high_queue_pg_num; + break; + case HALMAC_DMA_MAPPING_UNDEFINE: + break; + } + hmpriv->txpage[i] += fifosize.pub_queue_pg_num; + } + + return 0; +} + +/* + * Description: + * Get specific queue allocated page number + * + * Parameter: + * d pointer to struct dvobj_priv of driver + * queue target queue to query, VO/VI/BE/BK/.../TXCMD_QUEUE_INX + * page return allocated page number + * + * Return: + * 0 Success, "page" is valid. + * others Fail, "page" is invalid. + */ +int rtw_halmac_get_tx_queue_page_num(struct dvobj_priv *d, u8 queue, u32 *page) +{ + *page = 0; + if (queue < HW_QUEUE_ENTRY) + *page = d->hmpriv.txpage[queue]; + + return 0; +} + +/* + * Return: + * address for SDIO command + */ +u32 rtw_halmac_sdio_get_tx_addr(struct dvobj_priv *d, u8 *desc, u32 size) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + u32 addr; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_get_sdio_tx_addr(mac, desc, size, &addr); + if (HALMAC_RET_SUCCESS != status) + return 0; + + return addr; +} + +int rtw_halmac_sdio_tx_allowed(struct dvobj_priv *d, u8 *buf, u32 size) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_tx_allowed_sdio(mac, buf, size); + if (HALMAC_RET_SUCCESS != status) + return -1; + + return 0; +} + +u32 rtw_halmac_sdio_get_rx_addr(struct dvobj_priv *d, u8 *seq) +{ + u8 id; + +#define RTW_SDIO_ADDR_RX_RX0FF_PRFIX 0x0E000 +#define RTW_SDIO_ADDR_RX_RX0FF_GEN(a) (RTW_SDIO_ADDR_RX_RX0FF_PRFIX|(a&0x3)) + + id = *seq; + (*seq)++; + return RTW_SDIO_ADDR_RX_RX0FF_GEN(id); +} + +int rtw_halmac_sdio_set_tx_format(struct dvobj_priv *d, enum halmac_sdio_tx_format format) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_set_hw_value(mac, HALMAC_HW_SDIO_TX_FORMAT, &format); + if (HALMAC_RET_SUCCESS != status) + return -1; + + return 0; +} +#endif /* CONFIG_SDIO_HCI */ + +#ifdef CONFIG_USB_HCI +u8 rtw_halmac_usb_get_bulkout_id(struct dvobj_priv *d, u8 *buf, u32 size) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + u8 bulkout_id; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_get_usb_bulkout_id(mac, buf, size, &bulkout_id); + if (HALMAC_RET_SUCCESS != status) + return 0; + + return bulkout_id; +} + +/** + * rtw_halmac_usb_get_txagg_desc_num() - MAX descriptor number in one bulk for TX + * @d: struct dvobj_priv* + * @size: TX FIFO size, unit is byte. + * + * Get MAX descriptor number in one bulk out from HALMAC. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_usb_get_txagg_desc_num(struct dvobj_priv *d, u8 *num) +{ + struct halmac_adapter *halmac; + struct halmac_api *api; + enum halmac_ret_status status; + u8 val = 0; + + + halmac = dvobj_to_halmac(d); + api = HALMAC_GET_API(halmac); + + status = api->halmac_get_hw_value(halmac, HALMAC_HW_USB_TXAGG_DESC_NUM, &val); + if (status != HALMAC_RET_SUCCESS) + return -1; + + *num = val; + + return 0; +} + +static inline enum halmac_usb_mode _usb_mode_drv2halmac(enum RTW_USB_SPEED usb_mode) +{ + enum halmac_usb_mode halmac_usb_mode = HALMAC_USB_MODE_U2; + + switch (usb_mode) { + case RTW_USB_SPEED_2: + halmac_usb_mode = HALMAC_USB_MODE_U2; + break; + case RTW_USB_SPEED_3: + halmac_usb_mode = HALMAC_USB_MODE_U3; + break; + default: + halmac_usb_mode = HALMAC_USB_MODE_U2; + break; + } + + return halmac_usb_mode; +} + +u8 rtw_halmac_switch_usb_mode(struct dvobj_priv *d, enum RTW_USB_SPEED usb_mode) +{ + PADAPTER adapter; + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + enum halmac_usb_mode halmac_usb_mode; + + adapter = dvobj_get_primary_adapter(d); + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + halmac_usb_mode = _usb_mode_drv2halmac(usb_mode); + status = api->halmac_set_hw_value(mac, HALMAC_HW_USB_MODE, (void *)&halmac_usb_mode); + + if (HALMAC_RET_SUCCESS != status) + return _FAIL; + + return _SUCCESS; +} +#endif /* CONFIG_USB_HCI */ + +#ifdef CONFIG_BEAMFORMING +#ifdef RTW_BEAMFORMING_VERSION_2 +int rtw_halmac_bf_add_mu_bfer(struct dvobj_priv *d, u16 paid, u16 csi_para, + u16 my_aid, enum halmac_csi_seg_len sel, u8 *addr) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + struct halmac_mu_bfer_init_para param; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + _rtw_memset(¶m, 0, sizeof(param)); + param.paid = paid; + param.csi_para = csi_para; + param.my_aid = my_aid; + param.csi_length_sel = sel; + _rtw_memcpy(param.bfer_address.addr, addr, 6); + + status = api->halmac_mu_bfer_entry_init(mac, ¶m); + if (status != HALMAC_RET_SUCCESS) + return -1; + + return 0; +} + +int rtw_halmac_bf_del_mu_bfer(struct dvobj_priv *d) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_mu_bfer_entry_del(mac); + if (status != HALMAC_RET_SUCCESS) + return -1; + + return 0; +} + + +int rtw_halmac_bf_cfg_sounding(struct dvobj_priv *d, + enum halmac_snd_role role, enum halmac_data_rate rate) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_cfg_sounding(mac, role, rate); + if (status != HALMAC_RET_SUCCESS) + return -1; + + return 0; +} + +int rtw_halmac_bf_del_sounding(struct dvobj_priv *d, + enum halmac_snd_role role) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_del_sounding(mac, role); + if (status != HALMAC_RET_SUCCESS) + return -1; + + return 0; +} + +/** + * rtw_halmac_bf_cfg_csi_rate() - Config data rate for CSI report frame by RSSI + * @d: struct dvobj_priv* + * @rssi: RSSI vlaue, unit is percentage (0~100). + * @current_rate: Current CSI frame rate + * Valid value example + * 0 CCK 1M + * 3 CCK 11M + * 4 OFDM 6M + * and so on + * @fixrate_en: Enable to fix CSI frame in VHT rate, otherwise legacy OFDM rate. + * The value "0" for disable, otheriwse enable. + * @new_rate: Return new data rate, and value range is the same as + * current_rate + * @bmp_ofdm54: Return to suggest enabling OFDM 54M for CSI report frame or not, + * The valid values and meanings are: + * 0x00 disable + * 0x01 enable + * 0xFF Keep current setting + * + * According RSSI to config data rate for CSI report frame of Beamforming. + * + * Return 0 for OK, otherwise fail. + */ +int rtw_halmac_bf_cfg_csi_rate(struct dvobj_priv *d, u8 rssi, + u8 current_rate, u8 fixrate_en, u8 *new_rate, + u8 *bmp_ofdm54) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + status = api->halmac_cfg_csi_rate(mac, + rssi, current_rate, fixrate_en, new_rate, + bmp_ofdm54); + if (status != HALMAC_RET_SUCCESS) + return -1; + + return 0; +} + +int rtw_halmac_bf_cfg_mu_mimo(struct dvobj_priv *d, enum halmac_snd_role role, + u8 *sounding_sts, u16 grouping_bitmap, u8 mu_tx_en, + u32 *given_gid_tab, u32 *given_user_pos) +{ + struct halmac_adapter *mac; + struct halmac_api *api; + enum halmac_ret_status status; + struct halmac_cfg_mumimo_para param; + + + mac = dvobj_to_halmac(d); + api = HALMAC_GET_API(mac); + + _rtw_memset(¶m, 0, sizeof(param)); + + param.role = role; + param.grouping_bitmap = grouping_bitmap; + param.mu_tx_en = mu_tx_en; + + if (sounding_sts) + _rtw_memcpy(param.sounding_sts, sounding_sts, 6); + + if (given_gid_tab) + _rtw_memcpy(param.given_gid_tab, given_gid_tab, 8); + + if (given_user_pos) + _rtw_memcpy(param.given_user_pos, given_user_pos, 16); + + status = api->halmac_cfg_mumimo(mac, ¶m); + if (status != HALMAC_RET_SUCCESS) + return -1; + + return 0; +} + +#endif /* RTW_BEAMFORMING_VERSION_2 */ +#endif /* CONFIG_BEAMFORMING */ diff --git a/hal/hal_halmac.h b/hal/hal_halmac.h new file mode 100644 index 0000000..7388226 --- /dev/null +++ b/hal/hal_halmac.h @@ -0,0 +1,252 @@ +/****************************************************************************** + * + * Copyright(c) 2015 - 2019 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#ifndef _HAL_HALMAC_H_ +#define _HAL_HALMAC_H_ + +#include /* adapter_to_dvobj(), struct intf_hdl and etc. */ +#include /* struct hal_spec_t */ +#include "halmac/halmac_api.h" /* struct halmac_adapter* and etc. */ + +/* HALMAC Definition for Driver */ +#define RTW_HALMAC_H2C_MAX_SIZE 8 +#define RTW_HALMAC_BA_SSN_RPT_SIZE 4 + +#define dvobj_set_halmac(d, mac) ((d)->halmac = (mac)) +#define dvobj_to_halmac(d) ((struct halmac_adapter *)((d)->halmac)) +#define adapter_to_halmac(p) dvobj_to_halmac(adapter_to_dvobj(p)) + +/* for H2C cmd */ +#define MAX_H2C_BOX_NUMS 4 +#define MESSAGE_BOX_SIZE 4 +#define EX_MESSAGE_BOX_SIZE 4 + +typedef enum _RTW_HALMAC_MODE { + RTW_HALMAC_MODE_NORMAL, + RTW_HALMAC_MODE_WIFI_TEST, +} RTW_HALMAC_MODE; + +union rtw_phy_para_data { + struct _mac { + u32 value; /* value to be set in bit mask(msk) */ + u32 msk; /* bit mask */ + u16 offset; /* address */ + u8 msk_en; /* 0/1 for msk invalid/valid */ + u8 size; /* Unit is bytes, and value should be 1/2/4 */ + } mac; + struct _bb { + u32 value; + u32 msk; + u16 offset; + u8 msk_en; + u8 size; + } bb; + struct _rf { + u32 value; + u32 msk; + u8 offset; + u8 msk_en; + /* + * 0: path A + * 1: path B + * 2: path C + * 3: path D + */ + u8 path; + } rf; + struct _delay { + /* + * 0: microsecond (us) + * 1: millisecond (ms) + */ + u8 unit; + u16 value; + } delay; +}; + +struct rtw_phy_parameter { + /* + * 0: MAC register + * 1: BB register + * 2: RF register + * 3: Delay + * 0xFF: Latest(End) command + */ + u8 cmd; + union rtw_phy_para_data data; +}; + +struct rtw_halmac_bcn_ctrl { + u8 rx_bssid_fit:1; /* 0:HW handle beacon, 1:ignore */ + u8 txbcn_rpt:1; /* Enable TXBCN report in ad hoc and AP mode */ + u8 tsf_update:1; /* Update TSF when beacon or probe response */ + u8 enable_bcn:1; /* Enable beacon related functions */ + u8 rxbcn_rpt:1; /* Enable RXBCNOK report */ + u8 p2p_ctwin:1; /* Enable P2P CTN WINDOWS function */ + u8 p2p_bcn_area:1; /* Enable P2P BCN area on function */ +}; + +extern struct halmac_platform_api rtw_halmac_platform_api; + +/* HALMAC API for Driver(HAL) */ +u8 rtw_halmac_read8(struct intf_hdl *, u32 addr); +u16 rtw_halmac_read16(struct intf_hdl *, u32 addr); +u32 rtw_halmac_read32(struct intf_hdl *, u32 addr); +void rtw_halmac_read_mem(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem); +#ifdef CONFIG_SDIO_INDIRECT_ACCESS +u8 rtw_halmac_iread8(struct intf_hdl *pintfhdl, u32 addr); +u16 rtw_halmac_iread16(struct intf_hdl *pintfhdl, u32 addr); +u32 rtw_halmac_iread32(struct intf_hdl *pintfhdl, u32 addr); +#endif /* CONFIG_SDIO_INDIRECT_ACCESS */ +int rtw_halmac_write8(struct intf_hdl *, u32 addr, u8 value); +int rtw_halmac_write16(struct intf_hdl *, u32 addr, u16 value); +int rtw_halmac_write32(struct intf_hdl *, u32 addr, u32 value); + +/* Software Information */ +void rtw_halmac_get_version(char *str, u32 len); + +/* Software setting before Initialization */ +int rtw_halmac_preinit_sdio_io_indirect(struct dvobj_priv *d, bool enable); + +/* Software Initialization */ +int rtw_halmac_init_adapter(struct dvobj_priv *d, struct halmac_platform_api *pf_api); +int rtw_halmac_deinit_adapter(struct dvobj_priv *); + +/* Get operations */ +int rtw_halmac_get_hw_value(struct dvobj_priv *d, enum halmac_hw_id hw_id, void *pvalue); +int rtw_halmac_get_tx_fifo_size(struct dvobj_priv *d, u32 *size); +int rtw_halmac_get_rx_fifo_size(struct dvobj_priv *d, u32 *size); +int rtw_halmac_get_rsvd_drv_pg_bndy(struct dvobj_priv *d, u16 *bndy); +int rtw_halmac_get_page_size(struct dvobj_priv *d, u32 *size); +int rtw_halmac_get_tx_agg_align_size(struct dvobj_priv *d, u16 *size); +int rtw_halmac_get_rx_agg_align_size(struct dvobj_priv *d, u8 *size); +int rtw_halmac_get_rx_drv_info_sz(struct dvobj_priv *, u8 *sz); +int rtw_halmac_get_tx_desc_size(struct dvobj_priv *d, u32 *size); +int rtw_halmac_get_rx_desc_size(struct dvobj_priv *d, u32 *size); +int rtw_halmac_get_tx_dma_ch_map(struct dvobj_priv *d, u8 *dma_ch_map, u8 map_size); +int rtw_halmac_get_ori_h2c_size(struct dvobj_priv *d, u32 *size); +int rtw_halmac_get_oqt_size(struct dvobj_priv *d, u8 *size); +int rtw_halmac_get_ac_queue_number(struct dvobj_priv *d, u8 *num); +int rtw_halmac_get_mac_address(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr); +int rtw_halmac_get_network_type(struct dvobj_priv *d, enum _hw_port hwport, u8 *type); +int rtw_halmac_get_bcn_ctrl(struct dvobj_priv *d, enum _hw_port hwport, struct rtw_halmac_bcn_ctrl *bcn_ctrl); +/*int rtw_halmac_get_wow_reason(struct dvobj_priv *, u8 *reason);*/ + +/* Set operations */ +int rtw_halmac_config_rx_info(struct dvobj_priv *d, enum halmac_drv_info info); +int rtw_halmac_set_max_dl_fw_size(struct dvobj_priv *d, u32 size); +int rtw_halmac_set_mac_address(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr); +int rtw_halmac_set_bssid(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr); +int rtw_halmac_set_tx_address(struct dvobj_priv *d, enum _hw_port hwport, u8 *addr); +int rtw_halmac_set_network_type(struct dvobj_priv *d, enum _hw_port hwport, u8 type); +int rtw_halmac_reset_tsf(struct dvobj_priv *d, enum _hw_port hwport); +int rtw_halmac_set_bcn_interval(struct dvobj_priv *d, enum _hw_port hwport, u32 space); +int rtw_halmac_set_bcn_ctrl(struct dvobj_priv *d, enum _hw_port hwport, struct rtw_halmac_bcn_ctrl *bcn_ctrl); +int rtw_halmac_set_aid(struct dvobj_priv *d, enum _hw_port hwport, u16 aid); +int rtw_halmac_set_bandwidth(struct dvobj_priv *d, u8 channel, u8 pri_ch_idx, u8 bw); +int rtw_halmac_set_edca(struct dvobj_priv *d, u8 queue, u8 aifs, u8 cw, u16 txop); +int rtw_halmac_set_rts_full_bw(struct dvobj_priv *d, u8 enable); + +/* Functions */ +int rtw_halmac_poweron(struct dvobj_priv *); +int rtw_halmac_poweroff(struct dvobj_priv *); +int rtw_halmac_init_hal(struct dvobj_priv *); +int rtw_halmac_init_hal_fw(struct dvobj_priv *, u8 *fw, u32 fwsize); +int rtw_halmac_init_hal_fw_file(struct dvobj_priv *, u8 *fwpath); +int rtw_halmac_deinit_hal(struct dvobj_priv *); +int rtw_halmac_self_verify(struct dvobj_priv *); +int rtw_halmac_txfifo_wait_empty(struct dvobj_priv *d, u32 timeout); +int rtw_halmac_dlfw(struct dvobj_priv *, u8 *fw, u32 fwsize); +int rtw_halmac_dlfw_from_file(struct dvobj_priv *, u8 *fwpath); +int rtw_halmac_dlfw_mem(struct dvobj_priv *d, u8 *fw, u32 fwsize, enum fw_mem mem); +int rtw_halmac_dlfw_mem_from_file(struct dvobj_priv *d, u8 *fwpath, enum fw_mem mem); +int rtw_halmac_phy_power_switch(struct dvobj_priv *, u8 enable); +int rtw_halmac_send_h2c(struct dvobj_priv *, u8 *h2c); +int rtw_halmac_c2h_handle(struct dvobj_priv *, u8 *c2h, u32 size); + +/* eFuse */ +int rtw_halmac_get_available_efuse_size(struct dvobj_priv *d, u32 *size); +int rtw_halmac_get_physical_efuse_size(struct dvobj_priv *, u32 *size); +int rtw_halmac_read_physical_efuse_map(struct dvobj_priv *, u8 *map, u32 size); +int rtw_halmac_read_physical_efuse(struct dvobj_priv *, u32 offset, u32 cnt, u8 *data); +int rtw_halmac_write_physical_efuse(struct dvobj_priv *, u32 offset, u32 cnt, u8 *data); +int rtw_halmac_get_logical_efuse_size(struct dvobj_priv *, u32 *size); +int rtw_halmac_read_logical_efuse_map(struct dvobj_priv *, u8 *map, u32 size, u8 *maskmap, u32 masksize); +int rtw_halmac_write_logical_efuse_map(struct dvobj_priv *, u8 *map, u32 size, u8 *maskmap, u32 masksize); +int rtw_halmac_read_logical_efuse(struct dvobj_priv *, u32 offset, u32 cnt, u8 *data); +int rtw_halmac_write_logical_efuse(struct dvobj_priv *, u32 offset, u32 cnt, u8 *data); + +int rtw_halmac_write_bt_physical_efuse(struct dvobj_priv *, u32 offset, u32 cnt, u8 *data); +int rtw_halmac_read_bt_physical_efuse_map(struct dvobj_priv *, u8 *map, u32 size); + +int rtw_halmac_dump_fifo(struct dvobj_priv *d, u8 fifo_sel, u32 addr, u32 size, u8 *buffer); +int rtw_halmac_rx_agg_switch(struct dvobj_priv *, u8 enable); + +/* Specific function APIs*/ +int rtw_halmac_download_rsvd_page(struct dvobj_priv *dvobj, u8 pg_offset, u8 *pbuf, u32 size); +int rtw_halmac_fill_hal_spec(struct dvobj_priv *, struct hal_spec_t *); +int rtw_halmac_p2pps(struct dvobj_priv *dvobj, PHAL_P2P_PS_PARA pp2p_ps_para); +int rtw_halmac_iqk(struct dvobj_priv *d, u8 clear, u8 segment); +int rtw_halmac_dpk(struct dvobj_priv *d, u8 *buf, u32 bufsz); +int rtw_halmac_cfg_phy_para(struct dvobj_priv *d, struct rtw_phy_parameter *para); +int rtw_halmac_led_cfg(struct dvobj_priv *d, u8 enable, u8 mode); +void rtw_halmac_led_switch(struct dvobj_priv *d, u8 on); +int rtw_halmac_bt_wake_cfg(struct dvobj_priv *d, u8 enable); +int rtw_halmac_rfe_ctrl_cfg(struct dvobj_priv *d, u8 gpio); +#ifdef CONFIG_PNO_SUPPORT +int rtw_halmac_pno_scanoffload(struct dvobj_priv *d, u32 enable); +#endif + +#ifdef CONFIG_SDIO_HCI +int rtw_halmac_query_tx_page_num(struct dvobj_priv *); +int rtw_halmac_get_tx_queue_page_num(struct dvobj_priv *, u8 queue, u32 *page); +u32 rtw_halmac_sdio_get_tx_addr(struct dvobj_priv *, u8 *desc, u32 size); +int rtw_halmac_sdio_tx_allowed(struct dvobj_priv *, u8 *buf, u32 size); +u32 rtw_halmac_sdio_get_rx_addr(struct dvobj_priv *, u8 *seq); +int rtw_halmac_sdio_set_tx_format(struct dvobj_priv *d, enum halmac_sdio_tx_format format); +#endif /* CONFIG_SDIO_HCI */ + +#ifdef CONFIG_USB_HCI +u8 rtw_halmac_usb_get_bulkout_id(struct dvobj_priv *, u8 *buf, u32 size); +int rtw_halmac_usb_get_txagg_desc_num(struct dvobj_priv *d, u8 *num); +u8 rtw_halmac_switch_usb_mode(struct dvobj_priv *d, enum RTW_USB_SPEED usb_mode); +#endif /* CONFIG_USB_HCI */ + +#ifdef CONFIG_SUPPORT_TRX_SHARED +void dump_trx_share_mode(void *sel, _adapter *adapter); +#endif + +#ifdef CONFIG_BEAMFORMING +#ifdef RTW_BEAMFORMING_VERSION_2 +int rtw_halmac_bf_add_mu_bfer(struct dvobj_priv *d, u16 paid, u16 csi_para, + u16 my_aid, enum halmac_csi_seg_len sel, u8 *addr); +int rtw_halmac_bf_del_mu_bfer(struct dvobj_priv *d); + +int rtw_halmac_bf_cfg_sounding(struct dvobj_priv *d, enum halmac_snd_role role, + enum halmac_data_rate rate); +int rtw_halmac_bf_del_sounding(struct dvobj_priv *d, enum halmac_snd_role role); + +int rtw_halmac_bf_cfg_csi_rate(struct dvobj_priv *d, u8 rssi, u8 current_rate, + u8 fixrate_en, u8 *new_rate, u8 *bmp_ofdm54); + +int rtw_halmac_bf_cfg_mu_mimo(struct dvobj_priv *d, enum halmac_snd_role role, + u8 *sounding_sts, u16 grouping_bitmap, u8 mu_tx_en, + u32 *given_gid_tab, u32 *given_user_pos); +#define rtw_halmac_bf_cfg_mu_bfee(d, gid_tab, user_pos) \ + rtw_halmac_bf_cfg_mu_mimo(d, HAL_BFEE, NULL, 0, 0, gid_tab, user_pos) + +#endif /* RTW_BEAMFORMING_VERSION_2 */ +#endif /* CONFIG_BEAMFORMING */ + +#endif /* _HAL_HALMAC_H_ */ diff --git a/hal/hal_intf.c b/hal/hal_intf.c new file mode 100644 index 0000000..a6eb51e --- /dev/null +++ b/hal/hal_intf.c @@ -0,0 +1,2349 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ + +#define _HAL_INTF_C_ + +#include +#include + +const u32 _chip_type_to_odm_ic_type[] = { + 0, + ODM_RTL8188E, + ODM_RTL8192E, + ODM_RTL8812, + ODM_RTL8821, + ODM_RTL8723B, + ODM_RTL8814A, + ODM_RTL8703B, + ODM_RTL8188F, + ODM_RTL8188F, + ODM_RTL8822B, + ODM_RTL8723D, + ODM_RTL8821C, + ODM_RTL8710B, + ODM_RTL8192F, + ODM_RTL8822C, + ODM_RTL8814B, + ODM_RTL8723F, + 0, +}; + +void rtw_hal_chip_configure(_adapter *padapter) +{ + padapter->hal_func.intf_chip_configure(padapter); +} + +/* + * Description: + * Read chip internal ROM data + * + * Return: + * _SUCCESS success + * _FAIL fail + */ +u8 rtw_hal_read_chip_info(_adapter *padapter) +{ + u8 rtn = _SUCCESS; + u8 hci_type = rtw_get_intf_type(padapter); + systime start = rtw_get_current_time(); + + /* before access eFuse, make sure card enable has been called */ + if ((hci_type == RTW_SDIO || hci_type == RTW_GSPI) + && !rtw_is_hw_init_completed(padapter)) + rtw_hal_power_on(padapter); + + rtn = padapter->hal_func.read_adapter_info(padapter); + + if ((hci_type == RTW_SDIO || hci_type == RTW_GSPI) + && !rtw_is_hw_init_completed(padapter)) + rtw_hal_power_off(padapter); + + RTW_INFO("%s in %d ms\n", __func__, rtw_get_passing_time_ms(start)); + + return rtn; +} + +void rtw_hal_read_chip_version(_adapter *padapter) +{ + padapter->hal_func.read_chip_version(padapter); + rtw_odm_init_ic_type(padapter); +} + +static void rtw_init_wireless_mode(_adapter *padapter) +{ + u8 proto_wireless_mode = 0; + struct hal_spec_t *hal_spec = GET_HAL_SPEC(padapter); + if(hal_spec->proto_cap & PROTO_CAP_11B) + proto_wireless_mode |= WIRELESS_11B; + + if(hal_spec->proto_cap & PROTO_CAP_11G) + proto_wireless_mode |= WIRELESS_11G; + + if(hal_spec->band_cap & BAND_CAP_5G) + proto_wireless_mode |= WIRELESS_11A; + +#ifdef CONFIG_80211N_HT + if(hal_spec->proto_cap & PROTO_CAP_11N) { + + if(hal_spec->band_cap & BAND_CAP_2G) + proto_wireless_mode |= WIRELESS_11_24N; + if(hal_spec->band_cap & BAND_CAP_5G) + proto_wireless_mode |= WIRELESS_11_5N; + } +#endif + +#ifdef CONFIG_80211AC_VHT + if(hal_spec->proto_cap & PROTO_CAP_11AC) + proto_wireless_mode |= WIRELESS_11AC; +#endif + padapter->registrypriv.wireless_mode &= proto_wireless_mode; +} + +void rtw_hal_def_value_init(_adapter *padapter) +{ + if (is_primary_adapter(padapter)) { + /*init fw_psmode_iface_id*/ + adapter_to_pwrctl(padapter)->fw_psmode_iface_id = 0xff; + /*wireless_mode*/ + rtw_init_wireless_mode(padapter); + padapter->hal_func.init_default_value(padapter); + + rtw_init_hal_com_default_value(padapter); + + #ifdef CONFIG_FW_MULTI_PORT_SUPPORT + adapter_to_dvobj(padapter)->dft.port_id = 0xFF; + adapter_to_dvobj(padapter)->dft.mac_id = 0xFF; + #endif + #ifdef CONFIG_HW_P0_TSF_SYNC + adapter_to_dvobj(padapter)->p0_tsf.sync_port = MAX_HW_PORT; + adapter_to_dvobj(padapter)->p0_tsf.offset = 0; + #endif + + GET_HAL_DATA(padapter)->rx_tsf_addr_filter_config = 0; + } +} + +u8 rtw_hal_data_init(_adapter *padapter) +{ + if (is_primary_adapter(padapter)) { + padapter->hal_data_sz = sizeof(HAL_DATA_TYPE); + padapter->HalData = rtw_zvmalloc(padapter->hal_data_sz); + if (padapter->HalData == NULL) { + RTW_INFO("cant not alloc memory for HAL DATA\n"); + return _FAIL; + } + rtw_phydm_priv_init(padapter); + } + return _SUCCESS; +} + +void rtw_hal_data_deinit(_adapter *padapter) +{ + if (is_primary_adapter(padapter)) { + if (padapter->HalData) { +#ifdef CONFIG_LOAD_PHY_PARA_FROM_FILE + phy_free_filebuf(padapter); +#endif + rtw_vmfree(padapter->HalData, padapter->hal_data_sz); + padapter->HalData = NULL; + padapter->hal_data_sz = 0; + } + } +} + +void rtw_hal_free_data(_adapter *padapter) +{ + /* free HAL Data */ + rtw_hal_data_deinit(padapter); +} +void rtw_hal_dm_init(_adapter *padapter) +{ + if (is_primary_adapter(padapter)) { + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); + + padapter->hal_func.dm_init(padapter); + + _rtw_spinlock_init(&pHalData->IQKSpinLock); + + #ifdef CONFIG_TXPWR_PG_WITH_PWR_IDX + if (pHalData->txpwr_pg_mode == TXPWR_PG_WITH_PWR_IDX) + hal_load_txpwr_info(padapter); + #endif + phy_load_tx_power_ext_info(padapter, 1); + } +} +void rtw_hal_dm_deinit(_adapter *padapter) +{ + if (is_primary_adapter(padapter)) { + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); + + padapter->hal_func.dm_deinit(padapter); + + _rtw_spinlock_free(&pHalData->IQKSpinLock); + } +} + +enum rf_type rtw_chip_rftype_to_hal_rftype(_adapter *adapter, u8 limit) +{ + PHAL_DATA_TYPE hal_data = GET_HAL_DATA(adapter); + u8 tx_num = 0, rx_num = 0; + + /*get RF PATH from version_id.RF_TYPE */ + if (IS_1T1R(hal_data->version_id)) { + tx_num = 1; + rx_num = 1; + } else if (IS_1T2R(hal_data->version_id)) { + tx_num = 1; + rx_num = 2; + } else if (IS_2T2R(hal_data->version_id)) { + tx_num = 2; + rx_num = 2; + } else if (IS_2T3R(hal_data->version_id)) { + tx_num = 2; + rx_num = 3; + } else if (IS_2T4R(hal_data->version_id)) { + tx_num = 2; + rx_num = 4; + } else if (IS_3T3R(hal_data->version_id)) { + tx_num = 3; + rx_num = 3; + } else if (IS_3T4R(hal_data->version_id)) { + tx_num = 3; + rx_num = 4; + } else if (IS_4T4R(hal_data->version_id)) { + tx_num = 4; + rx_num = 4; + } + + if (limit) { + tx_num = rtw_min(tx_num, limit); + rx_num = rtw_min(rx_num, limit); + } + + return trx_num_to_rf_type(tx_num, rx_num); +} + +void dump_hal_runtime_trx_mode(void *sel, _adapter *adapter) +{ + struct registry_priv *regpriv = &adapter->registrypriv; + PHAL_DATA_TYPE hal_data = GET_HAL_DATA(adapter); + int i; + + RTW_PRINT_SEL(sel, "txpath=0x%x, rxpath=0x%x\n", hal_data->txpath, hal_data->rxpath); + for (i = 0; i < hal_data->tx_nss; i++) + RTW_PRINT_SEL(sel, "txpath_%uss:0x%x, num:%u\n" + , i + 1, hal_data->txpath_nss[i] + , hal_data->txpath_num_nss[i]); +} + +void dump_hal_trx_mode(void *sel, _adapter *adapter) +{ + struct registry_priv *regpriv = &adapter->registrypriv; + PHAL_DATA_TYPE hal_data = GET_HAL_DATA(adapter); + int i; + + RTW_PRINT_SEL(sel, "trx_path_bmp:0x%02x(%s), NumTotalRFPath:%u, max_tx_cnt:%u\n" + , hal_data->trx_path_bmp + , rf_type_to_rfpath_str(hal_data->rf_type) + , hal_data->NumTotalRFPath + , hal_data->max_tx_cnt + ); + RTW_PRINT_SEL(sel, "tx_nss:%u, rx_nss:%u\n" + , hal_data->tx_nss, hal_data->rx_nss); + for (i = 0; i < hal_data->tx_nss; i++) + RTW_PRINT_SEL(sel, "txpath_cap_num_%uss:%u\n" + , i + 1, hal_data->txpath_cap_num_nss[i]); + RTW_PRINT_SEL(sel, "\n"); + + dump_hal_runtime_trx_mode(sel, adapter); +} + +void _dump_rf_path(void *sel, _adapter *adapter) +{ + PHAL_DATA_TYPE hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + struct registry_priv *regsty = adapter_to_regsty(adapter); + + RTW_PRINT_SEL(sel, "[RF_PATH] ver_id.RF_TYPE:%s\n" + , rf_type_to_rfpath_str(rtw_chip_rftype_to_hal_rftype(adapter, 0))); + RTW_PRINT_SEL(sel, "[RF_PATH] HALSPEC's rf_reg_trx_path_bmp:0x%02x, rf_reg_path_avail_num:%u, max_tx_cnt:%u\n" + , hal_spec->rf_reg_trx_path_bmp, hal_spec->rf_reg_path_avail_num, hal_spec->max_tx_cnt); + RTW_PRINT_SEL(sel, "[RF_PATH] PG's trx_path_bmp:0x%02x, max_tx_cnt:%u\n" + , hal_data->eeprom_trx_path_bmp, hal_data->eeprom_max_tx_cnt); + RTW_PRINT_SEL(sel, "[RF_PATH] Registry's trx_path_bmp:0x%02x, tx_path_lmt:%u, rx_path_lmt:%u\n" + , regsty->trx_path_bmp, regsty->tx_path_lmt, regsty->rx_path_lmt); + RTW_PRINT_SEL(sel, "[RF_PATH] HALDATA's trx_path_bmp:0x%02x, max_tx_cnt:%u\n" + , hal_data->trx_path_bmp, hal_data->max_tx_cnt); + RTW_PRINT_SEL(sel, "[RF_PATH] HALDATA's rf_type:%s, NumTotalRFPath:%d\n" + , rf_type_to_rfpath_str(hal_data->rf_type), hal_data->NumTotalRFPath); +} + +#ifdef CONFIG_RTL8814A +extern enum rf_type rtl8814a_rfpath_decision(_adapter *adapter); +#endif + +u8 rtw_hal_rfpath_init(_adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + +#ifdef CONFIG_RTL8814A +if (IS_HARDWARE_TYPE_8814A(adapter)) { + enum bb_path tx_bmp, rx_bmp; + hal_data->rf_type = rtl8814a_rfpath_decision(adapter); + rf_type_to_default_trx_bmp(hal_data->rf_type, &tx_bmp, &rx_bmp); + hal_data->trx_path_bmp = (tx_bmp << 4) | rx_bmp; + hal_data->NumTotalRFPath = 4; + hal_data->max_tx_cnt = hal_spec->max_tx_cnt; + hal_data->max_tx_cnt = rtw_min(hal_data->max_tx_cnt, rf_type_to_rf_tx_cnt(hal_data->rf_type)); +} else +#endif +{ + struct registry_priv *regsty = adapter_to_regsty(adapter); + u8 trx_path_bmp; + u8 tx_path_num; + u8 rx_path_num; + int i; + + trx_path_bmp = hal_spec->rf_reg_trx_path_bmp; + + if (regsty->trx_path_bmp != 0x00) { + /* restrict trx_path_bmp with regsty.trx_path_bmp */ + trx_path_bmp &= regsty->trx_path_bmp; + if (!trx_path_bmp) { + RTW_ERR("%s hal_spec.rf_reg_trx_path_bmp:0x%02x, regsty->trx_path_bmp:0x%02x no intersection\n" + , __func__, hal_spec->rf_reg_trx_path_bmp, regsty->trx_path_bmp); + return _FAIL; + } + } else if (hal_data->eeprom_trx_path_bmp != 0x00) { + /* restrict trx_path_bmp with eeprom_trx_path_bmp */ + trx_path_bmp &= hal_data->eeprom_trx_path_bmp; + if (!trx_path_bmp) { + RTW_ERR("%s hal_spec.rf_reg_trx_path_bmp:0x%02x, hal_data->eeprom_trx_path_bmp:0x%02x no intersection\n" + , __func__, hal_spec->rf_reg_trx_path_bmp, hal_data->eeprom_trx_path_bmp); + return _FAIL; + } + } + + /* restrict trx_path_bmp with TX and RX num limit */ + trx_path_bmp = rtw_restrict_trx_path_bmp_by_trx_num_lmt(trx_path_bmp + , regsty->tx_path_lmt, regsty->rx_path_lmt, &tx_path_num, &rx_path_num); + if (!trx_path_bmp) { + RTW_ERR("%s rtw_restrict_trx_path_bmp_by_trx_num_lmt(0x%02x, %u, %u) failed\n" + , __func__, trx_path_bmp, regsty->tx_path_lmt, regsty->rx_path_lmt); + return _FAIL; + } + hal_data->trx_path_bmp = trx_path_bmp; + hal_data->rf_type = trx_bmp_to_rf_type((trx_path_bmp & 0xF0) >> 4, trx_path_bmp & 0x0F); + hal_data->NumTotalRFPath = rtw_max(tx_path_num, rx_path_num); + + hal_data->max_tx_cnt = hal_spec->max_tx_cnt; + hal_data->max_tx_cnt = rtw_min(hal_data->max_tx_cnt, tx_path_num); + if (hal_data->eeprom_max_tx_cnt) + hal_data->max_tx_cnt = rtw_min(hal_data->max_tx_cnt, hal_data->eeprom_max_tx_cnt); + + if (1) + _dump_rf_path(RTW_DBGDUMP, adapter); +} + + RTW_INFO("%s trx_path_bmp:0x%02x(%s), NumTotalRFPath:%u, max_tx_cnt:%u\n" + , __func__ + , hal_data->trx_path_bmp + , rf_type_to_rfpath_str(hal_data->rf_type) + , hal_data->NumTotalRFPath + , hal_data->max_tx_cnt); + + return _SUCCESS; +} + +void _dump_trx_nss(void *sel, _adapter *adapter) +{ + struct registry_priv *regpriv = &adapter->registrypriv; + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + + RTW_PRINT_SEL(sel, "[TRX_Nss] HALSPEC - tx_nss:%d, rx_nss:%d\n", hal_spec->tx_nss_num, hal_spec->rx_nss_num); + RTW_PRINT_SEL(sel, "[TRX_Nss] Registry - tx_nss:%d, rx_nss:%d\n", regpriv->tx_nss, regpriv->rx_nss); + RTW_PRINT_SEL(sel, "[TRX_Nss] HALDATA - tx_nss:%d, rx_nss:%d\n", GET_HAL_TX_NSS(adapter), GET_HAL_RX_NSS(adapter)); + +} +#define NSS_VALID(nss) (nss > 0) + +u8 rtw_hal_trxnss_init(_adapter *adapter) +{ + struct registry_priv *regpriv = &adapter->registrypriv; + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + PHAL_DATA_TYPE hal_data = GET_HAL_DATA(adapter); + enum rf_type rf_path = GET_HAL_RFPATH(adapter); + int i; + + hal_data->tx_nss = hal_spec->tx_nss_num; + hal_data->rx_nss = hal_spec->rx_nss_num; + + if (NSS_VALID(regpriv->tx_nss)) + hal_data->tx_nss = rtw_min(hal_data->tx_nss, regpriv->tx_nss); + hal_data->tx_nss = rtw_min(hal_data->tx_nss, hal_data->max_tx_cnt); + if (NSS_VALID(regpriv->rx_nss)) + hal_data->rx_nss = rtw_min(hal_data->rx_nss, regpriv->rx_nss); + hal_data->rx_nss = rtw_min(hal_data->rx_nss, rf_type_to_rf_rx_cnt(rf_path)); + + for (i = 0; i < 4; i++) { + if (hal_data->tx_nss < i + 1) + break; + + if (IS_HARDWARE_TYPE_8814B(adapter) /* 8814B is always full-TX */ + #ifdef CONFIG_RTW_TX_NPATH_EN + /* these IC is capable of full-TX when macro defined */ + || IS_HARDWARE_TYPE_8192E(adapter) || IS_HARDWARE_TYPE_8192F(adapter) + || IS_HARDWARE_TYPE_8812(adapter) || IS_HARDWARE_TYPE_8822B(adapter) + || IS_HARDWARE_TYPE_8822C(adapter) + #endif + ) + hal_data->txpath_cap_num_nss[i] = hal_data->max_tx_cnt; + else + hal_data->txpath_cap_num_nss[i] = i + 1; + } + + if (1) + _dump_trx_nss(RTW_DBGDUMP, adapter); + + RTW_INFO("%s tx_nss:%u, rx_nss:%u\n", __func__ + , hal_data->tx_nss, hal_data->rx_nss); + + return _SUCCESS; +} + +#ifdef CONFIG_RTW_SW_LED +void rtw_hal_sw_led_init(_adapter *padapter) +{ + struct led_priv *ledpriv = adapter_to_led(padapter); + + if (ledpriv->bRegUseLed == _FALSE) + return; + + if (!is_primary_adapter(padapter)) + return; + + if (padapter->hal_func.InitSwLeds) { + padapter->hal_func.InitSwLeds(padapter); + rtw_led_set_ctl_en_mask_primary(padapter); + rtw_led_set_iface_en(padapter, 1); + } +} + +void rtw_hal_sw_led_deinit(_adapter *padapter) +{ + struct led_priv *ledpriv = adapter_to_led(padapter); + + if (ledpriv->bRegUseLed == _FALSE) + return; + + if (!is_primary_adapter(padapter)) + return; + + if (padapter->hal_func.DeInitSwLeds) + padapter->hal_func.DeInitSwLeds(padapter); +} +#endif + +u32 rtw_hal_power_on(_adapter *padapter) +{ + u32 ret = 0; + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); + + ret = padapter->hal_func.hal_power_on(padapter); + +#ifdef CONFIG_BT_COEXIST + if ((ret == _SUCCESS) && (pHalData->EEPROMBluetoothCoexist == _TRUE)) + rtw_btcoex_PowerOnSetting(padapter); +#endif + + return ret; +} +void rtw_hal_power_off(_adapter *padapter) +{ + struct macid_ctl_t *macid_ctl = &padapter->dvobj->macid_ctl; + + _rtw_memset(macid_ctl->h2c_msr, 0, MACID_NUM_SW_LIMIT); + _rtw_memset(macid_ctl->op_num, 0, H2C_MSR_ROLE_MAX); + +#ifdef CONFIG_LPS_1T1R + GET_HAL_DATA(padapter)->lps_1t1r = 0; +#endif + +#ifdef CONFIG_BT_COEXIST + rtw_btcoex_PowerOffSetting(padapter); +#endif + + padapter->hal_func.hal_power_off(padapter); +} + + +void rtw_hal_init_opmode(_adapter *padapter) +{ + NDIS_802_11_NETWORK_INFRASTRUCTURE networkType = Ndis802_11InfrastructureMax; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + sint fw_state; + + fw_state = get_fwstate(pmlmepriv); + + if (fw_state & WIFI_ADHOC_STATE) + networkType = Ndis802_11IBSS; + else if (fw_state & WIFI_STATION_STATE) + networkType = Ndis802_11Infrastructure; +#ifdef CONFIG_AP_MODE + else if (fw_state & WIFI_AP_STATE) + networkType = Ndis802_11APMode; +#endif +#ifdef CONFIG_RTW_MESH + else if (fw_state & WIFI_MESH_STATE) + networkType = Ndis802_11_mesh; +#endif + else + return; + + rtw_setopmode_cmd(padapter, networkType, RTW_CMDF_DIRECTLY); +} + +#ifdef CONFIG_NEW_NETDEV_HDL +uint rtw_hal_iface_init(_adapter *adapter) +{ + uint status = _SUCCESS; + + rtw_hal_set_hwreg(adapter, HW_VAR_MAC_ADDR, adapter_mac_addr(adapter)); + #ifdef RTW_HALMAC + rtw_hal_hw_port_enable(adapter); + #endif + rtw_sec_restore_wep_key(adapter); + rtw_hal_init_opmode(adapter); + rtw_hal_start_thread(adapter); + return status; +} +uint rtw_hal_init(_adapter *padapter) +{ + uint status = _SUCCESS; + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); + + halrf_set_rfsupportability(adapter_to_phydm(padapter)); + + status = padapter->hal_func.hal_init(padapter); + + if(pHalData ->phydm_init_result) { + + status = _FAIL; + RTW_ERR("%s phydm init fail reason=%u \n", + __func__, + pHalData ->phydm_init_result); + } + + if (status == _SUCCESS) { + rtw_set_hw_init_completed(padapter, _TRUE); + if (padapter->registrypriv.notch_filter == 1) + rtw_hal_notch_filter(padapter, 1); + rtw_led_control(padapter, LED_CTL_POWER_ON); + init_hw_mlme_ext(padapter); + #ifdef CONFIG_RF_POWER_TRIM + rtw_bb_rf_gain_offset(padapter); + #endif /*CONFIG_RF_POWER_TRIM*/ + GET_PRIMARY_ADAPTER(padapter)->bup = _TRUE; /*temporary*/ + #ifdef CONFIG_MI_WITH_MBSSID_CAM + rtw_mi_set_mbid_cam(padapter); + #endif + #ifdef CONFIG_SUPPORT_MULTI_BCN + rtw_ap_multi_bcn_cfg(padapter); + #endif + #if (RTL8822B_SUPPORT == 1) || (RTL8192F_SUPPORT == 1) + #ifdef CONFIG_DYNAMIC_SOML + rtw_dyn_soml_config(padapter); + #endif + #endif + #ifdef CONFIG_TDMADIG + rtw_phydm_tdmadig(padapter, TDMADIG_INIT); + #endif/*CONFIG_TDMADIG*/ + rtw_phydm_dyn_rrsr_en(padapter,padapter->registrypriv.en_dyn_rrsr); + #ifdef RTW_HALMAC + RTW_INFO("%s: padapter->registrypriv.set_rrsr_value=0x%x\n", __func__,padapter->registrypriv.set_rrsr_value); + if(padapter->registrypriv.set_rrsr_value != 0xFFFFFFFF) + rtw_phydm_set_rrsr(padapter, padapter->registrypriv.set_rrsr_value, TRUE); + #endif + } else { + rtw_set_hw_init_completed(padapter, _FALSE); + RTW_ERR("%s: hal_init fail\n", __func__); + } + return status; +} +#else +uint rtw_hal_init(_adapter *padapter) +{ + uint status = _SUCCESS; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); + int i; + + halrf_set_rfsupportability(adapter_to_phydm(padapter)); + + status = padapter->hal_func.hal_init(padapter); + + if(pHalData ->phydm_init_result) { + + status = _FAIL; + RTW_ERR("%s phydm init fail reason=%u \n", + __func__, + pHalData->phydm_init_result); + } + + if (status == _SUCCESS) { + rtw_set_hw_init_completed(padapter, _TRUE); + rtw_mi_set_mac_addr(padapter);/*set mac addr of all ifaces*/ + #ifdef RTW_HALMAC + rtw_restore_hw_port_cfg(padapter); + #endif + if (padapter->registrypriv.notch_filter == 1) + rtw_hal_notch_filter(padapter, 1); + + for (i = 0; i < dvobj->iface_nums; i++) + rtw_sec_restore_wep_key(dvobj->padapters[i]); + + rtw_led_control(padapter, LED_CTL_POWER_ON); + + init_hw_mlme_ext(padapter); + + rtw_hal_init_opmode(padapter); + + #ifdef CONFIG_RF_POWER_TRIM + rtw_bb_rf_gain_offset(padapter); + #endif /*CONFIG_RF_POWER_TRIM*/ + + #ifdef CONFIG_SUPPORT_MULTI_BCN + rtw_ap_multi_bcn_cfg(padapter); + #endif + +#if (RTL8822B_SUPPORT == 1) || (RTL8192F_SUPPORT == 1) +#ifdef CONFIG_DYNAMIC_SOML + rtw_dyn_soml_config(padapter); +#endif +#endif + #ifdef CONFIG_TDMADIG + rtw_phydm_tdmadig(padapter, TDMADIG_INIT); + #endif/*CONFIG_TDMADIG*/ + + rtw_phydm_dyn_rrsr_en(padapter,padapter->registrypriv.en_dyn_rrsr); + #ifdef RTW_HALMAC + RTW_INFO("%s: padapter->registrypriv.set_rrsr_value=0x%x\n", __func__,padapter->registrypriv.set_rrsr_value); + if(padapter->registrypriv.set_rrsr_value != 0xFFFFFFFF) + rtw_phydm_set_rrsr(padapter, padapter->registrypriv.set_rrsr_value, TRUE); + #endif + + } else { + rtw_set_hw_init_completed(padapter, _FALSE); + RTW_ERR("%s: fail\n", __func__); + } + + + return status; + +} +#endif + +uint rtw_hal_deinit(_adapter *padapter) +{ + uint status = _SUCCESS; + + status = padapter->hal_func.hal_deinit(padapter); + + if (status == _SUCCESS) { + rtw_led_control(padapter, LED_CTL_POWER_OFF); + rtw_set_hw_init_completed(padapter, _FALSE); + } else + RTW_INFO("\n rtw_hal_deinit: hal_init fail\n"); + + + return status; +} + +u8 rtw_hal_set_hwreg(_adapter *padapter, u8 variable, u8 *val) +{ + return padapter->hal_func.set_hw_reg_handler(padapter, variable, val); +} + +void rtw_hal_get_hwreg(_adapter *padapter, u8 variable, u8 *val) +{ + padapter->hal_func.GetHwRegHandler(padapter, variable, val); +} + +u8 rtw_hal_set_def_var(_adapter *padapter, HAL_DEF_VARIABLE eVariable, void *pValue) +{ + return padapter->hal_func.SetHalDefVarHandler(padapter, eVariable, pValue); +} +u8 rtw_hal_get_def_var(_adapter *padapter, HAL_DEF_VARIABLE eVariable, void *pValue) +{ + return padapter->hal_func.get_hal_def_var_handler(padapter, eVariable, pValue); +} + +void rtw_hal_set_odm_var(_adapter *padapter, HAL_ODM_VARIABLE eVariable, void *pValue1, BOOLEAN bSet) +{ + padapter->hal_func.SetHalODMVarHandler(padapter, eVariable, pValue1, bSet); +} +void rtw_hal_get_odm_var(_adapter *padapter, HAL_ODM_VARIABLE eVariable, void *pValue1, void *pValue2) +{ + padapter->hal_func.GetHalODMVarHandler(padapter, eVariable, pValue1, pValue2); +} + +/* FOR SDIO & PCIE */ +void rtw_hal_enable_interrupt(_adapter *padapter) +{ +#if defined(CONFIG_PCI_HCI) || defined(CONFIG_SDIO_HCI) || defined (CONFIG_GSPI_HCI) + padapter->hal_func.enable_interrupt(padapter); +#endif /* #if defined(CONFIG_PCI_HCI) || defined (CONFIG_SDIO_HCI) || defined (CONFIG_GSPI_HCI) */ +} + +/* FOR SDIO & PCIE */ +void rtw_hal_disable_interrupt(_adapter *padapter) +{ +#if defined(CONFIG_PCI_HCI) || defined(CONFIG_SDIO_HCI) || defined (CONFIG_GSPI_HCI) + padapter->hal_func.disable_interrupt(padapter); +#endif /* #if defined(CONFIG_PCI_HCI) || defined (CONFIG_SDIO_HCI) || defined (CONFIG_GSPI_HCI) */ +} + + +u8 rtw_hal_check_ips_status(_adapter *padapter) +{ + u8 val = _FALSE; + if (padapter->hal_func.check_ips_status) + val = padapter->hal_func.check_ips_status(padapter); + else + RTW_INFO("%s: hal_func.check_ips_status is NULL!\n", __FUNCTION__); + + return val; +} + +s32 rtw_hal_fw_dl(_adapter *padapter, u8 wowlan) +{ + s32 ret; + + ret = padapter->hal_func.fw_dl(padapter, wowlan); + +#ifdef CONFIG_LPS_1T1R + GET_HAL_DATA(padapter)->lps_1t1r = 0; +#endif + + return ret; +} + +#ifdef RTW_HALMAC +s32 rtw_hal_fw_mem_dl(_adapter *padapter, enum fw_mem mem) +{ + systime dlfw_start_time = rtw_get_current_time(); + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct debug_priv *pdbgpriv = &dvobj->drv_dbg; + s32 rst = _FALSE; + + rst = padapter->hal_func.fw_mem_dl(padapter, mem); + RTW_INFO("%s in %dms\n", __func__, rtw_get_passing_time_ms(dlfw_start_time)); + + if (rst == _FALSE) + pdbgpriv->dbg_fw_mem_dl_error_cnt++; + if (1) + RTW_INFO("%s dbg_fw_mem_dl_error_cnt:%d\n", __func__, pdbgpriv->dbg_fw_mem_dl_error_cnt); + return rst; +} +#endif + +#if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN) +void rtw_hal_clear_interrupt(_adapter *padapter) +{ +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) + padapter->hal_func.clear_interrupt(padapter); +#endif +} +#endif + +#if defined(CONFIG_USB_HCI) || defined(CONFIG_PCI_HCI) +u32 rtw_hal_inirp_init(_adapter *padapter) +{ + if (is_primary_adapter(padapter)) + return padapter->hal_func.inirp_init(padapter); + return _SUCCESS; +} +u32 rtw_hal_inirp_deinit(_adapter *padapter) +{ + + if (is_primary_adapter(padapter)) + return padapter->hal_func.inirp_deinit(padapter); + + return _SUCCESS; +} +#endif /* #if defined(CONFIG_USB_HCI) || defined (CONFIG_PCI_HCI) */ + +#if defined(CONFIG_PCI_HCI) +void rtw_hal_irp_reset(_adapter *padapter) +{ + padapter->hal_func.irp_reset(GET_PRIMARY_ADAPTER(padapter)); +} + +void rtw_hal_pci_dbi_write(_adapter *padapter, u16 addr, u8 data) +{ + u16 cmd[2]; + + cmd[0] = addr; + cmd[1] = data; + + padapter->hal_func.set_hw_reg_handler(padapter, HW_VAR_DBI, (u8 *) cmd); +} + +u8 rtw_hal_pci_dbi_read(_adapter *padapter, u16 addr) +{ + padapter->hal_func.GetHwRegHandler(padapter, HW_VAR_DBI, (u8 *)(&addr)); + + return (u8)addr; +} + +void rtw_hal_pci_mdio_write(_adapter *padapter, u8 addr, u16 data) +{ + u16 cmd[2]; + + cmd[0] = (u16)addr; + cmd[1] = data; + + padapter->hal_func.set_hw_reg_handler(padapter, HW_VAR_MDIO, (u8 *) cmd); +} + +u16 rtw_hal_pci_mdio_read(_adapter *padapter, u8 addr) +{ + padapter->hal_func.GetHwRegHandler(padapter, HW_VAR_MDIO, &addr); + + return (u8)addr; +} + +u8 rtw_hal_pci_l1off_nic_support(_adapter *padapter) +{ + u8 l1off; + + padapter->hal_func.GetHwRegHandler(padapter, HW_VAR_L1OFF_NIC_SUPPORT, &l1off); + return l1off; +} + +u8 rtw_hal_pci_l1off_capability(_adapter *padapter) +{ + u8 l1off; + + padapter->hal_func.GetHwRegHandler(padapter, HW_VAR_L1OFF_CAPABILITY, &l1off); + return l1off; +} + + +#endif /* #if defined(CONFIG_PCI_HCI) */ + +/* for USB Auto-suspend */ +u8 rtw_hal_intf_ps_func(_adapter *padapter, HAL_INTF_PS_FUNC efunc_id, u8 *val) +{ + if (padapter->hal_func.interface_ps_func) + return padapter->hal_func.interface_ps_func(padapter, efunc_id, val); + return _FAIL; +} + +#ifdef CONFIG_RTW_MGMT_QUEUE +s32 rtw_hal_mgmt_xmitframe_enqueue(_adapter *padapter, struct xmit_frame *pxmitframe) +{ + return padapter->hal_func.hal_mgmt_xmitframe_enqueue(padapter, pxmitframe); +} +#endif + +s32 rtw_hal_xmitframe_enqueue(_adapter *padapter, struct xmit_frame *pxmitframe) +{ + return padapter->hal_func.hal_xmitframe_enqueue(padapter, pxmitframe); +} + +s32 rtw_hal_xmit(_adapter *padapter, struct xmit_frame *pxmitframe) +{ + return padapter->hal_func.hal_xmit(padapter, pxmitframe); +} + +/* + * [IMPORTANT] This function would be run in interrupt context. + */ +s32 rtw_hal_mgnt_xmit(_adapter *padapter, struct xmit_frame *pmgntframe) +{ +#ifdef CONFIG_RTW_MGMT_QUEUE + _irqL irqL; + struct xmit_priv *pxmitpriv = &(padapter->xmitpriv); +#endif + s32 ret = _FAIL; + + update_mgntframe_attrib_addr(padapter, pmgntframe); +#ifdef CONFIG_RTW_MGMT_QUEUE + update_mgntframe_subtype(padapter, pmgntframe); +#endif + +#if defined(CONFIG_IEEE80211W) || defined(CONFIG_RTW_MESH) + if ((!MLME_IS_MESH(padapter) && SEC_IS_BIP_KEY_INSTALLED(&padapter->securitypriv) == _TRUE) + #ifdef CONFIG_RTW_MESH + || (MLME_IS_MESH(padapter) && padapter->mesh_info.mesh_auth_id) + #endif + ) + rtw_mgmt_xmitframe_coalesce(padapter, pmgntframe->pkt, pmgntframe); +#endif + +#ifdef CONFIG_RTW_MGMT_QUEUE + if (MLME_IS_AP(padapter) || MLME_IS_MESH(padapter)) { + _enter_critical_bh(&pxmitpriv->lock, &irqL); + ret = mgmt_xmitframe_enqueue_for_sleeping_sta(padapter, pmgntframe); + _exit_critical_bh(&pxmitpriv->lock, &irqL); + + #ifdef DBG_MGMT_QUEUE + if (ret == _TRUE) + RTW_INFO("%s doesn't be queued, dattrib->ra:"MAC_FMT" seq_num = %u, subtype = 0x%x\n", + __func__, MAC_ARG(pmgntframe->attrib.ra), pmgntframe->attrib.seqnum, pmgntframe->attrib.subtype); + #endif + + if (ret == RTW_QUEUE_MGMT) + return ret; + } +#endif + + ret = padapter->hal_func.mgnt_xmit(padapter, pmgntframe); + return ret; +} + +s32 rtw_hal_init_xmit_priv(_adapter *padapter) +{ + return padapter->hal_func.init_xmit_priv(padapter); +} +void rtw_hal_free_xmit_priv(_adapter *padapter) +{ + padapter->hal_func.free_xmit_priv(padapter); +} + +s32 rtw_hal_init_recv_priv(_adapter *padapter) +{ + return padapter->hal_func.init_recv_priv(padapter); +} +void rtw_hal_free_recv_priv(_adapter *padapter) +{ + padapter->hal_func.free_recv_priv(padapter); +} + +void rtw_sta_ra_registed(_adapter *padapter, struct sta_info *psta) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(padapter); + + if (psta == NULL) { + RTW_ERR(FUNC_ADPT_FMT" sta is NULL\n", FUNC_ADPT_ARG(padapter)); + rtw_warn_on(1); + return; + } + +#ifdef CONFIG_AP_MODE + if (MLME_IS_AP(padapter) || MLME_IS_MESH(padapter)) { + if (psta->cmn.aid > padapter->stapriv.max_aid) { + RTW_ERR("station aid %d exceed the max number\n", psta->cmn.aid); + rtw_warn_on(1); + return; + } + rtw_ap_update_sta_ra_info(padapter, psta); + } +#endif + + psta->cmn.ra_info.ra_bw_mode = rtw_get_tx_bw_mode(padapter, psta); + /*set correct initial date rate for each mac_id */ + hal_data->INIDATA_RATE[psta->cmn.mac_id] = psta->init_rate; + + rtw_phydm_ra_registed(padapter, psta); +} + +void rtw_hal_update_ra_mask(struct sta_info *psta) +{ + _adapter *padapter; + + if (!psta) + return; + + padapter = psta->padapter; + rtw_sta_ra_registed(padapter, psta); +} + +/* Start specifical interface thread */ +void rtw_hal_start_thread(_adapter *padapter) +{ +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) +#ifndef CONFIG_SDIO_TX_TASKLET + padapter->hal_func.run_thread(padapter); +#endif +#endif +} +/* Start specifical interface thread */ +void rtw_hal_stop_thread(_adapter *padapter) +{ +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) +#ifndef CONFIG_SDIO_TX_TASKLET + + padapter->hal_func.cancel_thread(padapter); + +#endif +#endif +} + +u32 rtw_hal_read_bbreg(_adapter *padapter, u32 RegAddr, u32 BitMask) +{ + u32 data = 0; + if (padapter->hal_func.read_bbreg) + data = padapter->hal_func.read_bbreg(padapter, RegAddr, BitMask); + return data; +} +void rtw_hal_write_bbreg(_adapter *padapter, u32 RegAddr, u32 BitMask, u32 Data) +{ + if (padapter->hal_func.write_bbreg) + padapter->hal_func.write_bbreg(padapter, RegAddr, BitMask, Data); +} + +u32 rtw_hal_read_rfreg(_adapter *padapter, enum rf_path eRFPath, u32 RegAddr, u32 BitMask) +{ + u32 data = 0; + + if (padapter->hal_func.read_rfreg) { + data = padapter->hal_func.read_rfreg(padapter, eRFPath, RegAddr, BitMask); + + #ifdef DBG_IO + if (match_rf_read_sniff_ranges(padapter, eRFPath, RegAddr, BitMask)) { + RTW_INFO("DBG_IO rtw_hal_read_rfreg(%u, 0x%04x, 0x%08x) read:0x%08x(0x%08x)\n" + , eRFPath, RegAddr, BitMask, (data << PHY_CalculateBitShift(BitMask)), data); + } + #endif + } + + return data; +} + +void rtw_hal_write_rfreg(_adapter *padapter, enum rf_path eRFPath, u32 RegAddr, u32 BitMask, u32 Data) +{ + if (padapter->hal_func.write_rfreg) { + + #ifdef DBG_IO + if (match_rf_write_sniff_ranges(padapter, eRFPath, RegAddr, BitMask)) { + RTW_INFO("DBG_IO rtw_hal_write_rfreg(%u, 0x%04x, 0x%08x) write:0x%08x(0x%08x)\n" + , eRFPath, RegAddr, BitMask, (Data << PHY_CalculateBitShift(BitMask)), Data); + } + #endif + + padapter->hal_func.write_rfreg(padapter, eRFPath, RegAddr, BitMask, Data); + +#ifdef CONFIG_PCI_HCI + if (!IS_HARDWARE_TYPE_JAGUAR_AND_JAGUAR2(padapter)) /*For N-Series IC, suggest by Jenyu*/ + rtw_udelay_os(2); +#endif + } +} + +#ifdef CONFIG_SYSON_INDIRECT_ACCESS +u32 rtw_hal_read_syson_reg(PADAPTER padapter, u32 RegAddr, u32 BitMask) +{ + u32 data = 0; + if (padapter->hal_func.read_syson_reg) + data = padapter->hal_func.read_syson_reg(padapter, RegAddr, BitMask); + + return data; +} + +void rtw_hal_write_syson_reg(_adapter *padapter, u32 RegAddr, u32 BitMask, u32 Data) +{ + if (padapter->hal_func.write_syson_reg) + padapter->hal_func.write_syson_reg(padapter, RegAddr, BitMask, Data); +} +#endif + +#if defined(CONFIG_PCI_HCI) +s32 rtw_hal_interrupt_handler(_adapter *padapter) +{ + s32 ret = _FAIL; + ret = padapter->hal_func.interrupt_handler(padapter); + return ret; +} + +void rtw_hal_unmap_beacon_icf(_adapter *padapter) +{ + padapter->hal_func.unmap_beacon_icf(padapter); +} +#endif +#if defined(CONFIG_USB_HCI) && defined(CONFIG_SUPPORT_USB_INT) +void rtw_hal_interrupt_handler(_adapter *padapter, u16 pkt_len, u8 *pbuf) +{ + padapter->hal_func.interrupt_handler(padapter, pkt_len, pbuf); +} +#endif + +void rtw_hal_set_chnl_bw(_adapter *padapter, u8 channel, enum channel_width Bandwidth, u8 Offset40, u8 Offset80) +{ + PHAL_DATA_TYPE pHalData = GET_HAL_DATA(padapter); + /*u8 cch_160 = Bandwidth == CHANNEL_WIDTH_160 ? channel : 0;*/ + u8 cch_80 = Bandwidth == CHANNEL_WIDTH_80 ? channel : 0; + u8 cch_40 = Bandwidth == CHANNEL_WIDTH_40 ? channel : 0; + u8 cch_20 = Bandwidth == CHANNEL_WIDTH_20 ? channel : 0; + + if (rtw_phydm_is_iqk_in_progress(padapter)) + RTW_ERR("%s, %d, IQK may race condition\n", __func__, __LINE__); + +#ifdef CONFIG_MP_INCLUDED + /* MP mode channel don't use secondary channel */ + if (rtw_mp_mode_check(padapter) == _FALSE) +#endif + { + #if 0 + if (cch_160 != 0) + cch_80 = rtw_get_scch_by_cch_offset(cch_160, CHANNEL_WIDTH_160, Offset80); + #endif + if (cch_80 != 0) + cch_40 = rtw_get_scch_by_cch_offset(cch_80, CHANNEL_WIDTH_80, Offset80); + if (cch_40 != 0) + cch_20 = rtw_get_scch_by_cch_offset(cch_40, CHANNEL_WIDTH_40, Offset40); + } + + pHalData->cch_80 = cch_80; + pHalData->cch_40 = cch_40; + pHalData->cch_20 = cch_20; + + if (0) + RTW_INFO("%s cch:%u, %s, offset40:%u, offset80:%u (%u, %u, %u)\n", __func__ + , channel, ch_width_str(Bandwidth), Offset40, Offset80 + , pHalData->cch_80, pHalData->cch_40, pHalData->cch_20); + + padapter->hal_func.set_chnl_bw_handler(padapter, channel, Bandwidth, Offset40, Offset80); +} + +void rtw_hal_dm_watchdog(_adapter *padapter) +{ + + rtw_hal_turbo_edca(padapter); + padapter->hal_func.hal_dm_watchdog(padapter); +} + +#ifdef CONFIG_LPS_LCLK_WD_TIMER +void rtw_hal_dm_watchdog_in_lps(_adapter *padapter) +{ +#if defined(CONFIG_CONCURRENT_MODE) +#ifndef CONFIG_FW_MULTI_PORT_SUPPORT + if (padapter->hw_port != HW_PORT0) + return; +#endif +#endif + + if (adapter_to_pwrctl(padapter)->bFwCurrentInPSMode == _TRUE) + rtw_phydm_watchdog_in_lps_lclk(padapter);/* this function caller is in interrupt context */ +} +#endif /*CONFIG_LPS_LCLK_WD_TIMER*/ + +void rtw_hal_bcn_related_reg_setting(_adapter *padapter) +{ + padapter->hal_func.SetBeaconRelatedRegistersHandler(padapter); +} + +#ifdef CONFIG_HOSTAPD_MLME +s32 rtw_hal_hostap_mgnt_xmit_entry(_adapter *padapter, _pkt *pkt) +{ + if (padapter->hal_func.hostap_mgnt_xmit_entry) + return padapter->hal_func.hostap_mgnt_xmit_entry(padapter, pkt); + return _FAIL; +} +#endif /* CONFIG_HOSTAPD_MLME */ + +#ifdef DBG_CONFIG_ERROR_DETECT +void rtw_hal_sreset_init(_adapter *padapter) +{ + padapter->hal_func.sreset_init_value(padapter); +} +void rtw_hal_sreset_reset(_adapter *padapter) +{ + padapter = GET_PRIMARY_ADAPTER(padapter); + padapter->hal_func.silentreset(padapter); +} + +void rtw_hal_sreset_reset_value(_adapter *padapter) +{ + padapter->hal_func.sreset_reset_value(padapter); +} + +void rtw_hal_sreset_xmit_status_check(_adapter *padapter) +{ + padapter->hal_func.sreset_xmit_status_check(padapter); +} +void rtw_hal_sreset_linked_status_check(_adapter *padapter) +{ + padapter->hal_func.sreset_linked_status_check(padapter); +} +u8 rtw_hal_sreset_get_wifi_status(_adapter *padapter) +{ + return padapter->hal_func.sreset_get_wifi_status(padapter); +} + +bool rtw_hal_sreset_inprogress(_adapter *padapter) +{ + padapter = GET_PRIMARY_ADAPTER(padapter); + return padapter->hal_func.sreset_inprogress(padapter); +} +#endif /* DBG_CONFIG_ERROR_DETECT */ + +#ifdef CONFIG_IOL +int rtw_hal_iol_cmd(ADAPTER *adapter, struct xmit_frame *xmit_frame, u32 max_waiting_ms, u32 bndy_cnt) +{ + if (adapter->hal_func.IOL_exec_cmds_sync) + return adapter->hal_func.IOL_exec_cmds_sync(adapter, xmit_frame, max_waiting_ms, bndy_cnt); + return _FAIL; +} +#endif + +#ifdef CONFIG_XMIT_THREAD_MODE +s32 rtw_hal_xmit_thread_handler(_adapter *padapter) +{ + return padapter->hal_func.xmit_thread_handler(padapter); +} +#endif + +#ifdef CONFIG_RECV_THREAD_MODE +s32 rtw_hal_recv_hdl(_adapter *adapter) +{ + return adapter->hal_func.recv_hdl(adapter); +} +#endif + +void rtw_hal_notch_filter(_adapter *adapter, bool enable) +{ + if (adapter->hal_func.hal_notch_filter) + adapter->hal_func.hal_notch_filter(adapter, enable); +} + +#ifdef CONFIG_FW_C2H_REG +inline bool rtw_hal_c2h_valid(_adapter *adapter, u8 *buf) +{ + HAL_DATA_TYPE *HalData = GET_HAL_DATA(adapter); + bool ret = _FAIL; + + ret = C2H_ID_88XX(buf) || C2H_PLEN_88XX(buf); + + return ret; +} + +inline s32 rtw_hal_c2h_evt_read(_adapter *adapter, u8 *buf) +{ + HAL_DATA_TYPE *HalData = GET_HAL_DATA(adapter); + s32 ret = _FAIL; + + ret = c2h_evt_read_88xx(adapter, buf); + + return ret; +} + +bool rtw_hal_c2h_reg_hdr_parse(_adapter *adapter, u8 *buf, u8 *id, u8 *seq, u8 *plen, u8 **payload) +{ + HAL_DATA_TYPE *HalData = GET_HAL_DATA(adapter); + bool ret = _FAIL; + + *id = C2H_ID_88XX(buf); + *seq = C2H_SEQ_88XX(buf); + *plen = C2H_PLEN_88XX(buf); + *payload = C2H_PAYLOAD_88XX(buf); + ret = _SUCCESS; + + return ret; +} +#endif /* CONFIG_FW_C2H_REG */ + +#ifdef CONFIG_FW_C2H_PKT +bool rtw_hal_c2h_pkt_hdr_parse(_adapter *adapter, u8 *buf, u16 len, u8 *id, u8 *seq, u8 *plen, u8 **payload) +{ + HAL_DATA_TYPE *HalData = GET_HAL_DATA(adapter); + bool ret = _FAIL; + + if (!buf || len > 256 || len < 3) + goto exit; + + *id = C2H_ID_88XX(buf); + *seq = C2H_SEQ_88XX(buf); + *plen = len - 2; + *payload = C2H_PAYLOAD_88XX(buf); + ret = _SUCCESS; + +exit: + return ret; +} +#endif /* CONFIG_FW_C2H_PKT */ + +#if defined(CONFIG_MP_INCLUDED) && defined(CONFIG_RTL8723B) +#include /* for MPTBT_FwC2hBtMpCtrl */ +#endif +s32 c2h_handler(_adapter *adapter, u8 id, u8 seq, u8 plen, u8 *payload) +{ + u8 sub_id = 0; + s32 ret = _SUCCESS; + + switch (id) { + case C2H_FW_SCAN_COMPLETE: + RTW_INFO("[C2H], FW Scan Complete\n"); + break; + +#ifdef CONFIG_BT_COEXIST + case C2H_BT_INFO: + rtw_btcoex_BtInfoNotify(adapter, plen, payload); + break; + case C2H_BT_MP_INFO: + #if defined(CONFIG_MP_INCLUDED) && defined(CONFIG_RTL8723B) + MPTBT_FwC2hBtMpCtrl(adapter, payload, plen); + #endif + rtw_btcoex_BtMpRptNotify(adapter, plen, payload); + break; + case C2H_MAILBOX_STATUS: + RTW_DBG_DUMP("C2H_MAILBOX_STATUS: ", payload, plen); + break; + case C2H_WLAN_INFO: + rtw_btcoex_WlFwDbgInfoNotify(adapter, payload, plen); + break; +#endif /* CONFIG_BT_COEXIST */ + + case C2H_IQK_FINISH: + c2h_iqk_offload(adapter, payload, plen); + break; + +#if defined(CONFIG_TDLS) && defined(CONFIG_TDLS_CH_SW) + case C2H_FW_CHNL_SWITCH_COMPLETE: +#ifndef CONFIG_TDLS_CH_SW_V2 + rtw_tdls_chsw_oper_done(adapter); +#endif + break; +#endif + + case C2H_BCN_EARLY_RPT: + rtw_hal_bcn_early_rpt_c2h_handler(adapter); + break; + +#ifdef CONFIG_MCC_MODE + case C2H_MCC: + rtw_hal_mcc_c2h_handler(adapter, plen, payload); + break; +#endif + +#ifdef CONFIG_RTW_MAC_HIDDEN_RPT + case C2H_MAC_HIDDEN_RPT: + c2h_mac_hidden_rpt_hdl(adapter, payload, plen); + break; + case C2H_MAC_HIDDEN_RPT_2: + c2h_mac_hidden_rpt_2_hdl(adapter, payload, plen); + break; +#endif + + case C2H_DEFEATURE_DBG: + c2h_defeature_dbg_hdl(adapter, payload, plen); + break; + +#ifdef CONFIG_RTW_CUSTOMER_STR + case C2H_CUSTOMER_STR_RPT: + c2h_customer_str_rpt_hdl(adapter, payload, plen); + break; + case C2H_CUSTOMER_STR_RPT_2: + c2h_customer_str_rpt_2_hdl(adapter, payload, plen); + break; +#endif +#ifdef RTW_PER_CMD_SUPPORT_FW + case C2H_PER_RATE_RPT: + c2h_per_rate_rpt_hdl(adapter, payload, plen); + break; +#endif +#ifdef CONFIG_LPS_ACK + case C2H_LPS_STATUS_RPT: + c2h_lps_status_rpt(adapter, payload, plen); + break; +#endif +#ifdef CONFIG_FW_OFFLOAD_SET_TXPWR_IDX + case C2H_SET_TXPWR_FINISH: + c2h_txpwr_idx_offload_done(adapter, payload, plen); + break; +#endif + case C2H_EXTEND: + sub_id = payload[0]; + /* no handle, goto default */ + /* fall through */ + + default: + if (phydm_c2H_content_parsing(adapter_to_phydm(adapter), id, plen, payload) != TRUE) + ret = _FAIL; + break; + } + + if (ret != _SUCCESS) { + if (id == C2H_EXTEND) + RTW_WARN("%s: unknown C2H(0x%02x, 0x%02x)\n", __func__, id, sub_id); + else + RTW_WARN("%s: unknown C2H(0x%02x)\n", __func__, id); + } + + return ret; +} + +#ifndef RTW_HALMAC +s32 rtw_hal_c2h_handler(_adapter *adapter, u8 id, u8 seq, u8 plen, u8 *payload) +{ + s32 ret = _FAIL; + + ret = adapter->hal_func.c2h_handler(adapter, id, seq, plen, payload); + if (ret != _SUCCESS) + ret = c2h_handler(adapter, id, seq, plen, payload); + + return ret; +} + +s32 rtw_hal_c2h_id_handle_directly(_adapter *adapter, u8 id, u8 seq, u8 plen, u8 *payload) +{ + switch (id) { + case C2H_CCX_TX_RPT: + case C2H_BT_MP_INFO: + case C2H_FW_CHNL_SWITCH_COMPLETE: + case C2H_IQK_FINISH: + case C2H_MCC: + case C2H_BCN_EARLY_RPT: + case C2H_AP_REQ_TXRPT: + case C2H_SPC_STAT: + case C2H_SET_TXPWR_FINISH: + return _TRUE; + default: + return _FALSE; + } +} +#endif /* !RTW_HALMAC */ + +s32 rtw_hal_is_disable_sw_channel_plan(PADAPTER padapter) +{ + return GET_HAL_DATA(padapter)->bDisableSWChannelPlan; +} + +#ifdef CONFIG_PROTSEL_MACSLEEP +static s32 _rtw_hal_macid_sleep(_adapter *adapter, u8 macid, u8 sleep) +{ + struct macid_ctl_t *macid_ctl = adapter_to_macidctl(adapter); + u16 reg_sleep_info = macid_ctl->reg_sleep_info; + u16 reg_sleep_ctrl = macid_ctl->reg_sleep_ctrl; + const u32 sel_mask_sel = BIT(0) | BIT(1) | BIT(2); + u8 bit_shift; + u32 val32; + s32 ret = _FAIL; + + if (macid >= macid_ctl->num) { + RTW_ERR(ADPT_FMT" %s invalid macid(%u)\n" + , ADPT_ARG(adapter), sleep ? "sleep" : "wakeup" , macid); + goto exit; + } + + if (macid < 32) { + bit_shift = macid; + #if (MACID_NUM_SW_LIMIT > 32) + } else if (macid < 64) { + bit_shift = macid - 32; + #endif + #if (MACID_NUM_SW_LIMIT > 64) + } else if (macid < 96) { + bit_shift = macid - 64; + #endif + #if (MACID_NUM_SW_LIMIT > 96) + } else if (macid < 128) { + bit_shift = macid - 96; + #endif + } else { + rtw_warn_on(1); + goto exit; + } + + if (!reg_sleep_ctrl || !reg_sleep_info) { + rtw_warn_on(1); + goto exit; + } + + val32 = rtw_read32(adapter, reg_sleep_ctrl); + val32 = (val32 &~sel_mask_sel) | ((macid / 32) & sel_mask_sel); + rtw_write32(adapter, reg_sleep_ctrl, val32); + + val32 = rtw_read32(adapter, reg_sleep_info); + RTW_INFO(ADPT_FMT" %s macid=%d, ori reg_0x%03x=0x%08x\n" + , ADPT_ARG(adapter), sleep ? "sleep" : "wakeup" + , macid, reg_sleep_info, val32); + + ret = _SUCCESS; + + if (sleep) { + if (val32 & BIT(bit_shift)) + goto exit; + val32 |= BIT(bit_shift); + } else { + if (!(val32 & BIT(bit_shift))) + goto exit; + val32 &= ~BIT(bit_shift); + } + + rtw_write32(adapter, reg_sleep_info, val32); + +exit: + return ret; +} +#else +static s32 _rtw_hal_macid_sleep(_adapter *adapter, u8 macid, u8 sleep) +{ + struct macid_ctl_t *macid_ctl = adapter_to_macidctl(adapter); + u16 reg_sleep; + u8 bit_shift; + u32 val32; + s32 ret = _FAIL; + + if (macid >= macid_ctl->num) { + RTW_ERR(ADPT_FMT" %s invalid macid(%u)\n" + , ADPT_ARG(adapter), sleep ? "sleep" : "wakeup" , macid); + goto exit; + } + + if (macid < 32) { + reg_sleep = macid_ctl->reg_sleep_m0; + bit_shift = macid; + #if (MACID_NUM_SW_LIMIT > 32) + } else if (macid < 64) { + reg_sleep = macid_ctl->reg_sleep_m1; + bit_shift = macid - 32; + #endif + #if (MACID_NUM_SW_LIMIT > 64) + } else if (macid < 96) { + reg_sleep = macid_ctl->reg_sleep_m2; + bit_shift = macid - 64; + #endif + #if (MACID_NUM_SW_LIMIT > 96) + } else if (macid < 128) { + reg_sleep = macid_ctl->reg_sleep_m3; + bit_shift = macid - 96; + #endif + } else { + rtw_warn_on(1); + goto exit; + } + + if (!reg_sleep) { + rtw_warn_on(1); + goto exit; + } + + val32 = rtw_read32(adapter, reg_sleep); + RTW_INFO(ADPT_FMT" %s macid=%d, ori reg_0x%03x=0x%08x\n" + , ADPT_ARG(adapter), sleep ? "sleep" : "wakeup" + , macid, reg_sleep, val32); + + ret = _SUCCESS; + + if (sleep) { + if (val32 & BIT(bit_shift)) + goto exit; + val32 |= BIT(bit_shift); + } else { + if (!(val32 & BIT(bit_shift))) + goto exit; + val32 &= ~BIT(bit_shift); + } + + rtw_write32(adapter, reg_sleep, val32); + +exit: + return ret; +} +#endif + +inline s32 rtw_hal_macid_sleep(_adapter *adapter, u8 macid) +{ + return _rtw_hal_macid_sleep(adapter, macid, 1); +} + +inline s32 rtw_hal_macid_wakeup(_adapter *adapter, u8 macid) +{ + return _rtw_hal_macid_sleep(adapter, macid, 0); +} + +#ifdef CONFIG_PROTSEL_MACSLEEP +static s32 _rtw_hal_macid_bmp_sleep(_adapter *adapter, struct macid_bmp *bmp, u8 sleep) +{ + struct macid_ctl_t *macid_ctl = adapter_to_macidctl(adapter); + u16 reg_sleep_info = macid_ctl->reg_sleep_info; + u16 reg_sleep_ctrl = macid_ctl->reg_sleep_ctrl; + const u32 sel_mask_sel = BIT(0) | BIT(1) | BIT(2); + u32 m; + u8 mid = 0; + u32 val32; + + do { + if (mid == 0) { + m = bmp->m0; + #if (MACID_NUM_SW_LIMIT > 32) + } else if (mid == 1) { + m = bmp->m1; + #endif + #if (MACID_NUM_SW_LIMIT > 64) + } else if (mid == 2) { + m = bmp->m2; + #endif + #if (MACID_NUM_SW_LIMIT > 96) + } else if (mid == 3) { + m = bmp->m3; + #endif + } else { + rtw_warn_on(1); + break; + } + + if (m == 0) + goto move_next; + + if (!reg_sleep_ctrl || !reg_sleep_info) { + rtw_warn_on(1); + break; + } + + val32 = rtw_read32(adapter, reg_sleep_ctrl); + val32 = (val32 &~sel_mask_sel) | (mid & sel_mask_sel); + rtw_write32(adapter, reg_sleep_ctrl, val32); + + val32 = rtw_read32(adapter, reg_sleep_info); + RTW_INFO(ADPT_FMT" %s m%u=0x%08x, ori reg_0x%03x=0x%08x\n" + , ADPT_ARG(adapter), sleep ? "sleep" : "wakeup" + , mid, m, reg_sleep_info, val32); + + if (sleep) { + if ((val32 & m) == m) + goto move_next; + val32 |= m; + } else { + if ((val32 & m) == 0) + goto move_next; + val32 &= ~m; + } + + rtw_write32(adapter, reg_sleep_info, val32); + +move_next: + mid++; + } while (mid * 32 < MACID_NUM_SW_LIMIT); + + return _SUCCESS; +} +#else +static s32 _rtw_hal_macid_bmp_sleep(_adapter *adapter, struct macid_bmp *bmp, u8 sleep) +{ + struct macid_ctl_t *macid_ctl = adapter_to_macidctl(adapter); + u16 reg_sleep; + u32 m; + u8 mid = 0; + u32 val32; + + do { + if (mid == 0) { + m = bmp->m0; + reg_sleep = macid_ctl->reg_sleep_m0; + #if (MACID_NUM_SW_LIMIT > 32) + } else if (mid == 1) { + m = bmp->m1; + reg_sleep = macid_ctl->reg_sleep_m1; + #endif + #if (MACID_NUM_SW_LIMIT > 64) + } else if (mid == 2) { + m = bmp->m2; + reg_sleep = macid_ctl->reg_sleep_m2; + #endif + #if (MACID_NUM_SW_LIMIT > 96) + } else if (mid == 3) { + m = bmp->m3; + reg_sleep = macid_ctl->reg_sleep_m3; + #endif + } else { + rtw_warn_on(1); + break; + } + + if (m == 0) + goto move_next; + + if (!reg_sleep) { + rtw_warn_on(1); + break; + } + + val32 = rtw_read32(adapter, reg_sleep); + RTW_INFO(ADPT_FMT" %s m%u=0x%08x, ori reg_0x%03x=0x%08x\n" + , ADPT_ARG(adapter), sleep ? "sleep" : "wakeup" + , mid, m, reg_sleep, val32); + + if (sleep) { + if ((val32 & m) == m) + goto move_next; + val32 |= m; + } else { + if ((val32 & m) == 0) + goto move_next; + val32 &= ~m; + } + + rtw_write32(adapter, reg_sleep, val32); + +move_next: + mid++; + } while (mid * 32 < MACID_NUM_SW_LIMIT); + + return _SUCCESS; +} +#endif + +inline s32 rtw_hal_macid_sleep_all_used(_adapter *adapter) +{ + struct macid_ctl_t *macid_ctl = adapter_to_macidctl(adapter); + + return _rtw_hal_macid_bmp_sleep(adapter, &macid_ctl->used, 1); +} + +inline s32 rtw_hal_macid_wakeup_all_used(_adapter *adapter) +{ + struct macid_ctl_t *macid_ctl = adapter_to_macidctl(adapter); + + return _rtw_hal_macid_bmp_sleep(adapter, &macid_ctl->used, 0); +} + +static s32 _rtw_hal_macid_drop(_adapter *adapter, u8 macid, u8 drop) +{ + struct macid_ctl_t *macid_ctl = adapter_to_macidctl(adapter); +#ifndef CONFIG_PROTSEL_MACSLEEP + u16 reg_drop = 0; +#else + u16 reg_drop_info = macid_ctl->reg_drop_info; + u16 reg_drop_ctrl = macid_ctl->reg_drop_ctrl; + const u32 sel_mask_sel = BIT(0) | BIT(1) | BIT(2); +#endif /* CONFIG_PROTSEL_MACSLEEP */ + u8 bit_shift; + u32 val32; + s32 ret = _FAIL; +/* some IC doesn't have this register */ +#ifndef REG_PKT_BUFF_ACCESS_CTRL +#define REG_PKT_BUFF_ACCESS_CTRL 0 +#endif + + if (macid >= macid_ctl->num) { + RTW_ERR(ADPT_FMT" %s invalid macid(%u)\n" + , ADPT_ARG(adapter), drop ? "drop" : "undrop" , macid); + goto exit; + } + + if(_rtw_macid_ctl_chk_cap(adapter, MACID_DROP)) { + if (macid < 32) { +#ifndef CONFIG_PROTSEL_MACSLEEP + reg_drop = macid_ctl->reg_drop_m0; +#endif /* CONFIG_PROTSEL_MACSLEEP */ + bit_shift = macid; + #if (MACID_NUM_SW_LIMIT > 32) + } else if (macid < 64) { +#ifndef CONFIG_PROTSEL_MACSLEEP + reg_drop = macid_ctl->reg_drop_m1; +#endif /* CONFIG_PROTSEL_MACSLEEP */ + bit_shift = macid - 32; + #endif + #if (MACID_NUM_SW_LIMIT > 64) + } else if (macid < 96) { +#ifndef CONFIG_PROTSEL_MACSLEEP + reg_drop = macid_ctl->reg_drop_m2; +#endif /* CONFIG_PROTSEL_MACSLEEP */ + bit_shift = macid - 64; + #endif + #if (MACID_NUM_SW_LIMIT > 96) + } else if (macid < 128) { +#ifndef CONFIG_PROTSEL_MACSLEEP + reg_drop = macid_ctl->reg_drop_m3; +#endif /* CONFIG_PROTSEL_MACSLEEP */ + bit_shift = macid - 96; + #endif + } else { + rtw_warn_on(1); + goto exit; + } + +#ifndef CONFIG_PROTSEL_MACSLEEP + if (!reg_drop) { + rtw_warn_on(1); + goto exit; + } + val32 = rtw_read32(adapter, reg_drop); + /*RTW_INFO(ADPT_FMT" %s macid=%d, ori reg_0x%03x=0x%08x \n" + , ADPT_ARG(adapter), drop ? "drop" : "undrop" + , macid, reg_drop, val32);*/ +#else + if (!reg_drop_ctrl || !reg_drop_info) { + rtw_warn_on(1); + goto exit; + } + + val32 = rtw_read32(adapter, reg_drop_ctrl); + val32 = (val32 &~sel_mask_sel) | ((macid / 32) & sel_mask_sel); + rtw_write32(adapter, reg_drop_ctrl, val32); + + val32 = rtw_read32(adapter, reg_drop_info); + /*RTW_INFO(ADPT_FMT" %s macid=%d, ori reg_0x%03x=0x%08x\n" + , ADPT_ARG(adapter), drop ? "drop" : "undrop" + , macid, reg_drop_info, val32);*/ +#endif /* CONFIG_PROTSEL_MACSLEEP */ + ret = _SUCCESS; + + if (drop) { + if (val32 & BIT(bit_shift)) + goto exit; + val32 |= BIT(bit_shift); + } else { + if (!(val32 & BIT(bit_shift))) + goto exit; + val32 &= ~BIT(bit_shift); + } + +#ifndef CONFIG_PROTSEL_MACSLEEP + rtw_write32(adapter, reg_drop, val32); + RTW_INFO(ADPT_FMT" %s macid=%d, done reg_0x%03x=0x%08x\n" + , ADPT_ARG(adapter), drop ? "drop" : "undrop" + , macid, reg_drop, val32); +#else + rtw_write32(adapter, reg_drop_info, val32); + RTW_INFO(ADPT_FMT" %s macid=%d, done reg_0x%03x=0x%08x\n" + , ADPT_ARG(adapter), drop ? "drop" : "undrop" + , macid, reg_drop_info, val32); +#endif /* CONFIG_PROTSEL_MACSLEEP */ + + + } else if(_rtw_macid_ctl_chk_cap(adapter, MACID_DROP_INDIRECT)) { + u16 start_addr = macid_ctl->macid_txrpt/8; + u32 txrpt_h4b = 0; + u8 i; + + /* each address means 1 byte */ + start_addr += macid*(macid_ctl->macid_txrpt_pgsz/8); + /* select tx report buffer */ + rtw_write8(adapter, REG_PKT_BUFF_ACCESS_CTRL, TXREPORT_BUF_SELECT); + /* set tx report buffer start address for reading */ + rtw_write32(adapter, REG_PKTBUF_DBG_CTRL, start_addr); + txrpt_h4b = rtw_read32(adapter, REG_PKTBUF_DBG_DATA_H); + /* OFFSET5 BIT2 is BIT10 of high 4 bytes */ + if (drop) { + if (txrpt_h4b & BIT(10)) + goto exit; + txrpt_h4b |= BIT(10); + } else { + if (!(txrpt_h4b & BIT(10))) + goto exit; + txrpt_h4b &= ~BIT(10); + } + /* set to macid drop field */ + rtw_write32(adapter, REG_PKTBUF_DBG_DATA_H, txrpt_h4b); + /* 0x20800000 only write BIT10 of tx report buf */ + rtw_write32(adapter, REG_PKTBUF_DBG_CTRL, 0x20800000 | start_addr); +#if 0 /* some ICs doesn't clear the write done bit */ + /* checking TX queue status */ + for (i = 0 ; i < 50 ; i++) { + txrpt_h4b = rtw_read32(adapter, REG_PKTBUF_DBG_CTRL); + if (txrpt_h4b & BIT(23)) { + RTW_INFO("%s: wait to write TX RTP buf (%d)!\n", __func__, i); + rtw_mdelay_os(10); + } else { + RTW_INFO("%s: wait to write TX RTP buf done (%d)!\n", __func__, i); + break; + } + } +#endif + rtw_write32(adapter, REG_PKTBUF_DBG_CTRL, start_addr); + RTW_INFO("start_addr=%x, data_H:%08x, data_L:%08x, macid=%d, txrpt_h4b=%x\n", start_addr + ,rtw_read32(adapter, REG_PKTBUF_DBG_DATA_H), rtw_read32(adapter, REG_PKTBUF_DBG_DATA_L), macid, txrpt_h4b); + } else { + RTW_INFO("There is no definition for camctl cap , please correct it\n"); + } +exit: + return ret; +} + +inline s32 rtw_hal_macid_drop(_adapter *adapter, u8 macid) +{ + return _rtw_hal_macid_drop(adapter, macid, 1); +} + +inline s32 rtw_hal_macid_undrop(_adapter *adapter, u8 macid) +{ + return _rtw_hal_macid_drop(adapter, macid, 0); +} + +s32 rtw_hal_fill_h2c_cmd(PADAPTER padapter, u8 ElementID, u32 CmdLen, u8 *pCmdBuffer) +{ + _adapter *pri_adapter = GET_PRIMARY_ADAPTER(padapter); + + if (GET_HAL_DATA(pri_adapter)->bFWReady == _TRUE) + return padapter->hal_func.fill_h2c_cmd(padapter, ElementID, CmdLen, pCmdBuffer); + else if (padapter->registrypriv.mp_mode == 0) + RTW_PRINT(FUNC_ADPT_FMT" FW doesn't exit when no MP mode, by pass H2C id:0x%02x\n" + , FUNC_ADPT_ARG(padapter), ElementID); + return _FAIL; +} + +void rtw_hal_fill_fake_txdesc(_adapter *padapter, u8 *pDesc, u32 BufferLen, + u8 IsPsPoll, u8 IsBTQosNull, u8 bDataFrame) +{ + padapter->hal_func.fill_fake_txdesc(padapter, pDesc, BufferLen, IsPsPoll, IsBTQosNull, bDataFrame); + +} + +u8 rtw_hal_get_txbuff_rsvd_page_num(_adapter *adapter, bool wowlan) +{ + u8 num = 0; + + + if (adapter->hal_func.hal_get_tx_buff_rsvd_page_num) { + num = adapter->hal_func.hal_get_tx_buff_rsvd_page_num(adapter, wowlan); + } else { +#ifdef RTW_HALMAC + num = GET_HAL_DATA(adapter)->drv_rsvd_page_number; +#endif /* RTW_HALMAC */ + } + + return num; +} + +#ifdef CONFIG_GPIO_API +void rtw_hal_update_hisr_hsisr_ind(_adapter *padapter, u32 flag) +{ + if (padapter->hal_func.update_hisr_hsisr_ind) + padapter->hal_func.update_hisr_hsisr_ind(padapter, flag); +} + +int rtw_hal_gpio_func_check(_adapter *padapter, u8 gpio_num) +{ + int ret = _SUCCESS; + + if (padapter->hal_func.hal_gpio_func_check) + ret = padapter->hal_func.hal_gpio_func_check(padapter, gpio_num); + + return ret; +} + +void rtw_hal_gpio_multi_func_reset(_adapter *padapter, u8 gpio_num) +{ + if (padapter->hal_func.hal_gpio_multi_func_reset) + padapter->hal_func.hal_gpio_multi_func_reset(padapter, gpio_num); +} +#endif + +#ifdef CONFIG_FW_CORRECT_BCN +void rtw_hal_fw_correct_bcn(_adapter *padapter) +{ + if (padapter->hal_func.fw_correct_bcn) + padapter->hal_func.fw_correct_bcn(padapter); +} +#endif + +void rtw_hal_set_tx_power_level(_adapter *adapter, u8 channel) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + + if (phy_chk_ch_setting_consistency(adapter, channel) != _SUCCESS) + return; + + hal_data->set_entire_txpwr = 1; + + adapter->hal_func.set_tx_power_level_handler(adapter, channel); + rtw_hal_set_txpwr_done(adapter); + + hal_data->set_entire_txpwr = 0; +} + +void rtw_hal_update_txpwr_level(_adapter *adapter) +{ + HAL_DATA_TYPE *hal_data = GET_HAL_DATA(adapter); + + rtw_hal_set_tx_power_level(adapter, hal_data->current_channel); + rtw_rfctl_update_op_mode(adapter_to_rfctl(adapter), 0, 0); +} + +void rtw_hal_set_txpwr_done(_adapter *adapter) +{ + if (adapter->hal_func.set_txpwr_done) + adapter->hal_func.set_txpwr_done(adapter); +} + +void rtw_hal_set_tx_power_index(_adapter *adapter, u32 powerindex + , enum rf_path rfpath, u8 rate) +{ + adapter->hal_func.set_tx_power_index_handler(adapter, powerindex, rfpath, rate); +} + +u8 rtw_hal_get_tx_power_index(_adapter *adapter, enum rf_path rfpath + , RATE_SECTION rs, enum MGN_RATE rate, enum channel_width bw, BAND_TYPE band, u8 cch, u8 opch + , struct txpwr_idx_comp *tic) +{ + return adapter->hal_func.get_tx_power_index_handler(adapter, rfpath + , rs, rate, bw, band, cch, opch, tic); +} + +s8 rtw_hal_get_txpwr_target_extra_bias(_adapter *adapter, enum rf_path rfpath + , RATE_SECTION rs, enum MGN_RATE rate, enum channel_width bw, BAND_TYPE band, u8 cch) +{ + s8 val = 0; + + if (adapter->hal_func.get_txpwr_target_extra_bias) { + val = adapter->hal_func.get_txpwr_target_extra_bias(adapter + , rfpath, rs, rate, bw, band, cch); + } + + return val; +} + +#ifdef RTW_HALMAC +/* + * Description: + * Initialize MAC registers + * + * Return: + * _TRUE success + * _FALSE fail + */ +u8 rtw_hal_init_mac_register(PADAPTER adapter) +{ + return adapter->hal_func.init_mac_register(adapter); +} + +/* + * Description: + * Initialize PHY(BB/RF) related functions + * + * Return: + * _TRUE success + * _FALSE fail + */ +u8 rtw_hal_init_phy(PADAPTER adapter) +{ + return adapter->hal_func.init_phy(adapter); +} +#endif /* RTW_HALMAC */ + +#ifdef CONFIG_RFKILL_POLL +bool rtw_hal_rfkill_poll(_adapter *adapter, u8 *valid) +{ + bool ret; + + if (adapter->hal_func.hal_radio_onoff_check) + ret = adapter->hal_func.hal_radio_onoff_check(adapter, valid); + else { + *valid = 0; + ret = _FALSE; + } + return ret; +} +#endif + +#define rtw_hal_error_msg(ops_fun) \ + RTW_PRINT("### %s - Error : Please hook hal_func.%s ###\n", __FUNCTION__, ops_fun) + +u8 rtw_hal_ops_check(_adapter *padapter) +{ + u8 ret = _SUCCESS; +#if 1 + /*** initialize section ***/ + if (NULL == padapter->hal_func.read_chip_version) { + rtw_hal_error_msg("read_chip_version"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.init_default_value) { + rtw_hal_error_msg("init_default_value"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.intf_chip_configure) { + rtw_hal_error_msg("intf_chip_configure"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.read_adapter_info) { + rtw_hal_error_msg("read_adapter_info"); + ret = _FAIL; + } + + if (NULL == padapter->hal_func.hal_power_on) { + rtw_hal_error_msg("hal_power_on"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.hal_power_off) { + rtw_hal_error_msg("hal_power_off"); + ret = _FAIL; + } + + if (NULL == padapter->hal_func.hal_init) { + rtw_hal_error_msg("hal_init"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.hal_deinit) { + rtw_hal_error_msg("hal_deinit"); + ret = _FAIL; + } + + /*** xmit section ***/ + if (NULL == padapter->hal_func.init_xmit_priv) { + rtw_hal_error_msg("init_xmit_priv"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.free_xmit_priv) { + rtw_hal_error_msg("free_xmit_priv"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.hal_xmit) { + rtw_hal_error_msg("hal_xmit"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.mgnt_xmit) { + rtw_hal_error_msg("mgnt_xmit"); + ret = _FAIL; + } +#ifdef CONFIG_XMIT_THREAD_MODE + if (NULL == padapter->hal_func.xmit_thread_handler) { + rtw_hal_error_msg("xmit_thread_handler"); + ret = _FAIL; + } +#endif + if (NULL == padapter->hal_func.hal_xmitframe_enqueue) { + rtw_hal_error_msg("hal_xmitframe_enqueue"); + ret = _FAIL; + } +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) +#ifndef CONFIG_SDIO_TX_TASKLET + if (NULL == padapter->hal_func.run_thread) { + rtw_hal_error_msg("run_thread"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.cancel_thread) { + rtw_hal_error_msg("cancel_thread"); + ret = _FAIL; + } +#endif +#endif + + /*** recv section ***/ + if (NULL == padapter->hal_func.init_recv_priv) { + rtw_hal_error_msg("init_recv_priv"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.free_recv_priv) { + rtw_hal_error_msg("free_recv_priv"); + ret = _FAIL; + } +#ifdef CONFIG_RECV_THREAD_MODE + if (NULL == padapter->hal_func.recv_hdl) { + rtw_hal_error_msg("recv_hdl"); + ret = _FAIL; + } +#endif +#if defined(CONFIG_USB_HCI) || defined(CONFIG_PCI_HCI) + if (NULL == padapter->hal_func.inirp_init) { + rtw_hal_error_msg("inirp_init"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.inirp_deinit) { + rtw_hal_error_msg("inirp_deinit"); + ret = _FAIL; + } +#endif /* #if defined(CONFIG_USB_HCI) || defined (CONFIG_PCI_HCI) */ + + + /*** interrupt hdl section ***/ +#if defined(CONFIG_PCI_HCI) + if (NULL == padapter->hal_func.irp_reset) { + rtw_hal_error_msg("irp_reset"); + ret = _FAIL; + } +#endif/*#if defined(CONFIG_PCI_HCI)*/ +#if (defined(CONFIG_PCI_HCI)) || (defined(CONFIG_USB_HCI) && defined(CONFIG_SUPPORT_USB_INT)) + if (NULL == padapter->hal_func.interrupt_handler) { + rtw_hal_error_msg("interrupt_handler"); + ret = _FAIL; + } +#endif /*#if (defined(CONFIG_PCI_HCI)) || (defined(CONFIG_USB_HCI) && defined(CONFIG_SUPPORT_USB_INT))*/ + +#if defined(CONFIG_PCI_HCI) || defined(CONFIG_SDIO_HCI) || defined (CONFIG_GSPI_HCI) + if (NULL == padapter->hal_func.enable_interrupt) { + rtw_hal_error_msg("enable_interrupt"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.disable_interrupt) { + rtw_hal_error_msg("disable_interrupt"); + ret = _FAIL; + } +#endif /* defined(CONFIG_PCI_HCI) || defined (CONFIG_SDIO_HCI) || defined (CONFIG_GSPI_HCI) */ + + + /*** DM section ***/ + if (NULL == padapter->hal_func.dm_init) { + rtw_hal_error_msg("dm_init"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.dm_deinit) { + rtw_hal_error_msg("dm_deinit"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.hal_dm_watchdog) { + rtw_hal_error_msg("hal_dm_watchdog"); + ret = _FAIL; + } + + /*** xxx section ***/ + if (NULL == padapter->hal_func.set_chnl_bw_handler) { + rtw_hal_error_msg("set_chnl_bw_handler"); + ret = _FAIL; + } + + if (NULL == padapter->hal_func.set_hw_reg_handler) { + rtw_hal_error_msg("set_hw_reg_handler"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.GetHwRegHandler) { + rtw_hal_error_msg("GetHwRegHandler"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.get_hal_def_var_handler) { + rtw_hal_error_msg("get_hal_def_var_handler"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.SetHalDefVarHandler) { + rtw_hal_error_msg("SetHalDefVarHandler"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.GetHalODMVarHandler) { + rtw_hal_error_msg("GetHalODMVarHandler"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.SetHalODMVarHandler) { + rtw_hal_error_msg("SetHalODMVarHandler"); + ret = _FAIL; + } + + if (NULL == padapter->hal_func.SetBeaconRelatedRegistersHandler) { + rtw_hal_error_msg("SetBeaconRelatedRegistersHandler"); + ret = _FAIL; + } + + if (NULL == padapter->hal_func.fill_h2c_cmd) { + rtw_hal_error_msg("fill_h2c_cmd"); + ret = _FAIL; + } + +#ifdef RTW_HALMAC + if (NULL == padapter->hal_func.hal_mac_c2h_handler) { + rtw_hal_error_msg("hal_mac_c2h_handler"); + ret = _FAIL; + } +#elif !defined(CONFIG_RTL8188E) + if (NULL == padapter->hal_func.c2h_handler) { + rtw_hal_error_msg("c2h_handler"); + ret = _FAIL; + } +#endif + +#if defined(CONFIG_LPS) || defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN) + if (NULL == padapter->hal_func.fill_fake_txdesc) { + rtw_hal_error_msg("fill_fake_txdesc"); + ret = _FAIL; + } +#endif + +#ifndef RTW_HALMAC + if (NULL == padapter->hal_func.hal_get_tx_buff_rsvd_page_num) { + rtw_hal_error_msg("hal_get_tx_buff_rsvd_page_num"); + ret = _FAIL; + } +#endif /* !RTW_HALMAC */ + +#if defined(CONFIG_WOWLAN) || defined(CONFIG_AP_WOWLAN) +#if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI) + if (NULL == padapter->hal_func.clear_interrupt) { + rtw_hal_error_msg("clear_interrupt"); + ret = _FAIL; + } +#endif +#endif /* CONFIG_WOWLAN */ + + if (NULL == padapter->hal_func.fw_dl) { + rtw_hal_error_msg("fw_dl"); + ret = _FAIL; + } + + #ifdef CONFIG_FW_CORRECT_BCN + if (IS_HARDWARE_TYPE_8814A(padapter) + && NULL == padapter->hal_func.fw_correct_bcn) { + rtw_hal_error_msg("fw_correct_bcn"); + ret = _FAIL; + } + #endif + + if (!padapter->hal_func.set_tx_power_level_handler) { + rtw_hal_error_msg("set_tx_power_level_handler"); + ret = _FAIL; + } + if (!padapter->hal_func.set_tx_power_index_handler) { + rtw_hal_error_msg("set_tx_power_index_handler"); + ret = _FAIL; + } + if (!padapter->hal_func.get_tx_power_index_handler) { + rtw_hal_error_msg("get_tx_power_index_handler"); + ret = _FAIL; + } + + /*** SReset section ***/ +#ifdef DBG_CONFIG_ERROR_DETECT + if (NULL == padapter->hal_func.sreset_init_value) { + rtw_hal_error_msg("sreset_init_value"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.sreset_reset_value) { + rtw_hal_error_msg("sreset_reset_value"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.silentreset) { + rtw_hal_error_msg("silentreset"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.sreset_xmit_status_check) { + rtw_hal_error_msg("sreset_xmit_status_check"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.sreset_linked_status_check) { + rtw_hal_error_msg("sreset_linked_status_check"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.sreset_get_wifi_status) { + rtw_hal_error_msg("sreset_get_wifi_status"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.sreset_inprogress) { + rtw_hal_error_msg("sreset_inprogress"); + ret = _FAIL; + } +#endif /* #ifdef DBG_CONFIG_ERROR_DETECT */ + +#ifdef RTW_HALMAC + if (NULL == padapter->hal_func.init_mac_register) { + rtw_hal_error_msg("init_mac_register"); + ret = _FAIL; + } + if (NULL == padapter->hal_func.init_phy) { + rtw_hal_error_msg("init_phy"); + ret = _FAIL; + } +#endif /* RTW_HALMAC */ + +#ifdef CONFIG_RFKILL_POLL + if (padapter->hal_func.hal_radio_onoff_check == NULL) { + rtw_hal_error_msg("hal_radio_onoff_check"); + ret = _FAIL; + } +#endif +#endif + return ret; +} diff --git a/hal/hal_mcc.c b/hal/hal_mcc.c new file mode 100644 index 0000000..9043218 --- /dev/null +++ b/hal/hal_mcc.c @@ -0,0 +1,4078 @@ +/****************************************************************************** + * + * Copyright(c) 2015 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#ifdef CONFIG_MCC_MODE +#define _HAL_MCC_C_ + +#include /* PADAPTER */ +#include /* mcc structure */ +#include /* HAL_DATA */ +#include /* power control */ + +/* use for AP/GO + STA/GC case */ +#define MCC_DURATION_IDX 0 /* druration for station side */ +#define MCC_TSF_SYNC_OFFSET_IDX 1 +#define MCC_START_TIME_OFFSET_IDX 2 +#define MCC_INTERVAL_IDX 3 +#define MCC_GUARD_OFFSET0_IDX 4 +#define MCC_GUARD_OFFSET1_IDX 5 +#define MCC_STOP_THRESHOLD 6 +#define TU 1024 /* 1 TU equals 1024 microseconds */ +/* druration, TSF sync offset, start time offset, interval (unit:TU (1024 microseconds))*/ +u8 mcc_switch_channel_policy_table[][7]={ + {20, 50, 40, 100, 0, 0, 30}, + {80, 50, 10, 100, 0, 0, 30}, + {36, 50, 32, 100, 0, 0, 30}, + {30, 50, 35, 100, 0, 0, 30}, +}; + +const int mcc_max_policy_num = sizeof(mcc_switch_channel_policy_table) /sizeof(u8) /7; + +static void dump_iqk_val_table(PADAPTER padapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(padapter); + struct hal_iqk_reg_backup *iqk_reg_backup = pHalData->iqk_reg_backup; + u8 total_rf_path = hal_spec->rf_reg_path_num; + u8 rf_path_idx = 0; + u8 backup_chan_idx = 0; + u8 backup_reg_idx = 0; + +#ifdef CONFIG_MCC_MODE_V2 +#else + + RTW_INFO("=============dump IQK backup table================\n"); + for (backup_chan_idx = 0; backup_chan_idx < MAX_IQK_INFO_BACKUP_CHNL_NUM; backup_chan_idx++) { + for (rf_path_idx = 0; rf_path_idx < total_rf_path; rf_path_idx++) { + for(backup_reg_idx = 0; backup_reg_idx < MAX_IQK_INFO_BACKUP_REG_NUM; backup_reg_idx++) { + RTW_INFO("ch:%d. bw:%d. rf path:%d. reg[%d] = 0x%02x \n" + , iqk_reg_backup[backup_chan_idx].central_chnl + , iqk_reg_backup[backup_chan_idx].bw_mode + , rf_path_idx + , backup_reg_idx + , iqk_reg_backup[backup_chan_idx].reg_backup[rf_path_idx][backup_reg_idx] + ); + } + } + } + RTW_INFO("=============================================\n"); + +#endif +} + +static void rtw_hal_mcc_build_p2p_noa_attr(PADAPTER padapter, u8 *ie, u32 *ie_len) +{ + struct mcc_adapter_priv *pmccadapriv = &padapter->mcc_adapterpriv; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *pmccobjpriv = &(dvobj->mcc_objpriv); + u8 p2p_noa_attr_ie[MAX_P2P_IE_LEN] = {0x00}; + u32 p2p_noa_attr_len = 0; + u8 noa_desc_num = 1; + u8 opp_ps = 0; /* Disable OppPS */ + u8 noa_count = 255; + u32 noa_duration; + u32 noa_interval; + u8 noa_index = 0; + u8 mcc_policy_idx = 0; + + mcc_policy_idx = pmccobjpriv->policy_index; + noa_duration = mcc_switch_channel_policy_table[mcc_policy_idx][MCC_DURATION_IDX] * TU; + noa_interval = mcc_switch_channel_policy_table[mcc_policy_idx][MCC_INTERVAL_IDX] * TU; + + /* P2P OUI(4 bytes) */ + _rtw_memcpy(p2p_noa_attr_ie, P2P_OUI, 4); + p2p_noa_attr_len = p2p_noa_attr_len + 4; + + /* attrute ID(1 byte) */ + p2p_noa_attr_ie[p2p_noa_attr_len] = P2P_ATTR_NOA; + p2p_noa_attr_len = p2p_noa_attr_len + 1; + + /* attrute length(2 bytes) length = noa_desc_num*13 + 2 */ + RTW_PUT_LE16(p2p_noa_attr_ie + p2p_noa_attr_len, (noa_desc_num * 13 + 2)); + p2p_noa_attr_len = p2p_noa_attr_len + 2; + + /* Index (1 byte) */ + p2p_noa_attr_ie[p2p_noa_attr_len] = noa_index; + p2p_noa_attr_len = p2p_noa_attr_len + 1; + + /* CTWindow and OppPS Parameters (1 byte) */ + p2p_noa_attr_ie[p2p_noa_attr_len] = opp_ps; + p2p_noa_attr_len = p2p_noa_attr_len+ 1; + + /* NoA Count (1 byte) */ + p2p_noa_attr_ie[p2p_noa_attr_len] = noa_count; + p2p_noa_attr_len = p2p_noa_attr_len + 1; + + /* NoA Duration (4 bytes) unit: microseconds */ + RTW_PUT_LE32(p2p_noa_attr_ie + p2p_noa_attr_len, noa_duration); + p2p_noa_attr_len = p2p_noa_attr_len + 4; + + /* NoA Interval (4 bytes) unit: microseconds */ + RTW_PUT_LE32(p2p_noa_attr_ie + p2p_noa_attr_len, noa_interval); + p2p_noa_attr_len = p2p_noa_attr_len + 4; + + /* NoA Start Time (4 bytes) unit: microseconds */ + RTW_PUT_LE32(p2p_noa_attr_ie + p2p_noa_attr_len, pmccadapriv->noa_start_time); + if (0) + RTW_INFO("indxe:%d, start_time=0x%02x:0x%02x:0x%02x:0x%02x\n" + , noa_index + , p2p_noa_attr_ie[p2p_noa_attr_len] + , p2p_noa_attr_ie[p2p_noa_attr_len + 1] + , p2p_noa_attr_ie[p2p_noa_attr_len + 2] + , p2p_noa_attr_ie[p2p_noa_attr_len + 3]); + + p2p_noa_attr_len = p2p_noa_attr_len + 4; + rtw_set_ie(ie, _VENDOR_SPECIFIC_IE_, p2p_noa_attr_len, (u8 *)p2p_noa_attr_ie, ie_len); +} + + +/** + * rtw_hal_mcc_update_go_p2p_ie - update go p2p ie(add NoA attribute) + * @padapter: the adapter to be update go p2p ie + */ +static void rtw_hal_mcc_update_go_p2p_ie(PADAPTER padapter) +{ + struct mcc_adapter_priv *pmccadapriv = &padapter->mcc_adapterpriv; + struct mcc_obj_priv *mccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + u8 *pos = NULL; + + + /* no noa attribute, build it */ + if (pmccadapriv->p2p_go_noa_ie_len == 0) + rtw_hal_mcc_build_p2p_noa_attr(padapter, pmccadapriv->p2p_go_noa_ie, &pmccadapriv->p2p_go_noa_ie_len); + else { + /* has noa attribut, modify it */ + u32 noa_duration = 0; + + /* update index */ + pos = pmccadapriv->p2p_go_noa_ie + pmccadapriv->p2p_go_noa_ie_len - 15; + /* 0~255 */ + (*pos) = ((*pos) + 1) % 256; + if (0) + RTW_INFO("indxe:%d\n", (*pos)); + + + /* update duration */ + noa_duration = mcc_switch_channel_policy_table[mccobjpriv->policy_index][MCC_DURATION_IDX] * TU; + pos = pmccadapriv->p2p_go_noa_ie + pmccadapriv->p2p_go_noa_ie_len - 12; + RTW_PUT_LE32(pos, noa_duration); + + /* update start time */ + pos = pmccadapriv->p2p_go_noa_ie + pmccadapriv->p2p_go_noa_ie_len - 4; + RTW_PUT_LE32(pos, pmccadapriv->noa_start_time); + if (0) + RTW_INFO("start_time=0x%02x:0x%02x:0x%02x:0x%02x\n" + , ((u8*)(pos))[0] + , ((u8*)(pos))[1] + , ((u8*)(pos))[2] + , ((u8*)(pos))[3]); + + } + + if (0) { + RTW_INFO("p2p_go_noa_ie_len:%d\n", pmccadapriv->p2p_go_noa_ie_len); + RTW_INFO_DUMP("\n", pmccadapriv->p2p_go_noa_ie, pmccadapriv->p2p_go_noa_ie_len); + } + update_beacon(padapter, _VENDOR_SPECIFIC_IE_, P2P_OUI, _TRUE, 0); +} + +/** + * rtw_hal_mcc_remove_go_p2p_ie - remove go p2p ie(add NoA attribute) + * @padapter: the adapter to be update go p2p ie + */ +static void rtw_hal_mcc_remove_go_p2p_ie(PADAPTER padapter) +{ + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct mcc_adapter_priv *pmccadapriv = &padapter->mcc_adapterpriv; + + /* chech has noa ie or not */ + if (pmccadapriv->p2p_go_noa_ie_len == 0) + return; + + pmccadapriv->p2p_go_noa_ie_len = 0; + update_beacon(padapter, _VENDOR_SPECIFIC_IE_, P2P_OUI, _TRUE, 0); +} + +/* restore IQK value for all interface */ +void rtw_hal_mcc_restore_iqk_val(PADAPTER padapter) +{ + u8 take_care_iqk = _FALSE; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + _adapter *iface = NULL; + struct mcc_adapter_priv *mccadapriv = NULL; + u8 i = 0; + + rtw_hal_get_hwreg(padapter, HW_VAR_CH_SW_NEED_TO_TAKE_CARE_IQK_INFO, &take_care_iqk); + if (take_care_iqk == _TRUE && MCC_EN(padapter)) { + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (iface == NULL) + continue; + + mccadapriv = &iface->mcc_adapterpriv; + if (mccadapriv->role == MCC_ROLE_MAX) + continue; + + rtw_hal_ch_sw_iqk_info_restore(iface, CH_SW_USE_CASE_MCC); + } + } + + if (0) + dump_iqk_val_table(padapter); +} + +u8 rtw_hal_check_mcc_status(PADAPTER padapter, u8 mcc_status) +{ + struct mcc_obj_priv *pmccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + + if (pmccobjpriv->mcc_status & (mcc_status)) + return _TRUE; + else + return _FALSE; +} + +void rtw_hal_set_mcc_status(PADAPTER padapter, u8 mcc_status) +{ + struct mcc_obj_priv *pmccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + + pmccobjpriv->mcc_status |= (mcc_status); +} + +void rtw_hal_clear_mcc_status(PADAPTER padapter, u8 mcc_status) +{ + struct mcc_obj_priv *pmccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + + pmccobjpriv->mcc_status &= (~mcc_status); +} + +static void rtw_hal_mcc_update_policy_table(PADAPTER adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mcc_obj_priv *mccobjpriv = &(dvobj->mcc_objpriv); + u8 mcc_duration = mccobjpriv->duration; + s8 mcc_policy_idx = mccobjpriv->policy_index; + u8 interval = mcc_switch_channel_policy_table[mcc_policy_idx][MCC_INTERVAL_IDX]; + u8 new_mcc_duration_time = 0; + u8 new_starttime_offset = 0; + + /* convert % to ms */ + new_mcc_duration_time = mcc_duration * interval / 100; + + /* start time offset = (interval - duration time)/2 */ + new_starttime_offset = (interval - new_mcc_duration_time) >> 1; + + /* update modified parameters */ + mcc_switch_channel_policy_table[mcc_policy_idx][MCC_DURATION_IDX] + = new_mcc_duration_time; + + mcc_switch_channel_policy_table[mcc_policy_idx][MCC_START_TIME_OFFSET_IDX] + = new_starttime_offset; + + +} + +static void rtw_hal_config_mcc_switch_channel_setting(PADAPTER padapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *mccobjpriv = &(dvobj->mcc_objpriv); + struct registry_priv *registry_par = &padapter->registrypriv; + u8 mcc_duration = 0; + s8 mcc_policy_idx = 0; + + mcc_policy_idx = registry_par->rtw_mcc_policy_table_idx; + mcc_duration = mccobjpriv->duration; + + if (mcc_policy_idx < 0 || mcc_policy_idx >= mcc_max_policy_num) { + mccobjpriv->policy_index = 0; + RTW_INFO("[MCC] can't find table(%d), use default policy(%d)\n", + mcc_policy_idx, mccobjpriv->policy_index); + } else + mccobjpriv->policy_index = mcc_policy_idx; + + /* convert % to time */ + if (mcc_duration != 0) + rtw_hal_mcc_update_policy_table(padapter); + + RTW_INFO("[MCC] policy(%d): %d,%d,%d,%d,%d,%d\n" + , mccobjpriv->policy_index + , mcc_switch_channel_policy_table[mccobjpriv->policy_index][MCC_DURATION_IDX] + , mcc_switch_channel_policy_table[mccobjpriv->policy_index][MCC_TSF_SYNC_OFFSET_IDX] + , mcc_switch_channel_policy_table[mccobjpriv->policy_index][MCC_START_TIME_OFFSET_IDX] + , mcc_switch_channel_policy_table[mccobjpriv->policy_index][MCC_INTERVAL_IDX] + , mcc_switch_channel_policy_table[mccobjpriv->policy_index][MCC_GUARD_OFFSET0_IDX] + , mcc_switch_channel_policy_table[mccobjpriv->policy_index][MCC_GUARD_OFFSET1_IDX]); + +} + +static void rtw_hal_mcc_assign_tx_threshold(PADAPTER padapter) +{ + struct registry_priv *preg = &padapter->registrypriv; + struct mcc_adapter_priv *pmccadapriv = &padapter->mcc_adapterpriv; + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + + switch (pmccadapriv->role) { + case MCC_ROLE_STA: + case MCC_ROLE_GC: + switch (pmlmeext->cur_bwmode) { + case CHANNEL_WIDTH_20: + /* + * target tx byte(bytes) = target tx tp(Mbits/sec) * 1024 * 1024 / 8 * (duration(ms) / 1024) + * = target tx tp(Mbits/sec) * 128 * duration(ms) + * note: + * target tx tp(Mbits/sec) * 1024 * 1024 / 8 ==> Mbits to bytes + * duration(ms) / 1024 ==> msec to sec + */ + pmccadapriv->mcc_target_tx_bytes_to_port = preg->rtw_mcc_sta_bw20_target_tx_tp * 128 * pmccadapriv->mcc_duration; + break; + case CHANNEL_WIDTH_40: + pmccadapriv->mcc_target_tx_bytes_to_port = preg->rtw_mcc_sta_bw40_target_tx_tp * 128 * pmccadapriv->mcc_duration; + break; + case CHANNEL_WIDTH_80: + pmccadapriv->mcc_target_tx_bytes_to_port = preg->rtw_mcc_sta_bw80_target_tx_tp * 128 * pmccadapriv->mcc_duration; + break; + case CHANNEL_WIDTH_160: + case CHANNEL_WIDTH_80_80: + RTW_INFO(FUNC_ADPT_FMT": not support bwmode = %d\n" + , FUNC_ADPT_ARG(padapter), pmlmeext->cur_bwmode); + break; + } + break; + case MCC_ROLE_AP: + case MCC_ROLE_GO: + switch (pmlmeext->cur_bwmode) { + case CHANNEL_WIDTH_20: + pmccadapriv->mcc_target_tx_bytes_to_port = preg->rtw_mcc_ap_bw20_target_tx_tp * 128 * pmccadapriv->mcc_duration; + break; + case CHANNEL_WIDTH_40: + pmccadapriv->mcc_target_tx_bytes_to_port = preg->rtw_mcc_ap_bw40_target_tx_tp * 128 * pmccadapriv->mcc_duration; + break; + case CHANNEL_WIDTH_80: + pmccadapriv->mcc_target_tx_bytes_to_port = preg->rtw_mcc_ap_bw80_target_tx_tp * 128 * pmccadapriv->mcc_duration; + break; + case CHANNEL_WIDTH_160: + case CHANNEL_WIDTH_80_80: + RTW_INFO(FUNC_ADPT_FMT": not support bwmode = %d\n" + , FUNC_ADPT_ARG(padapter), pmlmeext->cur_bwmode); + break; + } + break; + default: + RTW_INFO(FUNC_ADPT_FMT": unknown role = %d\n" + , FUNC_ADPT_ARG(padapter), pmccadapriv->role); + break; + } +} + +#ifdef CONFIG_MCC_PHYDM_OFFLOAD +static void mcc_cfg_phdym_rf_ch (_adapter *adapter) +{ + struct mcc_adapter_priv *mccadapriv = &adapter->mcc_adapterpriv; + struct mlme_ext_priv *mlmeext = &adapter->mlmeextpriv; + HAL_DATA_TYPE *hal = GET_HAL_DATA(adapter); + struct dm_struct *dm = &hal->odmpriv; + struct _phydm_mcc_dm_ *mcc_dm = &dm->mcc_dm; + u8 order = 0; + + set_channel_bwmode(adapter, mlmeext->cur_channel, mlmeext->cur_ch_offset, mlmeext->cur_bwmode); + order = mccadapriv->order; + mcc_dm->mcc_rf_ch[order] = phy_query_rf_reg(adapter, RF_PATH_A, 0x18, 0x03ff); +} + +static void mcc_cfg_phdym_update_macid (_adapter *adapter, u8 add, u8 mac_id) +{ + struct mcc_adapter_priv *mccadapriv = &adapter->mcc_adapterpriv; + struct mlme_ext_priv *mlmeext = &adapter->mlmeextpriv; + HAL_DATA_TYPE *hal = GET_HAL_DATA(adapter); + struct dm_struct *dm = &hal->odmpriv; + struct _phydm_mcc_dm_ *mcc_dm = &dm->mcc_dm; + u8 order = 0, i = 0; + + order = mccadapriv->order; + if (add) { + for (i = 0; i < NUM_STA; i++) { + if (mcc_dm->sta_macid[order][i] == 0xff) { + mcc_dm->sta_macid[order][i] = mac_id; + break; + } + } + } else { + for (i = 0; i < NUM_STA; i++) { + if (mcc_dm->sta_macid[order][i] == mac_id) { + mcc_dm->sta_macid[order][i] = 0xff; + break; + } + } + } + + +} + +static void mcc_cfg_phdym_start(_adapter *adapter, u8 start) +{ + struct dvobj_priv *dvobj; + struct mcc_obj_priv *mccobjpriv; + HAL_DATA_TYPE *hal; + struct dm_struct *dm; + struct _phydm_mcc_dm_ *mcc_dm; + u8 rfk_forbidden = _TRUE; + u8 i = 0, j = 0; + + dvobj = adapter_to_dvobj(adapter); + mccobjpriv = adapter_to_mccobjpriv(adapter); + hal = GET_HAL_DATA(adapter); + dm = &hal->odmpriv; + mcc_dm = &dm->mcc_dm; + + if (start) { + #ifdef CONFIG_MCC_PHYDM_OFFLOAD + mcc_dm->mcc_status = mccobjpriv->mcc_phydm_offload; + #endif + + rfk_forbidden = _TRUE; + halrf_cmn_info_set(dm, HALRF_CMNINFO_RFK_FORBIDDEN, rfk_forbidden); + } else { + rfk_forbidden = _FALSE; + halrf_cmn_info_set(dm, HALRF_CMNINFO_RFK_FORBIDDEN, rfk_forbidden); + + #ifdef CONFIG_MCC_PHYDM_OFFLOAD + for(i = 0; i < MAX_MCC_NUM; i ++) { + for(j = 0; j < NUM_STA; j ++) { + if (mcc_dm->sta_macid[i][j] != 0xff) + /* clear all used value for mcc stop */ + /* do nothing for mcc start due to phydm will init to 0xff */ + mcc_dm->sta_macid[i][j] = 0xff; + } + mcc_dm->mcc_rf_ch[i] = 0xff; + } + mcc_dm->mcc_status = 0; + #endif + } +} + +static void mcc_cfg_phdym_dump(_adapter *adapter, void *sel) +{ + HAL_DATA_TYPE *hal; + struct dm_struct *dm; + struct _phydm_mcc_dm_ *mcc_dm; + u8 rfk_forbidden = _TRUE; + u8 i = 0, j = 0; + + + hal = GET_HAL_DATA(adapter); + dm = &hal->odmpriv; + mcc_dm = &dm->mcc_dm; + + rfk_forbidden = halrf_cmn_info_get(dm, HALRF_CMNINFO_RFK_FORBIDDEN); + RTW_PRINT_SEL(sel, "dump mcc dm info\n"); + RTW_PRINT_SEL(sel, "mcc_status=%d\n", mcc_dm->mcc_status); + RTW_PRINT_SEL(sel, "rfk_forbidden=%d\n", rfk_forbidden); + for(i = 0; i < MAX_MCC_NUM; i ++) { + + if (mcc_dm->mcc_rf_ch[i] != 0xff) + RTW_PRINT_SEL(sel, "mcc_dm->mcc_rf_ch[%d] = 0x%02x\n", i, mcc_dm->mcc_rf_ch[i]); + + for(j = 0; j < NUM_STA; j ++) { + if (mcc_dm->sta_macid[i][j] != 0xff) + RTW_PRINT_SEL(sel, "mcc_dm->sta_macid[%d][%d] = %d\n", i, j, mcc_dm->sta_macid[i][j]); + } + } +} + +static void mcc_cfg_phdym_offload(_adapter *adapter, u8 enable) +{ + struct mcc_obj_priv *mccobjpriv = adapter_to_mccobjpriv(adapter); + _adapter *iface = NULL; + struct mcc_adapter_priv *mccadapriv = NULL; + HAL_DATA_TYPE *hal = NULL; + struct dm_struct *dm = NULL; + struct _phydm_mcc_dm_ *mcc_dm = NULL; + struct sta_priv *stapriv = NULL; + struct sta_info *sta = NULL; + struct wlan_network *cur_network = NULL; + _irqL irqL; + _list *head = NULL, *list = NULL; + u8 i = 0; + + + hal = GET_HAL_DATA(adapter); + dm = &hal->odmpriv; + mcc_dm = &dm->mcc_dm; + + /* due to phydm will rst related date, driver must set related data */ + if (enable) { + for (i = 0; i < MAX_MCC_NUM; i++) { + iface = mccobjpriv->iface[i]; + if (!iface) + continue; + stapriv = &iface->stapriv; + mccadapriv = &iface->mcc_adapterpriv; + switch (mccadapriv->role) { + case MCC_ROLE_STA: + case MCC_ROLE_GC: + cur_network = &iface->mlmepriv.cur_network; + sta = rtw_get_stainfo(stapriv, cur_network->network.MacAddress); + if (sta) + mcc_cfg_phdym_update_macid(iface, _TRUE, sta->cmn.mac_id); + break; + case MCC_ROLE_AP: + case MCC_ROLE_GO: + _enter_critical_bh(&stapriv->asoc_list_lock, &irqL); + + head = &stapriv->asoc_list; + list = get_next(head); + + while ((rtw_end_of_queue_search(head, list)) == _FALSE) { + sta = LIST_CONTAINOR(list, struct sta_info, asoc_list); + list = get_next(list); + mcc_cfg_phdym_update_macid(iface, _TRUE, sta->cmn.mac_id); + } + + _exit_critical_bh(&stapriv->asoc_list_lock, &irqL); + break; + default: + RTW_INFO("Unknown role\n"); + rtw_warn_on(1); + break; + } + + } + } + + mcc_dm->mcc_status = enable; +} + +static void rtw_hal_mcc_cfg_phydm (_adapter *adapter, enum mcc_cfg_phydm_ops ops, void *data) +{ + switch (ops) { + case MCC_CFG_PHYDM_OFFLOAD: + mcc_cfg_phdym_offload(adapter, *(u8 *)data); + break; + case MCC_CFG_PHYDM_RF_CH: + mcc_cfg_phdym_rf_ch(adapter); + break; + case MCC_CFG_PHYDM_ADD_CLIENT: + mcc_cfg_phdym_update_macid(adapter, _TRUE, *(u8 *)data); + break; + case MCC_CFG_PHYDM_REMOVE_CLIENT: + mcc_cfg_phdym_update_macid(adapter, _FALSE, *(u8 *)data); + break; + case MCC_CFG_PHYDM_START: + mcc_cfg_phdym_start(adapter, _TRUE); + break; + case MCC_CFG_PHYDM_STOP: + mcc_cfg_phdym_start(adapter, _FALSE); + break; + case MCC_CFG_PHYDM_DUMP: + mcc_cfg_phdym_dump(adapter, data); + break; + case MCC_CFG_PHYDM_MAX: + default: + RTW_ERR("[MCC] rtw_hal_mcc_cfg_phydm ops error (%d)\n", ops); + break; + + } +} +#endif + +static void rtw_hal_config_mcc_role_setting(PADAPTER padapter, u8 order) +{ + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter); + struct mcc_obj_priv *pmccobjpriv = &(pdvobjpriv->mcc_objpriv); + struct mcc_adapter_priv *pmccadapriv = &padapter->mcc_adapterpriv; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct wlan_network *cur_network = &(pmlmepriv->cur_network); + struct sta_priv *pstapriv = &padapter->stapriv; + struct sta_info *psta = NULL; + struct registry_priv *preg = &padapter->registrypriv; + _irqL irqL; + _list *phead =NULL, *plist = NULL; + u8 policy_index = 0; + u8 mcc_duration = 0; + u8 mcc_interval = 0; + u8 starting_ap_num = DEV_AP_STARTING_NUM(pdvobjpriv); + u8 ap_num = DEV_AP_NUM(pdvobjpriv); + + policy_index = pmccobjpriv->policy_index; + mcc_duration = mcc_switch_channel_policy_table[pmccobjpriv->policy_index][MCC_DURATION_IDX] + - mcc_switch_channel_policy_table[pmccobjpriv->policy_index][MCC_GUARD_OFFSET0_IDX] + - mcc_switch_channel_policy_table[pmccobjpriv->policy_index][MCC_GUARD_OFFSET1_IDX]; + mcc_interval = mcc_switch_channel_policy_table[pmccobjpriv->policy_index][MCC_INTERVAL_IDX]; + + if (starting_ap_num == 0 && ap_num == 0) { + pmccadapriv->order = order; + + if (pmccadapriv->order == 0) { + /* setting is smiliar to GO/AP */ + /* pmccadapriv->mcc_duration = mcc_interval - mcc_duration;*/ + pmccadapriv->mgmt_queue_macid = MCC_ROLE_SOFTAP_GO_MGMT_QUEUE_MACID; + } else if (pmccadapriv->order == 1) { + /* pmccadapriv->mcc_duration = mcc_duration; */ + pmccadapriv->mgmt_queue_macid = MCC_ROLE_STA_GC_MGMT_QUEUE_MACID; + } else { + RTW_INFO("[MCC] not support >= 3 interface\n"); + rtw_warn_on(1); + } + + rtw_hal_mcc_assign_tx_threshold(padapter); + + psta = rtw_get_stainfo(pstapriv, cur_network->network.MacAddress); + if (psta) { + /* combine AP/GO macid and mgmt queue macid to bitmap */ + pmccadapriv->mcc_macid_bitmap = BIT(psta->cmn.mac_id) | BIT(pmccadapriv->mgmt_queue_macid); + #ifdef CONFIG_MCC_PHYDM_OFFLOAD + rtw_hal_mcc_cfg_phydm(padapter, MCC_CFG_PHYDM_ADD_CLIENT, &psta->cmn.mac_id); + #endif + } else { + RTW_INFO(FUNC_ADPT_FMT":AP/GO station info is NULL\n", FUNC_ADPT_ARG(padapter)); + rtw_warn_on(1); + } + } else { + /* GO/AP is 1nd order GC/STA is 2nd order */ + switch (pmccadapriv->role) { + case MCC_ROLE_STA: + case MCC_ROLE_GC: + pmccadapriv->order = 1; + pmccadapriv->mcc_duration = mcc_duration; + + rtw_hal_mcc_assign_tx_threshold(padapter); + /* assign used mac to avoid affecting RA */ + pmccadapriv->mgmt_queue_macid = MCC_ROLE_STA_GC_MGMT_QUEUE_MACID; + + psta = rtw_get_stainfo(pstapriv, cur_network->network.MacAddress); + if (psta) { + /* combine AP/GO macid and mgmt queue macid to bitmap */ + pmccadapriv->mcc_macid_bitmap = BIT(psta->cmn.mac_id) | BIT(pmccadapriv->mgmt_queue_macid); + #ifdef CONFIG_MCC_PHYDM_OFFLOAD + rtw_hal_mcc_cfg_phydm(padapter, MCC_CFG_PHYDM_ADD_CLIENT, &psta->cmn.mac_id); + #endif + } else { + RTW_INFO(FUNC_ADPT_FMT":AP/GO station info is NULL\n", FUNC_ADPT_ARG(padapter)); + rtw_warn_on(1); + } + break; + case MCC_ROLE_AP: + case MCC_ROLE_GO: + pmccadapriv->order = 0; + /* total druation value equals interval */ + pmccadapriv->mcc_duration = mcc_interval - mcc_duration; + pmccadapriv->p2p_go_noa_ie_len = 0; /* not NoA attribute at init time */ + + rtw_hal_mcc_assign_tx_threshold(padapter); + + _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL); + + phead = &pstapriv->asoc_list; + plist = get_next(phead); + pmccadapriv->mcc_macid_bitmap = 0; + + while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) { + psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list); + plist = get_next(plist); + pmccadapriv->mcc_macid_bitmap |= BIT(psta->cmn.mac_id); + #ifdef CONFIG_MCC_PHYDM_OFFLOAD + rtw_hal_mcc_cfg_phydm(padapter, MCC_CFG_PHYDM_ADD_CLIENT, &psta->cmn.mac_id); + #endif + } + + _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL); + + psta = rtw_get_bcmc_stainfo(padapter); + + if (psta != NULL) + pmccadapriv->mgmt_queue_macid = psta->cmn.mac_id; + else { + pmccadapriv->mgmt_queue_macid = MCC_ROLE_SOFTAP_GO_MGMT_QUEUE_MACID; + RTW_INFO(FUNC_ADPT_FMT":bcmc station is NULL, use macid %d\n" + , FUNC_ADPT_ARG(padapter), pmccadapriv->mgmt_queue_macid); + } + + /* combine client macid and mgmt queue macid to bitmap */ + pmccadapriv->mcc_macid_bitmap |= BIT(pmccadapriv->mgmt_queue_macid); + break; + default: + RTW_INFO("Unknown role\n"); + rtw_warn_on(1); + break; + } + + } + + /* setting Null data parameters */ + if (pmccadapriv->role == MCC_ROLE_STA) { + pmccadapriv->null_early = 3; + pmccadapriv->null_rty_num= 5; + } else if (pmccadapriv->role == MCC_ROLE_GC) { + pmccadapriv->null_early = 2; + pmccadapriv->null_rty_num= 5; + } else { + pmccadapriv->null_early = 0; + pmccadapriv->null_rty_num= 0; + } + + RTW_INFO("********* "FUNC_ADPT_FMT" *********\n", FUNC_ADPT_ARG(padapter)); + RTW_INFO("order:%d\n", pmccadapriv->order); + RTW_INFO("role:%d\n", pmccadapriv->role); + RTW_INFO("mcc duration:%d\n", pmccadapriv->mcc_duration); + RTW_INFO("null_early:%d\n", pmccadapriv->null_early); + RTW_INFO("null_rty_num:%d\n", pmccadapriv->null_rty_num); + RTW_INFO("mgmt queue macid:%d\n", pmccadapriv->mgmt_queue_macid); + RTW_INFO("bitmap:0x%02x\n", pmccadapriv->mcc_macid_bitmap); + RTW_INFO("target tx bytes:%d\n", pmccadapriv->mcc_target_tx_bytes_to_port); + RTW_INFO("**********************************\n"); + + pmccobjpriv->iface[pmccadapriv->order] = padapter; + #ifdef CONFIG_MCC_PHYDM_OFFLOAD + rtw_hal_mcc_cfg_phydm(padapter, MCC_CFG_PHYDM_RF_CH, NULL); + #endif + +} + +static void rtw_hal_mcc_rqt_tsf(PADAPTER padapter, u64 *out_tsf) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *mccobjpriv = &(dvobj->mcc_objpriv); + PADAPTER order0_iface = NULL; + PADAPTER order1_iface = NULL; + struct submit_ctx *tsf_req_sctx = NULL; + enum _hw_port tsfx = MAX_HW_PORT; + enum _hw_port tsfy = MAX_HW_PORT; + u8 cmd[H2C_MCC_RQT_TSF_LEN] = {0}; + + _enter_critical_mutex(&mccobjpriv->mcc_tsf_req_mutex, NULL); + + order0_iface = mccobjpriv->iface[0]; + order1_iface = mccobjpriv->iface[1]; + + tsf_req_sctx = &mccobjpriv->mcc_tsf_req_sctx; + rtw_sctx_init(tsf_req_sctx, MCC_EXPIRE_TIME); + mccobjpriv->mcc_tsf_req_sctx_order = 0; + tsfx = rtw_hal_get_port(order0_iface); + tsfy = rtw_hal_get_port(order1_iface); + + SET_H2CCMD_MCC_RQT_TSFX(cmd, tsfx); + SET_H2CCMD_MCC_RQT_TSFY(cmd, tsfy); + + rtw_hal_fill_h2c_cmd(padapter, H2C_MCC_RQT_TSF, H2C_MCC_RQT_TSF_LEN, cmd); + + if (!rtw_sctx_wait(tsf_req_sctx, __func__)) + RTW_INFO(FUNC_ADPT_FMT": wait for mcc tsf req C2H time out\n", FUNC_ADPT_ARG(padapter)); + + if (tsf_req_sctx->status == RTW_SCTX_DONE_SUCCESS && out_tsf != NULL) { + out_tsf[0] = order0_iface->mcc_adapterpriv.tsf; + out_tsf[1] = order1_iface->mcc_adapterpriv.tsf; + } + + + _exit_critical_mutex(&mccobjpriv->mcc_tsf_req_mutex, NULL); +} + +static u8 rtw_hal_mcc_check_start_time_is_valid(PADAPTER padapter, u8 case_num, + u32 tsfdiff, s8 *upper_bound_0, s8 *lower_bound_0, s8 *upper_bound_1, s8 *lower_bound_1) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *mccobjpriv = &(dvobj->mcc_objpriv); + u8 duration_0 = 0, duration_1 = 0; + s8 final_upper_bound = 0, final_lower_bound = 0; + u8 intersection = _FALSE; + u8 min_start_time = 5; + u8 max_start_time = 95; + + duration_0 = mccobjpriv->iface[0]->mcc_adapterpriv.mcc_duration; + duration_1 = mccobjpriv->iface[1]->mcc_adapterpriv.mcc_duration; + + switch(case_num) { + case 1: + *upper_bound_0 = tsfdiff; + *lower_bound_0 = tsfdiff - duration_1; + *upper_bound_1 = 150 - duration_1; + *lower_bound_1= 0; + break; + case 2: + *upper_bound_0 = tsfdiff + 100; + *lower_bound_0 = tsfdiff + 100 - duration_1; + *upper_bound_1 = 150 - duration_1; + *lower_bound_1= 0; + break; + case 3: + *upper_bound_0 = tsfdiff + 50; + *lower_bound_0 = tsfdiff + 50 - duration_1; + *upper_bound_1 = 150 - duration_1; + *lower_bound_1= 0; + break; + case 4: + *upper_bound_0 = tsfdiff; + *lower_bound_0 = tsfdiff - duration_1; + *upper_bound_1 = 150 - duration_1; + *lower_bound_1= 0; + break; + case 5: + *upper_bound_0 = 200 - tsfdiff; + *lower_bound_0 = 200 - tsfdiff - duration_1; + *upper_bound_1 = 150 - duration_1; + *lower_bound_1= 0; + break; + case 6: + *upper_bound_0 = tsfdiff - 50; + *lower_bound_0 = tsfdiff - 50 - duration_1; + *upper_bound_1 = 150 - duration_1; + *lower_bound_1= 0; + break; + default: + RTW_ERR("[MCC] %s: error case number(%d\n)", __func__, case_num); + } + + + /* check Intersection or not */ + if ((*lower_bound_1 >= *upper_bound_0) || + (*lower_bound_0 >= *upper_bound_1)) + intersection = _FALSE; + else + intersection = _TRUE; + + if (intersection) { + if (*upper_bound_0 > *upper_bound_1) + final_upper_bound = *upper_bound_1; + else + final_upper_bound = *upper_bound_0; + + if (*lower_bound_0 > *lower_bound_1) + final_lower_bound = *lower_bound_0; + else + final_lower_bound = *lower_bound_1; + + mccobjpriv->start_time = (final_lower_bound + final_upper_bound) / 2; + + /* check start time less than 5ms, request by Pablo@SD1 */ + if (mccobjpriv->start_time <= min_start_time) { + mccobjpriv->start_time = 6; + if (mccobjpriv->start_time < final_lower_bound && mccobjpriv->start_time > final_upper_bound) { + intersection = _FALSE; + goto exit; + } + } + + /* check start time less than 95ms */ + if (mccobjpriv->start_time >= max_start_time) { + mccobjpriv->start_time = 90; + if (mccobjpriv->start_time < final_lower_bound && mccobjpriv->start_time > final_upper_bound) { + intersection = _FALSE; + goto exit; + } + } + } + +exit: + return intersection; +} + +static void rtw_hal_mcc_decide_duration(PADAPTER padapter) +{ + struct registry_priv *registry_par = &padapter->registrypriv; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *mccobjpriv = &(dvobj->mcc_objpriv); + struct mcc_adapter_priv *mccadapriv = NULL, *mccadapriv_order0 = NULL, *mccadapriv_order1 = NULL; + _adapter *iface = NULL, *iface_order0 = NULL, *iface_order1 = NULL; + u8 duration = 0, i = 0, duration_time; + u8 mcc_interval = 150; + + iface_order0 = mccobjpriv->iface[0]; + iface_order1 = mccobjpriv->iface[1]; + mccadapriv_order0 = &iface_order0->mcc_adapterpriv; + mccadapriv_order1 = &iface_order1->mcc_adapterpriv; + + if (mccobjpriv->duration == 0) { + /* default */ + duration = 30;/*(%)*/ + RTW_INFO("%s: mccobjpriv->duration=0, use default value(%d)\n", + __FUNCTION__, duration); + } else { + duration = mccobjpriv->duration;/*(%)*/ + RTW_INFO("%s: mccobjpriv->duration=%d\n", + __FUNCTION__, duration); + } + + mccobjpriv->interval = mcc_interval; + mccobjpriv->mcc_stop_threshold = 2000 * 4 / 300 - 6; + /* convert % to ms, for primary adapter */ + duration_time = mccobjpriv->interval * duration / 100; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + + if (!iface) + continue; + + mccadapriv = &iface->mcc_adapterpriv; + if (mccadapriv->role == MCC_ROLE_MAX) + continue; + + if (is_primary_adapter(iface)) + mccadapriv->mcc_duration = duration_time; + else + mccadapriv->mcc_duration = mccobjpriv->interval - duration_time; + } + + RTW_INFO("[MCC]" FUNC_ADPT_FMT " order 0 duration=%d\n", FUNC_ADPT_ARG(iface_order0), mccadapriv_order0->mcc_duration); + RTW_INFO("[MCC]" FUNC_ADPT_FMT " order 1 duration=%d\n", FUNC_ADPT_ARG(iface_order1), mccadapriv_order1->mcc_duration); +} + +static u8 rtw_hal_mcc_update_timing_parameters(PADAPTER padapter, u8 force_update) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + u8 need_update = _FALSE; + u8 starting_ap_num = DEV_AP_STARTING_NUM(dvobj); + u8 ap_num = DEV_AP_NUM(dvobj); + + + /* for STA+STA, modify policy table */ + if (starting_ap_num == 0 && ap_num == 0) { + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *pmccobjpriv = &(dvobj->mcc_objpriv); + struct mcc_adapter_priv *pmccadapriv = NULL; + _adapter *iface = NULL; + u64 tsf[MAX_MCC_NUM] = {0}; + u64 tsf0 = 0, tsf1 = 0; + u32 beaconperiod_0 = 0, beaconperiod_1 = 0, tsfdiff = 0; + s8 upper_bound_0 = 0, lower_bound_0 = 0; + s8 upper_bound_1 = 0, lower_bound_1 = 0; + u8 valid = _FALSE; + u8 case_num = 1; + u8 i = 0; + + /* query TSF */ + rtw_hal_mcc_rqt_tsf(padapter, tsf); + + /* selecet policy table according TSF diff */ + tsf0 = tsf[0]; + beaconperiod_0 = pmccobjpriv->iface[0]->mlmepriv.cur_network.network.Configuration.BeaconPeriod; + tsf0 = rtw_modular64(tsf0, (beaconperiod_0 * TU)); + + tsf1 = tsf[1]; + beaconperiod_1 = pmccobjpriv->iface[1]->mlmepriv.cur_network.network.Configuration.BeaconPeriod; + tsf1 = rtw_modular64(tsf1, (beaconperiod_1 * TU)); + + if (tsf0 > tsf1) + tsfdiff = tsf0- tsf1; + else + tsfdiff = (tsf0 + beaconperiod_0 * TU) - tsf1; + + /* convert to ms */ + tsfdiff = (tsfdiff / TU); + + /* force update*/ + if (force_update) { + RTW_INFO("orig TSF0:%lld, orig TSF1:%lld\n", + pmccobjpriv->iface[0]->mcc_adapterpriv.tsf, pmccobjpriv->iface[1]->mcc_adapterpriv.tsf); + RTW_INFO("tsf0:%lld, tsf1:%lld\n", tsf0, tsf1); + RTW_INFO("%s: force=%d, last_tsfdiff=%d, tsfdiff=%d, THRESHOLD=%d\n", + __func__, force_update, pmccobjpriv->last_tsfdiff, tsfdiff, MCC_UPDATE_PARAMETER_THRESHOLD); + pmccobjpriv->last_tsfdiff = tsfdiff; + need_update = _TRUE; + } else { + if (pmccobjpriv->last_tsfdiff > tsfdiff) { + /* last tsfdiff - current tsfdiff > THRESHOLD, update parameters */ + if (pmccobjpriv->last_tsfdiff > (tsfdiff + MCC_UPDATE_PARAMETER_THRESHOLD)) { + RTW_INFO("orig TSF0:%lld, orig TSF1:%lld\n", + pmccobjpriv->iface[0]->mcc_adapterpriv.tsf, pmccobjpriv->iface[1]->mcc_adapterpriv.tsf); + RTW_INFO("tsf0:%lld, tsf1:%lld\n", tsf0, tsf1); + RTW_INFO("%s: force=%d, last_tsfdiff=%d, tsfdiff=%d, THRESHOLD=%d\n", + __func__, force_update, pmccobjpriv->last_tsfdiff, tsfdiff, MCC_UPDATE_PARAMETER_THRESHOLD); + + pmccobjpriv->last_tsfdiff = tsfdiff; + need_update = _TRUE; + } else { + need_update = _FALSE; + } + } else if (tsfdiff > pmccobjpriv->last_tsfdiff){ + /* current tsfdiff - last tsfdiff > THRESHOLD, update parameters */ + if (tsfdiff > (pmccobjpriv->last_tsfdiff + MCC_UPDATE_PARAMETER_THRESHOLD)) { + RTW_INFO("orig TSF0:%lld, orig TSF1:%lld\n", + pmccobjpriv->iface[0]->mcc_adapterpriv.tsf, pmccobjpriv->iface[1]->mcc_adapterpriv.tsf); + RTW_INFO("tsf0:%lld, tsf1:%lld\n", tsf0, tsf1); + RTW_INFO("%s: force=%d, last_tsfdiff=%d, tsfdiff=%d, THRESHOLD=%d\n", + __func__, force_update, pmccobjpriv->last_tsfdiff, tsfdiff, MCC_UPDATE_PARAMETER_THRESHOLD); + + pmccobjpriv->last_tsfdiff = tsfdiff; + need_update = _TRUE; + } else { + need_update = _FALSE; + } + } else { + need_update = _FALSE; + } + } + + if (need_update == _FALSE) + goto exit; + + rtw_hal_mcc_decide_duration(padapter); + + if (tsfdiff <= 50) { + + /* RX TBTT 0 */ + case_num = 1; + valid = rtw_hal_mcc_check_start_time_is_valid(padapter, case_num, tsfdiff, + &upper_bound_0, &lower_bound_0, &upper_bound_1, &lower_bound_1); + + if (valid) + goto valid_result; + + /* RX TBTT 1 */ + case_num = 2; + valid = rtw_hal_mcc_check_start_time_is_valid(padapter, case_num, tsfdiff, + &upper_bound_0, &lower_bound_0, &upper_bound_1, &lower_bound_1); + + if (valid) + goto valid_result; + + /* RX TBTT 2 */ + case_num = 3; + valid = rtw_hal_mcc_check_start_time_is_valid(padapter, case_num, tsfdiff, + &upper_bound_0, &lower_bound_0, &upper_bound_1, &lower_bound_1); + + if (valid) + goto valid_result; + + if (valid == _FALSE) { + RTW_INFO("[MCC] do not find fit start time\n"); + RTW_INFO("[MCC] tsfdiff:%d, duration:%d(%c), interval:%d\n", + tsfdiff, pmccobjpriv->duration, 37, pmccobjpriv->interval); + + } + + } else { + + /* RX TBTT 0 */ + case_num = 4; + valid = rtw_hal_mcc_check_start_time_is_valid(padapter, case_num, tsfdiff, + &upper_bound_0, &lower_bound_0, &upper_bound_1, &lower_bound_1); + + if (valid) + goto valid_result; + + + /* RX TBTT 1 */ + case_num = 5; + valid = rtw_hal_mcc_check_start_time_is_valid(padapter, case_num, tsfdiff, + &upper_bound_0, &lower_bound_0, &upper_bound_1, &lower_bound_1); + + if (valid) + goto valid_result; + + + /* RX TBTT 2 */ + case_num = 6; + valid = rtw_hal_mcc_check_start_time_is_valid(padapter, case_num, tsfdiff, + &upper_bound_0, &lower_bound_0, &upper_bound_1, &lower_bound_1); + + if (valid) + goto valid_result; + + if (valid == _FALSE) { + RTW_INFO("[MCC] do not find fit start time\n"); + RTW_INFO("[MCC] tsfdiff:%d, duration:%d(%c), interval:%d\n", + tsfdiff, pmccobjpriv->duration, 37, pmccobjpriv->interval); + } + } + + + + valid_result: + RTW_INFO("********************\n"); + RTW_INFO("%s: case_num:%d, start time:%d\n", + __func__, case_num, pmccobjpriv->start_time); + RTW_INFO("%s: upper_bound_0:%d, lower_bound_0:%d\n", + __func__, upper_bound_0, lower_bound_0); + RTW_INFO("%s: upper_bound_1:%d, lower_bound_1:%d\n", + __func__, upper_bound_1, lower_bound_1); + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (iface == NULL) + continue; + + pmccadapriv = &iface->mcc_adapterpriv; + pmccadapriv = &iface->mcc_adapterpriv; + if (pmccadapriv->role == MCC_ROLE_MAX) + continue; +#if 0 + if (pmccadapriv->order == 0) { + pmccadapriv->mcc_duration = mcc_duration; + } else if (pmccadapriv->order == 1) { + pmccadapriv->mcc_duration = mcc_interval - mcc_duration; + } else { + RTW_INFO("[MCC] not support >= 3 interface\n"); + rtw_warn_on(1); + } +#endif + RTW_INFO("********************\n"); + RTW_INFO(FUNC_ADPT_FMT": order:%d, role:%d\n", + FUNC_ADPT_ARG(iface), pmccadapriv->order, pmccadapriv->role); + RTW_INFO(FUNC_ADPT_FMT": mcc duration:%d, target tx bytes:%d\n", + FUNC_ADPT_ARG(iface), pmccadapriv->mcc_duration, pmccadapriv->mcc_target_tx_bytes_to_port); + RTW_INFO(FUNC_ADPT_FMT": mgmt queue macid:%d, bitmap:0x%02x\n", + FUNC_ADPT_ARG(iface), pmccadapriv->mgmt_queue_macid, pmccadapriv->mcc_macid_bitmap); + RTW_INFO("********************\n"); + } + + } +exit: + return need_update; +} + +static u8 rtw_hal_decide_mcc_role(PADAPTER padapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + _adapter *iface = NULL; + struct mcc_adapter_priv *pmccadapriv = NULL; + struct wifidirect_info *pwdinfo = NULL; + struct mlme_priv *pmlmepriv = NULL; + u8 ret = _SUCCESS, i = 0; + u8 order = 1; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (iface == NULL) + continue; + + pmccadapriv = &iface->mcc_adapterpriv; + pwdinfo = &iface->wdinfo; + + if (MLME_IS_GO(iface)) + pmccadapriv->role = MCC_ROLE_GO; + else if (MLME_IS_AP(iface)) + pmccadapriv->role = MCC_ROLE_AP; + else if (MLME_IS_GC(iface)) + pmccadapriv->role = MCC_ROLE_GC; + else if (MLME_IS_STA(iface)) { + if (MLME_IS_LINKING(iface) || MLME_IS_ASOC(iface)) + pmccadapriv->role = MCC_ROLE_STA; + else { + /* bypass non-linked/non-linking interface */ + RTW_INFO(FUNC_ADPT_FMT" mlme state:0x%2x\n", + FUNC_ADPT_ARG(iface), MLME_STATE(iface)); + continue; + } + } else { + /* bypass non-linked/non-linking interface */ + RTW_INFO(FUNC_ADPT_FMT" P2P Role:%d, mlme state:0x%2x\n", + FUNC_ADPT_ARG(iface), pwdinfo->role, MLME_STATE(iface)); + continue; + } + + if (padapter == iface) { + /* current adapter is order 0 */ + rtw_hal_config_mcc_role_setting(iface, 0); + } else { + rtw_hal_config_mcc_role_setting(iface, order); + order ++; + } + } + + rtw_hal_mcc_update_timing_parameters(padapter, _TRUE); + + return ret; +} + +static void rtw_hal_construct_CTS(PADAPTER padapter, u8 *pframe, u32 *pLength) +{ + u8 broadcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + + /* frame type, length = 1*/ + set_frame_sub_type(pframe, WIFI_RTS); + + /* frame control flag, length = 1 */ + *(pframe + 1) = 0; + + /* frame duration, length = 2 */ + *(pframe + 2) = 0x00; + *(pframe + 3) = 0x78; + + /* frame recvaddr, length = 6 */ + _rtw_memcpy((pframe + 4), broadcast_addr, ETH_ALEN); + _rtw_memcpy((pframe + 4 + ETH_ALEN), adapter_mac_addr(padapter), ETH_ALEN); + _rtw_memcpy((pframe + 4 + ETH_ALEN*2), adapter_mac_addr(padapter), ETH_ALEN); + *pLength = 22; +} + +/* avoid wrong information for power limit */ +void rtw_hal_mcc_upadate_chnl_bw(_adapter *padapter, u8 ch, u8 ch_offset, u8 bw, u8 print) +{ + + u8 center_ch, chnl_offset80 = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + PHAL_DATA_TYPE hal = GET_HAL_DATA(padapter); + u8 cch_160, cch_80, cch_40, cch_20; + + center_ch = rtw_get_center_ch(ch, bw, ch_offset); + + if (bw == CHANNEL_WIDTH_80) { + if (center_ch > ch) + chnl_offset80 = HAL_PRIME_CHNL_OFFSET_LOWER; + else if (center_ch < ch) + chnl_offset80 = HAL_PRIME_CHNL_OFFSET_UPPER; + else + chnl_offset80 = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + } + + /* set Channel */ + /* saved channel/bw info */ + rtw_set_oper_ch(padapter, ch); + rtw_set_oper_bw(padapter, bw); + rtw_set_oper_choffset(padapter, ch_offset); + + cch_80 = bw == CHANNEL_WIDTH_80 ? center_ch : 0; + cch_40 = bw == CHANNEL_WIDTH_40 ? center_ch : 0; + cch_20 = bw == CHANNEL_WIDTH_20 ? center_ch : 0; + + if (cch_80 != 0) + cch_40 = rtw_get_scch_by_cch_offset(cch_80, CHANNEL_WIDTH_80, chnl_offset80); + if (cch_40 != 0) + cch_20 = rtw_get_scch_by_cch_offset(cch_40, CHANNEL_WIDTH_40, ch_offset); + + + hal->cch_80 = cch_80; + hal->cch_40 = cch_40; + hal->cch_20 = cch_20; + hal->current_channel = center_ch; + hal->CurrentCenterFrequencyIndex1 = center_ch; + hal->current_channel_bw = bw; + hal->nCur40MhzPrimeSC = ch_offset; + hal->nCur80MhzPrimeSC = chnl_offset80; + hal->current_band_type = ch > 14 ? BAND_ON_5G:BAND_ON_2_4G; + + if (print) { + RTW_INFO(FUNC_ADPT_FMT" cch:%u, %s, offset40:%u, offset80:%u (%u, %u, %u), band:%s\n" + , FUNC_ADPT_ARG(padapter), center_ch, ch_width_str(bw) + , ch_offset, chnl_offset80 + , hal->cch_80, hal->cch_40, hal->cch_20 + , band_str(hal->current_band_type)); + } +} + +u8 rtw_hal_dl_mcc_fw_rsvd_page(_adapter *adapter, u8 *pframe, u16 *index, + u8 tx_desc, u32 page_size, u8 *total_page_num, RSVDPAGE_LOC *rsvd_page_loc, u8 *page_num) +{ + u32 len = 0; + _adapter *iface = NULL; + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mcc_obj_priv *pmccobjpriv = &(dvobj->mcc_objpriv); + struct mlme_ext_info *pmlmeinfo = NULL; + struct mlme_ext_priv *pmlmeext = NULL; + struct hal_com_data *hal = GET_HAL_DATA(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + struct mcc_adapter_priv *mccadapriv = NULL; +#if defined(CONFIG_RTL8822C) + struct dm_struct *phydm = adapter_to_phydm(adapter); + struct txagc_table_8822c tab; + u8 agc_buff[2][NUM_RATE_AC_2SS]; /* tatol 0x40 rate index for PATH A/B */ +#endif + + u8 ret = _SUCCESS, i = 0, j =0, order = 0, CurtPktPageNum = 0; + u8 *start = NULL; + u8 path = RF_PATH_A; + + if (page_num) { +#ifdef CONFIG_MCC_MODE_V2 + if (!hal->RegIQKFWOffload) + RTW_WARN("[MCC] must enable FW IQK for New IC\n"); +#endif /* CONFIG_MCC_MODE_V2 */ + *total_page_num += (2 * MAX_MCC_NUM+ 1); + RTW_INFO("[MCC] allocate mcc rsvd page num = %d\n", *total_page_num); + goto exit; + } + + /* check proccess mcc start setting */ + if (!rtw_hal_check_mcc_status(adapter, MCC_STATUS_PROCESS_MCC_START_SETTING)) { + ret = _FAIL; + goto exit; + } + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (iface == NULL) + continue; + + mccadapriv = &iface->mcc_adapterpriv; + if (mccadapriv->role == MCC_ROLE_MAX) + continue; + + order = mccadapriv->order; + pmccobjpriv->mcc_loc_rsvd_paga[order] = *total_page_num; + + switch (mccadapriv->role) { + case MCC_ROLE_STA: + case MCC_ROLE_GC: + /* Build NULL DATA */ + RTW_INFO("LocNull(order:%d): %d\n" + , order, pmccobjpriv->mcc_loc_rsvd_paga[order]); + len = 0; + + rtw_hal_construct_NullFunctionData(iface + , &pframe[*index], &len, _FALSE, 0, 0, _FALSE); + rtw_hal_fill_fake_txdesc(iface, &pframe[*index-tx_desc], + len, _FALSE, _FALSE, _FALSE); + + CurtPktPageNum = (u8)PageNum(tx_desc + len, page_size); + *total_page_num += CurtPktPageNum; + *index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("LocNull", CurtPktPageNum, *total_page_num, *index); + break; + case MCC_ROLE_AP: + /* Bulid CTS */ + RTW_INFO("LocCTS(order:%d): %d\n" + , order, pmccobjpriv->mcc_loc_rsvd_paga[order]); + + len = 0; + rtw_hal_construct_CTS(iface, &pframe[*index], &len); + rtw_hal_fill_fake_txdesc(iface, &pframe[*index-tx_desc], + len, _FALSE, _FALSE, _FALSE); + + CurtPktPageNum = (u8)PageNum(tx_desc + len, page_size); + *total_page_num += CurtPktPageNum; + *index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("LocCTS", CurtPktPageNum, *total_page_num, *index); + break; + case MCC_ROLE_GO: + /* To DO */ + break; + default: + RTW_INFO(FUNC_ADPT_FMT": unknown role = %d\n" + , FUNC_ADPT_ARG(iface), mccadapriv->role); + break; + } + } + + for (i = 0; i < MAX_MCC_NUM; i++) { + u8 center_ch = 0, ch = 0, bw = 0, bw_offset = 0; + BAND_TYPE band = BAND_MAX; + u8 power_index = 0; + u8 rate_array_sz = 0; + u8 *rates = NULL; + u8 rate = 0; + u8 shift = 0; + u32 power_index_4bytes = 0; + u8 total_rate = 0; + u8 *total_rate_offset = NULL; + + iface = pmccobjpriv->iface[i]; + pmlmeext = &iface->mlmeextpriv; + ch = pmlmeext->cur_channel; + bw = pmlmeext->cur_bwmode; + bw_offset = pmlmeext->cur_ch_offset; + center_ch = rtw_get_center_ch(ch, bw, bw_offset); + band = center_ch <= 14 ? BAND_ON_2_4G : BAND_ON_5G; + rtw_hal_mcc_upadate_chnl_bw(iface, ch, bw_offset, bw, _TRUE); + + start = &pframe[*index - tx_desc]; + _rtw_memset(start, 0, page_size); + pmccobjpriv->mcc_pwr_idx_rsvd_page[i] = *total_page_num; + RTW_INFO(ADPT_FMT" order:%d, pwr_idx_rsvd_page location[%d]: %d\n", + ADPT_ARG(iface), mccadapriv->order, + i, pmccobjpriv->mcc_pwr_idx_rsvd_page[i]); + + total_rate_offset = start; +#if !defined(CONFIG_RTL8822C) + for (path = RF_PATH_A; path < hal_spec->rf_reg_path_num; ++path) { + total_rate = 0; + /* PATH A for 0~63 byte, PATH B for 64~127 byte*/ + if (path == RF_PATH_A) + start = total_rate_offset + 1; + else if (path == RF_PATH_B) + start = total_rate_offset + 64; + else { + RTW_INFO("[MCC] %s: unknow RF PATH(%d)\n", __func__, path); + break; + } + + /* CCK */ + if (ch <= 14) { + rate_array_sz = rates_by_sections[CCK].rate_num; + rates = rates_by_sections[CCK].rates; + for (j = 0; j < rate_array_sz; ++j) { + power_index = phy_get_tx_power_index_ex(iface, path, CCK, rates[j], bw, band, center_ch, ch); + rate = phy_get_rate_idx_of_txpwr_by_rate(rates[j]); + + shift = rate % 4; + if (shift == 0) { + *start = rate; + start++; + total_rate++; + + #ifdef DBG_PWR_IDX_RSVD_PAGE + RTW_INFO("TXPWR("ADPT_FMT"): [%c][%s]ch:%u, %s, pwr_idx:%u\n", + ADPT_ARG(iface), rf_path_char(path), ch_width_str(bw), + center_ch, MGN_RATE_STR(rates[j]), power_index); + #endif + } + + *start = power_index; + start++; + + #ifdef DBG_PWR_IDX_RSVD_PAGE + RTW_INFO("TXPWR("ADPT_FMT"): [%c][%s]ch:%u, %s, pwr_idx:%u\n", + ADPT_ARG(iface), rf_path_char(path), ch_width_str(bw), + center_ch, MGN_RATE_STR(rates[j]), power_index); + + + shift = rate % 4; + power_index_4bytes |= ((power_index & 0xff) << (shift * 8)); + if (shift == 3) { + rate = rate - 3; + RTW_INFO("(index:0x%02x, rfpath:%d, rate:0x%02x)\n", index, path, rate); + power_index_4bytes = 0; + total_rate++; + } + #endif + + } + } + + /* OFDM */ + rate_array_sz = rates_by_sections[OFDM].rate_num; + rates = rates_by_sections[OFDM].rates; + for (j = 0; j < rate_array_sz; ++j) { + power_index = phy_get_tx_power_index_ex(iface, path, OFDM, rates[j], bw, band, center_ch, ch); + rate = phy_get_rate_idx_of_txpwr_by_rate(rates[j]); + + shift = rate % 4; + if (shift == 0) { + *start = rate; + start++; + total_rate++; + + #ifdef DBG_PWR_IDX_RSVD_PAGE + RTW_INFO("TXPWR("ADPT_FMT"): [%c][%s]ch:%u, %s, pwr_idx:%u\n", + ADPT_ARG(iface), rf_path_char(path), ch_width_str(bw), + center_ch, MGN_RATE_STR(rates[j]), power_index); + #endif + + } + + *start = power_index; + start++; + + #ifdef DBG_PWR_IDX_RSVD_PAGE + RTW_INFO("TXPWR("ADPT_FMT"): [%c][%s]ch:%u, %s, pwr_idx:%u\n", + ADPT_ARG(iface), rf_path_char(path), ch_width_str(bw), + center_ch, MGN_RATE_STR(rates[j]), power_index); + + shift = rate % 4; + power_index_4bytes |= ((power_index & 0xff) << (shift * 8)); + if (shift == 3) { + rate = rate - 3; + RTW_INFO("(index:0x%02x, rfpath:%d, rate:0x%02x)\n", index, path, rate); + power_index_4bytes = 0; + total_rate++; + } + #endif + } + + /* HT_MCS0_MCS7 */ + rate_array_sz = rates_by_sections[HT_MCS0_MCS7].rate_num; + rates = rates_by_sections[HT_MCS0_MCS7].rates; + for (j = 0; j < rate_array_sz; ++j) { + power_index = phy_get_tx_power_index_ex(iface, path, HT_1SS, rates[j], bw, band, center_ch, ch); + rate = phy_get_rate_idx_of_txpwr_by_rate(rates[j]); + + shift = rate % 4; + if (shift == 0) { + *start = rate; + start++; + total_rate++; + + #ifdef DBG_PWR_IDX_RSVD_PAGE + RTW_INFO("TXPWR("ADPT_FMT"): [%c][%s]ch:%u, %s, pwr_idx:%u\n", + ADPT_ARG(iface), rf_path_char(path), ch_width_str(bw), + center_ch, MGN_RATE_STR(rates[j]), power_index); + #endif + + } + + *start = power_index; + start++; + + #ifdef DBG_PWR_IDX_RSVD_PAGE + RTW_INFO("TXPWR("ADPT_FMT"): [%c][%s]ch:%u, %s, pwr_idx:%u\n", + ADPT_ARG(iface), rf_path_char(path), ch_width_str(bw), + center_ch, MGN_RATE_STR(rates[j]), power_index); + + shift = rate % 4; + power_index_4bytes |= ((power_index & 0xff) << (shift * 8)); + if (shift == 3) { + rate = rate - 3; + RTW_INFO("(index:0x%02x, rfpath:%d, rate:0x%02x)\n", index, path, rate); + power_index_4bytes = 0; + total_rate++; + } + #endif + } + + /* HT_MCS8_MCS15 */ + rate_array_sz = rates_by_sections[HT_MCS8_MCS15].rate_num; + rates = rates_by_sections[HT_MCS8_MCS15].rates; + for (j = 0; j < rate_array_sz; ++j) { + power_index = phy_get_tx_power_index_ex(iface, path, HT_2SS, rates[j], bw, band, center_ch, ch); + rate = phy_get_rate_idx_of_txpwr_by_rate(rates[j]); + + shift = rate % 4; + if (shift == 0) { + *start = rate; + start++; + total_rate++; + + #ifdef DBG_PWR_IDX_RSVD_PAGE + RTW_INFO("TXPWR("ADPT_FMT"): [%c][%s]ch:%u, %s, pwr_idx:%u\n", + ADPT_ARG(iface), rf_path_char(path), ch_width_str(bw), + center_ch, MGN_RATE_STR(rates[j]), power_index); + #endif + } + + *start = power_index; + start++; + + #ifdef DBG_PWR_IDX_RSVD_PAGE + RTW_INFO("TXPWR("ADPT_FMT"): [%c][%s]ch:%u, %s, pwr_idx:%u\n", + ADPT_ARG(iface), rf_path_char(path), ch_width_str(bw), + center_ch, MGN_RATE_STR(rates[j]), power_index); + + shift = rate % 4; + power_index_4bytes |= ((power_index & 0xff) << (shift * 8)); + if (shift == 3) { + rate = rate - 3; + RTW_INFO("(index:0x%02x, rfpath:%d, rate:0x%02x)\n", index, path, rate); + power_index_4bytes = 0; + total_rate++; + } + #endif + } + + /* VHT_1SSMCS0_1SSMCS9 */ + rate_array_sz = rates_by_sections[VHT_1SSMCS0_1SSMCS9].rate_num; + rates = rates_by_sections[VHT_1SSMCS0_1SSMCS9].rates; + for (j = 0; j < rate_array_sz; ++j) { + power_index = phy_get_tx_power_index_ex(iface, path, VHT_1SS, rates[j], bw, band, center_ch, ch); + rate = phy_get_rate_idx_of_txpwr_by_rate(rates[j]); + + shift = rate % 4; + if (shift == 0) { + *start = rate; + start++; + total_rate++; + #ifdef DBG_PWR_IDX_RSVD_PAGE + RTW_INFO("TXPWR("ADPT_FMT"): [%c][%s]ch:%u, %s, pwr_idx:0x%02x\n", + ADPT_ARG(iface), rf_path_char(path), ch_width_str(bw), + center_ch, MGN_RATE_STR(rates[j]), power_index); + #endif + } + *start = power_index; + start++; + #ifdef DBG_PWR_IDX_RSVD_PAGE + RTW_INFO("TXPWR("ADPT_FMT"): [%c][%s]ch:%u, %s, pwr_idx:%u\n", + ADPT_ARG(iface), rf_path_char(path), ch_width_str(bw), + center_ch, MGN_RATE_STR(rates[j]), power_index); + + shift = rate % 4; + power_index_4bytes |= ((power_index & 0xff) << (shift * 8)); + if (shift == 3) { + rate = rate - 3; + RTW_INFO("(index:0x%02x, rfpath:%d, rate:0x%02x)\n", index, path, rate); + power_index_4bytes = 0; + total_rate++; + } + #endif + } + + /* VHT_2SSMCS0_2SSMCS9 */ + rate_array_sz = rates_by_sections[VHT_2SSMCS0_2SSMCS9].rate_num; + rates = rates_by_sections[VHT_2SSMCS0_2SSMCS9].rates; + for (j = 0; j < rate_array_sz; ++j) { + power_index = phy_get_tx_power_index_ex(iface, path, VHT_2SS, rates[j], bw, band, center_ch, ch); + rate = phy_get_rate_idx_of_txpwr_by_rate(rates[j]); + + shift = rate % 4; + if (shift == 0) { + *start = rate; + start++; + total_rate++; + #ifdef DBG_PWR_IDX_RSVD_PAGE + RTW_INFO("TXPWR("ADPT_FMT"): [%c][%s]ch:%u, %s, pwr_idx:%u\n", + ADPT_ARG(iface), rf_path_char(path), ch_width_str(bw), + center_ch, MGN_RATE_STR(rates[j]), power_index); + #endif + } + *start = power_index; + start++; + #ifdef DBG_PWR_IDX_RSVD_PAGE + RTW_INFO("TXPWR("ADPT_FMT"): [%c][%s]ch:%u, %s, pwr_idx:%u\n", + ADPT_ARG(iface), rf_path_char(path), ch_width_str(bw), + center_ch, MGN_RATE_STR(rates[j]), power_index); + + shift = rate % 4; + power_index_4bytes |= ((power_index & 0xff) << (shift * 8)); + if (shift == 3) { + rate = rate - 3; + RTW_INFO("(index:0x%02x, rfpath:%d, rate:0x%02x)\n", index, path, rate); + power_index_4bytes = 0; + total_rate++; + } + #endif + } + + } + /* total rate store in offset 0 */ + *total_rate_offset = total_rate; + +#ifdef DBG_PWR_IDX_RSVD_PAGE + RTW_INFO("total_rate=%d\n", total_rate); + RTW_INFO(" ======================="ADPT_FMT"===========================\n", ADPT_ARG(iface)); + RTW_INFO_DUMP("\n", total_rate_offset, 128); + RTW_INFO(" ==================================================\n"); +#endif + + CurtPktPageNum = 1; + *total_page_num += CurtPktPageNum; + *index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("mcc_pwr_idx_rsvd_page", CurtPktPageNum, *total_page_num, *index); +#else /* 8822C */ + for (path = RF_PATH_A; path < hal_spec->rf_reg_path_num; ++path) { + /* CCK */ + if (ch <= 14) { + rate_array_sz = rates_by_sections[CCK].rate_num; + rates = rates_by_sections[CCK].rates; + for (j = 0; j < rate_array_sz; ++j) { + power_index = phy_get_tx_power_index_ex(iface, path, CCK, rates[j], bw, band, center_ch, ch); + rate = phy_get_rate_idx_of_txpwr_by_rate(rates[j]); + agc_buff[path][rate] = power_index; + } + } + + /* OFDM */ + rate_array_sz = rates_by_sections[OFDM].rate_num; + rates = rates_by_sections[OFDM].rates; + for (j = 0; j < rate_array_sz; ++j) { + power_index = phy_get_tx_power_index_ex(iface, path, OFDM, rates[j], bw, band, center_ch, ch); + rate = phy_get_rate_idx_of_txpwr_by_rate(rates[j]); + agc_buff[path][rate] = power_index; + } + /* HT */ + rate_array_sz = rates_by_sections[HT_MCS0_MCS7].rate_num; + rates = rates_by_sections[HT_MCS0_MCS7].rates; + for (j = 0; j < rate_array_sz; ++j) { + power_index = phy_get_tx_power_index_ex(iface, path, HT_1SS, rates[j], bw, band, center_ch, ch); + rate = phy_get_rate_idx_of_txpwr_by_rate(rates[j]); + agc_buff[path][rate] = power_index; + } + + rate_array_sz = rates_by_sections[HT_MCS8_MCS15].rate_num; + rates = rates_by_sections[HT_MCS8_MCS15].rates; + for (j = 0; j < rate_array_sz; ++j) { + power_index = phy_get_tx_power_index_ex(iface, path, HT_2SS, rates[j], bw, band, center_ch, ch); + rate = phy_get_rate_idx_of_txpwr_by_rate(rates[j]); + agc_buff[path][rate] = power_index; + } + /* VHT */ + rate_array_sz = rates_by_sections[VHT_1SSMCS0_1SSMCS9].rate_num; + rates = rates_by_sections[VHT_1SSMCS0_1SSMCS9].rates; + for (j = 0; j < rate_array_sz; ++j) { + power_index = phy_get_tx_power_index_ex(iface, path, VHT_1SS, rates[j], bw, band, center_ch, ch); + rate = phy_get_rate_idx_of_txpwr_by_rate(rates[j]); + agc_buff[path][rate] = power_index; + } + + rate_array_sz = rates_by_sections[VHT_2SSMCS0_2SSMCS9].rate_num; + rates = rates_by_sections[VHT_2SSMCS0_2SSMCS9].rates; + for (j = 0; j < rate_array_sz; ++j) { + power_index = phy_get_tx_power_index_ex(iface, path, VHT_2SS, rates[j], bw, band, center_ch, ch); + rate = phy_get_rate_idx_of_txpwr_by_rate(rates[j]); + agc_buff[path][rate] = power_index; + } + } + phydm_get_txagc_ref_and_diff_8822c(phydm, agc_buff, NUM_RATE_AC_2SS, &tab); + *start = tab.ref_pow_cck[0]; + start++; + *start = tab.ref_pow_cck[1]; + start++; + *start = tab.ref_pow_ofdm[0]; + start++; + *start = tab.ref_pow_ofdm[1]; + start++; + _rtw_memcpy(start, tab.diff_t, sizeof(tab.diff_t)); + CurtPktPageNum = 1; + *total_page_num += CurtPktPageNum; + *index += (CurtPktPageNum * page_size); + RSVD_PAGE_CFG("mcc_pwr_idx_rsvd_page", CurtPktPageNum, *total_page_num, *index); + #ifdef DBG_PWR_IDX_RSVD_PAGE + if (1) { + u8 path_idx; + for (path_idx = 0; path_idx < 2; path_idx++) { + for (j = 0; j < NUM_RATE_AC_2SS; j++) + RTW_INFO("agc_buff[%d][%d]=%x\n", i, j, agc_buff[i][j]); + } + RTW_INFO("tab->ref_pow_cck[0]=%x\n", tab.ref_pow_cck[0]); + RTW_INFO("tab->ref_pow_cck[1]=%x\n", tab.ref_pow_cck[1]); + RTW_INFO("tab->ref_pow_ofdm[0]=%x\n", tab.ref_pow_ofdm[0]); + RTW_INFO("tab->ref_pow_ofdm[1]=%x\n", tab.ref_pow_ofdm[1]); + RTW_INFO_DUMP("diff_t ", tab.diff_t, NUM_RATE_AC_2SS); + RTW_INFO_DUMP("tab ", (u8 *)&tab, sizeof(tab)); + } + #endif + +#endif + } + +exit: + return ret; +} + +/* +* 1. Download MCC rsvd page +* 2. Re-Download beacon after download rsvd page +*/ +static void rtw_hal_set_fw_mcc_rsvd_page(PADAPTER padapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct mcc_adapter_priv *pmccadapriv = &padapter->mcc_adapterpriv; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + PADAPTER port0_iface = dvobj_get_port0_adapter(dvobj); + PADAPTER iface = NULL; + struct mcc_obj_priv *pmccobjpriv = &(dvobj->mcc_objpriv); + u8 mstatus = RT_MEDIA_CONNECT, i = 0; + + RTW_INFO(FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(padapter)); + + rtw_hal_set_hwreg(port0_iface, HW_VAR_H2C_FW_JOINBSSRPT, (u8 *)(&mstatus)); + +#ifdef CONFIG_AP_MODE + /* Re-Download beacon */ + for (i = 0; i < MAX_MCC_NUM; i++) { + iface = pmccobjpriv->iface[i]; + if (iface == NULL) + continue; + + pmccadapriv = &iface->mcc_adapterpriv; + + if (pmccadapriv->role == MCC_ROLE_AP + || pmccadapriv->role == MCC_ROLE_GO) { + tx_beacon_hdl(iface, NULL); + } + } +#endif +} + +static void rtw_hal_set_mcc_rsvdpage_cmd(_adapter *padapter) +{ + u8 cmd[H2C_MCC_LOCATION_LEN] = {0}, i = 0, order = 0; + _adapter *iface = NULL; + struct hal_spec_t *hal_spec = GET_HAL_SPEC(padapter); + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *pmccobjpriv = &(dvobj->mcc_objpriv); + + SET_H2CCMD_MCC_PWRIDX_OFFLOAD_EN(cmd, _TRUE); + SET_H2CCMD_MCC_PWRIDX_OFFLOAD_RFNUM(cmd, hal_spec->rf_reg_path_num); + for (order = 0; order < MAX_MCC_NUM; order++) { + iface = pmccobjpriv->iface[i]; + + SET_H2CCMD_MCC_RSVDPAGE_LOC((cmd + order), pmccobjpriv->mcc_loc_rsvd_paga[order]); + SET_H2CCMD_MCC_PWRIDX_RSVDPAGE_LOC ((cmd + order), pmccobjpriv->mcc_pwr_idx_rsvd_page[order]); + } + +#ifdef CONFIG_MCC_MODE_DEBUG + RTW_INFO("=========================\n"); + RTW_INFO("MCC RSVD PAGE LOC:\n"); + for (i = 0; i < H2C_MCC_LOCATION_LEN; i++) + pr_dbg("0x%x ", cmd[i]); + pr_dbg("\n"); + RTW_INFO("=========================\n"); +#endif /* CONFIG_MCC_MODE_DEBUG */ + + rtw_hal_fill_h2c_cmd(padapter, H2C_MCC_LOCATION, H2C_MCC_LOCATION_LEN, cmd); +} + +static void rtw_hal_set_mcc_time_setting_cmd(PADAPTER padapter) +{ + struct mcc_adapter_priv *pmccadapriv = &padapter->mcc_adapterpriv; + struct mlme_priv *pmlmepriv = &(padapter->mlmepriv); + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *mccobjpriv = &(dvobj->mcc_objpriv); + u8 cmd[H2C_MCC_TIME_SETTING_LEN] = {0}; + u8 fw_eable = 1; + u8 swchannel_early_time = MCC_SWCH_FW_EARLY_TIME; + u8 starting_ap_num = DEV_AP_STARTING_NUM(dvobj); + u8 ap_num = DEV_AP_NUM(dvobj); + + if (starting_ap_num == 0 && ap_num == 0) + /* For STA+GC/STA+STA, TSF of GC/STA does not need to sync from TSF of other STA/GC */ + fw_eable = 0; + else + /* Only for STA+GO/STA+AP, TSF of AP/GO need to sync from TSF of STA */ + fw_eable = 1; + + if (fw_eable == 1) { + PADAPTER order0_iface = NULL; + PADAPTER order1_iface = NULL; + u8 policy_idx = mccobjpriv->policy_index; + u8 tsf_sync_offset = mcc_switch_channel_policy_table[policy_idx][MCC_TSF_SYNC_OFFSET_IDX]; + u8 start_time_offset = mcc_switch_channel_policy_table[policy_idx][MCC_START_TIME_OFFSET_IDX]; + u8 interval = mcc_switch_channel_policy_table[policy_idx][MCC_INTERVAL_IDX]; + u8 guard_offset0 = mcc_switch_channel_policy_table[policy_idx][MCC_GUARD_OFFSET0_IDX]; + u8 guard_offset1 = mcc_switch_channel_policy_table[policy_idx][MCC_GUARD_OFFSET1_IDX]; + enum _hw_port tsf_bsae_port = MAX_HW_PORT; + enum _hw_port tsf_sync_port = MAX_HW_PORT; + order0_iface = mccobjpriv->iface[0]; + order1_iface = mccobjpriv->iface[1]; + + tsf_bsae_port = rtw_hal_get_port(order1_iface); + tsf_sync_port = rtw_hal_get_port(order0_iface); + + /* FW set enable */ + SET_H2CCMD_MCC_TIME_SETTING_FW_EN(cmd, fw_eable); + /* TSF Sync offset */ + SET_H2CCMD_MCC_TIME_SETTING_TSF_SYNC_OFFSET(cmd, tsf_sync_offset); + /* start time offset */ + SET_H2CCMD_MCC_TIME_SETTING_START_TIME(cmd, (start_time_offset + guard_offset0)); + /* interval */ + SET_H2CCMD_MCC_TIME_SETTING_INTERVAL(cmd, interval); + /* Early time to inform driver by C2H before switch channel */ + SET_H2CCMD_MCC_TIME_SETTING_EARLY_SWITCH_RPT(cmd, swchannel_early_time); + /* Port0 sync from Port1, not support multi-port */ + SET_H2CCMD_MCC_TIME_SETTING_ORDER_BASE(cmd, tsf_bsae_port); + SET_H2CCMD_MCC_TIME_SETTING_ORDER_SYNC(cmd, tsf_sync_port); + } else { + /* start time offset */ + SET_H2CCMD_MCC_TIME_SETTING_START_TIME(cmd, mccobjpriv->start_time); + /* interval */ + SET_H2CCMD_MCC_TIME_SETTING_INTERVAL(cmd, mccobjpriv->interval); + /* Early time to inform driver by C2H before switch channel */ + SET_H2CCMD_MCC_TIME_SETTING_EARLY_SWITCH_RPT(cmd, swchannel_early_time); + } + +#ifdef CONFIG_MCC_MODE_DEBUG + { + u8 i = 0; + + RTW_INFO("=========================\n"); + RTW_INFO("NoA:\n"); + for (i = 0; i < H2C_MCC_TIME_SETTING_LEN; i++) + pr_dbg("0x%x ", cmd[i]); + pr_dbg("\n"); + RTW_INFO("=========================\n"); + } +#endif /* CONFIG_MCC_MODE_DEBUG */ + + rtw_hal_fill_h2c_cmd(padapter, H2C_MCC_TIME_SETTING, H2C_MCC_TIME_SETTING_LEN, cmd); +} + +#ifndef CONFIG_MCC_MODE_V2 +static void rtw_hal_set_mcc_IQK_offload_cmd(PADAPTER padapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *pmccobjpriv = &(dvobj->mcc_objpriv); + struct mcc_adapter_priv *pmccadapriv = NULL; + _adapter *iface = NULL; + u8 cmd[H2C_MCC_IQK_PARAM_LEN] = {0}, bready = 0, i = 0, order = 0; + u16 TX_X = 0, TX_Y = 0, RX_X = 0, RX_Y = 0; + u8 total_rf_path = GET_HAL_SPEC(padapter)->rf_reg_path_num; + u8 rf_path_idx = 0, last_order = MAX_MCC_NUM - 1, last_rf_path_index = total_rf_path - 1; + + /* by order, last order & last_rf_path_index must set ready bit = 1 */ + for (i = 0; i < MAX_MCC_NUM; i++) { + iface = pmccobjpriv->iface[i]; + if (iface == NULL) + continue; + + pmccadapriv = &iface->mcc_adapterpriv; + order = pmccadapriv->order; + + for (rf_path_idx = 0; rf_path_idx < total_rf_path; rf_path_idx ++) { + + _rtw_memset(cmd, 0, H2C_MCC_IQK_PARAM_LEN); + TX_X = pmccadapriv->mcc_iqk_arr[rf_path_idx].TX_X & 0x7ff;/* [10:0] */ + TX_Y = pmccadapriv->mcc_iqk_arr[rf_path_idx].TX_Y & 0x7ff;/* [10:0] */ + RX_X = pmccadapriv->mcc_iqk_arr[rf_path_idx].RX_X & 0x3ff;/* [9:0] */ + RX_Y = pmccadapriv->mcc_iqk_arr[rf_path_idx].RX_Y & 0x3ff;/* [9:0] */ + + /* ready or not */ + if (order == last_order && rf_path_idx == last_rf_path_index) + bready = 1; + else + bready = 0; + + SET_H2CCMD_MCC_IQK_READY(cmd, bready); + SET_H2CCMD_MCC_IQK_ORDER(cmd, order); + SET_H2CCMD_MCC_IQK_PATH(cmd, rf_path_idx); + + /* fill RX_X[7:0] to (cmd+1)[7:0] bitlen=8 */ + SET_H2CCMD_MCC_IQK_RX_L(cmd, (u8)(RX_X & 0xff)); + /* fill RX_X[9:8] to (cmd+2)[1:0] bitlen=2 */ + SET_H2CCMD_MCC_IQK_RX_M1(cmd, (u8)((RX_X >> 8) & 0x03)); + /* fill RX_Y[5:0] to (cmd+2)[7:2] bitlen=6 */ + SET_H2CCMD_MCC_IQK_RX_M2(cmd, (u8)(RX_Y & 0x3f)); + /* fill RX_Y[9:6] to (cmd+3)[3:0] bitlen=4 */ + SET_H2CCMD_MCC_IQK_RX_H(cmd, (u8)((RX_Y >> 6) & 0x0f)); + + + /* fill TX_X[7:0] to (cmd+4)[7:0] bitlen=8 */ + SET_H2CCMD_MCC_IQK_TX_L(cmd, (u8)(TX_X & 0xff)); + /* fill TX_X[10:8] to (cmd+5)[2:0] bitlen=3 */ + SET_H2CCMD_MCC_IQK_TX_M1(cmd, (u8)((TX_X >> 8) & 0x07)); + /* fill TX_Y[4:0] to (cmd+5)[7:3] bitlen=5 */ + SET_H2CCMD_MCC_IQK_TX_M2(cmd, (u8)(TX_Y & 0x1f)); + /* fill TX_Y[10:5] to (cmd+6)[5:0] bitlen=6 */ + SET_H2CCMD_MCC_IQK_TX_H(cmd, (u8)((TX_Y >> 5) & 0x3f)); + +#ifdef CONFIG_MCC_MODE_DEBUG + RTW_INFO("=========================\n"); + RTW_INFO(FUNC_ADPT_FMT" IQK:\n", FUNC_ADPT_ARG(iface)); + RTW_INFO("TX_X: 0x%02x\n", TX_X); + RTW_INFO("TX_Y: 0x%02x\n", TX_Y); + RTW_INFO("RX_X: 0x%02x\n", RX_X); + RTW_INFO("RX_Y: 0x%02x\n", RX_Y); + RTW_INFO("cmd[0]:0x%02x\n", cmd[0]); + RTW_INFO("cmd[1]:0x%02x\n", cmd[1]); + RTW_INFO("cmd[2]:0x%02x\n", cmd[2]); + RTW_INFO("cmd[3]:0x%02x\n", cmd[3]); + RTW_INFO("cmd[4]:0x%02x\n", cmd[4]); + RTW_INFO("cmd[5]:0x%02x\n", cmd[5]); + RTW_INFO("cmd[6]:0x%02x\n", cmd[6]); + RTW_INFO("=========================\n"); +#endif /* CONFIG_MCC_MODE_DEBUG */ + + rtw_hal_fill_h2c_cmd(padapter, H2C_MCC_IQK_PARAM, H2C_MCC_IQK_PARAM_LEN, cmd); + } + } +} +#endif + + +static void rtw_hal_set_mcc_macid_cmd(PADAPTER padapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_adapter_priv *pmccadapriv = NULL; + _adapter *iface = NULL; + u8 cmd[H2C_MCC_MACID_BITMAP_LEN] = {0}, i = 0, order = 0; + u16 bitmap = 0; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (iface == NULL) + continue; + + pmccadapriv = &iface->mcc_adapterpriv; + if (pmccadapriv->role == MCC_ROLE_MAX) + continue; + + order = pmccadapriv->order; + bitmap = pmccadapriv->mcc_macid_bitmap; + + if (order >= (H2C_MCC_MACID_BITMAP_LEN/2)) { + RTW_INFO(FUNC_ADPT_FMT" only support 3 interface at most(%d)\n" + , FUNC_ADPT_ARG(padapter), order); + continue; + } + SET_H2CCMD_MCC_MACID_BITMAP_L((cmd + order * 2), (u8)(bitmap & 0xff)); + SET_H2CCMD_MCC_MACID_BITMAP_H((cmd + order * 2), (u8)((bitmap >> 8) & 0xff)); + } + +#ifdef CONFIG_MCC_MODE_DEBUG + RTW_INFO("=========================\n"); + RTW_INFO("MACID BITMAP: "); + for (i = 0; i < H2C_MCC_MACID_BITMAP_LEN; i++) + printk("0x%x ", cmd[i]); + printk("\n"); + RTW_INFO("=========================\n"); +#endif /* CONFIG_MCC_MODE_DEBUG */ + rtw_hal_fill_h2c_cmd(padapter, H2C_MCC_MACID_BITMAP, H2C_MCC_MACID_BITMAP_LEN, cmd); +} + +#ifdef CONFIG_MCC_MODE_V2 +static u8 get_pri_ch_idx_by_adapter(u8 center_ch, u8 channel, u8 bw, u8 ch_offset40) +{ + u8 pri_ch_idx = 0, chnl_offset80 = 0; + + if (bw == CHANNEL_WIDTH_80) { + if (center_ch > channel) + chnl_offset80 = HAL_PRIME_CHNL_OFFSET_LOWER; + else if (center_ch < channel) + chnl_offset80 = HAL_PRIME_CHNL_OFFSET_UPPER; + else + chnl_offset80 = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + } + + if (bw == CHANNEL_WIDTH_80) { + /* primary channel is at lower subband of 80MHz & 40MHz */ + if ((ch_offset40 == HAL_PRIME_CHNL_OFFSET_LOWER) && (chnl_offset80 == HAL_PRIME_CHNL_OFFSET_LOWER)) + pri_ch_idx = VHT_DATA_SC_20_LOWEST_OF_80MHZ; + /* primary channel is at lower subband of 80MHz & upper subband of 40MHz */ + else if ((ch_offset40 == HAL_PRIME_CHNL_OFFSET_UPPER) && (chnl_offset80 == HAL_PRIME_CHNL_OFFSET_LOWER)) + pri_ch_idx = VHT_DATA_SC_20_LOWER_OF_80MHZ; + /* primary channel is at upper subband of 80MHz & lower subband of 40MHz */ + else if ((ch_offset40 == HAL_PRIME_CHNL_OFFSET_LOWER) && (chnl_offset80 == HAL_PRIME_CHNL_OFFSET_UPPER)) + pri_ch_idx = VHT_DATA_SC_20_UPPER_OF_80MHZ; + /* primary channel is at upper subband of 80MHz & upper subband of 40MHz */ + else if ((ch_offset40 == HAL_PRIME_CHNL_OFFSET_UPPER) && (chnl_offset80 == HAL_PRIME_CHNL_OFFSET_UPPER)) + pri_ch_idx = VHT_DATA_SC_20_UPPERST_OF_80MHZ; + else { + if (chnl_offset80 == HAL_PRIME_CHNL_OFFSET_LOWER) + pri_ch_idx = VHT_DATA_SC_40_LOWER_OF_80MHZ; + else if (chnl_offset80 == HAL_PRIME_CHNL_OFFSET_UPPER) + pri_ch_idx = VHT_DATA_SC_40_UPPER_OF_80MHZ; + else + RTW_INFO("SCMapping: DONOT CARE Mode Setting\n"); + } + } else if (bw == CHANNEL_WIDTH_40) { + /* primary channel is at upper subband of 40MHz */ + if (ch_offset40== HAL_PRIME_CHNL_OFFSET_UPPER) + pri_ch_idx = VHT_DATA_SC_20_UPPER_OF_80MHZ; + /* primary channel is at lower subband of 40MHz */ + else if (ch_offset40 == HAL_PRIME_CHNL_OFFSET_LOWER) + pri_ch_idx = VHT_DATA_SC_20_LOWER_OF_80MHZ; + else + RTW_INFO("SCMapping: DONOT CARE Mode Setting\n"); + } + + return pri_ch_idx; +} + +static void rtw_hal_set_mcc_ctrl_cmd_v2(PADAPTER padapter, u8 stop) +{ + u8 cmd[H2C_MCC_CTRL_LEN] = {0}, i = 0; + u8 order = 0, totalnum = 0; + u8 center_ch = 0, pri_ch_idx = 0, bw = 0; + u8 duration = 0, role = 0, incurch = 0, rfetype = 0, distxnull = 0, c2hrpt = 0; + u8 dis_sw_retry = 0, null_early_time=2, tsfx = 0, update_parm = 0; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *pmccobjpriv = &(dvobj->mcc_objpriv); + struct mcc_adapter_priv *mccadapriv = NULL; + struct mlme_ext_priv *pmlmeext = NULL; + struct mlme_ext_info *pmlmeinfo = NULL; + _adapter *iface = NULL; + + RTW_INFO(FUNC_ADPT_FMT": stop=%d\n", FUNC_ADPT_ARG(padapter), stop); + + for (i = 0; i < MAX_MCC_NUM; i++) { + iface = pmccobjpriv->iface[i]; + if (iface == NULL) + continue; + + if (stop) { + if (iface != padapter) + continue; + } + + mccadapriv = &iface->mcc_adapterpriv; + order = mccadapriv->order; + + if (!stop) + totalnum = MAX_MCC_NUM; + else + totalnum = 0xff; /* 0xff means stop */ + + pmlmeext = &iface->mlmeextpriv; + center_ch = rtw_get_center_ch(pmlmeext->cur_channel, pmlmeext->cur_bwmode, pmlmeext->cur_ch_offset); + pri_ch_idx = get_pri_ch_idx_by_adapter(center_ch, pmlmeext->cur_channel, pmlmeext->cur_bwmode, pmlmeext->cur_ch_offset); + bw = pmlmeext->cur_bwmode; + duration = mccadapriv->mcc_duration; + role = mccadapriv->role; + + incurch = _FALSE; + dis_sw_retry = _TRUE; + + /* STA/GC TX NULL data to inform AP/GC for ps mode */ + switch (role) { + case MCC_ROLE_GO: + case MCC_ROLE_AP: + distxnull = MCC_DISABLE_TX_NULL; + break; + case MCC_ROLE_GC: + set_channel_bwmode(iface, pmlmeext->cur_channel, pmlmeext->cur_ch_offset, pmlmeext->cur_bwmode); + distxnull = MCC_ENABLE_TX_NULL; + break; + case MCC_ROLE_STA: + distxnull = MCC_ENABLE_TX_NULL; + break; + } + + null_early_time = mccadapriv->null_early; + + c2hrpt = MCC_C2H_REPORT_ALL_STATUS; + tsfx = rtw_hal_get_port(iface); + update_parm = 0; + + SET_H2CCMD_MCC_CTRL_V2_ORDER(cmd, order); + SET_H2CCMD_MCC_CTRL_V2_TOTALNUM(cmd, totalnum); + SET_H2CCMD_MCC_CTRL_V2_CENTRAL_CH(cmd, center_ch); + SET_H2CCMD_MCC_CTRL_V2_PRIMARY_CH(cmd, pri_ch_idx); + SET_H2CCMD_MCC_CTRL_V2_BW(cmd, bw); + SET_H2CCMD_MCC_CTRL_V2_DURATION(cmd, duration); + SET_H2CCMD_MCC_CTRL_V2_ROLE(cmd, role); + SET_H2CCMD_MCC_CTRL_V2_INCURCH(cmd, incurch); + SET_H2CCMD_MCC_CTRL_V2_DIS_SW_RETRY(cmd, dis_sw_retry); + SET_H2CCMD_MCC_CTRL_V2_DISTXNULL(cmd, distxnull); + SET_H2CCMD_MCC_CTRL_V2_C2HRPT(cmd, c2hrpt); + SET_H2CCMD_MCC_CTRL_V2_TSFX(cmd, tsfx); + SET_H2CCMD_MCC_CTRL_V2_NULL_EARLY(cmd, null_early_time); + SET_H2CCMD_MCC_CTRL_V2_UPDATE_PARM(cmd, update_parm); + +#ifdef CONFIG_MCC_MODE_DEBUG + RTW_INFO("=========================\n"); + RTW_INFO(FUNC_ADPT_FMT" MCC INFO:\n", FUNC_ADPT_ARG(iface)); + RTW_INFO("cmd[0]:0x%02x\n", cmd[0]); + RTW_INFO("cmd[1]:0x%02x\n", cmd[1]); + RTW_INFO("cmd[2]:0x%02x\n", cmd[2]); + RTW_INFO("cmd[3]:0x%02x\n", cmd[3]); + RTW_INFO("cmd[4]:0x%02x\n", cmd[4]); + RTW_INFO("cmd[5]:0x%02x\n", cmd[5]); + RTW_INFO("cmd[6]:0x%02x\n", cmd[6]); + RTW_INFO("=========================\n"); +#endif /* CONFIG_MCC_MODE_DEBUG */ + + rtw_hal_fill_h2c_cmd(padapter, H2C_MCC_CTRL_V2, H2C_MCC_CTRL_LEN, cmd); + } +} + +#else +static void rtw_hal_set_mcc_ctrl_cmd_v1(PADAPTER padapter, u8 stop) +{ + u8 cmd[H2C_MCC_CTRL_LEN] = {0}, i = 0; + u8 order = 0, totalnum = 0, chidx = 0, bw = 0, bw40sc = 0, bw80sc = 0; + u8 duration = 0, role = 0, incurch = 0, rfetype = 0, distxnull = 0, c2hrpt = 0, chscan = 0; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *pmccobjpriv = &(dvobj->mcc_objpriv); + struct mcc_adapter_priv *mccadapriv = NULL; + struct mlme_ext_priv *pmlmeext = NULL; + struct mlme_ext_info *pmlmeinfo = NULL; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + _adapter *iface = NULL; + + RTW_INFO(FUNC_ADPT_FMT": stop=%d\n", FUNC_ADPT_ARG(padapter), stop); + + for (i = 0; i < MAX_MCC_NUM; i++) { + iface = pmccobjpriv->iface[i]; + if (iface == NULL) + continue; + + if (stop) { + if (iface != padapter) + continue; + } + + mccadapriv = &iface->mcc_adapterpriv; + order = mccadapriv->order; + + if (!stop) + totalnum = MAX_MCC_NUM; + else + totalnum = 0xff; /* 0xff means stop */ + + pmlmeext = &iface->mlmeextpriv; + chidx = pmlmeext->cur_channel; + bw = pmlmeext->cur_bwmode; + bw40sc = pmlmeext->cur_ch_offset; + + /* decide 80 band width offset */ + if (bw == CHANNEL_WIDTH_80) { + u8 center_ch = rtw_get_center_ch(chidx, bw, bw40sc); + + if (center_ch > chidx) + bw80sc = HAL_PRIME_CHNL_OFFSET_LOWER; + else if (center_ch < chidx) + bw80sc = HAL_PRIME_CHNL_OFFSET_UPPER; + else + bw80sc = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + } else + bw80sc = HAL_PRIME_CHNL_OFFSET_DONT_CARE; + + duration = mccadapriv->mcc_duration; + role = mccadapriv->role; + + incurch = _FALSE; + + if (IS_HARDWARE_TYPE_8812(padapter)) + rfetype = pHalData->rfe_type; /* RFETYPE (only for 8812)*/ + else + rfetype = 0; + + /* STA/GC TX NULL data to inform AP/GC for ps mode */ + switch (role) { + case MCC_ROLE_GO: + case MCC_ROLE_AP: + distxnull = MCC_DISABLE_TX_NULL; + break; + case MCC_ROLE_GC: + case MCC_ROLE_STA: + distxnull = MCC_ENABLE_TX_NULL; + break; + } + + c2hrpt = MCC_C2H_REPORT_ALL_STATUS; + chscan = MCC_CHIDX; + + SET_H2CCMD_MCC_CTRL_ORDER(cmd, order); + SET_H2CCMD_MCC_CTRL_TOTALNUM(cmd, totalnum); + SET_H2CCMD_MCC_CTRL_CHIDX(cmd, chidx); + SET_H2CCMD_MCC_CTRL_BW(cmd, bw); + SET_H2CCMD_MCC_CTRL_BW40SC(cmd, bw40sc); + SET_H2CCMD_MCC_CTRL_BW80SC(cmd, bw80sc); + SET_H2CCMD_MCC_CTRL_DURATION(cmd, duration); + SET_H2CCMD_MCC_CTRL_ROLE(cmd, role); + SET_H2CCMD_MCC_CTRL_INCURCH(cmd, incurch); + SET_H2CCMD_MCC_CTRL_RFETYPE(cmd, rfetype); + SET_H2CCMD_MCC_CTRL_DISTXNULL(cmd, distxnull); + SET_H2CCMD_MCC_CTRL_C2HRPT(cmd, c2hrpt); + SET_H2CCMD_MCC_CTRL_CHSCAN(cmd, chscan); + +#ifdef CONFIG_MCC_MODE_DEBUG + RTW_INFO("=========================\n"); + RTW_INFO(FUNC_ADPT_FMT" MCC INFO:\n", FUNC_ADPT_ARG(iface)); + RTW_INFO("cmd[0]:0x%02x\n", cmd[0]); + RTW_INFO("cmd[1]:0x%02x\n", cmd[1]); + RTW_INFO("cmd[2]:0x%02x\n", cmd[2]); + RTW_INFO("cmd[3]:0x%02x\n", cmd[3]); + RTW_INFO("cmd[4]:0x%02x\n", cmd[4]); + RTW_INFO("cmd[5]:0x%02x\n", cmd[5]); + RTW_INFO("cmd[6]:0x%02x\n", cmd[6]); + RTW_INFO("=========================\n"); +#endif /* CONFIG_MCC_MODE_DEBUG */ + + rtw_hal_fill_h2c_cmd(padapter, H2C_MCC_CTRL, H2C_MCC_CTRL_LEN, cmd); + } +} +#endif + +static void rtw_hal_set_mcc_ctrl_cmd(PADAPTER padapter, u8 stop) +{ + #ifdef CONFIG_MCC_MODE_V2 + /* new cmd 0x17 */ + rtw_hal_set_mcc_ctrl_cmd_v2(padapter, stop); + #else + /* old cmd 0x18 */ + rtw_hal_set_mcc_ctrl_cmd_v1(padapter, stop); + #endif +} + +static u8 check_mcc_support(PADAPTER adapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + u8 sta_linked_num = DEV_STA_LD_NUM(dvobj); + u8 starting_ap_num = DEV_AP_STARTING_NUM(dvobj); + u8 ap_num = DEV_AP_NUM(dvobj); + u8 ret = _FAIL; + + RTW_INFO("[MCC] sta_linked_num=%d, starting_ap_num=%d,ap_num=%d\n", + sta_linked_num, starting_ap_num, ap_num); + + /* case for sta + sta case */ + if (sta_linked_num == MAX_MCC_NUM) { + ret = _SUCCESS; + goto exit; + } + + /* case for starting AP + linked sta */ + if ((starting_ap_num + sta_linked_num) == MAX_MCC_NUM) { + ret = _SUCCESS; + goto exit; + } + + /* case for started AP + linked sta */ + if ((ap_num + sta_linked_num) == MAX_MCC_NUM) { + ret = _SUCCESS; + goto exit; + } + +exit: + return ret; +} + +static void rtw_hal_mcc_start_prehdl(PADAPTER padapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + _adapter *iface = NULL; + struct mcc_adapter_priv *mccadapriv = NULL; + u8 i = 1; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (iface == NULL) + continue; + + mccadapriv = &iface->mcc_adapterpriv; + mccadapriv->role = MCC_ROLE_MAX; + } + +#ifdef CONFIG_RTL8822C + if (IS_HARDWARE_TYPE_8822C(padapter)) { + HAL_DATA_TYPE *hal = GET_HAL_DATA(padapter); + struct dm_struct *dm = &hal->odmpriv; + + odm_cmn_info_update(dm, ODM_CMNINFO_IS_DOWNLOAD_FW, hal->bFWReady); + } +#endif +} + +static u8 rtw_hal_set_mcc_start_setting(PADAPTER padapter, u8 status) +{ + u8 ret = _SUCCESS, enable_tsf_auto_sync = _FALSE; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj); + + if (pwrpriv->pwr_mode != PS_MODE_ACTIVE) { + rtw_warn_on(1); + RTW_INFO("PS mode is not active before start mcc, force exit ps mode\n"); + LeaveAllPowerSaveModeDirect(padapter); + } + + if (check_mcc_support(padapter) == _FAIL) { + ret = _FAIL; + goto exit; + } + + rtw_hal_mcc_start_prehdl(padapter); + + /* configure mcc switch channel setting */ + rtw_hal_config_mcc_switch_channel_setting(padapter); + + if (rtw_hal_decide_mcc_role(padapter) == _FAIL) { + ret = _FAIL; + goto exit; + } + + /* set mcc status to indicate process mcc start setting */ + rtw_hal_set_mcc_status(padapter, MCC_STATUS_PROCESS_MCC_START_SETTING); + + /* only download rsvd page for connect */ + if (status == MCC_SETCMD_STATUS_START_CONNECT) { + /* download mcc rsvd page */ + rtw_hal_set_fw_mcc_rsvd_page(padapter); + rtw_hal_set_mcc_rsvdpage_cmd(padapter); + } + + /* configure time setting */ + rtw_hal_set_mcc_time_setting_cmd(padapter); + +#ifndef CONFIG_MCC_MODE_V2 + /* IQK value offload */ + rtw_hal_set_mcc_IQK_offload_cmd(padapter); +#endif + + /* set mac id to fw */ + rtw_hal_set_mcc_macid_cmd(padapter); +#ifdef CONFIG_HW_P0_TSF_SYNC + if (dvobj->p0_tsf.sync_port != MAX_HW_PORT ) { + /* disable tsf auto sync */ + RTW_INFO("[MCC] disable HW TSF sync\n"); + rtw_hal_set_hwreg(padapter, HW_VAR_TSF_AUTO_SYNC, &enable_tsf_auto_sync); + } else { + RTW_INFO("[MCC] already disable HW TSF sync\n"); + } +#endif + /* set mcc parameter */ + rtw_hal_set_mcc_ctrl_cmd(padapter, _FALSE); + +exit: + return ret; +} + +static void rtw_hal_set_mcc_stop_setting(PADAPTER padapter, u8 status) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *mccobjpriv = &dvobj->mcc_objpriv; + _adapter *iface = NULL; + struct mcc_adapter_priv *mccadapriv = NULL; + u8 i = 0; + /* + * when adapter disconnect, stop mcc mod + * total=0xf means stop mcc mode + */ + + switch (status) { + default: + /* let fw switch to other interface channel */ + for (i = 0; i < MAX_MCC_NUM; i++) { + iface = mccobjpriv->iface[i]; + if (iface == NULL) + continue; + + mccadapriv = &iface->mcc_adapterpriv; + + /* use other interface to set cmd */ + if (iface != padapter) { + rtw_hal_set_mcc_ctrl_cmd(iface, _TRUE); + break; + } + } + break; + } +} + +static void rtw_hal_mcc_status_hdl(PADAPTER padapter, u8 status) +{ + switch (status) { + case MCC_SETCMD_STATUS_STOP_DISCONNECT: + rtw_hal_clear_mcc_status(padapter, MCC_STATUS_NEED_MCC | MCC_STATUS_DOING_MCC); + break; + case MCC_SETCMD_STATUS_STOP_SCAN_START: + rtw_hal_set_mcc_status(padapter, MCC_STATUS_NEED_MCC); + rtw_hal_clear_mcc_status(padapter, MCC_STATUS_DOING_MCC); + break; + + case MCC_SETCMD_STATUS_START_CONNECT: + case MCC_SETCMD_STATUS_START_SCAN_DONE: + rtw_hal_set_mcc_status(padapter, MCC_STATUS_NEED_MCC | MCC_STATUS_DOING_MCC); + break; + default: + RTW_INFO(FUNC_ADPT_FMT" error status(%d)\n", FUNC_ADPT_ARG(padapter), status); + break; + } +} + +static void rtw_hal_mcc_stop_posthdl(PADAPTER padapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *mccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + struct mcc_adapter_priv *mccadapriv = NULL; + _adapter *iface = NULL; + PHAL_DATA_TYPE hal; + u8 i = 0; + u8 enable_rx_bar = _FALSE; + + hal = GET_HAL_DATA(padapter); + + for (i = 0; i < MAX_MCC_NUM; i++) { + iface = mccobjpriv->iface[i]; + if (iface == NULL) + continue; + + /* release network queue */ + rtw_netif_wake_queue(iface->pnetdev); + mccadapriv = &iface->mcc_adapterpriv; + mccadapriv->mcc_tx_bytes_from_kernel = 0; + mccadapriv->mcc_last_tx_bytes_from_kernel = 0; + mccadapriv->mcc_tx_bytes_to_port = 0; + + if (mccadapriv->role == MCC_ROLE_GO) + rtw_hal_mcc_remove_go_p2p_ie(iface); + +#ifdef CONFIG_TDLS + if (MLME_IS_STA(iface)) { + if (mccadapriv->backup_tdls_en) { + rtw_enable_tdls_func(iface); + RTW_INFO("%s: Disable MCC, Enable TDLS\n", __func__); + mccadapriv->backup_tdls_en = _FALSE; + } + } +#endif /* CONFIG_TDLS */ + + mccadapriv->role = MCC_ROLE_MAX; + mccobjpriv->iface[i] = NULL; + } + + /* force switch channel */ + hal->current_channel = 0; + hal->current_channel_bw = CHANNEL_WIDTH_MAX; + #ifdef CONFIG_MCC_PHYDM_OFFLOAD + rtw_hal_mcc_cfg_phydm(padapter, MCC_CFG_PHYDM_STOP, NULL); + #endif + +#ifdef CONFIG_RTL8822C + if (IS_HARDWARE_TYPE_8822C(padapter)) { + HAL_DATA_TYPE *hal = GET_HAL_DATA(padapter); + struct dm_struct *dm = &hal->odmpriv; + + odm_cmn_info_update(dm, ODM_CMNINFO_IS_DOWNLOAD_FW, _FALSE); + } +#endif +} + +static void rtw_hal_mcc_start_posthdl(PADAPTER padapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *mccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + struct mcc_adapter_priv *mccadapriv = NULL; + struct pwrctrl_priv *pwrpriv = adapter_to_pwrctl(padapter); + _adapter *iface = NULL; + u8 i = 0, order = 0; + u8 enable_rx_bar = _TRUE; + + for (i = 0; i < MAX_MCC_NUM; i++) { + iface = mccobjpriv->iface[i]; + if (iface == NULL) + continue; + + mccadapriv = &iface->mcc_adapterpriv; + if (mccadapriv->role == MCC_ROLE_MAX) + continue; + + mccadapriv->mcc_tx_bytes_from_kernel = 0; + mccadapriv->mcc_last_tx_bytes_from_kernel = 0; + mccadapriv->mcc_tx_bytes_to_port = 0; + +#ifdef CONFIG_TDLS + if (MLME_IS_STA(iface)) { + if (rtw_is_tdls_enabled(iface)) { + mccadapriv->backup_tdls_en = _TRUE; + rtw_disable_tdls_func(iface, _TRUE); + RTW_INFO("%s: Enable MCC, Disable TDLS\n", __func__); + } + } +#endif /* CONFIG_TDLS */ + } + #ifdef CONFIG_MCC_PHYDM_OFFLOAD + rtw_hal_mcc_cfg_phydm(padapter, MCC_CFG_PHYDM_START, NULL); + #endif +} + +/* + * rtw_hal_set_mcc_setting - set mcc setting + * @padapter: currnet padapter to stop/start MCC + * @stop: stop mcc or not + * @return val: 1 for SUCCESS, 0 for fail + */ +static u8 rtw_hal_set_mcc_setting(PADAPTER padapter, u8 status) +{ + u8 ret = _FAIL; + struct mcc_obj_priv *pmccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + u8 stop = (status < MCC_SETCMD_STATUS_START_CONNECT) ? _TRUE : _FALSE; + systime start_time = rtw_get_current_time(); + + RTW_INFO("===> "FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(padapter)); + + rtw_sctx_init(&pmccobjpriv->mcc_sctx, MCC_EXPIRE_TIME); + pmccobjpriv->mcc_c2h_status = MCC_RPT_MAX; + + if (stop == _FALSE) { + /* handle mcc start */ + if (rtw_hal_set_mcc_start_setting(padapter, status) == _FAIL) + goto exit; + + /* wait for C2H */ + if (!rtw_sctx_wait(&pmccobjpriv->mcc_sctx, __func__)) + RTW_INFO(FUNC_ADPT_FMT": wait for mcc start C2H time out\n", FUNC_ADPT_ARG(padapter)); + else + ret = _SUCCESS; + + if (ret == _SUCCESS) { + RTW_INFO(FUNC_ADPT_FMT": mcc start sucecssfully\n", FUNC_ADPT_ARG(padapter)); + rtw_hal_mcc_status_hdl(padapter, status); + rtw_hal_mcc_start_posthdl(padapter); + } + } else { + + /* set mcc status to indicate process mcc start setting */ + rtw_hal_set_mcc_status(padapter, MCC_STATUS_PROCESS_MCC_STOP_SETTING); + + /* handle mcc stop */ + rtw_hal_set_mcc_stop_setting(padapter, status); + + /* wait for C2H */ + if (!rtw_sctx_wait(&pmccobjpriv->mcc_sctx, __func__)) + RTW_INFO(FUNC_ADPT_FMT": wait for mcc stop C2H time out\n", FUNC_ADPT_ARG(padapter)); + else { + ret = _SUCCESS; + rtw_hal_mcc_status_hdl(padapter, status); + rtw_hal_mcc_stop_posthdl(padapter); + } + } + +exit: + /* clear mcc status */ + rtw_hal_clear_mcc_status(padapter + , MCC_STATUS_PROCESS_MCC_START_SETTING | MCC_STATUS_PROCESS_MCC_STOP_SETTING); + + RTW_INFO(FUNC_ADPT_FMT" in %dms <===\n" + , FUNC_ADPT_ARG(padapter), rtw_get_passing_time_ms(start_time)); + return ret; +} + +/** + * rtw_hal_mcc_check_case_not_limit_traffic - handler flow ctrl for special case + * @cur_iface: fw stay channel setting of this iface + * @next_iface: fw will swich channel setting of this iface + */ +static void rtw_hal_mcc_check_case_not_limit_traffic(PADAPTER cur_iface, PADAPTER next_iface) +{ + u8 cur_bw = cur_iface->mlmeextpriv.cur_bwmode; + u8 next_bw = next_iface->mlmeextpriv.cur_bwmode; + + /* for both interface are VHT80, doesn't limit_traffic according to iperf results */ + if (cur_bw == CHANNEL_WIDTH_80 && next_bw == CHANNEL_WIDTH_80) { + cur_iface->mcc_adapterpriv.mcc_tp_limit = _FALSE; + next_iface->mcc_adapterpriv.mcc_tp_limit = _FALSE; + } +} + + +/** + * rtw_hal_mcc_sw_ch_fw_notify_hdl - handler flow ctrl + */ +static void rtw_hal_mcc_sw_ch_fw_notify_hdl(PADAPTER padapter) +{ + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter); + struct mcc_obj_priv *pmccobjpriv = &(pdvobjpriv->mcc_objpriv); + struct mcc_adapter_priv *cur_mccadapriv = NULL, *next_mccadapriv = NULL; + _adapter *iface = NULL, *cur_iface = NULL, *next_iface = NULL; + struct registry_priv *preg = &padapter->registrypriv; + u8 cur_op_ch = pdvobjpriv->oper_channel; + u8 i = 0, iface_num = pdvobjpriv->iface_nums, cur_order = 0, next_order = 0; + static u8 cnt = 1; + u32 single_tx_cri = preg->rtw_mcc_single_tx_cri; + + for (i = 0; i < iface_num; i++) { + iface = pdvobjpriv->padapters[i]; + if (iface == NULL) + continue; + + if (cur_op_ch == iface->mlmeextpriv.cur_channel) { + cur_iface = iface; + cur_mccadapriv = &cur_iface->mcc_adapterpriv; + cur_order = cur_mccadapriv->order; + next_order = (cur_order + 1) % iface_num; + next_iface = pmccobjpriv->iface[next_order]; + next_mccadapriv = &next_iface->mcc_adapterpriv; + break; + } + } + + if (cur_iface == NULL || next_iface == NULL) { + RTW_ERR("cur_iface=%p,next_iface=%p\n", cur_iface, next_iface); + rtw_warn_on(1); + return; + } + + /* check other interface tx busy traffic or not under every 2 switch channel notify(Mbits/100ms) */ + if (cnt == 2) { + cur_mccadapriv->mcc_tp = (cur_mccadapriv->mcc_tx_bytes_from_kernel + - cur_mccadapriv->mcc_last_tx_bytes_from_kernel) * 10 * 8 / 1024 / 1024; + cur_mccadapriv->mcc_last_tx_bytes_from_kernel = cur_mccadapriv->mcc_tx_bytes_from_kernel; + + next_mccadapriv->mcc_tp = (next_mccadapriv->mcc_tx_bytes_from_kernel + - next_mccadapriv->mcc_last_tx_bytes_from_kernel) * 10 * 8 / 1024 / 1024; + next_mccadapriv->mcc_last_tx_bytes_from_kernel = next_mccadapriv->mcc_tx_bytes_from_kernel; + + cnt = 1; + } else + cnt = 2; + + /* check single TX or cuncurrnet TX */ + if (next_mccadapriv->mcc_tp < single_tx_cri) { + /* single TX, does not stop */ + cur_mccadapriv->mcc_tx_stop = _FALSE; + cur_mccadapriv->mcc_tp_limit = _FALSE; + } else { + /* concurrent TX, stop */ + cur_mccadapriv->mcc_tx_stop = _TRUE; + cur_mccadapriv->mcc_tp_limit = _TRUE; + } + + if (cur_mccadapriv->mcc_tp < single_tx_cri) { + next_mccadapriv->mcc_tx_stop = _FALSE; + next_mccadapriv->mcc_tp_limit = _FALSE; + } else { + next_mccadapriv->mcc_tx_stop = _FALSE; + next_mccadapriv->mcc_tp_limit = _TRUE; + next_mccadapriv->mcc_tx_bytes_to_port = 0; + } + + /* stop current iface kernel queue or not */ + if (cur_mccadapriv->mcc_tx_stop) + rtw_netif_stop_queue(cur_iface->pnetdev); + else + rtw_netif_wake_queue(cur_iface->pnetdev); + + /* stop next iface kernel queue or not */ + if (next_mccadapriv->mcc_tx_stop) + rtw_netif_stop_queue(next_iface->pnetdev); + else + rtw_netif_wake_queue(next_iface->pnetdev); + + /* start xmit tasklet */ + rtw_os_xmit_schedule(next_iface); + + rtw_hal_mcc_check_case_not_limit_traffic(cur_iface, next_iface); + + if (0) { + RTW_INFO("order:%d, mcc_tx_stop:%d, mcc_tp:%d\n", + cur_mccadapriv->order, cur_mccadapriv->mcc_tx_stop, cur_mccadapriv->mcc_tp); + dump_os_queue(0, cur_iface); + RTW_INFO("order:%d, mcc_tx_stop:%d, mcc_tp:%d\n", + next_mccadapriv->order, next_mccadapriv->mcc_tx_stop, next_mccadapriv->mcc_tp); + dump_os_queue(0, next_iface); + } +} + +static void rtw_hal_mcc_update_noa_start_time_hdl(PADAPTER padapter, u8 buflen, u8 *tmpBuf) +{ + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter); + struct mcc_obj_priv *pmccobjpriv = &(pdvobjpriv->mcc_objpriv); + struct mcc_adapter_priv *pmccadapriv = NULL; + PADAPTER iface = NULL; + u8 i = 0; + u8 policy_idx = pmccobjpriv->policy_index; + u8 noa_tsf_sync_offset = mcc_switch_channel_policy_table[policy_idx][MCC_TSF_SYNC_OFFSET_IDX]; + u8 noa_start_time_offset = mcc_switch_channel_policy_table[policy_idx][MCC_START_TIME_OFFSET_IDX]; + + for (i = 0; i < pdvobjpriv->iface_nums; i++) { + iface = pdvobjpriv->padapters[i]; + if (iface == NULL) + continue; + + pmccadapriv = &iface->mcc_adapterpriv; + if (pmccadapriv->role == MCC_ROLE_MAX) + continue; + + /* GO & channel match */ + if (pmccadapriv->role == MCC_ROLE_GO) { + /* convert GO TBTT from FW to noa_start_time(TU convert to mircosecond) */ + pmccadapriv->noa_start_time = RTW_GET_LE32(tmpBuf + 2) + noa_start_time_offset * TU; + + if (0) { + RTW_INFO("TBTT:0x%02x\n", RTW_GET_LE32(tmpBuf + 2)); + RTW_INFO("noa_tsf_sync_offset:%d, noa_start_time_offset:%d\n", noa_tsf_sync_offset, noa_start_time_offset); + RTW_INFO(FUNC_ADPT_FMT"buf=0x%02x:0x%02x:0x%02x:0x%02x, noa_start_time=0x%02x\n" + , FUNC_ADPT_ARG(iface) + , tmpBuf[2] + , tmpBuf[3] + , tmpBuf[4] + , tmpBuf[5] + ,pmccadapriv->noa_start_time); + } + + rtw_hal_mcc_update_go_p2p_ie(iface); + + break; + } + } + +} + +static u8 mcc_get_reg_hdl(PADAPTER adapter, const u8 *val) +{ + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mcc_obj_priv *mccobjpriv = &(dvobj->mcc_objpriv); + _adapter *cur_iface = NULL; + u8 ret = _SUCCESS; + u8 cur_order = 0; + #ifdef CONFIG_RTL8822C + u16 dbg_reg[DBG_MCC_REG_NUM] = {0x4d4,0x522,0x1d70}; + #else + u16 dbg_reg[DBG_MCC_REG_NUM] = {0x4d4,0x522,0xc50,0xe50}; + #endif + u16 dbg_rf_reg[DBG_MCC_RF_REG_NUM] = {0x18}; + u8 i; + u32 reg_val; + u8 path = 0, path_nums = 0; + + if (!rtw_hal_check_mcc_status(adapter, MCC_STATUS_DOING_MCC)) { + ret = _FAIL; + goto exit; + } + + if (!val) + cur_order = 0xff; + else + cur_order = *val; + + if (cur_order >= MAX_MCC_NUM && cur_order != 0xff) { + RTW_ERR("%s: cur_order=%d\n", __func__, cur_order); + ret = _FAIL; + goto exit; + } + + path_nums = hal_spec->rf_reg_path_num; + if (cur_order == 0xff) + cur_iface = adapter; + else + cur_iface = mccobjpriv->iface[cur_order]; + + if (!cur_iface) { + RTW_ERR("%s: cur_iface = NULL, cur_order=%d\n", __func__, cur_order); + ret = _FAIL; + goto exit; + } + + _enter_critical_mutex(&mccobjpriv->mcc_dbg_reg_mutex, NULL); + if (!RTW_CANNOT_IO(adapter)) { + /* RTW_INFO("=================================\n"); + RTW_INFO(ADPT_FMT": cur_order:%d\n", ADPT_ARG(cur_iface), cur_order); */ + + for (i = 0; i < ARRAY_SIZE(dbg_reg); i++) { + reg_val = rtw_read32(adapter, dbg_reg[i]); + mccobjpriv->dbg_reg[i] = dbg_reg[i]; + mccobjpriv->dbg_reg_val[i] = reg_val; + /* RTW_PRINT("REG_%X:0x%08x\n", dbg_reg[i], reg_val); */ + } + for (i = 0; i < ARRAY_SIZE(dbg_rf_reg); i++) { + for (path = 0; path < path_nums; path++) { + reg_val = rtw_hal_read_rfreg(adapter, path, dbg_rf_reg[i], 0xffffffff); + /* RTW_PRINT("RF_PATH_%d_REG_%X:0x%08x\n", + path, dbg_rf_reg[i], reg_val); */ + mccobjpriv->dbg_rf_reg[i] = dbg_rf_reg[i]; + mccobjpriv->dbg_rf_reg_val[i][path] = reg_val; + } + } + } + _exit_critical_mutex(&mccobjpriv->mcc_dbg_reg_mutex, NULL); + +exit: + return ret; +} + +static u8 mcc_get_reg_cmd(_adapter *adapter, u8 cur_order) +{ + struct cmd_obj *cmdobj; + struct drvextra_cmd_parm *pdrvextra_cmd_parm; + struct cmd_priv *pcmdpriv = &adapter->cmdpriv; + u8 *mcc_cur_order = NULL; + u8 res = _SUCCESS; + + + cmdobj = (struct cmd_obj *)rtw_zmalloc(sizeof(struct cmd_obj)); + if (cmdobj == NULL) { + res = _FAIL; + goto exit; + } + + pdrvextra_cmd_parm = (struct drvextra_cmd_parm *)rtw_zmalloc(sizeof(struct drvextra_cmd_parm)); + if (pdrvextra_cmd_parm == NULL) { + rtw_mfree((u8 *)cmdobj, sizeof(struct cmd_obj)); + res = _FAIL; + goto exit; + } + + mcc_cur_order = rtw_zmalloc(sizeof(u8)); + if (mcc_cur_order == NULL) { + rtw_mfree((u8 *)cmdobj, sizeof(struct cmd_obj)); + rtw_mfree((u8 *)pdrvextra_cmd_parm, sizeof(struct drvextra_cmd_parm)); + res = _FAIL; + goto exit; + } + + pdrvextra_cmd_parm->ec_id = MCC_CMD_WK_CID; + pdrvextra_cmd_parm->type = MCC_GET_DBG_REG_WK_CID; + pdrvextra_cmd_parm->size = 1; + pdrvextra_cmd_parm->pbuf = mcc_cur_order; + + _rtw_memcpy(mcc_cur_order, &cur_order, 1); + + init_h2fwcmd_w_parm_no_rsp(cmdobj, pdrvextra_cmd_parm, CMD_SET_DRV_EXTRA); + res = rtw_enqueue_cmd(pcmdpriv, cmdobj); + +exit: + return res; +} + +static void rtw_hal_mcc_rpt_tsf_hdl(PADAPTER padapter, u8 buflen, u8 *tmpBuf) +{ + struct dvobj_priv *dvobjpriv = adapter_to_dvobj(padapter); + struct mcc_obj_priv *mccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + struct submit_ctx *mcc_tsf_req_sctx = &mccobjpriv->mcc_tsf_req_sctx; + struct mcc_adapter_priv *mccadapriv = NULL; + _adapter *iface = NULL; + u8 order = 0; + + order = mccobjpriv->mcc_tsf_req_sctx_order; + iface = mccobjpriv->iface[order]; + mccadapriv = &iface->mcc_adapterpriv; + mccadapriv->tsf = RTW_GET_LE64(tmpBuf + 2); + + + if (0) + RTW_INFO(FUNC_ADPT_FMT" TSF(order:%d):0x%02llx\n", FUNC_ADPT_ARG(iface), mccadapriv->order, mccadapriv->tsf); + + if (mccadapriv->order == (MAX_MCC_NUM - 1)) + rtw_sctx_done(&mcc_tsf_req_sctx); + else + mccobjpriv->mcc_tsf_req_sctx_order ++; + +} + +/** + * rtw_hal_mcc_c2h_handler - mcc c2h handler + */ +void rtw_hal_mcc_c2h_handler(PADAPTER padapter, u8 buflen, u8 *tmpBuf) +{ + struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter); + struct mcc_obj_priv *pmccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + struct mcc_adapter_priv *pmccadapriv = &padapter->mcc_adapterpriv; + struct submit_ctx *mcc_sctx = &pmccobjpriv->mcc_sctx; + _adapter *cur_adapter = NULL; + u8 cur_ch = 0, cur_bw = 0, cur_ch_offset = 0; + _irqL irqL; + + /* RTW_INFO("[length]=%d, [C2H data]="MAC_FMT"\n", buflen, MAC_ARG(tmpBuf)); */ + /* To avoid reg is set, but driver recive c2h to set wrong oper_channel */ + if (MCC_RPT_STOPMCC == pmccobjpriv->mcc_c2h_status) { + RTW_INFO(FUNC_ADPT_FMT" MCC alread stops return\n", FUNC_ADPT_ARG(padapter)); + return; + } + + _enter_critical_bh(&pmccobjpriv->mcc_lock, &irqL); + pmccobjpriv->mcc_c2h_status = tmpBuf[0]; + pmccobjpriv->current_order = tmpBuf[1]; + cur_adapter = pmccobjpriv->iface[pmccobjpriv->current_order]; + cur_ch = cur_adapter->mlmeextpriv.cur_channel; + cur_bw = cur_adapter->mlmeextpriv.cur_bwmode; + cur_ch_offset = cur_adapter->mlmeextpriv.cur_ch_offset; + rtw_set_oper_ch(cur_adapter, cur_ch); + rtw_set_oper_bw(cur_adapter, cur_bw); + rtw_set_oper_choffset(cur_adapter, cur_ch_offset); + _exit_critical_bh(&pmccobjpriv->mcc_lock, &irqL); + + if (0) + RTW_INFO("%d,order:%d,TSF:0x%llx\n", tmpBuf[0], tmpBuf[1], RTW_GET_LE64(tmpBuf + 2)); + + switch (pmccobjpriv->mcc_c2h_status) { + case MCC_RPT_SUCCESS: + _enter_critical_bh(&pmccobjpriv->mcc_lock, &irqL); + pmccobjpriv->cur_mcc_success_cnt++; + rtw_hal_mcc_upadate_chnl_bw(cur_adapter, cur_ch, cur_ch_offset, cur_bw, _FALSE); + mcc_get_reg_cmd(padapter, pmccobjpriv->current_order); + _exit_critical_bh(&pmccobjpriv->mcc_lock, &irqL); + break; + case MCC_RPT_TXNULL_FAIL: + RTW_INFO("[MCC] TXNULL FAIL\n"); + break; + case MCC_RPT_STOPMCC: + RTW_INFO("[MCC] MCC stop\n"); + pmccobjpriv->mcc_c2h_status = MCC_RPT_STOPMCC; + rtw_hal_mcc_upadate_chnl_bw(cur_adapter, cur_ch, cur_ch_offset, cur_bw, _TRUE); + rtw_sctx_done(&mcc_sctx); + break; + case MCC_RPT_READY: + _enter_critical_bh(&pmccobjpriv->mcc_lock, &irqL); + /* initialize counter & time */ + pmccobjpriv->mcc_launch_time = rtw_get_current_time(); + pmccobjpriv->mcc_c2h_status = MCC_RPT_READY; + pmccobjpriv->cur_mcc_success_cnt = 0; + pmccobjpriv->prev_mcc_success_cnt = 0; + pmccobjpriv->mcc_tolerance_time = MCC_TOLERANCE_TIME; + _exit_critical_bh(&pmccobjpriv->mcc_lock, &irqL); + + RTW_INFO("[MCC] MCC ready\n"); + rtw_sctx_done(&mcc_sctx); + break; + case MCC_RPT_SWICH_CHANNEL_NOTIFY: + rtw_hal_mcc_sw_ch_fw_notify_hdl(padapter); + break; + case MCC_RPT_UPDATE_NOA_START_TIME: + rtw_hal_mcc_update_noa_start_time_hdl(padapter, buflen, tmpBuf); + break; + case MCC_RPT_TSF: + _enter_critical_bh(&pmccobjpriv->mcc_lock, &irqL); + rtw_hal_mcc_rpt_tsf_hdl(padapter, buflen, tmpBuf); + _exit_critical_bh(&pmccobjpriv->mcc_lock, &irqL); + break; + default: + /* RTW_INFO("[MCC] Other MCC status(%d)\n", pmccobjpriv->mcc_c2h_status); */ + break; + } +} + +void rtw_hal_mcc_update_parameter(PADAPTER padapter, u8 force_update) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *mccobjpriv = &(dvobj->mcc_objpriv); + u8 cmd[H2C_MCC_TIME_SETTING_LEN] = {0}; + u8 swchannel_early_time = MCC_SWCH_FW_EARLY_TIME; + u8 ap_num = DEV_AP_NUM(dvobj); + + if (ap_num == 0) { + u8 need_update = _FALSE; + u8 start_time_offset = 0, interval = 0, duration = 0; + + need_update = rtw_hal_mcc_update_timing_parameters(padapter, force_update); + + if (need_update == _FALSE) + return; + + start_time_offset = mccobjpriv->start_time; + interval = mccobjpriv->interval; + duration = mccobjpriv->iface[0]->mcc_adapterpriv.mcc_duration; + + SET_H2CCMD_MCC_TIME_SETTING_START_TIME(cmd, start_time_offset); + SET_H2CCMD_MCC_TIME_SETTING_INTERVAL(cmd, interval); + SET_H2CCMD_MCC_TIME_SETTING_EARLY_SWITCH_RPT(cmd, swchannel_early_time); + SET_H2CCMD_MCC_TIME_SETTING_UPDATE(cmd, _TRUE); + SET_H2CCMD_MCC_TIME_SETTING_ORDER0_DURATION(cmd, duration); + } else { + PADAPTER order0_iface = NULL; + PADAPTER order1_iface = NULL; + u8 policy_idx = mccobjpriv->policy_index; + u8 duration = mcc_switch_channel_policy_table[policy_idx][MCC_DURATION_IDX]; + u8 tsf_sync_offset = mcc_switch_channel_policy_table[policy_idx][MCC_TSF_SYNC_OFFSET_IDX]; + u8 start_time_offset = mcc_switch_channel_policy_table[policy_idx][MCC_START_TIME_OFFSET_IDX]; + u8 interval = mcc_switch_channel_policy_table[policy_idx][MCC_INTERVAL_IDX]; + u8 guard_offset0 = mcc_switch_channel_policy_table[policy_idx][MCC_GUARD_OFFSET0_IDX]; + u8 guard_offset1 = mcc_switch_channel_policy_table[policy_idx][MCC_GUARD_OFFSET1_IDX]; + u8 order0_duration = 0; + u8 i = 0; + enum _hw_port tsf_bsae_port = MAX_HW_PORT; + enum _hw_port tsf_sync_port = MAX_HW_PORT; + + RTW_INFO("%s: policy_idx=%d\n", __func__, policy_idx); + + order0_iface = mccobjpriv->iface[0]; + order1_iface = mccobjpriv->iface[1]; + + /* GO/AP is order 0, GC/STA is order 1 */ + order0_duration = order0_iface->mcc_adapterpriv.mcc_duration = interval - duration; + order0_iface->mcc_adapterpriv.mcc_duration = duration; + + tsf_bsae_port = rtw_hal_get_port(order1_iface); + tsf_sync_port = rtw_hal_get_port(order0_iface); + + /* update IE */ + for (i = 0; i < dvobj->iface_nums; i++) { + PADAPTER iface = NULL; + struct mcc_adapter_priv *mccadapriv = NULL; + + iface = dvobj->padapters[i]; + if (iface == NULL) + continue; + + mccadapriv = &iface->mcc_adapterpriv; + if (mccadapriv->role == MCC_ROLE_MAX) + continue; + + if (mccadapriv->role == MCC_ROLE_GO) + rtw_hal_mcc_update_go_p2p_ie(iface); + } + + /* update H2C cmd */ + /* FW set enable */ + SET_H2CCMD_MCC_TIME_SETTING_FW_EN(cmd, _TRUE); + /* TSF Sync offset */ + SET_H2CCMD_MCC_TIME_SETTING_TSF_SYNC_OFFSET(cmd, tsf_sync_offset); + /* start time offset */ + SET_H2CCMD_MCC_TIME_SETTING_START_TIME(cmd, (start_time_offset + guard_offset0)); + /* interval */ + SET_H2CCMD_MCC_TIME_SETTING_INTERVAL(cmd, interval); + /* Early time to inform driver by C2H before switch channel */ + SET_H2CCMD_MCC_TIME_SETTING_EARLY_SWITCH_RPT(cmd, swchannel_early_time); + /* Port0 sync from Port1, not support multi-port */ + SET_H2CCMD_MCC_TIME_SETTING_ORDER_BASE(cmd, tsf_bsae_port); + SET_H2CCMD_MCC_TIME_SETTING_ORDER_SYNC(cmd, tsf_sync_port); + SET_H2CCMD_MCC_TIME_SETTING_UPDATE(cmd, _TRUE); + SET_H2CCMD_MCC_TIME_SETTING_ORDER0_DURATION(cmd, order0_duration); + } + + rtw_hal_fill_h2c_cmd(padapter, H2C_MCC_TIME_SETTING, H2C_MCC_TIME_SETTING_LEN, cmd); +} + +/** + * rtw_hal_mcc_sw_status_check - check mcc swich channel status + * @padapter: primary adapter + */ +void rtw_hal_mcc_sw_status_check(PADAPTER padapter) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *pmccobjpriv = &(dvobj->mcc_objpriv); + struct pwrctrl_priv *pwrpriv = dvobj_to_pwrctl(dvobj); + struct mcc_adapter_priv *mccadapriv = NULL; + _adapter *iface = NULL; + u8 cur_cnt = 0, prev_cnt = 0, diff_cnt = 0, check_ret = _FAIL, threshold = 0; + u8 policy_idx = pmccobjpriv->policy_index; + u8 noa_enable = _FALSE; + u8 i = 0; + _irqL irqL; + u8 ap_num = DEV_AP_NUM(dvobj); + +/* #define MCC_RESTART 1 */ + + if (!MCC_EN(padapter)) + return; + + _enter_critical_mutex(&pmccobjpriv->mcc_mutex, NULL); + + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC)) { + + /* check noa enable or not */ + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (iface == NULL) + continue; + + mccadapriv = &iface->mcc_adapterpriv; + if (mccadapriv->role == MCC_ROLE_MAX) + continue; + + if (iface->wdinfo.p2p_ps_mode == P2P_PS_NOA) { + noa_enable = _TRUE; + break; + } + } + + if (!noa_enable && ap_num == 0) + rtw_hal_mcc_update_parameter(padapter, _FALSE); + + threshold = pmccobjpriv->mcc_stop_threshold; + + if (pwrpriv->pwr_mode != PS_MODE_ACTIVE) { + rtw_warn_on(1); + RTW_INFO("PS mode is not active under mcc, force exit ps mode\n"); + LeaveAllPowerSaveModeDirect(padapter); + } + + if (rtw_get_passing_time_ms(pmccobjpriv->mcc_launch_time) > 2000) { + _enter_critical_bh(&pmccobjpriv->mcc_lock, &irqL); + + cur_cnt = pmccobjpriv->cur_mcc_success_cnt; + prev_cnt = pmccobjpriv->prev_mcc_success_cnt; + if (cur_cnt < prev_cnt) + diff_cnt = (cur_cnt + 255) - prev_cnt; + else + diff_cnt = cur_cnt - prev_cnt; + + if (diff_cnt < threshold) { + pmccobjpriv->mcc_tolerance_time--; + RTW_INFO("%s: diff_cnt:%d, tolerance_time:%d\n", + __func__, diff_cnt, pmccobjpriv->mcc_tolerance_time); + } else + pmccobjpriv->mcc_tolerance_time = MCC_TOLERANCE_TIME; + + pmccobjpriv->prev_mcc_success_cnt = pmccobjpriv->cur_mcc_success_cnt; + + if (pmccobjpriv->mcc_tolerance_time != 0) + check_ret = _SUCCESS; + + _exit_critical_bh(&pmccobjpriv->mcc_lock, &irqL); + + if (check_ret != _SUCCESS) { + RTW_INFO("============ MCC swich channel check fail (%d)=============\n", diff_cnt); + /* restart MCC */ + #ifdef MCC_RESTART + rtw_hal_set_mcc_setting(padapter, MCC_SETCMD_STATUS_STOP_DISCONNECT); + rtw_hal_set_mcc_setting(padapter, MCC_SETCMD_STATUS_START_CONNECT); + #endif /* MCC_RESTART */ + } + } else { + _enter_critical_bh(&pmccobjpriv->mcc_lock, &irqL); + pmccobjpriv->prev_mcc_success_cnt = pmccobjpriv->cur_mcc_success_cnt; + _exit_critical_bh(&pmccobjpriv->mcc_lock, &irqL); + } + + } + _exit_critical_mutex(&pmccobjpriv->mcc_mutex, NULL); +} + +/** + * rtw_hal_mcc_change_scan_flag - change scan flag under mcc + * + * MCC mode under sitesurvey goto AP channel to tx bcn & data + * MCC mode under sitesurvey doesn't support TX data for station mode (FW not support) + * + * @padapter: the adapter to be change scan flag + * @ch: pointer to rerurn ch + * @bw: pointer to rerurn bw + * @offset: pointer to rerurn offset + */ +u8 rtw_hal_mcc_change_scan_flag(PADAPTER padapter, u8 *ch, u8 *bw, u8 *offset) +{ + u8 need_ch_setting_union = _TRUE, i = 0, flags = 0, back_op = _FALSE; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_adapter_priv *mccadapriv = NULL; + struct mlme_ext_priv *mlmeext = NULL; + _adapter *iface = NULL; + + if (!MCC_EN(padapter)) + goto exit; + + if (!rtw_hal_check_mcc_status(padapter, MCC_STATUS_NEED_MCC)) + goto exit; + + /* disable PS_ANNC & TX_RESUME for all interface */ + /* ToDo: TX_RESUME by interface in SCAN_BACKING_OP */ + mlmeext = &padapter->mlmeextpriv; + + flags = mlmeext_scan_backop_flags(mlmeext); + if (mlmeext_chk_scan_backop_flags(mlmeext, SS_BACKOP_PS_ANNC)) + flags &= ~SS_BACKOP_PS_ANNC; + + if (mlmeext_chk_scan_backop_flags(mlmeext, SS_BACKOP_TX_RESUME)) + flags &= ~SS_BACKOP_TX_RESUME; + + mlmeext_assign_scan_backop_flags(mlmeext, flags); + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (!iface) + continue; + + mlmeext = &iface->mlmeextpriv; + + if (MLME_IS_GO(iface) || MLME_IS_AP(iface)) + back_op = _TRUE; + else if (MLME_IS_GC(iface) && (iface != padapter)) + /* switch to another linked interface(GO) to receive beacon to avoid no beacon disconnect */ + back_op = _TRUE; + else if (MLME_IS_STA(iface) && MLME_IS_ASOC(iface) && (iface != padapter)) + /* switch to another linked interface(STA) to receive beacon to avoid no beacon disconnect */ + back_op = _TRUE; + else { + /* bypass non-linked/non-linking interface/scan interface */ + continue; + } + + if (back_op) { + *ch = mlmeext->cur_channel; + *bw = mlmeext->cur_bwmode; + *offset = mlmeext->cur_ch_offset; + need_ch_setting_union = _FALSE; + } + } +exit: + return need_ch_setting_union; +} + +/** + * rtw_hal_mcc_calc_tx_bytes_from_kernel - calculte tx bytes from kernel to check concurrent tx or not + * @padapter: the adapter to be record tx bytes + * @len: data len + */ +inline void rtw_hal_mcc_calc_tx_bytes_from_kernel(PADAPTER padapter, u32 len) +{ + struct mcc_adapter_priv *pmccadapriv = &padapter->mcc_adapterpriv; + + if (MCC_EN(padapter)) { + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC)) { + pmccadapriv->mcc_tx_bytes_from_kernel += len; + if (0) + RTW_INFO("%s(order:%d): mcc tx bytes from kernel:%lld\n" + , __func__, pmccadapriv->order, pmccadapriv->mcc_tx_bytes_from_kernel); + } + } +} + +/** + * rtw_hal_mcc_calc_tx_bytes_to_port - calculte tx bytes to write port in order to flow crtl + * @padapter: the adapter to be record tx bytes + * @len: data len + */ +inline void rtw_hal_mcc_calc_tx_bytes_to_port(PADAPTER padapter, u32 len) +{ + if (MCC_EN(padapter)) { + struct mcc_obj_priv *pmccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + struct mcc_adapter_priv *pmccadapriv = &padapter->mcc_adapterpriv; + + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC)) { + pmccadapriv->mcc_tx_bytes_to_port += len; + if (0) + RTW_INFO("%s(order:%d): mcc tx bytes to port:%d, mcc target tx bytes to port:%d\n" + , __func__, pmccadapriv->order, pmccadapriv->mcc_tx_bytes_to_port + , pmccadapriv->mcc_target_tx_bytes_to_port); + } + } +} + +/** + * rtw_hal_mcc_stop_tx_bytes_to_port - stop write port to hw or not + * @padapter: the adapter to be stopped + */ +inline u8 rtw_hal_mcc_stop_tx_bytes_to_port(PADAPTER padapter) +{ + if (MCC_EN(padapter)) { + struct mcc_obj_priv *pmccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + struct mcc_adapter_priv *pmccadapriv = &padapter->mcc_adapterpriv; + + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC)) { + if (pmccadapriv->mcc_tp_limit) { + if (pmccadapriv->mcc_tx_bytes_to_port >= pmccadapriv->mcc_target_tx_bytes_to_port) { + pmccadapriv->mcc_tx_stop = _TRUE; + rtw_netif_stop_queue(padapter->pnetdev); + return _TRUE; + } + } + } + } + + return _FALSE; +} + +static void rtw_hal_mcc_assign_scan_flag(PADAPTER padapter, u8 scan_done) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_adapter_priv *mccadapriv = NULL; + _adapter *iface = NULL; + struct mlme_ext_priv *pmlmeext = NULL; + u8 i = 0, flags; + + if (!MCC_EN(padapter)) + return; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (iface == NULL) + continue; + + mccadapriv = &iface->mcc_adapterpriv; + if (mccadapriv->role == MCC_ROLE_MAX) + continue; + + pmlmeext = &iface->mlmeextpriv; + if (is_client_associated_to_ap(iface)) { + flags = mlmeext_scan_backop_flags_sta(pmlmeext); + if (scan_done) { + if (mlmeext_chk_scan_backop_flags_sta(pmlmeext, SS_BACKOP_EN)) { + flags &= ~SS_BACKOP_EN; + mlmeext_assign_scan_backop_flags_sta(pmlmeext, flags); + } + } else { + if (!mlmeext_chk_scan_backop_flags_sta(pmlmeext, SS_BACKOP_EN)) { + flags |= SS_BACKOP_EN; + mlmeext_assign_scan_backop_flags_sta(pmlmeext, flags); + } + } + + } + } +} + +/** + * rtw_hal_set_mcc_setting_scan_start - setting mcc under scan start + * @padapter: the adapter to be setted + * @ch_setting_changed: softap channel setting to be changed or not + */ +u8 rtw_hal_set_mcc_setting_scan_start(PADAPTER padapter) +{ + u8 ret = _FAIL; + + if (MCC_EN(padapter)) { + struct mcc_obj_priv *pmccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + + _enter_critical_mutex(&pmccobjpriv->mcc_mutex, NULL); + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_NEED_MCC)) { + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC)) { + ret = rtw_hal_set_mcc_setting(padapter, MCC_SETCMD_STATUS_STOP_SCAN_START); + rtw_hal_mcc_assign_scan_flag(padapter, 0); + } + } + _exit_critical_mutex(&pmccobjpriv->mcc_mutex, NULL); + } + + return ret; +} + +/** + * rtw_hal_set_mcc_setting_scan_complete - setting mcc after scan commplete + * @padapter: the adapter to be setted + * @ch_setting_changed: softap channel setting to be changed or not + */ +u8 rtw_hal_set_mcc_setting_scan_complete(PADAPTER padapter) +{ + u8 ret = _FAIL; + + if (MCC_EN(padapter)) { + struct mcc_obj_priv *pmccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + + _enter_critical_mutex(&pmccobjpriv->mcc_mutex, NULL); + + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_NEED_MCC)) { + rtw_hal_mcc_assign_scan_flag(padapter, 1); + ret = rtw_hal_set_mcc_setting(padapter, MCC_SETCMD_STATUS_START_SCAN_DONE); + } + _exit_critical_mutex(&pmccobjpriv->mcc_mutex, NULL); + } + + return ret; +} + + +/** + * rtw_hal_set_mcc_setting_start_bss_network - setting mcc under softap start + * @padapter: the adapter to be setted + * @chbw_grouped: channel bw offset can not be allowed or not + */ +u8 rtw_hal_set_mcc_setting_start_bss_network(PADAPTER padapter, u8 chbw_allow) +{ + u8 ret = _FAIL; + + if (MCC_EN(padapter)) { + /* channel bw offset can not be allowed, start MCC */ + if (chbw_allow == _FALSE) { + struct mcc_obj_priv *pmccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + + //rtw_hal_mcc_restore_iqk_val(padapter); + _enter_critical_mutex(&pmccobjpriv->mcc_mutex, NULL); + ret = rtw_hal_set_mcc_setting(padapter, MCC_SETCMD_STATUS_START_CONNECT); + _exit_critical_mutex(&pmccobjpriv->mcc_mutex, NULL); + + if (ret == _FAIL) { /* MCC Start fail, AP/GO switch to buddy's channel */ + u8 ch_to_set = 0, bw_to_set, offset_to_set; + + rtw_hal_set_mcc_status(padapter, MCC_STATUS_NEED_MCC | MCC_STATUS_DOING_MCC); + rtw_hal_set_mcc_setting_disconnect(padapter); + if (rtw_mi_get_ch_setting_union_no_self( + padapter, &ch_to_set, &bw_to_set, + &offset_to_set) != 0) { + PHAL_DATA_TYPE hal = GET_HAL_DATA(padapter); + u8 doiqk = _TRUE; + + rtw_hal_set_hwreg(padapter, HW_VAR_DO_IQK, &doiqk); + hal->current_channel = 0; + hal->current_channel_bw = CHANNEL_WIDTH_MAX; + set_channel_bwmode(padapter, ch_to_set, offset_to_set, bw_to_set); + doiqk = _FALSE; + rtw_hal_set_hwreg(padapter, HW_VAR_DO_IQK, &doiqk); + } + } + } + } + + return ret; +} + +/** + * rtw_hal_set_mcc_setting_disconnect - setting mcc under mlme disconnect(stop softap/disconnect from AP) + * @padapter: the adapter to be setted + */ +u8 rtw_hal_set_mcc_setting_disconnect(PADAPTER padapter) +{ + u8 ret = _FAIL; + + if (MCC_EN(padapter)) { + struct mcc_obj_priv *pmccobjpriv = &(adapter_to_dvobj(padapter)->mcc_objpriv); + + _enter_critical_mutex(&pmccobjpriv->mcc_mutex, NULL); + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_NEED_MCC)) { + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC)) + ret = rtw_hal_set_mcc_setting(padapter, MCC_SETCMD_STATUS_STOP_DISCONNECT); + } + _exit_critical_mutex(&pmccobjpriv->mcc_mutex, NULL); + } + + return ret; +} + +/** + * rtw_hal_set_mcc_setting_join_done_chk_ch - setting mcc under join done + * @padapter: the adapter to be checked + */ +u8 rtw_hal_set_mcc_setting_join_done_chk_ch(PADAPTER padapter) +{ + u8 ret = _FAIL; + + if (MCC_EN(padapter)) { + struct mi_state mstate; + + rtw_mi_status_no_self(padapter, &mstate); + + if (MSTATE_STA_LD_NUM(&mstate) || MSTATE_STA_LG_NUM(&mstate) || MSTATE_AP_NUM(&mstate)) { + bool chbw_allow = _TRUE; + u8 u_ch, u_offset, u_bw; + struct mlme_ext_priv *cur_mlmeext = &padapter->mlmeextpriv; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + + if (rtw_mi_get_ch_setting_union_no_self(padapter, &u_ch, &u_bw, &u_offset) <= 0) { + dump_adapters_status(RTW_DBGDUMP , dvobj); + rtw_warn_on(1); + } + + RTW_INFO(FUNC_ADPT_FMT" union no self: %u,%u,%u\n" + , FUNC_ADPT_ARG(padapter), u_ch, u_bw, u_offset); + + /* chbw_allow? */ + chbw_allow = rtw_is_chbw_grouped(cur_mlmeext->cur_channel + , cur_mlmeext->cur_bwmode, cur_mlmeext->cur_ch_offset + , u_ch, u_bw, u_offset); + + RTW_INFO(FUNC_ADPT_FMT" chbw_allow:%d\n" + , FUNC_ADPT_ARG(padapter), chbw_allow); + + /* if chbw_allow = false, start MCC setting */ + if (chbw_allow == _FALSE) { + struct mcc_obj_priv *pmccobjpriv = &dvobj->mcc_objpriv; + + rtw_hal_mcc_restore_iqk_val(padapter); + _enter_critical_mutex(&pmccobjpriv->mcc_mutex, NULL); + ret = rtw_hal_set_mcc_setting(padapter, MCC_SETCMD_STATUS_START_CONNECT); + _exit_critical_mutex(&pmccobjpriv->mcc_mutex, NULL); + + if (ret == _FAIL) { /* MCC Start Fail, then disconenct client join */ + rtw_hal_set_mcc_status(padapter, MCC_STATUS_NEED_MCC | MCC_STATUS_DOING_MCC); + rtw_disassoc_cmd(padapter, 0, RTW_CMDF_DIRECTLY); + rtw_indicate_disconnect(padapter, 0, _FALSE); + rtw_free_assoc_resources(padapter, _TRUE); + rtw_free_network_queue(padapter, _TRUE); + } + } + } + } + + return ret; +} + +/** + * rtw_hal_set_mcc_setting_chk_start_clnt_join - check change channel under start clnt join + * @padapter: the adapter to be checked + * @ch: pointer to rerurn ch + * @bw: pointer to rerurn bw + * @offset: pointer to rerurn offset + * @chbw_allow: allow to use adapter's channel setting + */ +u8 rtw_hal_set_mcc_setting_chk_start_clnt_join(PADAPTER padapter, u8 *ch, u8 *bw, u8 *offset, u8 chbw_allow) +{ + u8 ret = _FAIL; + + /* if chbw_allow = false under en_mcc = TRUE, we do not change channel related setting */ + if (MCC_EN(padapter)) { + /* restore union channel related setting to current channel related setting */ + if (chbw_allow == _FALSE) { + struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv; + + /* issue null data to other interface connected to AP */ + rtw_hal_mcc_issue_null_data(padapter, chbw_allow, _TRUE); + + *ch = pmlmeext->cur_channel; + *bw = pmlmeext->cur_bwmode; + *offset = pmlmeext->cur_ch_offset; + + RTW_INFO(FUNC_ADPT_FMT" en_mcc:%d(%d,%d,%d,)\n" + , FUNC_ADPT_ARG(padapter), MCC_EN(padapter) + , *ch, *bw, *offset); + ret = _SUCCESS; + } + } + + return ret; +} + +static void rtw_hal_mcc_dump_noa_content(void *sel, PADAPTER padapter) +{ + struct mcc_adapter_priv *pmccadapriv = NULL; + u8 *pos = NULL; + pmccadapriv = &padapter->mcc_adapterpriv; + /* last position for NoA attribute */ + pos = pmccadapriv->p2p_go_noa_ie + pmccadapriv->p2p_go_noa_ie_len; + + + RTW_PRINT_SEL(sel, "\nStart to dump NoA Content\n"); + RTW_PRINT_SEL(sel, "NoA Counts:%d\n", *(pos - 13)); + RTW_PRINT_SEL(sel, "NoA Duration(TU):%d\n", (RTW_GET_LE32(pos - 12))/TU); + RTW_PRINT_SEL(sel, "NoA Interval(TU):%d\n", (RTW_GET_LE32(pos - 8))/TU); + RTW_PRINT_SEL(sel, "NoA Start time(microseconds):0x%02x\n", RTW_GET_LE32(pos - 4)); + RTW_PRINT_SEL(sel, "End to dump NoA Content\n"); +} + +static void mcc_dump_dbg_reg(void *sel, _adapter *adapter) +{ + struct mcc_obj_priv *mccobjpriv = adapter_to_mccobjpriv(adapter); + struct hal_spec_t *hal_spec = GET_HAL_SPEC(adapter); + u8 i,j; + _irqL irqL; + + _enter_critical_bh(&mccobjpriv->mcc_lock, &irqL); + RTW_PRINT_SEL(sel, "current order=%d\n", mccobjpriv->current_order); + _exit_critical_bh(&mccobjpriv->mcc_lock, &irqL); + + _enter_critical_mutex(&mccobjpriv->mcc_dbg_reg_mutex, NULL); + for (i = 0; i < ARRAY_SIZE(mccobjpriv->dbg_reg); i++) + RTW_PRINT_SEL(sel, "REG_0x%X:0x%08x\n", mccobjpriv->dbg_reg[i], mccobjpriv->dbg_reg_val[i]); + + for (i = 0; i < ARRAY_SIZE(mccobjpriv->dbg_rf_reg); i++) { + for (j = 0; j < hal_spec->rf_reg_path_num; j++) + RTW_PRINT_SEL(sel, "RF_PATH_%d_REG_0x%X:0x%08x\n", + j, mccobjpriv->dbg_rf_reg[i], mccobjpriv->dbg_rf_reg_val[i][j]); + } + _exit_critical_mutex(&mccobjpriv->mcc_dbg_reg_mutex, NULL); +} + + +void rtw_hal_dump_mcc_info(void *sel, struct dvobj_priv *dvobj) +{ + struct mcc_obj_priv *mccobjpriv = &(dvobj->mcc_objpriv); + struct mcc_adapter_priv *mccadapriv = NULL; + _adapter *iface = NULL, *pri_adapter = NULL; + struct registry_priv *regpriv = NULL; + HAL_DATA_TYPE *hal = NULL; + u8 i = 0, j = 0; + u64 tsf[MAX_MCC_NUM] = {0}; + + /* regpriv is common for all adapter */ + pri_adapter = dvobj_get_primary_adapter(dvobj); + hal = GET_HAL_DATA(pri_adapter); + + RTW_PRINT_SEL(sel, "**********************************************\n"); + RTW_PRINT_SEL(sel, "en_mcc:%d\n", MCC_EN(pri_adapter)); + RTW_PRINT_SEL(sel, "primary adapter("ADPT_FMT") duration:%d%c\n", + ADPT_ARG(dvobj_get_primary_adapter(dvobj)), mccobjpriv->duration, 37); + RTW_PRINT_SEL(sel, "runtime duration:%s\n", mccobjpriv->enable_runtime_duration ? "enable":"disable"); + RTW_PRINT_SEL(sel, "phydm offload:%s\n", mccobjpriv->mcc_phydm_offload ? "enable":"disable"); + + if (rtw_hal_check_mcc_status(pri_adapter, MCC_STATUS_DOING_MCC)) { + rtw_hal_mcc_rqt_tsf(pri_adapter, tsf); + + for (i = 0; i < MAX_MCC_NUM; i++) { + iface = mccobjpriv->iface[i]; + if (!iface) + continue; + + regpriv = &iface->registrypriv; + mccadapriv = &iface->mcc_adapterpriv; + + if (mccadapriv) { + u8 p2p_ps_mode = iface->wdinfo.p2p_ps_mode; + + RTW_PRINT_SEL(sel, "adapter mcc info:\n"); + RTW_PRINT_SEL(sel, "ifname:%s\n", ADPT_ARG(iface)); + RTW_PRINT_SEL(sel, "order:%d\n", mccadapriv->order); + RTW_PRINT_SEL(sel, "duration:%d\n", mccadapriv->mcc_duration); + RTW_PRINT_SEL(sel, "target tx bytes:%d\n", mccadapriv->mcc_target_tx_bytes_to_port); + RTW_PRINT_SEL(sel, "current TP:%d\n", mccadapriv->mcc_tp); + RTW_PRINT_SEL(sel, "mgmt queue macid:%d\n", mccadapriv->mgmt_queue_macid); + RTW_PRINT_SEL(sel, "macid bitmap:0x%02x\n", mccadapriv->mcc_macid_bitmap); + RTW_PRINT_SEL(sel, "P2P NoA:%s\n\n", p2p_ps_mode == P2P_PS_NOA ? "enable":"disable"); + RTW_PRINT_SEL(sel, "registry data:\n"); + RTW_PRINT_SEL(sel, "ap target tx TP(BW:20M):%d Mbps\n", regpriv->rtw_mcc_ap_bw20_target_tx_tp); + RTW_PRINT_SEL(sel, "ap target tx TP(BW:40M):%d Mbps\n", regpriv->rtw_mcc_ap_bw40_target_tx_tp); + RTW_PRINT_SEL(sel, "ap target tx TP(BW:80M):%d Mbps\n", regpriv->rtw_mcc_ap_bw80_target_tx_tp); + RTW_PRINT_SEL(sel, "sta target tx TP(BW:20M):%d Mbps\n", regpriv->rtw_mcc_sta_bw20_target_tx_tp); + RTW_PRINT_SEL(sel, "sta target tx TP(BW:40M ):%d Mbps\n", regpriv->rtw_mcc_sta_bw40_target_tx_tp); + RTW_PRINT_SEL(sel, "sta target tx TP(BW:80M):%d Mbps\n", regpriv->rtw_mcc_sta_bw80_target_tx_tp); + RTW_PRINT_SEL(sel, "single tx criteria:%d Mbps\n", regpriv->rtw_mcc_single_tx_cri); + RTW_PRINT_SEL(sel, "HW TSF=0x%llx\n", tsf[mccadapriv->order]); + if (MLME_IS_GO(iface)) + rtw_hal_mcc_dump_noa_content(sel, iface); + RTW_PRINT_SEL(sel, "**********************************************\n"); + } + } + + mcc_dump_dbg_reg(sel, pri_adapter); + } + + #ifdef CONFIG_MCC_PHYDM_OFFLOAD + RTW_PRINT_SEL(sel, "@@@@@@@@@@@@@@@@@@@@\n"); + rtw_hal_mcc_cfg_phydm(pri_adapter, MCC_CFG_PHYDM_DUMP, sel); + RTW_PRINT_SEL(sel, "@@@@@@@@@@@@@@@@@@@@\n"); + #endif + + RTW_PRINT_SEL(sel, "------------------------------------------\n"); + RTW_PRINT_SEL(sel, "policy index:%d\n", mccobjpriv->policy_index); + RTW_PRINT_SEL(sel, "------------------------------------------\n"); + RTW_PRINT_SEL(sel, "define data:\n"); + RTW_PRINT_SEL(sel, "ap target tx TP(BW:20M):%d Mbps\n", MCC_AP_BW20_TARGET_TX_TP); + RTW_PRINT_SEL(sel, "ap target tx TP(BW:40M):%d Mbps\n", MCC_AP_BW40_TARGET_TX_TP); + RTW_PRINT_SEL(sel, "ap target tx TP(BW:80M):%d Mbps\n", MCC_AP_BW80_TARGET_TX_TP); + RTW_PRINT_SEL(sel, "sta target tx TP(BW:20M):%d Mbps\n", MCC_STA_BW20_TARGET_TX_TP); + RTW_PRINT_SEL(sel, "sta target tx TP(BW:40M):%d Mbps\n", MCC_STA_BW40_TARGET_TX_TP); + RTW_PRINT_SEL(sel, "sta target tx TP(BW:80M):%d Mbps\n", MCC_STA_BW80_TARGET_TX_TP); + RTW_PRINT_SEL(sel, "single tx criteria:%d Mbps\n", MCC_SINGLE_TX_CRITERIA); + RTW_PRINT_SEL(sel, "------------------------------------------\n"); +} + +inline void update_mcc_mgntframe_attrib(_adapter *padapter, struct pkt_attrib *pattrib) +{ + if (MCC_EN(padapter)) { + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC)) { + /* use QSLT_MGNT to check mgnt queue or bcn queue */ + if (pattrib->qsel == QSLT_MGNT) { + pattrib->mac_id = padapter->mcc_adapterpriv.mgmt_queue_macid; + pattrib->qsel = QSLT_VO; + } + } + } +} + +inline u8 rtw_hal_mcc_link_status_chk(_adapter *padapter, const char *msg) +{ + u8 ret = _TRUE, i = 0; + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + _adapter *iface; + struct mlme_ext_priv *mlmeext; + + if (MCC_EN(padapter)) { + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_NEED_MCC)) { + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + mlmeext = &iface->mlmeextpriv; + if (mlmeext_scan_state(mlmeext) != SCAN_DISABLE) { + #ifdef DBG_EXPIRATION_CHK + RTW_INFO(FUNC_ADPT_FMT" don't enter %s under scan for MCC mode\n", FUNC_ADPT_ARG(padapter), msg); + #endif + ret = _FALSE; + goto exit; + } + } + } + } + +exit: + return ret; +} + +void rtw_hal_mcc_issue_null_data(_adapter *padapter, u8 chbw_allow, u8 ps_mode) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + _adapter *iface = NULL; + systime start = rtw_get_current_time(); + u8 i = 0; + + if (!MCC_EN(padapter)) + return; + + if (rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC)) + return; + + if (chbw_allow == _TRUE) + return; + + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + /* issue null data to inform ap station will leave */ + if (is_client_associated_to_ap(iface)) { + struct mlme_ext_priv *mlmeext = &iface->mlmeextpriv; + struct mlme_ext_info *mlmeextinfo = &mlmeext->mlmext_info; + u8 ch = mlmeext->cur_channel; + u8 bw = mlmeext->cur_bwmode; + u8 offset = mlmeext->cur_ch_offset; + struct sta_info *sta = rtw_get_stainfo(&iface->stapriv, get_my_bssid(&(mlmeextinfo->network))); + + if (!sta) + continue; + + set_channel_bwmode(iface, ch, offset, bw); + + if (ps_mode) + rtw_hal_macid_sleep(iface, sta->cmn.mac_id); + else + rtw_hal_macid_wakeup(iface, sta->cmn.mac_id); + + issue_nulldata(iface, NULL, ps_mode, 3, 50); + } + } + RTW_INFO("%s(%d ms)\n", __func__, rtw_get_passing_time_ms(start)); +} + +u8 *rtw_hal_mcc_append_go_p2p_ie(PADAPTER padapter, u8 *pframe, u32 *len) +{ + struct mcc_adapter_priv *pmccadapriv = &padapter->mcc_adapterpriv; + + if (!MCC_EN(padapter)) + return pframe; + + if (!rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC)) + return pframe; + + if (pmccadapriv->p2p_go_noa_ie_len == 0) + return pframe; + + _rtw_memcpy(pframe, pmccadapriv->p2p_go_noa_ie, pmccadapriv->p2p_go_noa_ie_len); + *len = *len + pmccadapriv->p2p_go_noa_ie_len; + + return pframe + pmccadapriv->p2p_go_noa_ie_len; +} + +void rtw_hal_dump_mcc_policy_table(void *sel) +{ + u8 idx = 0; + RTW_PRINT_SEL(sel, "duration\t,tsf sync offset\t,start time offset\t,interval\t,guard offset0\t,guard offset1\n"); + + for (idx = 0; idx < mcc_max_policy_num; idx ++) { + RTW_PRINT_SEL(sel, "%d\t\t,%d\t\t\t,%d\t\t\t,%d\t\t,%d\t\t,%d\n" + , mcc_switch_channel_policy_table[idx][MCC_DURATION_IDX] + , mcc_switch_channel_policy_table[idx][MCC_TSF_SYNC_OFFSET_IDX] + , mcc_switch_channel_policy_table[idx][MCC_START_TIME_OFFSET_IDX] + , mcc_switch_channel_policy_table[idx][MCC_INTERVAL_IDX] + , mcc_switch_channel_policy_table[idx][MCC_GUARD_OFFSET0_IDX] + , mcc_switch_channel_policy_table[idx][MCC_GUARD_OFFSET1_IDX]); + } +} + +void rtw_hal_mcc_update_macid_bitmap(PADAPTER padapter, int mac_id, u8 add) +{ + struct mcc_adapter_priv *pmccadapriv = &padapter->mcc_adapterpriv; + + if (!MCC_EN(padapter)) + return; + + if (!rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC)) + return; + + if (pmccadapriv->role == MCC_ROLE_GC || pmccadapriv->role == MCC_ROLE_STA) + return; + + if (mac_id < 0) { + RTW_WARN("%s: mac_id < 0(%d)\n", __func__, mac_id); + return; + } + + RTW_INFO(ADPT_FMT" %s macid=%d, ori mcc_macid_bitmap=0x%08x\n" + , ADPT_ARG(padapter), add ? "add" : "clear" + , mac_id, pmccadapriv->mcc_macid_bitmap); + + if (add) { + #ifdef CONFIG_MCC_PHYDM_OFFLOAD + rtw_hal_mcc_cfg_phydm(padapter, MCC_CFG_PHYDM_ADD_CLIENT, &mac_id); + #endif + pmccadapriv->mcc_macid_bitmap |= BIT(mac_id); + } else { + #ifdef CONFIG_MCC_PHYDM_OFFLOAD + rtw_hal_mcc_cfg_phydm(padapter, MCC_CFG_PHYDM_REMOVE_CLIENT, &mac_id); + #endif + pmccadapriv->mcc_macid_bitmap &= ~(BIT(mac_id)); + } + rtw_hal_set_mcc_macid_cmd(padapter); +} + +void rtw_hal_mcc_process_noa(PADAPTER padapter) +{ + struct wifidirect_info *pwdinfo = &(padapter->wdinfo); + struct dvobj_priv *dvobj = adapter_to_dvobj(padapter); + struct mcc_obj_priv *pmccobjpriv = &(dvobj->mcc_objpriv); + + if (!MCC_EN(padapter)) + return; + + if (!rtw_hal_check_mcc_status(padapter, MCC_STATUS_DOING_MCC)) + return; + + if (!MLME_IS_GC(padapter)) + return; + + switch(pwdinfo->p2p_ps_mode) { + case P2P_PS_NONE: + RTW_INFO("[MCC] Disable NoA under MCC\n"); + rtw_hal_mcc_update_parameter(padapter, _TRUE); + break; + case P2P_PS_NOA: + RTW_INFO("[MCC] Enable NoA under MCC\n"); + break; + default: + break; + + } +} + +void rtw_hal_mcc_parameter_init(PADAPTER padapter) +{ + if (!padapter->registrypriv.en_mcc) + return; + + if (is_primary_adapter(padapter)) { + SET_MCC_EN_FLAG(padapter, padapter->registrypriv.en_mcc); + SET_MCC_DURATION(padapter, padapter->registrypriv.rtw_mcc_duration); + SET_MCC_RUNTIME_DURATION(padapter, padapter->registrypriv.rtw_mcc_enable_runtime_duration); + SET_MCC_PHYDM_OFFLOAD(padapter, padapter->registrypriv.rtw_mcc_phydm_offload); + } +} + + +static u8 set_mcc_duration_hdl(PADAPTER adapter, const u8 *val) +{ + struct dvobj_priv *dvobj = adapter_to_dvobj(adapter); + struct mcc_obj_priv *mccobjpriv = &(dvobj->mcc_objpriv); + _adapter *iface = NULL; + u8 duration = 50; + u8 ret = _SUCCESS, noa_enable = _FALSE, i = 0; + enum mcc_duration_setting type; + + if (!mccobjpriv->enable_runtime_duration) + goto exit; + +#ifdef CONFIG_P2P_PS + /* check noa enable or not */ + for (i = 0; i < dvobj->iface_nums; i++) { + iface = dvobj->padapters[i]; + if (iface->wdinfo.p2p_ps_mode == P2P_PS_NOA) { + noa_enable = _TRUE; + break; + } + } +#endif /* CONFIG_P2P_PS */ + + type = val[0]; + duration = val[1]; + + if (type == MCC_DURATION_MAPPING) { + switch (duration) { + /* 0 = fair scheduling */ + case 0: + mccobjpriv->duration= 40; + mccobjpriv->policy_index = 2; + mccobjpriv->mchan_sched_mode = MCC_FAIR_SCHEDULE; + break; + /* 1 = favor STA */ + case 1: + mccobjpriv->duration= 70; + mccobjpriv->policy_index = 1; + mccobjpriv->mchan_sched_mode = MCC_FAVOR_STA; + break; + /* 2 = favor P2P*/ + case 2: + default: + mccobjpriv->duration= 30; + mccobjpriv->policy_index = 0; + mccobjpriv->mchan_sched_mode = MCC_FAVOR_P2P; + break; + } + } else { + mccobjpriv->duration = duration; + rtw_hal_mcc_update_policy_table(adapter); + } + + /* only update sw parameter under MCC + it will be force update during */ + if (noa_enable) + goto exit; + + if (rtw_hal_check_mcc_status(adapter, MCC_STATUS_DOING_MCC)) + rtw_hal_mcc_update_parameter(adapter, _TRUE); +exit: + return ret; +} + +u8 rtw_set_mcc_duration_cmd(_adapter *adapter, u8 type, u8 val) +{ + struct cmd_obj *cmdobj; + struct drvextra_cmd_parm *pdrvextra_cmd_parm; + struct cmd_priv *pcmdpriv = &adapter->cmdpriv; + u8 *buf = NULL; + u8 sz = 2; + u8 res = _SUCCESS; + + + cmdobj = (struct cmd_obj *)rtw_zmalloc(sizeof(struct cmd_obj)); + if (cmdobj == NULL) { + res = _FAIL; + goto exit; + } + + pdrvextra_cmd_parm = (struct drvextra_cmd_parm *)rtw_zmalloc(sizeof(struct drvextra_cmd_parm)); + if (pdrvextra_cmd_parm == NULL) { + rtw_mfree((u8 *)cmdobj, sizeof(struct cmd_obj)); + res = _FAIL; + goto exit; + } + + buf = rtw_zmalloc(sizeof(u8) * sz); + if (buf == NULL) { + rtw_mfree((u8 *)cmdobj, sizeof(struct cmd_obj)); + rtw_mfree((u8 *)pdrvextra_cmd_parm, sizeof(struct drvextra_cmd_parm)); + res = _FAIL; + goto exit; + } + + pdrvextra_cmd_parm->ec_id = MCC_CMD_WK_CID; + pdrvextra_cmd_parm->type = MCC_SET_DURATION_WK_CID; + pdrvextra_cmd_parm->size = sz; + pdrvextra_cmd_parm->pbuf = buf; + + _rtw_memcpy(buf, &type, 1); + _rtw_memcpy(buf + 1, &val, 1); + + init_h2fwcmd_w_parm_no_rsp(cmdobj, pdrvextra_cmd_parm, CMD_SET_DRV_EXTRA); + res = rtw_enqueue_cmd(pcmdpriv, cmdobj); + +exit: + return res; +} + +#ifdef CONFIG_MCC_PHYDM_OFFLOAD +static u8 mcc_phydm_offload_enable_hdl(_adapter *adapter, const u8 *val) +{ + struct mcc_obj_priv *mccobjpriv = adapter_to_mccobjpriv(adapter); + u8 ret = _SUCCESS; + u8 enable = *val; + + /*only modify driver parameter during non-mcc status */ + if (!rtw_hal_check_mcc_status(adapter, MCC_STATUS_DOING_MCC)) { + mccobjpriv->mcc_phydm_offload = enable; + } else { + /*modify both driver & phydm parameter during mcc status */ + mccobjpriv->mcc_phydm_offload = enable; + rtw_hal_mcc_cfg_phydm(adapter, MCC_CFG_PHYDM_OFFLOAD, &mccobjpriv->mcc_phydm_offload); + } + + RTW_INFO("[MCC] phydm offload enable hdl(%d)\n", mccobjpriv->mcc_phydm_offload); + + return ret; +} + +u8 rtw_set_mcc_phydm_offload_enable_cmd(_adapter *adapter, u8 enable, u8 enqueue) +{ + u8 res = _SUCCESS; + + if (enqueue) { + struct cmd_obj *cmdobj; + struct drvextra_cmd_parm *pdrvextra_cmd_parm; + struct cmd_priv *pcmdpriv = &adapter->cmdpriv; + u8 *mcc_phydm_offload_enable = NULL; + + + cmdobj = (struct cmd_obj *)rtw_zmalloc(sizeof(struct cmd_obj)); + if (cmdobj == NULL) { + res = _FAIL; + goto exit; + } + + pdrvextra_cmd_parm = (struct drvextra_cmd_parm *)rtw_zmalloc(sizeof(struct drvextra_cmd_parm)); + if (pdrvextra_cmd_parm == NULL) { + rtw_mfree((u8 *)cmdobj, sizeof(struct cmd_obj)); + res = _FAIL; + goto exit; + } + + mcc_phydm_offload_enable = rtw_zmalloc(sizeof(u8)); + if (mcc_phydm_offload_enable == NULL) { + rtw_mfree((u8 *)cmdobj, sizeof(struct cmd_obj)); + rtw_mfree((u8 *)pdrvextra_cmd_parm, sizeof(struct drvextra_cmd_parm)); + res = _FAIL; + goto exit; + } + + pdrvextra_cmd_parm->ec_id = MCC_CMD_WK_CID; + pdrvextra_cmd_parm->type = MCC_SET_PHYDM_OFFLOAD_WK_CID; + pdrvextra_cmd_parm->size = 1; + pdrvextra_cmd_parm->pbuf = mcc_phydm_offload_enable; + + _rtw_memcpy(mcc_phydm_offload_enable, &enable, 1); + init_h2fwcmd_w_parm_no_rsp(cmdobj, pdrvextra_cmd_parm, CMD_SET_DRV_EXTRA); + res = rtw_enqueue_cmd(pcmdpriv, cmdobj); + } else { + mcc_phydm_offload_enable_hdl(adapter, &enable); + } + +exit: + return res; +} +#endif + +u8 rtw_mcc_cmd_hdl(_adapter *adapter, u8 type, const u8 *val) +{ + struct mcc_obj_priv *mccobjpriv = adapter_to_mccobjpriv(adapter); + u8 ret = _SUCCESS; + + switch (type) { + case MCC_SET_DURATION_WK_CID: + set_mcc_duration_hdl(adapter, val); + break; + case MCC_GET_DBG_REG_WK_CID: + mcc_get_reg_hdl(adapter, val); + break; + #ifdef CONFIG_MCC_PHYDM_OFFLOAD + case MCC_SET_PHYDM_OFFLOAD_WK_CID: + mcc_phydm_offload_enable_hdl(adapter, val); + break; + #endif + default: + RTW_ERR("[MCC] rtw_mcc_cmd_hdl fail(%d)\n", type); + break; + } + + + + return ret; +} + +#endif /* CONFIG_MCC_MODE */ diff --git a/hal/hal_mp.c b/hal/hal_mp.c new file mode 100644 index 0000000..e83584e --- /dev/null +++ b/hal/hal_mp.c @@ -0,0 +1,2657 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#define _HAL_MP_C_ + +#include + +#ifdef CONFIG_MP_INCLUDED + +#ifdef RTW_HALMAC + #include /* struct HAL_DATA_TYPE, RF register definition and etc. */ +#else /* !RTW_HALMAC */ + #ifdef CONFIG_RTL8188E + #include + #endif + #ifdef CONFIG_RTL8723B + #include + #endif + #ifdef CONFIG_RTL8192E + #include + #endif + #ifdef CONFIG_RTL8814A + #include + #endif + #if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A) + #include + #endif + #ifdef CONFIG_RTL8703B + #include + #endif + #ifdef CONFIG_RTL8723D + #include + #endif + #ifdef CONFIG_RTL8710B + #include + #endif + #ifdef CONFIG_RTL8188F + #include + #endif + #ifdef CONFIG_RTL8188GTV + #include + #endif + #ifdef CONFIG_RTL8192F + #include + #endif +#endif /* !RTW_HALMAC */ + + +u8 MgntQuery_NssTxRate(u16 Rate) +{ + u8 NssNum = RF_TX_NUM_NONIMPLEMENT; + + if ((Rate >= MGN_MCS8 && Rate <= MGN_MCS15) || + (Rate >= MGN_VHT2SS_MCS0 && Rate <= MGN_VHT2SS_MCS9)) + NssNum = RF_2TX; + else if ((Rate >= MGN_MCS16 && Rate <= MGN_MCS23) || + (Rate >= MGN_VHT3SS_MCS0 && Rate <= MGN_VHT3SS_MCS9)) + NssNum = RF_3TX; + else if ((Rate >= MGN_MCS24 && Rate <= MGN_MCS31) || + (Rate >= MGN_VHT4SS_MCS0 && Rate <= MGN_VHT4SS_MCS9)) + NssNum = RF_4TX; + else + NssNum = RF_1TX; + + return NssNum; +} + +void hal_mpt_SwitchRfSetting(PADAPTER pAdapter) +{ + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + u8 ChannelToSw = pMptCtx->MptChannelToSw; + u32 ulRateIdx = pMptCtx->mpt_rate_index; + u32 ulbandwidth = pMptCtx->MptBandWidth; + + /* <20120525, Kordan> Dynamic mechanism for APK, asked by Dennis.*/ + if (IS_HARDWARE_TYPE_8188ES(pAdapter) && (1 <= ChannelToSw && ChannelToSw <= 11) && + (ulRateIdx == MPT_RATE_MCS0 || ulRateIdx == MPT_RATE_1M || ulRateIdx == MPT_RATE_6M)) { + pMptCtx->backup0x52_RF_A = (u8)phy_query_rf_reg(pAdapter, RF_PATH_A, RF_0x52, 0x000F0); + pMptCtx->backup0x52_RF_B = (u8)phy_query_rf_reg(pAdapter, RF_PATH_B, RF_0x52, 0x000F0); + + if ((PlatformEFIORead4Byte(pAdapter, 0xF4) & BIT29) == BIT29) { + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_0x52, 0x000F0, 0xB); + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_0x52, 0x000F0, 0xB); + } else { + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_0x52, 0x000F0, 0xD); + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_0x52, 0x000F0, 0xD); + } + } else if (IS_HARDWARE_TYPE_8188EE(pAdapter)) { /* <20140903, VincentL> Asked by RF Eason and Edlu*/ + if (ChannelToSw == 3 && ulbandwidth == MPT_BW_40MHZ) { + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_0x52, 0x000F0, 0xB); /*RF 0x52 = 0x0007E4BD*/ + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_0x52, 0x000F0, 0xB); /*RF 0x52 = 0x0007E4BD*/ + } else { + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_0x52, 0x000F0, 0x9); /*RF 0x52 = 0x0007E49D*/ + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_0x52, 0x000F0, 0x9); /*RF 0x52 = 0x0007E49D*/ + } + } else if (IS_HARDWARE_TYPE_8188E(pAdapter)) { + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_0x52, 0x000F0, pMptCtx->backup0x52_RF_A); + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_0x52, 0x000F0, pMptCtx->backup0x52_RF_B); + } +} + +s32 hal_mpt_SetPowerTracking(PADAPTER padapter, u8 enable) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct dm_struct *pDM_Odm = &(pHalData->odmpriv); + + + if (!netif_running(padapter->pnetdev)) { + return _FAIL; + } + + if (check_fwstate(&padapter->mlmepriv, WIFI_MP_STATE) == _FALSE) { + return _FAIL; + } + if (enable) + pDM_Odm->rf_calibrate_info.txpowertrack_control = _TRUE; + else + pDM_Odm->rf_calibrate_info.txpowertrack_control = _FALSE; + + return _SUCCESS; +} + +void hal_mpt_GetPowerTracking(PADAPTER padapter, u8 *enable) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter); + struct dm_struct *pDM_Odm = &(pHalData->odmpriv); + + + *enable = pDM_Odm->rf_calibrate_info.txpowertrack_control; +} + + +void hal_mpt_CCKTxPowerAdjust(PADAPTER Adapter, BOOLEAN bInCH14) +{ + u32 TempVal = 0, TempVal2 = 0, TempVal3 = 0; + u32 CurrCCKSwingVal = 0, CCKSwingIndex = 12; + u8 i; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + PMPT_CONTEXT pMptCtx = &(Adapter->mppriv.mpt_ctx); + u8 u1Channel = pHalData->current_channel; + u32 ulRateIdx = pMptCtx->mpt_rate_index; + u8 DataRate = 0xFF; + + /* Do not modify CCK TX filter parameters for 8822B*/ + if(IS_HARDWARE_TYPE_8822B(Adapter) || IS_HARDWARE_TYPE_8821C(Adapter) || + IS_HARDWARE_TYPE_8723D(Adapter) || IS_HARDWARE_TYPE_8192F(Adapter) || IS_HARDWARE_TYPE_8822C(Adapter)) + return; + + DataRate = mpt_to_mgnt_rate(ulRateIdx); + + if (u1Channel == 14 && IS_CCK_RATE(DataRate)) + pHalData->bCCKinCH14 = TRUE; + else + pHalData->bCCKinCH14 = FALSE; + + if (IS_HARDWARE_TYPE_8703B(Adapter)) { + if ((u1Channel == 14) && IS_CCK_RATE(DataRate)) { + /* Channel 14 in CCK, need to set 0xA26~0xA29 to 0 for 8703B */ + phy_set_bb_reg(Adapter, rCCK0_TxFilter2, bMaskHWord, 0); + phy_set_bb_reg(Adapter, rCCK0_DebugPort, bMaskLWord, 0); + + } else { + /* Normal setting for 8703B, just recover to the default setting. */ + /* This hardcore values reference from the parameter which BB team gave. */ + for (i = 0 ; i < 2 ; ++i) + phy_set_bb_reg(Adapter, pHalData->RegForRecover[i].offset, bMaskDWord, pHalData->RegForRecover[i].value); + + } + } else if (IS_HARDWARE_TYPE_8723D(Adapter)) { + /* 2.4G CCK TX DFIR */ + /* 2016.01.20 Suggest from RS BB mingzhi*/ + if ((u1Channel == 14)) { + phy_set_bb_reg(Adapter, rCCK0_TxFilter2, bMaskDWord, 0x0000B81C); + phy_set_bb_reg(Adapter, rCCK0_DebugPort, bMaskDWord, 0x00000000); + phy_set_bb_reg(Adapter, 0xAAC, bMaskDWord, 0x00003667); + } else { + for (i = 0 ; i < 3 ; ++i) { + phy_set_bb_reg(Adapter, + pHalData->RegForRecover[i].offset, + bMaskDWord, + pHalData->RegForRecover[i].value); + } + } + } else if (IS_HARDWARE_TYPE_8188F(Adapter) || IS_HARDWARE_TYPE_8188GTV(Adapter)) { + /* get current cck swing value and check 0xa22 & 0xa23 later to match the table.*/ + CurrCCKSwingVal = read_bbreg(Adapter, rCCK0_TxFilter1, bMaskHWord); + CCKSwingIndex = 20; /* default index */ + + if (!pHalData->bCCKinCH14) { + /* Readback the current bb cck swing value and compare with the table to */ + /* get the current swing index */ + for (i = 0; i < CCK_TABLE_SIZE_88F; i++) { + if (((CurrCCKSwingVal & 0xff) == (u32)cck_swing_table_ch1_ch13_88f[i][0]) && + (((CurrCCKSwingVal & 0xff00) >> 8) == (u32)cck_swing_table_ch1_ch13_88f[i][1])) { + CCKSwingIndex = i; + break; + } + } + write_bbreg(Adapter, 0xa22, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][0]); + write_bbreg(Adapter, 0xa23, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][1]); + write_bbreg(Adapter, 0xa24, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][2]); + write_bbreg(Adapter, 0xa25, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][3]); + write_bbreg(Adapter, 0xa26, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][4]); + write_bbreg(Adapter, 0xa27, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][5]); + write_bbreg(Adapter, 0xa28, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][6]); + write_bbreg(Adapter, 0xa29, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][7]); + write_bbreg(Adapter, 0xa9a, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][8]); + write_bbreg(Adapter, 0xa9b, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][9]); + write_bbreg(Adapter, 0xa9c, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][10]); + write_bbreg(Adapter, 0xa9d, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][11]); + write_bbreg(Adapter, 0xaa0, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][12]); + write_bbreg(Adapter, 0xaa1, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][13]); + write_bbreg(Adapter, 0xaa2, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][14]); + write_bbreg(Adapter, 0xaa3, bMaskByte0, cck_swing_table_ch1_ch13_88f[CCKSwingIndex][15]); + RTW_INFO("%s , cck_swing_table_ch1_ch13_88f[%d]\n", __func__, CCKSwingIndex); + } else { + for (i = 0; i < CCK_TABLE_SIZE_88F; i++) { + if (((CurrCCKSwingVal & 0xff) == (u32)cck_swing_table_ch14_88f[i][0]) && + (((CurrCCKSwingVal & 0xff00) >> 8) == (u32)cck_swing_table_ch14_88f[i][1])) { + CCKSwingIndex = i; + break; + } + } + write_bbreg(Adapter, 0xa22, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][0]); + write_bbreg(Adapter, 0xa23, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][1]); + write_bbreg(Adapter, 0xa24, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][2]); + write_bbreg(Adapter, 0xa25, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][3]); + write_bbreg(Adapter, 0xa26, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][4]); + write_bbreg(Adapter, 0xa27, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][5]); + write_bbreg(Adapter, 0xa28, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][6]); + write_bbreg(Adapter, 0xa29, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][7]); + write_bbreg(Adapter, 0xa9a, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][8]); + write_bbreg(Adapter, 0xa9b, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][9]); + write_bbreg(Adapter, 0xa9c, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][10]); + write_bbreg(Adapter, 0xa9d, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][11]); + write_bbreg(Adapter, 0xaa0, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][12]); + write_bbreg(Adapter, 0xaa1, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][13]); + write_bbreg(Adapter, 0xaa2, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][14]); + write_bbreg(Adapter, 0xaa3, bMaskByte0, cck_swing_table_ch14_88f[CCKSwingIndex][15]); + RTW_INFO("%s , cck_swing_table_ch14_88f[%d]\n", __func__, CCKSwingIndex); + } + } else { + + /* get current cck swing value and check 0xa22 & 0xa23 later to match the table.*/ + CurrCCKSwingVal = read_bbreg(Adapter, rCCK0_TxFilter1, bMaskHWord); + + if (!pHalData->bCCKinCH14) { + /* Readback the current bb cck swing value and compare with the table to */ + /* get the current swing index */ + for (i = 0; i < CCK_TABLE_SIZE; i++) { + if (((CurrCCKSwingVal & 0xff) == (u32)cck_swing_table_ch1_ch13[i][0]) && + (((CurrCCKSwingVal & 0xff00) >> 8) == (u32)cck_swing_table_ch1_ch13[i][1])) { + CCKSwingIndex = i; + break; + } + } + + /*Write 0xa22 0xa23*/ + TempVal = cck_swing_table_ch1_ch13[CCKSwingIndex][0] + + (cck_swing_table_ch1_ch13[CCKSwingIndex][1] << 8); + + + /*Write 0xa24 ~ 0xa27*/ + TempVal2 = 0; + TempVal2 = cck_swing_table_ch1_ch13[CCKSwingIndex][2] + + (cck_swing_table_ch1_ch13[CCKSwingIndex][3] << 8) + + (cck_swing_table_ch1_ch13[CCKSwingIndex][4] << 16) + + (cck_swing_table_ch1_ch13[CCKSwingIndex][5] << 24); + + /*Write 0xa28 0xa29*/ + TempVal3 = 0; + TempVal3 = cck_swing_table_ch1_ch13[CCKSwingIndex][6] + + (cck_swing_table_ch1_ch13[CCKSwingIndex][7] << 8); + } else { + for (i = 0; i < CCK_TABLE_SIZE; i++) { + if (((CurrCCKSwingVal & 0xff) == (u32)cck_swing_table_ch14[i][0]) && + (((CurrCCKSwingVal & 0xff00) >> 8) == (u32)cck_swing_table_ch14[i][1])) { + CCKSwingIndex = i; + break; + } + } + + /*Write 0xa22 0xa23*/ + TempVal = cck_swing_table_ch14[CCKSwingIndex][0] + + (cck_swing_table_ch14[CCKSwingIndex][1] << 8); + + /*Write 0xa24 ~ 0xa27*/ + TempVal2 = 0; + TempVal2 = cck_swing_table_ch14[CCKSwingIndex][2] + + (cck_swing_table_ch14[CCKSwingIndex][3] << 8) + + (cck_swing_table_ch14[CCKSwingIndex][4] << 16) + + (cck_swing_table_ch14[CCKSwingIndex][5] << 24); + + /*Write 0xa28 0xa29*/ + TempVal3 = 0; + TempVal3 = cck_swing_table_ch14[CCKSwingIndex][6] + + (cck_swing_table_ch14[CCKSwingIndex][7] << 8); + } + + write_bbreg(Adapter, rCCK0_TxFilter1, bMaskHWord, TempVal); + write_bbreg(Adapter, rCCK0_TxFilter2, bMaskDWord, TempVal2); + write_bbreg(Adapter, rCCK0_DebugPort, bMaskLWord, TempVal3); + } + +} + +void hal_mpt_SetChannel(PADAPTER pAdapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + struct mp_priv *pmp = &pAdapter->mppriv; + u8 channel = pmp->channel; + u8 bandwidth = pmp->bandwidth; + + hal_mpt_SwitchRfSetting(pAdapter); + + pHalData->bSwChnl = _TRUE; + pHalData->bSetChnlBW = _TRUE; + + if (bandwidth == 2) { + rtw_hal_set_chnl_bw(pAdapter, channel, bandwidth, HAL_PRIME_CHNL_OFFSET_LOWER, HAL_PRIME_CHNL_OFFSET_UPPER); + } else if (bandwidth == 1) { + rtw_hal_set_chnl_bw(pAdapter, channel, bandwidth, HAL_PRIME_CHNL_OFFSET_UPPER, 0); + } else + rtw_hal_set_chnl_bw(pAdapter, channel, bandwidth, pmp->prime_channel_offset, 0); + + hal_mpt_CCKTxPowerAdjust(pAdapter, pHalData->bCCKinCH14); + rtw_btcoex_wifionly_scan_notify(pAdapter); + +} + +/* + * Notice + * Switch bandwitdth may change center frequency(channel) + */ +void hal_mpt_SetBandwidth(PADAPTER pAdapter) +{ + struct mp_priv *pmp = &pAdapter->mppriv; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + + u8 channel = pmp->channel; + u8 bandwidth = pmp->bandwidth; + + pHalData->bSwChnl = _TRUE; + pHalData->bSetChnlBW = _TRUE; + + if (bandwidth == 2) { + rtw_hal_set_chnl_bw(pAdapter, channel, bandwidth, HAL_PRIME_CHNL_OFFSET_LOWER, HAL_PRIME_CHNL_OFFSET_UPPER); + } else if (bandwidth == 1) { + rtw_hal_set_chnl_bw(pAdapter, channel, bandwidth, HAL_PRIME_CHNL_OFFSET_UPPER, 0); + } else + rtw_hal_set_chnl_bw(pAdapter, channel, bandwidth, pmp->prime_channel_offset, 0); + + hal_mpt_SwitchRfSetting(pAdapter); + rtw_btcoex_wifionly_scan_notify(pAdapter); + +} + +void mpt_SetTxPower_Old(PADAPTER pAdapter, MPT_TXPWR_DEF Rate, u8 *pTxPower) +{ + switch (Rate) { + case MPT_CCK: { + u32 TxAGC = 0, pwr = 0; + + pwr = pTxPower[RF_PATH_A]; + if (pwr < 0x3f) { + TxAGC = (pwr << 16) | (pwr << 8) | (pwr); + phy_set_bb_reg(pAdapter, rTxAGC_A_CCK1_Mcs32, bMaskByte1, pTxPower[RF_PATH_A]); + phy_set_bb_reg(pAdapter, rTxAGC_B_CCK11_A_CCK2_11, 0xffffff00, TxAGC); + } + pwr = pTxPower[RF_PATH_B]; + if (pwr < 0x3f) { + TxAGC = (pwr << 16) | (pwr << 8) | (pwr); + phy_set_bb_reg(pAdapter, rTxAGC_B_CCK11_A_CCK2_11, bMaskByte0, pTxPower[RF_PATH_B]); + phy_set_bb_reg(pAdapter, rTxAGC_B_CCK1_55_Mcs32, 0xffffff00, TxAGC); + } + } + break; + + case MPT_OFDM_AND_HT: { + u32 TxAGC = 0; + u8 pwr = 0; + + pwr = pTxPower[0]; + if (pwr < 0x3f) { + TxAGC |= ((pwr << 24) | (pwr << 16) | (pwr << 8) | pwr); + RTW_INFO("HT Tx-rf(A) Power = 0x%x\n", TxAGC); + phy_set_bb_reg(pAdapter, rTxAGC_A_Rate18_06, bMaskDWord, TxAGC); + phy_set_bb_reg(pAdapter, rTxAGC_A_Rate54_24, bMaskDWord, TxAGC); + phy_set_bb_reg(pAdapter, rTxAGC_A_Mcs03_Mcs00, bMaskDWord, TxAGC); + phy_set_bb_reg(pAdapter, rTxAGC_A_Mcs07_Mcs04, bMaskDWord, TxAGC); + phy_set_bb_reg(pAdapter, rTxAGC_A_Mcs11_Mcs08, bMaskDWord, TxAGC); + phy_set_bb_reg(pAdapter, rTxAGC_A_Mcs15_Mcs12, bMaskDWord, TxAGC); + } + TxAGC = 0; + pwr = pTxPower[1]; + if (pwr < 0x3f) { + TxAGC |= ((pwr << 24) | (pwr << 16) | (pwr << 8) | pwr); + RTW_INFO("HT Tx-rf(B) Power = 0x%x\n", TxAGC); + phy_set_bb_reg(pAdapter, rTxAGC_B_Rate18_06, bMaskDWord, TxAGC); + phy_set_bb_reg(pAdapter, rTxAGC_B_Rate54_24, bMaskDWord, TxAGC); + phy_set_bb_reg(pAdapter, rTxAGC_B_Mcs03_Mcs00, bMaskDWord, TxAGC); + phy_set_bb_reg(pAdapter, rTxAGC_B_Mcs07_Mcs04, bMaskDWord, TxAGC); + phy_set_bb_reg(pAdapter, rTxAGC_B_Mcs11_Mcs08, bMaskDWord, TxAGC); + phy_set_bb_reg(pAdapter, rTxAGC_B_Mcs15_Mcs12, bMaskDWord, TxAGC); + } + } + break; + + default: + break; + } + RTW_INFO("<===mpt_SetTxPower_Old()\n"); +} + +void +mpt_SetTxPower( + PADAPTER pAdapter, + MPT_TXPWR_DEF Rate, + u8 *pTxPower +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + + u8 path = 0 , i = 0, MaxRate = MGN_6M; + u8 StartPath = RF_PATH_A, EndPath = RF_PATH_B; + u8 tx_nss = 2; + + if (IS_HARDWARE_TYPE_8814A(pAdapter) || IS_HARDWARE_TYPE_8814B(pAdapter)) { + EndPath = RF_PATH_D; + tx_nss = 4; + } else if (IS_HARDWARE_TYPE_8188F(pAdapter) || IS_HARDWARE_TYPE_8188GTV(pAdapter) + || IS_HARDWARE_TYPE_8723D(pAdapter) || IS_HARDWARE_TYPE_8821C(pAdapter) || IS_HARDWARE_TYPE_8723F(pAdapter)) { + EndPath = RF_PATH_A; + tx_nss = 1; + } + + switch (Rate) { + case MPT_CCK: { + u8 rate[] = {MGN_1M, MGN_2M, MGN_5_5M, MGN_11M}; + + for (path = StartPath; path <= EndPath; path++) + for (i = 0; i < sizeof(rate); ++i) + PHY_SetTxPowerIndex(pAdapter, pTxPower[path], path, rate[i]); + } + break; + case MPT_OFDM: { + u8 rate[] = { + MGN_6M, MGN_9M, MGN_12M, MGN_18M, + MGN_24M, MGN_36M, MGN_48M, MGN_54M, + }; + + for (path = StartPath; path <= EndPath; path++) + for (i = 0; i < sizeof(rate); ++i) + PHY_SetTxPowerIndex(pAdapter, pTxPower[path], path, rate[i]); + } + break; + case MPT_HT: { + u8 rate[] = { + MGN_MCS0, MGN_MCS1, MGN_MCS2, MGN_MCS3, MGN_MCS4, + MGN_MCS5, MGN_MCS6, MGN_MCS7, MGN_MCS8, MGN_MCS9, + MGN_MCS10, MGN_MCS11, MGN_MCS12, MGN_MCS13, MGN_MCS14, + MGN_MCS15, MGN_MCS16, MGN_MCS17, MGN_MCS18, MGN_MCS19, + MGN_MCS20, MGN_MCS21, MGN_MCS22, MGN_MCS23, MGN_MCS24, + MGN_MCS25, MGN_MCS26, MGN_MCS27, MGN_MCS28, MGN_MCS29, + MGN_MCS30, MGN_MCS31, + }; + if (tx_nss == 4) + MaxRate = MGN_MCS31; + else if (tx_nss == 3) + MaxRate = MGN_MCS23; + else if (tx_nss == 2) + MaxRate = MGN_MCS15; + else + MaxRate = MGN_MCS7; + + for (path = StartPath; path <= EndPath; path++) { + for (i = 0; i < sizeof(rate); ++i) { + if (rate[i] > MaxRate) + break; + PHY_SetTxPowerIndex(pAdapter, pTxPower[path], path, rate[i]); + } + } + } + break; + case MPT_VHT: { + u8 rate[] = { + MGN_VHT1SS_MCS0, MGN_VHT1SS_MCS1, MGN_VHT1SS_MCS2, MGN_VHT1SS_MCS3, MGN_VHT1SS_MCS4, + MGN_VHT1SS_MCS5, MGN_VHT1SS_MCS6, MGN_VHT1SS_MCS7, MGN_VHT1SS_MCS8, MGN_VHT1SS_MCS9, + MGN_VHT2SS_MCS0, MGN_VHT2SS_MCS1, MGN_VHT2SS_MCS2, MGN_VHT2SS_MCS3, MGN_VHT2SS_MCS4, + MGN_VHT2SS_MCS5, MGN_VHT2SS_MCS6, MGN_VHT2SS_MCS7, MGN_VHT2SS_MCS8, MGN_VHT2SS_MCS9, + MGN_VHT3SS_MCS0, MGN_VHT3SS_MCS1, MGN_VHT3SS_MCS2, MGN_VHT3SS_MCS3, MGN_VHT3SS_MCS4, + MGN_VHT3SS_MCS5, MGN_VHT3SS_MCS6, MGN_VHT3SS_MCS7, MGN_VHT3SS_MCS8, MGN_VHT3SS_MCS9, + MGN_VHT4SS_MCS0, MGN_VHT4SS_MCS1, MGN_VHT4SS_MCS2, MGN_VHT4SS_MCS3, MGN_VHT4SS_MCS4, + MGN_VHT4SS_MCS5, MGN_VHT4SS_MCS6, MGN_VHT4SS_MCS7, MGN_VHT4SS_MCS8, MGN_VHT4SS_MCS9, + }; + if (tx_nss == 4) + MaxRate = MGN_VHT4SS_MCS9; + else if (tx_nss == 3) + MaxRate = MGN_VHT3SS_MCS9; + else if (tx_nss == 2) + MaxRate = MGN_VHT2SS_MCS9; + else + MaxRate = MGN_VHT1SS_MCS9; + + for (path = StartPath; path <= EndPath; path++) { + for (i = 0; i < sizeof(rate); ++i) { + if (rate[i] > MaxRate) + break; + PHY_SetTxPowerIndex(pAdapter, pTxPower[path], path, rate[i]); + } + } + } + break; + default: + RTW_INFO("<===mpt_SetTxPower: Illegal channel!!\n"); + break; + } +} + +void hal_mpt_SetTxPower(PADAPTER pAdapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + + if (pHalData->rf_chip < RF_CHIP_MAX) { + if (IS_HARDWARE_TYPE_8188E(pAdapter) || + IS_HARDWARE_TYPE_8723B(pAdapter) || + IS_HARDWARE_TYPE_8192E(pAdapter) || + IS_HARDWARE_TYPE_8703B(pAdapter) || + IS_HARDWARE_TYPE_8188F(pAdapter) || + IS_HARDWARE_TYPE_8188GTV(pAdapter) + ) { + + RTW_INFO("===> MPT_ProSetTxPower: Old\n"); + + mpt_SetTxPower_Old(pAdapter, MPT_CCK, pMptCtx->TxPwrLevel); + mpt_SetTxPower_Old(pAdapter, MPT_OFDM_AND_HT, pMptCtx->TxPwrLevel); + + } else { + + mpt_SetTxPower(pAdapter, MPT_CCK, pMptCtx->TxPwrLevel); + mpt_SetTxPower(pAdapter, MPT_OFDM, pMptCtx->TxPwrLevel); + mpt_SetTxPower(pAdapter, MPT_HT, pMptCtx->TxPwrLevel); + if(IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter)) { + RTW_INFO("===> MPT_ProSetTxPower: Jaguar/Jaguar2\n"); + mpt_SetTxPower(pAdapter, MPT_VHT, pMptCtx->TxPwrLevel); + } + } + + rtw_hal_set_txpwr_done(pAdapter); + } else + RTW_INFO("RFChipID < RF_CHIP_MAX, the RF chip is not supported - %d\n", pHalData->rf_chip); + + odm_clear_txpowertracking_state(pDM_Odm); +} + +void hal_mpt_SetDataRate(PADAPTER pAdapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + u32 DataRate; + + DataRate = mpt_to_mgnt_rate(pMptCtx->mpt_rate_index); + + hal_mpt_SwitchRfSetting(pAdapter); + + hal_mpt_CCKTxPowerAdjust(pAdapter, pHalData->bCCKinCH14); +#ifdef CONFIG_RTL8723B + if (IS_HARDWARE_TYPE_8723B(pAdapter)) { + if (IS_CCK_RATE(DataRate)) { + if (pMptCtx->mpt_rf_path == RF_PATH_A) + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x51, 0xF, 0x6); + else + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x71, 0xF, 0x6); + } else { + if (pMptCtx->mpt_rf_path == RF_PATH_A) + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x51, 0xF, 0xE); + else + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x71, 0xF, 0xE); + } + } + + if ((IS_HARDWARE_TYPE_8723BS(pAdapter) && + ((pHalData->PackageType == PACKAGE_TFBGA79) || (pHalData->PackageType == PACKAGE_TFBGA90)))) { + if (pMptCtx->mpt_rf_path == RF_PATH_A) + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x51, 0xF, 0xE); + else + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x71, 0xF, 0xE); + } +#endif +} + +#define RF_PATH_AB 22 + +#ifdef CONFIG_RTL8814A +void mpt_ToggleIG_8814A(PADAPTER pAdapter) +{ + u8 Path; + u32 IGReg = rA_IGI_Jaguar, IGvalue = 0; + + for (Path = 0; Path <= RF_PATH_D; Path++) { + switch (Path) { + case RF_PATH_B: + IGReg = rB_IGI_Jaguar; + break; + case RF_PATH_C: + IGReg = rC_IGI_Jaguar2; + break; + case RF_PATH_D: + IGReg = rD_IGI_Jaguar2; + break; + default: + IGReg = rA_IGI_Jaguar; + break; + } + + IGvalue = phy_query_bb_reg(pAdapter, IGReg, bMaskByte0); + phy_set_bb_reg(pAdapter, IGReg, bMaskByte0, IGvalue + 2); + phy_set_bb_reg(pAdapter, IGReg, bMaskByte0, IGvalue); + } +} + +void mpt_SetRFPath_8814A(PADAPTER pAdapter) +{ + + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + PMPT_CONTEXT pMptCtx = &pAdapter->mppriv.mpt_ctx; + R_ANTENNA_SELECT_OFDM *p_ofdm_tx; /* OFDM Tx register */ + R_ANTENNA_SELECT_CCK *p_cck_txrx; + u8 ForcedDataRate = mpt_to_mgnt_rate(pMptCtx->mpt_rate_index); + /*/PRT_HIGH_THROUGHPUT pHTInfo = GET_HT_INFO(pMgntInfo);*/ + /*/PRT_VERY_HIGH_THROUGHPUT pVHTInfo = GET_VHT_INFO(pMgntInfo);*/ + + u32 ulAntennaTx = pHalData->antenna_tx_path; + u32 ulAntennaRx = pHalData->AntennaRxPath; + u8 NssforRate = MgntQuery_NssTxRate(ForcedDataRate); + + if (NssforRate == RF_3TX) { + RTW_INFO("===> SetAntenna 3T Rate ForcedDataRate %d NssforRate %d AntennaTx %d\n", ForcedDataRate, NssforRate, ulAntennaTx); + + switch (ulAntennaTx) { + case ANTENNA_BCD: + pMptCtx->mpt_rf_path = RF_PATH_BCD; + /*pHalData->ValidTxPath = 0x0e;*/ + phy_set_bb_reg(pAdapter, rTxAnt_23Nsts_Jaguar2, 0x0fff0000, 0x90e); /*/ 0x940[27:16]=12'b0010_0100_0111*/ + break; + + case ANTENNA_ABC: + default: + pMptCtx->mpt_rf_path = RF_PATH_ABC; + /*pHalData->ValidTxPath = 0x0d;*/ + phy_set_bb_reg(pAdapter, rTxAnt_23Nsts_Jaguar2, 0x0fff0000, 0x247); /*/ 0x940[27:16]=12'b0010_0100_0111*/ + break; + } + + } else { /*/if(NssforRate == RF_1TX)*/ + RTW_INFO("===> SetAntenna for 1T/2T Rate, ForcedDataRate %d NssforRate %d AntennaTx %d\n", ForcedDataRate, NssforRate, ulAntennaTx); + switch (ulAntennaTx) { + case ANTENNA_BCD: + pMptCtx->mpt_rf_path = RF_PATH_BCD; + /*pHalData->ValidTxPath = 0x0e;*/ + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, 0xf0000000, 0x7); + phy_set_bb_reg(pAdapter, rTxAnt_1Nsts_Jaguar2, 0x000f00000, 0xe); + phy_set_bb_reg(pAdapter, rTxPath_Jaguar, 0xf0, 0xe); + break; + + case ANTENNA_BC: + pMptCtx->mpt_rf_path = RF_PATH_BC; + /*pHalData->ValidTxPath = 0x06;*/ + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, 0xf0000000, 0x6); + phy_set_bb_reg(pAdapter, rTxAnt_1Nsts_Jaguar2, 0x000f00000, 0x6); + phy_set_bb_reg(pAdapter, rTxPath_Jaguar, 0xf0, 0x6); + break; + case ANTENNA_B: + pMptCtx->mpt_rf_path = RF_PATH_B; + /*pHalData->ValidTxPath = 0x02;*/ + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, 0xf0000000, 0x4); /*/ 0xa07[7:4] = 4'b0100*/ + phy_set_bb_reg(pAdapter, rTxAnt_1Nsts_Jaguar2, 0xfff00000, 0x002); /*/ 0x93C[31:20]=12'b0000_0000_0010*/ + phy_set_bb_reg(pAdapter, rTxPath_Jaguar, 0xf0, 0x2); /* 0x80C[7:4] = 4'b0010*/ + break; + + case ANTENNA_C: + pMptCtx->mpt_rf_path = RF_PATH_C; + /*pHalData->ValidTxPath = 0x04;*/ + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, 0xf0000000, 0x2); /*/ 0xa07[7:4] = 4'b0010*/ + phy_set_bb_reg(pAdapter, rTxAnt_1Nsts_Jaguar2, 0xfff00000, 0x004); /*/ 0x93C[31:20]=12'b0000_0000_0100*/ + phy_set_bb_reg(pAdapter, rTxPath_Jaguar, 0xf0, 0x4); /*/ 0x80C[7:4] = 4'b0100*/ + break; + + case ANTENNA_D: + pMptCtx->mpt_rf_path = RF_PATH_D; + /*pHalData->ValidTxPath = 0x08;*/ + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, 0xf0000000, 0x1); /*/ 0xa07[7:4] = 4'b0001*/ + phy_set_bb_reg(pAdapter, rTxAnt_1Nsts_Jaguar2, 0xfff00000, 0x008); /*/ 0x93C[31:20]=12'b0000_0000_1000*/ + phy_set_bb_reg(pAdapter, rTxPath_Jaguar, 0xf0, 0x8); /*/ 0x80C[7:4] = 4'b1000*/ + break; + + case ANTENNA_A: + default: + pMptCtx->mpt_rf_path = RF_PATH_A; + /*pHalData->ValidTxPath = 0x01;*/ + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, 0xf0000000, 0x8); /*/ 0xa07[7:4] = 4'b1000*/ + phy_set_bb_reg(pAdapter, rTxAnt_1Nsts_Jaguar2, 0xfff00000, 0x001); /*/ 0x93C[31:20]=12'b0000_0000_0001*/ + phy_set_bb_reg(pAdapter, rTxPath_Jaguar, 0xf0, 0x1); /*/ 0x80C[7:4] = 4'b0001*/ + break; + } + } + + switch (ulAntennaRx) { + case ANTENNA_A: + /*pHalData->ValidRxPath = 0x01;*/ + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x1); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x2); + phy_set_bb_reg(pAdapter, rRxPath_Jaguar, bMaskByte0, 0x11); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x3); + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x0); + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, 0x0C000000, 0x0); + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_A_0x0[19:16] = 3, RX mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_B_0x0[19:16] = 1, Standby mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_C, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_C_0x0[19:16] = 1, Standby mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_D, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_D_0x0[19:16] = 1, Standby mode*/ + /*/ CCA related PD_delay_th*/ + phy_set_bb_reg(pAdapter, rAGC_table_Jaguar, 0x0F000000, 0x5); + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, 0x0000000F, 0xA); + break; + + case ANTENNA_B: + /*pHalData->ValidRxPath = 0x02;*/ + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x1); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x2); + phy_set_bb_reg(pAdapter, rRxPath_Jaguar, bMaskByte0, 0x22); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x3); + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x0); + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, 0x0C000000, 0x1); + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_A_0x0[19:16] = 1, Standby mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_B_0x0[19:16] = 3, RX mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_C, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_C_0x0[19:16] = 1, Standby mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_D, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_D_0x0[19:16] = 1, Standby mode*/ + /*/ CCA related PD_delay_th*/ + phy_set_bb_reg(pAdapter, rAGC_table_Jaguar, 0x0F000000, 0x5); + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, 0x0000000F, 0xA); + break; + + case ANTENNA_C: + /*pHalData->ValidRxPath = 0x04;*/ + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x1); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x2); + phy_set_bb_reg(pAdapter, rRxPath_Jaguar, bMaskByte0, 0x44); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x3); + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x0); + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, 0x0C000000, 0x2); + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_A_0x0[19:16] = 1, Standby mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_B_0x0[19:16] = 1, Standby mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_C, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_C_0x0[19:16] = 3, RX mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_D, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_D_0x0[19:16] = 1, Standby mode*/ + /*/ CCA related PD_delay_th*/ + phy_set_bb_reg(pAdapter, rAGC_table_Jaguar, 0x0F000000, 0x5); + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, 0x0000000F, 0xA); + break; + + case ANTENNA_D: + /*pHalData->ValidRxPath = 0x08;*/ + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x1); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x2); + phy_set_bb_reg(pAdapter, rRxPath_Jaguar, bMaskByte0, 0x88); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x3); + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x0); + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, 0x0C000000, 0x3); + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_A_0x0[19:16] = 1, Standby mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_B_0x0[19:16] = 1, Standby mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_C, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_C_0x0[19:16] = 1, Standby mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_D, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_D_0x0[19:16] = 3, RX mode*/ + /*/ CCA related PD_delay_th*/ + phy_set_bb_reg(pAdapter, rAGC_table_Jaguar, 0x0F000000, 0x5); + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, 0x0000000F, 0xA); + break; + + case ANTENNA_BC: + /*pHalData->ValidRxPath = 0x06;*/ + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x1); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x2); + phy_set_bb_reg(pAdapter, rRxPath_Jaguar, bMaskByte0, 0x66); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x3); + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x0); + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, 0x0f000000, 0x6); + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_A_0x0[19:16] = 1, Standby mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_B_0x0[19:16] = 3, RX mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_C, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_C_0x0[19:16] = 3, Rx mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_D, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_D_0x0[19:16] = 1, Standby mode*/ + /*/ CCA related PD_delay_th*/ + phy_set_bb_reg(pAdapter, rAGC_table_Jaguar, 0x0F000000, 0x5); + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, 0x0000000F, 0xA); + break; + + case ANTENNA_CD: + /*pHalData->ValidRxPath = 0x0C;*/ + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x1); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x2); + phy_set_bb_reg(pAdapter, rRxPath_Jaguar, bMaskByte0, 0xcc); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x3); + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x0); + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, 0x0f000000, 0xB); + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_A_0x0[19:16] = 1, Standby mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_B_0x0[19:16] = 1, Standby mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_C, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_C_0x0[19:16] = 3, Rx mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_D, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_D_0x0[19:16] = 3, RX mode*/ + /*/ CCA related PD_delay_th*/ + phy_set_bb_reg(pAdapter, rAGC_table_Jaguar, 0x0F000000, 0x5); + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, 0x0000000F, 0xA); + break; + + case ANTENNA_BCD: + /*pHalData->ValidRxPath = 0x0e;*/ + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x1); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x2); + phy_set_bb_reg(pAdapter, rRxPath_Jaguar, bMaskByte0, 0xee); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x3); + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x0); + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, 0x0f000000, 0x6); + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_A_0x0[19:16] = 1, Standby mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_B_0x0[19:16] = 3, RX mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_C, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_C_0x0[19:16] = 3, RX mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_D, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_D_0x0[19:16] = 3, Rx mode*/ + /*/ CCA related PD_delay_th*/ + phy_set_bb_reg(pAdapter, rAGC_table_Jaguar, 0x0F000000, 0x3); + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, 0x0000000F, 0x8); + break; + + case ANTENNA_ABCD: + /*pHalData->ValidRxPath = 0x0f;*/ + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x1); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x2); + phy_set_bb_reg(pAdapter, rRxPath_Jaguar, bMaskByte0, 0xff); + phy_set_bb_reg(pAdapter, 0x1000, bMaskByte2, 0x3); + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x0); + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, 0x0f000000, 0x1); + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_A_0x0[19:16] = 3, RX mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_B_0x0[19:16] = 3, RX mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_C, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_C_0x0[19:16] = 3, RX mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_D, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_D_0x0[19:16] = 3, RX mode*/ + /*/ CCA related PD_delay_th*/ + phy_set_bb_reg(pAdapter, rAGC_table_Jaguar, 0x0F000000, 0x3); + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, 0x0000000F, 0x8); + break; + + default: + break; + } + + PHY_Set_SecCCATH_by_RXANT_8814A(pAdapter, ulAntennaRx); + + mpt_ToggleIG_8814A(pAdapter); +} + +#endif /* CONFIG_RTL8814A */ +#if defined(CONFIG_RTL8814A) || defined(CONFIG_RTL8822B) || defined(CONFIG_RTL8821C) \ + || defined(CONFIG_RTL8822C) || defined(CONFIG_RTL8814B) || defined(CONFIG_RTL8723F) +void +mpt_SetSingleTone_8814A( + PADAPTER pAdapter, + BOOLEAN bSingleTone, + BOOLEAN bEnPMacTx) +{ + + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + u8 StartPath = RF_PATH_A, EndPath = RF_PATH_A, path; + static u32 regIG0 = 0, regIG1 = 0, regIG2 = 0, regIG3 = 0; + + if (bSingleTone) { + regIG0 = phy_query_bb_reg(pAdapter, rA_TxScale_Jaguar, bMaskDWord); /*/ 0xC1C[31:21]*/ + regIG1 = phy_query_bb_reg(pAdapter, rB_TxScale_Jaguar, bMaskDWord); /*/ 0xE1C[31:21]*/ + regIG2 = phy_query_bb_reg(pAdapter, rC_TxScale_Jaguar2, bMaskDWord); /*/ 0x181C[31:21]*/ + regIG3 = phy_query_bb_reg(pAdapter, rD_TxScale_Jaguar2, bMaskDWord); /*/ 0x1A1C[31:21]*/ + + switch (pMptCtx->mpt_rf_path) { + case RF_PATH_A: + case RF_PATH_B: + case RF_PATH_C: + case RF_PATH_D: + StartPath = pMptCtx->mpt_rf_path; + EndPath = pMptCtx->mpt_rf_path; + break; + case RF_PATH_AB: + EndPath = RF_PATH_B; + break; + case RF_PATH_BC: + StartPath = RF_PATH_B; + EndPath = RF_PATH_C; + break; + case RF_PATH_ABC: + EndPath = RF_PATH_C; + break; + case RF_PATH_BCD: + StartPath = RF_PATH_B; + EndPath = RF_PATH_D; + break; + case RF_PATH_ABCD: + EndPath = RF_PATH_D; + break; + } + + if (bEnPMacTx == FALSE) { + hal_mpt_SetContinuousTx(pAdapter, _TRUE); + issue_nulldata(pAdapter, NULL, 1, 3, 500); + } + + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x1); /*/ Disable CCA*/ + + for (path = StartPath; path <= EndPath; path++) { + phy_set_rf_reg(pAdapter, path, RF_AC_Jaguar, 0xF0000, 0x2); /*/ Tx mode: RF0x00[19:16]=4'b0010 */ + phy_set_rf_reg(pAdapter, path, RF_AC_Jaguar, 0x1F, 0x0); /*/ Lowest RF gain index: RF_0x0[4:0] = 0*/ + + phy_set_rf_reg(pAdapter, path, lna_low_gain_3, BIT1, 0x1); /*/ RF LO enabled*/ + } + + phy_set_bb_reg(pAdapter, rA_TxScale_Jaguar, 0xFFE00000, 0); /*/ 0xC1C[31:21]*/ + phy_set_bb_reg(pAdapter, rB_TxScale_Jaguar, 0xFFE00000, 0); /*/ 0xE1C[31:21]*/ + phy_set_bb_reg(pAdapter, rC_TxScale_Jaguar2, 0xFFE00000, 0); /*/ 0x181C[31:21]*/ + phy_set_bb_reg(pAdapter, rD_TxScale_Jaguar2, 0xFFE00000, 0); /*/ 0x1A1C[31:21]*/ + } else { + switch (pMptCtx->mpt_rf_path) { + case RF_PATH_A: + case RF_PATH_B: + case RF_PATH_C: + case RF_PATH_D: + StartPath = pMptCtx->mpt_rf_path; + EndPath = pMptCtx->mpt_rf_path; + break; + case RF_PATH_AB: + EndPath = RF_PATH_B; + break; + case RF_PATH_BC: + StartPath = RF_PATH_B; + EndPath = RF_PATH_C; + break; + case RF_PATH_ABC: + EndPath = RF_PATH_C; + break; + case RF_PATH_BCD: + StartPath = RF_PATH_B; + EndPath = RF_PATH_D; + break; + case RF_PATH_ABCD: + EndPath = RF_PATH_D; + break; + } + for (path = StartPath; path <= EndPath; path++) + phy_set_rf_reg(pAdapter, path, lna_low_gain_3, BIT1, 0x0); /* RF LO disabled */ + + phy_set_bb_reg(pAdapter, rCCAonSec_Jaguar, BIT1, 0x0); /* Enable CCA*/ + + if (bEnPMacTx == FALSE) { + if (IS_HARDWARE_TYPE_JAGUAR3(pAdapter) || IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)) { +#ifdef PHYDM_MP_SUPPORT + phydm_stop_ofdm_cont_tx(pAdapter); + pMptCtx->bCckContTx = FALSE; + pMptCtx->bOfdmContTx = FALSE; +#endif + } else + hal_mpt_SetContinuousTx(pAdapter, _FALSE); + } + + phy_set_bb_reg(pAdapter, rA_TxScale_Jaguar, bMaskDWord, regIG0); /* 0xC1C[31:21]*/ + phy_set_bb_reg(pAdapter, rB_TxScale_Jaguar, bMaskDWord, regIG1); /* 0xE1C[31:21]*/ + phy_set_bb_reg(pAdapter, rC_TxScale_Jaguar2, bMaskDWord, regIG2); /* 0x181C[31:21]*/ + phy_set_bb_reg(pAdapter, rD_TxScale_Jaguar2, bMaskDWord, regIG3); /* 0x1A1C[31:21]*/ + } +} + +#endif + +#if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A) +void mpt_SetRFPath_8812A(PADAPTER pAdapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + PMPT_CONTEXT pMptCtx = &pAdapter->mppriv.mpt_ctx; + struct mp_priv *pmp = &pAdapter->mppriv; + u8 channel = pmp->channel; + u8 bandwidth = pmp->bandwidth; + u8 eLNA_2g = pHalData->ExternalLNA_2G; + u32 ulAntennaTx, ulAntennaRx; + u32 reg0xC50 = 0; + + ulAntennaTx = pHalData->antenna_tx_path; + ulAntennaRx = pHalData->AntennaRxPath; + + switch (ulAntennaTx) { + case ANTENNA_A: + pMptCtx->mpt_rf_path = RF_PATH_A; + phy_set_bb_reg(pAdapter, rTxPath_Jaguar, bMaskLWord, 0x1111); + if (pHalData->rfe_type == 3 && IS_HARDWARE_TYPE_8812(pAdapter)) + phy_set_bb_reg(pAdapter, r_ANTSEL_SW_Jaguar, bMask_AntselPathFollow_Jaguar, 0x0); + break; + case ANTENNA_B: + pMptCtx->mpt_rf_path = RF_PATH_B; + phy_set_bb_reg(pAdapter, rTxPath_Jaguar, bMaskLWord, 0x2222); + if (pHalData->rfe_type == 3 && IS_HARDWARE_TYPE_8812(pAdapter)) + phy_set_bb_reg(pAdapter, r_ANTSEL_SW_Jaguar, bMask_AntselPathFollow_Jaguar, 0x1); + break; + case ANTENNA_AB: + pMptCtx->mpt_rf_path = RF_PATH_AB; + phy_set_bb_reg(pAdapter, rTxPath_Jaguar, bMaskLWord, 0x3333); + if (pHalData->rfe_type == 3 && IS_HARDWARE_TYPE_8812(pAdapter)) + phy_set_bb_reg(pAdapter, r_ANTSEL_SW_Jaguar, bMask_AntselPathFollow_Jaguar, 0x0); + break; + default: + pMptCtx->mpt_rf_path = RF_PATH_AB; + RTW_INFO("Unknown Tx antenna.\n"); + break; + } + + switch (ulAntennaRx) { + case ANTENNA_A: + phy_set_bb_reg(pAdapter, rRxPath_Jaguar, bMaskByte0, 0x11); + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_AC_Jaguar, 0xF0000, 0x1); /*/ RF_B_0x0[19:16] = 1, Standby mode*/ + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, bCCK_RX_Jaguar, 0x0); + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC_Jaguar, BIT19 | BIT18 | BIT17 | BIT16, 0x3); + + /*/ <20121101, Kordan> To prevent gain table from not switched, asked by Ynlin.*/ + reg0xC50 = phy_query_bb_reg(pAdapter, rA_IGI_Jaguar, bMaskByte0); + phy_set_bb_reg(pAdapter, rA_IGI_Jaguar, bMaskByte0, reg0xC50 + 2); + phy_set_bb_reg(pAdapter, rA_IGI_Jaguar, bMaskByte0, reg0xC50); + + /* set PWED_TH for BB Yn user guide R29 */ + if (IS_HARDWARE_TYPE_8812(pAdapter)) { + if (channel <= 14) { /* 2.4G */ + if (bandwidth == CHANNEL_WIDTH_20 + && eLNA_2g == 0) { + /* 0x830[3:1]=3'b010 */ + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, BIT1 | BIT2 | BIT3, 0x02); + } else + /* 0x830[3:1]=3'b100 */ + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, BIT1 | BIT2 | BIT3, 0x04); + } else + /* 0x830[3:1]=3'b100 for 5G */ + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, BIT1 | BIT2 | BIT3, 0x04); + } + break; + case ANTENNA_B: + phy_set_bb_reg(pAdapter, rRxPath_Jaguar, bMaskByte0, 0x22); + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC_Jaguar, 0xF0000, 0x1);/*/ RF_A_0x0[19:16] = 1, Standby mode */ + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, bCCK_RX_Jaguar, 0x1); + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_AC_Jaguar, BIT19 | BIT18 | BIT17 | BIT16, 0x3); + + /*/ <20121101, Kordan> To prevent gain table from not switched, asked by Ynlin.*/ + reg0xC50 = phy_query_bb_reg(pAdapter, rB_IGI_Jaguar, bMaskByte0); + phy_set_bb_reg(pAdapter, rB_IGI_Jaguar, bMaskByte0, reg0xC50 + 2); + phy_set_bb_reg(pAdapter, rB_IGI_Jaguar, bMaskByte0, reg0xC50); + + /* set PWED_TH for BB Yn user guide R29 */ + if (IS_HARDWARE_TYPE_8812(pAdapter)) { + if (channel <= 14) { + if (bandwidth == CHANNEL_WIDTH_20 + && eLNA_2g == 0) { + /* 0x830[3:1]=3'b010 */ + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, BIT1 | BIT2 | BIT3, 0x02); + } else + /* 0x830[3:1]=3'b100 */ + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, BIT1 | BIT2 | BIT3, 0x04); + } else + /* 0x830[3:1]=3'b100 for 5G */ + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, BIT1 | BIT2 | BIT3, 0x04); + } + break; + case ANTENNA_AB: + phy_set_bb_reg(pAdapter, rRxPath_Jaguar, bMaskByte0, 0x33); + phy_set_rf_reg(pAdapter, RF_PATH_B, RF_AC_Jaguar, 0xF0000, 0x3); /*/ RF_B_0x0[19:16] = 3, Rx mode*/ + phy_set_bb_reg(pAdapter, rCCK_RX_Jaguar, bCCK_RX_Jaguar, 0x0); + /* set PWED_TH for BB Yn user guide R29 */ + phy_set_bb_reg(pAdapter, rPwed_TH_Jaguar, BIT1 | BIT2 | BIT3, 0x04); + break; + default: + RTW_INFO("Unknown Rx antenna.\n"); + break; + } + + if (pHalData->rfe_type == 5 || pHalData->rfe_type == 1) { + if (ulAntennaTx == ANTENNA_A || ulAntennaTx == ANTENNA_AB) { + /* WiFi */ + phy_set_bb_reg(pAdapter, r_ANTSEL_SW_Jaguar, BIT(1) | BIT(0), 0x2); + phy_set_bb_reg(pAdapter, r_ANTSEL_SW_Jaguar, BIT(9) | BIT(8), 0x3); + } else { + /* BT */ + phy_set_bb_reg(pAdapter, r_ANTSEL_SW_Jaguar, BIT(1) | BIT(0), 0x1); + phy_set_bb_reg(pAdapter, r_ANTSEL_SW_Jaguar, BIT(9) | BIT(8), 0x3); + } + } +} +#endif + +#ifdef CONFIG_RTL8723B +void mpt_SetRFPath_8723B(PADAPTER pAdapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + u32 ulAntennaTx, ulAntennaRx; + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + struct dm_rf_calibration_struct *pRFCalibrateInfo = &(pDM_Odm->rf_calibrate_info); + u8 i; + + ulAntennaTx = pHalData->antenna_tx_path; + ulAntennaRx = pHalData->AntennaRxPath; + + if (pHalData->rf_chip >= RF_CHIP_MAX) { + RTW_INFO("This RF chip ID is not supported\n"); + return; + } + + switch (pAdapter->mppriv.antenna_tx) { + case ANTENNA_A: { /*/ Actually path S1 (Wi-Fi)*/ + pMptCtx->mpt_rf_path = RF_PATH_A; + phy_set_bb_reg(pAdapter, rS0S1_PathSwitch, BIT9 | BIT8 | BIT7, 0x0); + phy_set_bb_reg(pAdapter, 0xB2C, BIT31, 0x0); /* AGC Table Sel*/ + + for (i = 0; i < 3; ++i) { + u32 offset = pRFCalibrateInfo->tx_iqc_8723b[RF_PATH_A][i][0]; + u32 data = pRFCalibrateInfo->tx_iqc_8723b[RF_PATH_A][i][1]; + + if (offset != 0) { + phy_set_bb_reg(pAdapter, offset, bMaskDWord, data); + RTW_INFO("Switch to S1 TxIQC(offset, data) = (0x%X, 0x%X)\n", offset, data); + } + } + for (i = 0; i < 2; ++i) { + u32 offset = pRFCalibrateInfo->rx_iqc_8723b[RF_PATH_A][i][0]; + u32 data = pRFCalibrateInfo->rx_iqc_8723b[RF_PATH_A][i][1]; + + if (offset != 0) { + phy_set_bb_reg(pAdapter, offset, bMaskDWord, data); + RTW_INFO("Switch to S1 RxIQC (offset, data) = (0x%X, 0x%X)\n", offset, data); + } + } + } + break; + case ANTENNA_B: { /*/ Actually path S0 (BT)*/ + u32 offset; + u32 data; + + pMptCtx->mpt_rf_path = RF_PATH_B; + phy_set_bb_reg(pAdapter, rS0S1_PathSwitch, BIT9 | BIT8 | BIT7, 0x5); + phy_set_bb_reg(pAdapter, 0xB2C, BIT31, 0x1); /*/ AGC Table Sel.*/ + + for (i = 0; i < 3; ++i) { + /*/ <20130603, Kordan> Because BB suppors only 1T1R, we restore IQC to S1 instead of S0.*/ + offset = pRFCalibrateInfo->tx_iqc_8723b[RF_PATH_A][i][0]; + data = pRFCalibrateInfo->tx_iqc_8723b[RF_PATH_B][i][1]; + if (pRFCalibrateInfo->tx_iqc_8723b[RF_PATH_B][i][0] != 0) { + phy_set_bb_reg(pAdapter, offset, bMaskDWord, data); + RTW_INFO("Switch to S0 TxIQC (offset, data) = (0x%X, 0x%X)\n", offset, data); + } + } + /*/ <20130603, Kordan> Because BB suppors only 1T1R, we restore IQC to S1 instead of S0.*/ + for (i = 0; i < 2; ++i) { + offset = pRFCalibrateInfo->rx_iqc_8723b[RF_PATH_A][i][0]; + data = pRFCalibrateInfo->rx_iqc_8723b[RF_PATH_B][i][1]; + if (pRFCalibrateInfo->rx_iqc_8723b[RF_PATH_B][i][0] != 0) { + phy_set_bb_reg(pAdapter, offset, bMaskDWord, data); + RTW_INFO("Switch to S0 RxIQC (offset, data) = (0x%X, 0x%X)\n", offset, data); + } + } + } + break; + default: + pMptCtx->mpt_rf_path = RF_PATH_AB; + break; + } +} +#endif + +#ifdef CONFIG_RTL8703B +void mpt_SetRFPath_8703B(PADAPTER pAdapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + u32 ulAntennaTx, ulAntennaRx; + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + struct dm_rf_calibration_struct *pRFCalibrateInfo = &(pDM_Odm->rf_calibrate_info); + u8 i; + + ulAntennaTx = pHalData->antenna_tx_path; + ulAntennaRx = pHalData->AntennaRxPath; + + if (pHalData->rf_chip >= RF_CHIP_MAX) { + RTW_INFO("This RF chip ID is not supported\n"); + return; + } + + switch (pAdapter->mppriv.antenna_tx) { + case ANTENNA_A: { /* Actually path S1 (Wi-Fi) */ + pMptCtx->mpt_rf_path = RF_PATH_A; + phy_set_bb_reg(pAdapter, rS0S1_PathSwitch, BIT9 | BIT8 | BIT7, 0x0); + phy_set_bb_reg(pAdapter, 0xB2C, BIT31, 0x0); /* AGC Table Sel*/ + + for (i = 0; i < 3; ++i) { + u32 offset = pRFCalibrateInfo->tx_iqc_8703b[i][0]; + u32 data = pRFCalibrateInfo->tx_iqc_8703b[i][1]; + + if (offset != 0) { + phy_set_bb_reg(pAdapter, offset, bMaskDWord, data); + RTW_INFO("Switch to S1 TxIQC(offset, data) = (0x%X, 0x%X)\n", offset, data); + } + + } + for (i = 0; i < 2; ++i) { + u32 offset = pRFCalibrateInfo->rx_iqc_8703b[i][0]; + u32 data = pRFCalibrateInfo->rx_iqc_8703b[i][1]; + + if (offset != 0) { + phy_set_bb_reg(pAdapter, offset, bMaskDWord, data); + RTW_INFO("Switch to S1 RxIQC (offset, data) = (0x%X, 0x%X)\n", offset, data); + } + } + } + break; + case ANTENNA_B: { /* Actually path S0 (BT)*/ + pMptCtx->mpt_rf_path = RF_PATH_B; + phy_set_bb_reg(pAdapter, rS0S1_PathSwitch, BIT9 | BIT8 | BIT7, 0x5); + phy_set_bb_reg(pAdapter, 0xB2C, BIT31, 0x1); /* AGC Table Sel */ + + for (i = 0; i < 3; ++i) { + u32 offset = pRFCalibrateInfo->tx_iqc_8703b[i][0]; + u32 data = pRFCalibrateInfo->tx_iqc_8703b[i][1]; + + if (pRFCalibrateInfo->tx_iqc_8703b[i][0] != 0) { + phy_set_bb_reg(pAdapter, offset, bMaskDWord, data); + RTW_INFO("Switch to S0 TxIQC (offset, data) = (0x%X, 0x%X)\n", offset, data); + } + } + for (i = 0; i < 2; ++i) { + u32 offset = pRFCalibrateInfo->rx_iqc_8703b[i][0]; + u32 data = pRFCalibrateInfo->rx_iqc_8703b[i][1]; + + if (pRFCalibrateInfo->rx_iqc_8703b[i][0] != 0) { + phy_set_bb_reg(pAdapter, offset, bMaskDWord, data); + RTW_INFO("Switch to S0 RxIQC (offset, data) = (0x%X, 0x%X)\n", offset, data); + } + } + } + break; + default: + pMptCtx->mpt_rf_path = RF_PATH_AB; + break; + } + +} +#endif + +#ifdef CONFIG_RTL8723D +void mpt_SetRFPath_8723D(PADAPTER pAdapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + u8 p = 0, i = 0; + u32 ulAntennaTx, ulAntennaRx, offset = 0, data = 0, val32 = 0; + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + struct dm_rf_calibration_struct *pRFCalibrateInfo = &(pDM_Odm->rf_calibrate_info); + + ulAntennaTx = pHalData->antenna_tx_path; + ulAntennaRx = pHalData->AntennaRxPath; + + if (pHalData->rf_chip >= RF_CHIP_MAX) { + RTW_INFO("This RF chip ID is not supported\n"); + return; + } + + switch (pAdapter->mppriv.antenna_tx) { + /* Actually path S1 (Wi-Fi) */ + case ANTENNA_A: { + pMptCtx->mpt_rf_path = RF_PATH_A; + phy_set_bb_reg(pAdapter, rS0S1_PathSwitch, BIT9|BIT8|BIT7|BIT6, 0); + } + break; + /* Actually path S0 (BT) */ + case ANTENNA_B: { + pMptCtx->mpt_rf_path = RF_PATH_B; + phy_set_bb_reg(pAdapter, rS0S1_PathSwitch, BIT9|BIT8|BIT7|BIT6, 0xA); + + } + break; + default: + pMptCtx->mpt_rf_path = RF_PATH_AB; + break; + } +} +#endif + +void mpt_SetRFPath_819X(PADAPTER pAdapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + u32 ulAntennaTx, ulAntennaRx; + R_ANTENNA_SELECT_OFDM *p_ofdm_tx; /* OFDM Tx register */ + R_ANTENNA_SELECT_CCK *p_cck_txrx; + u8 r_rx_antenna_ofdm = 0, r_ant_select_cck_val = 0; + u8 chgTx = 0, chgRx = 0; + u32 r_ant_sel_cck_val = 0, r_ant_select_ofdm_val = 0, r_ofdm_tx_en_val = 0; + + ulAntennaTx = pHalData->antenna_tx_path; + ulAntennaRx = pHalData->AntennaRxPath; + + p_ofdm_tx = (R_ANTENNA_SELECT_OFDM *)&r_ant_select_ofdm_val; + p_cck_txrx = (R_ANTENNA_SELECT_CCK *)&r_ant_select_cck_val; + + p_ofdm_tx->r_ant_ht1 = 0x1; + p_ofdm_tx->r_ant_ht2 = 0x2;/*Second TX RF path is A*/ + p_ofdm_tx->r_ant_non_ht = 0x3;/*/ 0x1+0x2=0x3 */ + + switch (ulAntennaTx) { + case ANTENNA_A: + p_ofdm_tx->r_tx_antenna = 0x1; + r_ofdm_tx_en_val = 0x1; + p_ofdm_tx->r_ant_l = 0x1; + p_ofdm_tx->r_ant_ht_s1 = 0x1; + p_ofdm_tx->r_ant_non_ht_s1 = 0x1; + p_cck_txrx->r_ccktx_enable = 0x8; + chgTx = 1; + /*/ From SD3 Willis suggestion !!! Set RF A=TX and B as standby*/ + /*/if (IS_HARDWARE_TYPE_8192S(pAdapter))*/ + { + phy_set_bb_reg(pAdapter, rFPGA0_XA_HSSIParameter2, 0xe, 2); + phy_set_bb_reg(pAdapter, rFPGA0_XB_HSSIParameter2, 0xe, 1); + r_ofdm_tx_en_val = 0x3; + /*/ Power save*/ + /*/cosa r_ant_select_ofdm_val = 0x11111111;*/ + /*/ We need to close RFB by SW control*/ + if (pHalData->rf_type == RF_2T2R) { + phy_set_bb_reg(pAdapter, rFPGA0_XAB_RFInterfaceSW, BIT10, 0); + phy_set_bb_reg(pAdapter, rFPGA0_XAB_RFInterfaceSW, BIT26, 1); + phy_set_bb_reg(pAdapter, rFPGA0_XB_RFInterfaceOE, BIT10, 0); + phy_set_bb_reg(pAdapter, rFPGA0_XAB_RFParameter, BIT1, 1); + phy_set_bb_reg(pAdapter, rFPGA0_XAB_RFParameter, BIT17, 0); + } + } + pMptCtx->mpt_rf_path = RF_PATH_A; + break; + case ANTENNA_B: + p_ofdm_tx->r_tx_antenna = 0x2; + r_ofdm_tx_en_val = 0x2; + p_ofdm_tx->r_ant_l = 0x2; + p_ofdm_tx->r_ant_ht_s1 = 0x2; + p_ofdm_tx->r_ant_non_ht_s1 = 0x2; + p_cck_txrx->r_ccktx_enable = 0x4; + chgTx = 1; + /*/ From SD3 Willis suggestion !!! Set RF A as standby*/ + /*/if (IS_HARDWARE_TYPE_8192S(pAdapter))*/ + { + phy_set_bb_reg(pAdapter, rFPGA0_XA_HSSIParameter2, 0xe, 1); + phy_set_bb_reg(pAdapter, rFPGA0_XB_HSSIParameter2, 0xe, 2); + + /*/ 2008/10/31 MH From SD3 Willi's suggestion. We must read RF 1T table.*/ + /*/ 2009/01/08 MH From Sd3 Willis. We need to close RFA by SW control*/ + if (pHalData->rf_type == RF_2T2R || pHalData->rf_type == RF_1T2R) { + phy_set_bb_reg(pAdapter, rFPGA0_XAB_RFInterfaceSW, BIT10, 1); + phy_set_bb_reg(pAdapter, rFPGA0_XA_RFInterfaceOE, BIT10, 0); + phy_set_bb_reg(pAdapter, rFPGA0_XAB_RFInterfaceSW, BIT26, 0); + /*/phy_set_bb_reg(pAdapter, rFPGA0_XB_RFInterfaceOE, BIT10, 0);*/ + phy_set_bb_reg(pAdapter, rFPGA0_XAB_RFParameter, BIT1, 0); + phy_set_bb_reg(pAdapter, rFPGA0_XAB_RFParameter, BIT17, 1); + } + } + pMptCtx->mpt_rf_path = RF_PATH_B; + break; + case ANTENNA_AB:/*/ For 8192S*/ + p_ofdm_tx->r_tx_antenna = 0x3; + r_ofdm_tx_en_val = 0x3; + p_ofdm_tx->r_ant_l = 0x3; + p_ofdm_tx->r_ant_ht_s1 = 0x3; + p_ofdm_tx->r_ant_non_ht_s1 = 0x3; + p_cck_txrx->r_ccktx_enable = 0xC; + chgTx = 1; + /*/ From SD3Willis suggestion !!! Set RF B as standby*/ + /*/if (IS_HARDWARE_TYPE_8192S(pAdapter))*/ + { + phy_set_bb_reg(pAdapter, rFPGA0_XA_HSSIParameter2, 0xe, 2); + phy_set_bb_reg(pAdapter, rFPGA0_XB_HSSIParameter2, 0xe, 2); + /* Disable Power save*/ + /*cosa r_ant_select_ofdm_val = 0x3321333;*/ + /* 2009/01/08 MH From Sd3 Willis. We need to enable RFA/B by SW control*/ + if (pHalData->rf_type == RF_2T2R) { + phy_set_bb_reg(pAdapter, rFPGA0_XAB_RFInterfaceSW, BIT10, 0); + + phy_set_bb_reg(pAdapter, rFPGA0_XAB_RFInterfaceSW, BIT26, 0); + /*/phy_set_bb_reg(pAdapter, rFPGA0_XB_RFInterfaceOE, BIT10, 0);*/ + phy_set_bb_reg(pAdapter, rFPGA0_XAB_RFParameter, BIT1, 1); + phy_set_bb_reg(pAdapter, rFPGA0_XAB_RFParameter, BIT17, 1); + } + } + pMptCtx->mpt_rf_path = RF_PATH_AB; + break; + default: + break; + } +#if 0 + /* r_rx_antenna_ofdm, bit0=A, bit1=B, bit2=C, bit3=D */ + /* r_cckrx_enable : CCK default, 0=A, 1=B, 2=C, 3=D */ + /* r_cckrx_enable_2 : CCK option, 0=A, 1=B, 2=C, 3=D */ +#endif + switch (ulAntennaRx) { + case ANTENNA_A: + r_rx_antenna_ofdm = 0x1; /* A*/ + p_cck_txrx->r_cckrx_enable = 0x0; /* default: A*/ + p_cck_txrx->r_cckrx_enable_2 = 0x0; /* option: A*/ + chgRx = 1; + break; + case ANTENNA_B: + r_rx_antenna_ofdm = 0x2; /*/ B*/ + p_cck_txrx->r_cckrx_enable = 0x1; /*/ default: B*/ + p_cck_txrx->r_cckrx_enable_2 = 0x1; /*/ option: B*/ + chgRx = 1; + break; + case ANTENNA_AB:/*/ For 8192S and 8192E/U...*/ + r_rx_antenna_ofdm = 0x3;/*/ AB*/ + p_cck_txrx->r_cckrx_enable = 0x0;/*/ default:A*/ + p_cck_txrx->r_cckrx_enable_2 = 0x1;/*/ option:B*/ + chgRx = 1; + break; + default: + break; + } + + + if (chgTx && chgRx) { + switch (pHalData->rf_chip) { + case RF_8225: + case RF_8256: + case RF_6052: + /*/r_ant_sel_cck_val = r_ant_select_cck_val;*/ + phy_set_bb_reg(pAdapter, rFPGA1_TxInfo, 0x7fffffff, r_ant_select_ofdm_val); /*/OFDM Tx*/ + phy_set_bb_reg(pAdapter, rFPGA0_TxInfo, 0x0000000f, r_ofdm_tx_en_val); /*/OFDM Tx*/ + phy_set_bb_reg(pAdapter, rOFDM0_TRxPathEnable, 0x0000000f, r_rx_antenna_ofdm); /*/OFDM Rx*/ + phy_set_bb_reg(pAdapter, rOFDM1_TRxPathEnable, 0x0000000f, r_rx_antenna_ofdm); /*/OFDM Rx*/ + if (IS_HARDWARE_TYPE_8192E(pAdapter)) { + phy_set_bb_reg(pAdapter, rOFDM0_TRxPathEnable, 0x000000F0, r_rx_antenna_ofdm); /*/OFDM Rx*/ + phy_set_bb_reg(pAdapter, rOFDM1_TRxPathEnable, 0x000000F0, r_rx_antenna_ofdm); /*/OFDM Rx*/ + } + phy_set_bb_reg(pAdapter, rCCK0_AFESetting, bMaskByte3, r_ant_select_cck_val);/*/r_ant_sel_cck_val); /CCK TxRx*/ + break; + + default: + RTW_INFO("Unsupported RFChipID for switching antenna.\n"); + break; + } + } +} /* MPT_ProSetRFPath */ + +#ifdef CONFIG_RTL8192F + +void mpt_set_rfpath_8192f(PADAPTER pAdapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + + u16 ForcedDataRate = mpt_to_mgnt_rate(pMptCtx->mpt_rate_index); + u8 NssforRate, odmNssforRate; + u32 ulAntennaTx, ulAntennaRx; + enum bb_path RxAntToPhyDm; + enum bb_path TxAntToPhyDm; + + ulAntennaTx = pHalData->antenna_tx_path; + ulAntennaRx = pHalData->AntennaRxPath; + NssforRate = MgntQuery_NssTxRate(ForcedDataRate); + + if (pHalData->rf_chip >= RF_TYPE_MAX) + RTW_INFO("This RF chip ID is not supported\n"); + + switch (ulAntennaTx) { + case ANTENNA_A: + pMptCtx->mpt_rf_path = RF_PATH_A; + TxAntToPhyDm = BB_PATH_A; + break; + case ANTENNA_B: + pMptCtx->mpt_rf_path = RF_PATH_B; + TxAntToPhyDm = BB_PATH_B; + break; + case ANTENNA_AB: + pMptCtx->mpt_rf_path = RF_PATH_AB; + TxAntToPhyDm = (BB_PATH_A|BB_PATH_B); + break; + default: + pMptCtx->mpt_rf_path = RF_PATH_AB; + TxAntToPhyDm = (BB_PATH_A|BB_PATH_B); + break; + } + + switch (ulAntennaRx) { + case ANTENNA_A: + RxAntToPhyDm = BB_PATH_A; + break; + case ANTENNA_B: + RxAntToPhyDm = BB_PATH_B; + break; + case ANTENNA_AB: + RxAntToPhyDm = (BB_PATH_A|BB_PATH_B); + break; + default: + RxAntToPhyDm = (BB_PATH_A|BB_PATH_B); + break; + } + + phydm_api_trx_mode(GET_PDM_ODM(pAdapter), TxAntToPhyDm, RxAntToPhyDm, TxAntToPhyDm); + +} + +#endif + +void hal_mpt_SetAntenna(PADAPTER pAdapter) +{ + PHAL_DATA_TYPE hal; + ANTENNA_PATH anttx; + enum bb_path bb_tx = 0; + + + hal = GET_HAL_DATA(pAdapter); + anttx = hal->antenna_tx_path; + + switch (anttx) { + case ANTENNA_A: + bb_tx = BB_PATH_A; + break; + case ANTENNA_B: + bb_tx = BB_PATH_B; + break; + case ANTENNA_C: + bb_tx = BB_PATH_C; + break; + case ANTENNA_D: + bb_tx = BB_PATH_D; + break; + case ANTENNA_AB: + bb_tx = BB_PATH_AB; + break; + case ANTENNA_AC: + bb_tx = BB_PATH_AC; + break; + case ANTENNA_AD: + bb_tx = BB_PATH_AD; + break; + case ANTENNA_BC: + bb_tx = BB_PATH_BC; + break; + case ANTENNA_BD: + bb_tx = BB_PATH_BD; + break; + case ANTENNA_CD: + bb_tx = BB_PATH_CD; + break; + case ANTENNA_ABC: + bb_tx = BB_PATH_ABC; + break; + case ANTENNA_BCD: + bb_tx = BB_PATH_BCD; + break; + case ANTENNA_ABD: + bb_tx = BB_PATH_ABD; + break; + case ANTENNA_ACD: + bb_tx = BB_PATH_ACD; + break; + case ANTENNA_ABCD: + bb_tx = BB_PATH_ABCD; + break; + default: + bb_tx = BB_PATH_A; + break; + } + tx_path_nss_set_full_tx(hal->txpath_nss, hal->txpath_num_nss, bb_tx); + RTW_INFO("%s ,ant idx %d, tx path_num_nss = %d\n", __func__, anttx, hal->txpath_num_nss[0]); + +#ifdef CONFIG_RTL8822C + if (IS_HARDWARE_TYPE_8822C(pAdapter)) { + rtl8822c_mp_config_rfpath(pAdapter); + return; + } +#endif +#ifdef CONFIG_RTL8814A + if (IS_HARDWARE_TYPE_8814A(pAdapter)) { + mpt_SetRFPath_8814A(pAdapter); + return; + } +#endif +#ifdef CONFIG_RTL8822B + if (IS_HARDWARE_TYPE_8822B(pAdapter)) { + rtl8822b_mp_config_rfpath(pAdapter); + return; + } +#endif +#ifdef CONFIG_RTL8821C + if (IS_HARDWARE_TYPE_8821C(pAdapter)) { + rtl8821c_mp_config_rfpath(pAdapter); + return; + } +#endif +#if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A) + if (IS_HARDWARE_TYPE_JAGUAR(pAdapter)) { + mpt_SetRFPath_8812A(pAdapter); + return; + } +#endif +#ifdef CONFIG_RTL8723B + if (IS_HARDWARE_TYPE_8723B(pAdapter)) { + mpt_SetRFPath_8723B(pAdapter); + return; + } +#endif + +#ifdef CONFIG_RTL8703B + if (IS_HARDWARE_TYPE_8703B(pAdapter)) { + mpt_SetRFPath_8703B(pAdapter); + return; + } +#endif + +#ifdef CONFIG_RTL8723D + if (IS_HARDWARE_TYPE_8723D(pAdapter)) { + mpt_SetRFPath_8723D(pAdapter); + return; + } +#endif + +#ifdef CONFIG_RTL8192F + if (IS_HARDWARE_TYPE_8192F(pAdapter)) { + mpt_set_rfpath_8192f(pAdapter); + return; + } +#endif + +#ifdef CONFIG_RTL8814B + if (IS_HARDWARE_TYPE_8814B(pAdapter)) { + rtl8814b_mp_config_rfpath(pAdapter); + return; + } +#endif + + /* else if (IS_HARDWARE_TYPE_8821B(pAdapter)) + mpt_SetRFPath_8821B(pAdapter); + Prepare for 8822B + else if (IS_HARDWARE_TYPE_8822B(Context)) + mpt_SetRFPath_8822B(Context); + */ + mpt_SetRFPath_819X(pAdapter); + RTW_INFO("mpt_SetRFPath_819X Do %s\n", __func__); +} + +s32 hal_mpt_SetThermalMeter(PADAPTER pAdapter, u8 target_ther) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + + if (!netif_running(pAdapter->pnetdev)) { + return _FAIL; + } + + + if (check_fwstate(&pAdapter->mlmepriv, WIFI_MP_STATE) == _FALSE) { + return _FAIL; + } + + target_ther &= 0xff; + + pHalData->eeprom_thermal_meter = target_ther; + + return _SUCCESS; +} + + +void hal_mpt_TriggerRFThermalMeter(PADAPTER pAdapter) +{ + if (IS_HARDWARE_TYPE_JAGUAR3(pAdapter) || IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)) { + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x42, BIT19, 0x1); + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x42, BIT19, 0x0); + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x42, BIT19, 0x1); + } else + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x42, BIT17 | BIT16, 0x03); + +} + + +u8 hal_mpt_ReadRFThermalMeter(PADAPTER pAdapter, u8 rf_path) + +{ + struct dm_struct *p_dm_odm = adapter_to_phydm(pAdapter); + + u32 ThermalValue = 0; + s32 thermal_value_temp = 0; + s8 thermal_offset = 0; + u32 thermal_reg_mask = 0; + + if (IS_8822C_SERIES(GET_HAL_DATA(pAdapter)->version_id)) + thermal_reg_mask = 0x007e; /*0x42: RF Reg[6:1], 35332(themal K & bias k & power trim) & 35325(tssi )*/ + else + thermal_reg_mask = 0xfc00; /*0x42: RF Reg[15:10]*/ + + ThermalValue = (u8)phy_query_rf_reg(pAdapter, rf_path, 0x42, thermal_reg_mask); + + thermal_offset = phydm_get_thermal_offset(p_dm_odm); + + thermal_value_temp = ThermalValue + thermal_offset; + + if (thermal_value_temp > 63) + ThermalValue = 63; + else if (thermal_value_temp < 0) + ThermalValue = 0; + else + ThermalValue = thermal_value_temp; + + return (u8)ThermalValue; +} + + +void hal_mpt_GetThermalMeter(PADAPTER pAdapter, u8 rfpath, u8 *value) +{ +#if 0 + fw_cmd(pAdapter, IOCMD_GET_THERMAL_METER); + rtw_msleep_os(1000); + fw_cmd_data(pAdapter, value, 1); + *value &= 0xFF; +#else + hal_mpt_TriggerRFThermalMeter(pAdapter); + rtw_msleep_os(1000); + *value = hal_mpt_ReadRFThermalMeter(pAdapter, rfpath); +#endif + +} + + +void hal_mpt_SetSingleCarrierTx(PADAPTER pAdapter, u8 bStart) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + + pAdapter->mppriv.mpt_ctx.bSingleCarrier = bStart; + + if (bStart) {/*/ Start Single Carrier.*/ + /*/ Start Single Carrier.*/ + /*/ 1. if OFDM block on?*/ + if (!phy_query_bb_reg(pAdapter, rFPGA0_RFMOD, bOFDMEn)) + phy_set_bb_reg(pAdapter, rFPGA0_RFMOD, bOFDMEn, 1); /*set OFDM block on*/ + + /*/ 2. set CCK test mode off, set to CCK normal mode*/ + phy_set_bb_reg(pAdapter, rCCK0_System, bCCKBBMode, 0); + + /*/ 3. turn on scramble setting*/ + phy_set_bb_reg(pAdapter, rCCK0_System, bCCKScramble, 1); + + /*/ 4. Turn On Continue Tx and turn off the other test modes.*/ +#if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A) || defined(CONFIG_RTL8814A)\ + || defined(CONFIG_RTL8822B) || defined(CONFIG_RTL8821C) || defined(CONFIG_RTL8723F) + if (IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter) || IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)) + phy_set_bb_reg(pAdapter, rSingleTone_ContTx_Jaguar, BIT18 | BIT17 | BIT16, OFDM_SingleCarrier); + else +#endif /* CONFIG_RTL8812A || CONFIG_RTL8821A || CONFIG_RTL8814A || CONFIG_RTL8822B || CONFIG_RTL8821C */ + phy_set_bb_reg(pAdapter, rOFDM1_LSTF, BIT30 | BIT29 | BIT28, OFDM_SingleCarrier); + + } else { + /*/ Stop Single Carrier.*/ + /*/ Stop Single Carrier.*/ + /*/ Turn off all test modes.*/ +#if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A) || defined(CONFIG_RTL8814A) || defined(CONFIG_RTL8822B)\ + || defined(CONFIG_RTL8821C) || defined(CONFIG_RTL8723F) + if (IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter)|| IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)) + phy_set_bb_reg(pAdapter, rSingleTone_ContTx_Jaguar, BIT18 | BIT17 | BIT16, OFDM_ALL_OFF); + else +#endif /* CONFIG_RTL8812A || CONFIG_RTL8821A || CONFIG_RTL8814A || CONFIG_RTL8822B || CONFIG_RTL8821C */ + phy_set_bb_reg(pAdapter, rOFDM1_LSTF, BIT30 | BIT29 | BIT28, OFDM_ALL_OFF); + + rtw_msleep_os(10); + /*/BB Reset*/ + phy_set_bb_reg(pAdapter, rPMAC_Reset, bBBResetB, 0x0); + phy_set_bb_reg(pAdapter, rPMAC_Reset, bBBResetB, 0x1); + } +} + + +void hal_mpt_SetSingleToneTx(PADAPTER pAdapter, u8 bStart) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + u32 ulAntennaTx = pHalData->antenna_tx_path; + static u32 regRF = 0, regBB0 = 0, regBB1 = 0, regBB2 = 0, regBB3 = 0; + u8 rfPath; + + if (IS_HARDWARE_TYPE_JAGUAR3(pAdapter) || IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)) { +#ifdef PHYDM_MP_SUPPORT +#ifdef CONFIG_RTL8814B + if(pHalData->current_channel_bw == CHANNEL_WIDTH_80_80) + { + /* @Tx mode: RF0x00[19:16]=4'b0010 */ + config_phydm_write_rf_syn_8814b(pDM_Odm, RF_SYN0, RF_0x0, 0xF0000, 0x2); + /* @Lowest RF gain index: RF_0x0[4:0] = 0*/ + config_phydm_write_rf_syn_8814b(pDM_Odm, RF_SYN0, RF_0x0, 0x1F, 0x0); + /* @RF LO enabled */ + config_phydm_write_rf_syn_8814b(pDM_Odm, RF_SYN0, RF_0x58, BIT(1), 0x1); + /* @SYN1 */ + config_phydm_write_rf_syn_8814b(pDM_Odm, RF_SYN1, RF_0x0, 0xF0000, 0x2); + config_phydm_write_rf_syn_8814b(pDM_Odm, RF_SYN1, RF_0x0, 0x1F, 0x0); + config_phydm_write_rf_syn_8814b(pDM_Odm, RF_SYN1, RF_0x58, BIT(1), 0x1); + } +#endif + phydm_mp_set_single_tone(pDM_Odm, bStart, pMptCtx->mpt_rf_path); +#endif + return; + } + + switch (ulAntennaTx) { + case ANTENNA_B: + rfPath = RF_PATH_B; + break; + case ANTENNA_C: + rfPath = RF_PATH_C; + break; + case ANTENNA_D: + rfPath = RF_PATH_D; + break; + case ANTENNA_A: + default: + rfPath = RF_PATH_A; + break; + } + + pAdapter->mppriv.mpt_ctx.is_single_tone = bStart; + if (bStart) { + /*/ Start Single Tone.*/ + /*/ <20120326, Kordan> To amplify the power of tone for Xtal calibration. (asked by Edlu)*/ + if (IS_HARDWARE_TYPE_8188E(pAdapter)) { + regRF = phy_query_rf_reg(pAdapter, rfPath, lna_low_gain_3, bRFRegOffsetMask); + phy_set_rf_reg(pAdapter, RF_PATH_A, lna_low_gain_3, BIT1, 0x1); /*/ RF LO enabled*/ + phy_set_bb_reg(pAdapter, rFPGA0_RFMOD, bCCKEn, 0x0); + phy_set_bb_reg(pAdapter, rFPGA0_RFMOD, bOFDMEn, 0x0); + } else if (IS_HARDWARE_TYPE_8192E(pAdapter)) { /*/ USB need to do RF LO disable first, PCIE isn't required to follow this order.*/ + /*/Set MAC REG 88C: Prevent SingleTone Fail*/ + phy_set_mac_reg(pAdapter, 0x88C, 0xF00000, 0xF); + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, lna_low_gain_3, BIT1, 0x1); /*/ RF LO disabled*/ + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, RF_AC, 0xF0000, 0x2); /*/ Tx mode*/ + } else if (IS_HARDWARE_TYPE_8192F(pAdapter)) { /* USB need to do RF LO disable first, PCIE isn't required to follow this order.*/ + #ifdef CONFIG_RTL8192F + phy_set_mac_reg(pAdapter, REG_LEDCFG0_8192F, BIT23, 0x1); + phy_set_mac_reg(pAdapter, REG_LEDCFG0_8192F, BIT26, 0x1); + phy_set_mac_reg(pAdapter, REG_PAD_CTRL1_8192F, BIT7, 0x1); + phy_set_mac_reg(pAdapter, REG_PAD_CTRL1_8192F, BIT1, 0x1); + phy_set_mac_reg(pAdapter, REG_PAD_CTRL1_8192F, BIT0, 0x1); + phy_set_mac_reg(pAdapter, REG_AFE_CTRL_4_8192F, BIT16, 0x1); + phy_set_bb_reg(pAdapter, 0x88C, 0xF00000, 0xF); + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, 0x57, BIT1, 0x1); /* RF LO disabled*/ + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, RF_AC, 0xF0000, 0x2); /* Tx mode*/ +#endif + } else if (IS_HARDWARE_TYPE_8723B(pAdapter)) { + if (pMptCtx->mpt_rf_path == RF_PATH_A) { + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC, 0xF0000, 0x2); /*/ Tx mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x56, 0xF, 0x1); /*/ RF LO enabled*/ + } else { + /*/ S0/S1 both use PATH A to configure*/ + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC, 0xF0000, 0x2); /*/ Tx mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x76, 0xF, 0x1); /*/ RF LO enabled*/ + } + } else if (IS_HARDWARE_TYPE_8703B(pAdapter)) { + if (pMptCtx->mpt_rf_path == RF_PATH_A) { + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC, 0xF0000, 0x2); /* Tx mode */ + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x53, 0xF000, 0x1); /* RF LO enabled */ + } + } else if (IS_HARDWARE_TYPE_8188F(pAdapter) || IS_HARDWARE_TYPE_8188GTV(pAdapter)) { + /*Set BB REG 88C: Prevent SingleTone Fail*/ + phy_set_bb_reg(pAdapter, rFPGA0_AnalogParameter4, 0xF00000, 0xF); + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, lna_low_gain_3, BIT1, 0x1); + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, RF_AC, 0xF0000, 0x2); + + } else if (IS_HARDWARE_TYPE_8723D(pAdapter)) { + if (pMptCtx->mpt_rf_path == RF_PATH_A) { + phy_set_bb_reg(pAdapter, rFPGA0_RFMOD, bCCKEn|bOFDMEn, 0); + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC, BIT16, 0x0); + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x53, BIT0, 0x1); + } else {/* S0/S1 both use PATH A to configure */ + phy_set_bb_reg(pAdapter, rFPGA0_RFMOD, bCCKEn|bOFDMEn, 0); + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC, BIT16, 0x0); + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x63, BIT0, 0x1); + } + } else if (IS_HARDWARE_TYPE_JAGUAR(pAdapter) || IS_HARDWARE_TYPE_8822B(pAdapter) || IS_HARDWARE_TYPE_8821C(pAdapter)) { +#if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A) || defined(CONFIG_RTL8822B) || defined(CONFIG_RTL8821C) + u8 p = RF_PATH_A; + + regRF = phy_query_rf_reg(pAdapter, RF_PATH_A, RF_AC_Jaguar, bRFRegOffsetMask); + regBB0 = phy_query_bb_reg(pAdapter, rA_RFE_Pinmux_Jaguar, bMaskDWord); + regBB1 = phy_query_bb_reg(pAdapter, rB_RFE_Pinmux_Jaguar, bMaskDWord); + regBB2 = phy_query_bb_reg(pAdapter, rA_RFE_Pinmux_Jaguar + 4, bMaskDWord); + regBB3 = phy_query_bb_reg(pAdapter, rB_RFE_Pinmux_Jaguar + 4, bMaskDWord); + + phy_set_bb_reg(pAdapter, rOFDMCCKEN_Jaguar, BIT29 | BIT28, 0x0); /*/ Disable CCK and OFDM*/ + + if (pMptCtx->mpt_rf_path == RF_PATH_AB) { + for (p = RF_PATH_A; p <= RF_PATH_B; ++p) { + phy_set_rf_reg(pAdapter, p, RF_AC_Jaguar, 0xF0000, 0x2); /*/ Tx mode: RF0x00[19:16]=4'b0010 */ + phy_set_rf_reg(pAdapter, p, RF_AC_Jaguar, 0x1F, 0x0); /*/ Lowest RF gain index: RF_0x0[4:0] = 0*/ + phy_set_rf_reg(pAdapter, p, lna_low_gain_3, BIT1, 0x1); /*/ RF LO enabled*/ + } + } else { + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, RF_AC_Jaguar, 0xF0000, 0x2); /*/ Tx mode: RF0x00[19:16]=4'b0010 */ + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, RF_AC_Jaguar, 0x1F, 0x0); /*/ Lowest RF gain index: RF_0x0[4:0] = 0*/ +#ifdef CONFIG_RTL8821C + if (IS_HARDWARE_TYPE_8821C(pAdapter) && pDM_Odm->current_rf_set_8821c == SWITCH_TO_BTG) + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, 0x75, BIT16, 0x1); /* RF LO (for BTG) enabled */ + else +#endif + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, lna_low_gain_3, BIT1, 0x1); /*/ RF LO enabled*/ + } + if (IS_HARDWARE_TYPE_8822B(pAdapter)) { + phy_set_bb_reg(pAdapter, rA_RFE_Pinmux_Jaguar, bMaskDWord, 0x77777777); /* 0xCB0=0x77777777*/ + phy_set_bb_reg(pAdapter, rB_RFE_Pinmux_Jaguar, bMaskDWord, 0x77777777); /* 0xEB0=0x77777777*/ + phy_set_bb_reg(pAdapter, rA_RFE_Pinmux_Jaguar + 4, bMaskLWord, 0x7777); /* 0xCB4[15:0] = 0x7777*/ + phy_set_bb_reg(pAdapter, rB_RFE_Pinmux_Jaguar + 4, bMaskLWord, 0x7777); /* 0xEB4[15:0] = 0x7777*/ + phy_set_bb_reg(pAdapter, rA_RFE_Inverse_Jaguar, 0xFFF, 0xb); /* 0xCBC[23:16] = 0x12*/ + phy_set_bb_reg(pAdapter, rB_RFE_Inverse_Jaguar, 0xFFF, 0x830); /* 0xEBC[23:16] = 0x12*/ + } else if (IS_HARDWARE_TYPE_8821C(pAdapter)) { + phy_set_bb_reg(pAdapter, rA_RFE_Pinmux_Jaguar, 0xF0F0, 0x707); /* 0xCB0[[15:12, 7:4] = 0x707*/ + + if (pHalData->external_pa_5g) + { + phy_set_bb_reg(pAdapter, rA_RFE_Pinmux_Jaguar + 4, 0xA00000, 0x1); /* 0xCB4[23, 21] = 0x1*/ + } + else if (pHalData->ExternalPA_2G) + { + phy_set_bb_reg(pAdapter, rA_RFE_Pinmux_Jaguar + 4, 0xA00000, 0x1); /* 0xCB4[23, 21] = 0x1*/ + } + } else { + phy_set_bb_reg(pAdapter, rA_RFE_Pinmux_Jaguar, 0xFF00F0, 0x77007); /*/ 0xCB0[[23:16, 7:4] = 0x77007*/ + phy_set_bb_reg(pAdapter, rB_RFE_Pinmux_Jaguar, 0xFF00F0, 0x77007); /*/ 0xCB0[[23:16, 7:4] = 0x77007*/ + + if (pHalData->external_pa_5g) { + phy_set_bb_reg(pAdapter, rA_RFE_Pinmux_Jaguar + 4, 0xFF00000, 0x12); /*/ 0xCB4[23:16] = 0x12*/ + phy_set_bb_reg(pAdapter, rB_RFE_Pinmux_Jaguar + 4, 0xFF00000, 0x12); /*/ 0xEB4[23:16] = 0x12*/ + } else if (pHalData->ExternalPA_2G) { + phy_set_bb_reg(pAdapter, rA_RFE_Pinmux_Jaguar + 4, 0xFF00000, 0x11); /*/ 0xCB4[23:16] = 0x11*/ + phy_set_bb_reg(pAdapter, rB_RFE_Pinmux_Jaguar + 4, 0xFF00000, 0x11); /*/ 0xEB4[23:16] = 0x11*/ + } + } +#endif + } +#if defined(CONFIG_RTL8814A) + else if (IS_HARDWARE_TYPE_8814A(pAdapter)) + mpt_SetSingleTone_8814A(pAdapter, TRUE, FALSE); +#endif + else /*/ Turn On SingleTone and turn off the other test modes.*/ + phy_set_bb_reg(pAdapter, rOFDM1_LSTF, BIT30 | BIT29 | BIT28, OFDM_SingleTone); + + write_bbreg(pAdapter, rFPGA0_XA_HSSIParameter1, bMaskDWord, 0x01000500); + write_bbreg(pAdapter, rFPGA0_XB_HSSIParameter1, bMaskDWord, 0x01000500); + + } else {/*/ Stop Single Ton e.*/ + + if (IS_HARDWARE_TYPE_8188E(pAdapter)) { + phy_set_rf_reg(pAdapter, RF_PATH_A, lna_low_gain_3, bRFRegOffsetMask, regRF); + phy_set_bb_reg(pAdapter, rFPGA0_RFMOD, bCCKEn, 0x1); + phy_set_bb_reg(pAdapter, rFPGA0_RFMOD, bOFDMEn, 0x1); + } else if (IS_HARDWARE_TYPE_8192E(pAdapter)) { + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, RF_AC, 0xF0000, 0x3);/*/ Tx mode*/ + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, lna_low_gain_3, BIT1, 0x0);/*/ RF LO disabled */ + /*/ RESTORE MAC REG 88C: Enable RF Functions*/ + phy_set_mac_reg(pAdapter, 0x88C, 0xF00000, 0x0); + } else if (IS_HARDWARE_TYPE_8192F(pAdapter)){ +#ifdef CONFIG_RTL8192F + phy_set_mac_reg(pAdapter, REG_LEDCFG0_8192F, BIT23, 0x0); + phy_set_mac_reg(pAdapter, REG_LEDCFG0_8192F, BIT26, 0x0); + phy_set_mac_reg(pAdapter, REG_PAD_CTRL1_8192F, BIT7, 0x0); + phy_set_mac_reg(pAdapter, REG_PAD_CTRL1_8192F, BIT1, 0x0); + phy_set_mac_reg(pAdapter, REG_PAD_CTRL1_8192F, BIT0, 0x0); + phy_set_mac_reg(pAdapter, REG_AFE_CTRL_4_8192F, BIT16, 0x0); + phy_set_bb_reg(pAdapter, 0x88C, 0xF00000, 0x0); + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, 0x57, BIT1, 0x0); /* RF LO disabled*/ + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, RF_AC, 0xF0000, 0x3); /* Rx mode*/ +#endif + } else if (IS_HARDWARE_TYPE_8723B(pAdapter)) { + if (pMptCtx->mpt_rf_path == RF_PATH_A) { + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC, 0xF0000, 0x3); /*/ Rx mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x56, 0xF, 0x0); /*/ RF LO disabled*/ + } else { + /*/ S0/S1 both use PATH A to configure*/ + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC, 0xF0000, 0x3); /*/ Rx mode*/ + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x76, 0xF, 0x0); /*/ RF LO disabled*/ + } + } else if (IS_HARDWARE_TYPE_8703B(pAdapter)) { + if (pMptCtx->mpt_rf_path == RF_PATH_A) { + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC, 0xF0000, 0x3); /* Rx mode */ + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x53, 0xF000, 0x0); /* RF LO disabled */ + } + } else if (IS_HARDWARE_TYPE_8188F(pAdapter) || IS_HARDWARE_TYPE_8188GTV(pAdapter)) { + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, RF_AC, 0xF0000, 0x3); /*Tx mode*/ + phy_set_rf_reg(pAdapter, pMptCtx->mpt_rf_path, lna_low_gain_3, BIT1, 0x0); /*RF LO disabled*/ + /*Set BB REG 88C: Prevent SingleTone Fail*/ + phy_set_bb_reg(pAdapter, rFPGA0_AnalogParameter4, 0xF00000, 0xc); + } else if (IS_HARDWARE_TYPE_8723D(pAdapter)) { + if (pMptCtx->mpt_rf_path == RF_PATH_A) { + phy_set_bb_reg(pAdapter, rFPGA0_RFMOD, bCCKEn|bOFDMEn, 0x3); + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC, BIT16, 0x1); + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x53, BIT0, 0x0); + } else { /* S0/S1 both use PATH A to configure */ + phy_set_bb_reg(pAdapter, rFPGA0_RFMOD, bCCKEn|bOFDMEn, 0x3); + phy_set_rf_reg(pAdapter, RF_PATH_A, RF_AC, BIT16, 0x1); + phy_set_rf_reg(pAdapter, RF_PATH_A, 0x63, BIT0, 0x0); + } + } else if (IS_HARDWARE_TYPE_JAGUAR(pAdapter) || IS_HARDWARE_TYPE_8822B(pAdapter) || IS_HARDWARE_TYPE_8821C(pAdapter)) { +#if defined(CONFIG_RTL8812A) || defined(CONFIG_RTL8821A) || defined(CONFIG_RTL8822B) || defined(CONFIG_RTL8821C) + u8 p = RF_PATH_A; + + phy_set_bb_reg(pAdapter, rOFDMCCKEN_Jaguar, BIT29 | BIT28, 0x3); /*/ Disable CCK and OFDM*/ + + if (pMptCtx->mpt_rf_path == RF_PATH_AB) { + for (p = RF_PATH_A; p <= RF_PATH_B; ++p) { + phy_set_rf_reg(pAdapter, p, RF_AC_Jaguar, bRFRegOffsetMask, regRF); + phy_set_rf_reg(pAdapter, p, lna_low_gain_3, BIT1, 0x0); /*/ RF LO disabled*/ + } + } else { + p = pMptCtx->mpt_rf_path; + phy_set_rf_reg(pAdapter, p, RF_AC_Jaguar, bRFRegOffsetMask, regRF); + + if (IS_HARDWARE_TYPE_8821C(pAdapter)) + phy_set_rf_reg(pAdapter, p, 0x75, BIT16, 0x0); /* RF LO (for BTG) disabled */ + + phy_set_rf_reg(pAdapter, p, lna_low_gain_3, BIT1, 0x0); /*/ RF LO disabled*/ + } + + phy_set_bb_reg(pAdapter, rA_RFE_Pinmux_Jaguar, bMaskDWord, regBB0); + phy_set_bb_reg(pAdapter, rB_RFE_Pinmux_Jaguar, bMaskDWord, regBB1); + phy_set_bb_reg(pAdapter, rA_RFE_Pinmux_Jaguar + 4, bMaskDWord, regBB2); + phy_set_bb_reg(pAdapter, rB_RFE_Pinmux_Jaguar + 4, bMaskDWord, regBB3); + + if (IS_HARDWARE_TYPE_8822B(pAdapter)) { + RTW_INFO("Restore RFE control Pin cbc\n"); + phy_set_bb_reg(pAdapter, rA_RFE_Inverse_Jaguar, 0xfff, 0x0); + phy_set_bb_reg(pAdapter, rB_RFE_Inverse_Jaguar, 0xfff, 0x0); + } +#endif + } +#if defined(CONFIG_RTL8814A) + else if (IS_HARDWARE_TYPE_8814A(pAdapter)) + mpt_SetSingleTone_8814A(pAdapter, FALSE, FALSE); + + else/*/ Turn off all test modes.*/ + phy_set_bb_reg(pAdapter, rSingleTone_ContTx_Jaguar, BIT18 | BIT17 | BIT16, OFDM_ALL_OFF); +#endif + write_bbreg(pAdapter, rFPGA0_XA_HSSIParameter1, bMaskDWord, 0x01000100); + write_bbreg(pAdapter, rFPGA0_XB_HSSIParameter1, bMaskDWord, 0x01000100); + + } +} + +void hal_mpt_SetCarrierSuppressionTx(PADAPTER pAdapter, u8 bStart) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + struct dm_struct *pdm_odm = &pHalData->odmpriv; + u8 Rate; + + pAdapter->mppriv.mpt_ctx.is_carrier_suppression = bStart; + + if (IS_HARDWARE_TYPE_JAGUAR3(pAdapter) || IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)) { +#ifdef PHYDM_MP_SUPPORT + phydm_mp_set_carrier_supp(pdm_odm, bStart, pAdapter->mppriv.rateidx); +#endif + return; + } + + Rate = HwRateToMPTRate(pAdapter->mppriv.rateidx); + if (bStart) {/* Start Carrier Suppression.*/ + if (Rate <= MPT_RATE_11M) { + /*/ 1. if CCK block on?*/ + if (!read_bbreg(pAdapter, rFPGA0_RFMOD, bCCKEn)) + write_bbreg(pAdapter, rFPGA0_RFMOD, bCCKEn, bEnable);/*set CCK block on*/ + + /*/Turn Off All Test Mode*/ + if (IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter) || IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)) + phy_set_bb_reg(pAdapter, 0x914, BIT18 | BIT17 | BIT16, OFDM_ALL_OFF); /* rSingleTone_ContTx_Jaguar*/ + else + phy_set_bb_reg(pAdapter, rOFDM1_LSTF, BIT30 | BIT29 | BIT28, OFDM_ALL_OFF); + + write_bbreg(pAdapter, rCCK0_System, bCCKBBMode, 0x2); /*/transmit mode*/ + write_bbreg(pAdapter, rCCK0_System, bCCKScramble, 0x0); /*/turn off scramble setting*/ + + /*/Set CCK Tx Test Rate*/ + write_bbreg(pAdapter, rCCK0_System, bCCKTxRate, 0x0); /*/Set FTxRate to 1Mbps*/ + } + + /*Set for dynamic set Power index*/ + write_bbreg(pAdapter, rFPGA0_XA_HSSIParameter1, bMaskDWord, 0x01000500); + write_bbreg(pAdapter, rFPGA0_XB_HSSIParameter1, bMaskDWord, 0x01000500); + + } else {/* Stop Carrier Suppression.*/ + + if (Rate <= MPT_RATE_11M) { + write_bbreg(pAdapter, rCCK0_System, bCCKBBMode, 0x0); /*normal mode*/ + write_bbreg(pAdapter, rCCK0_System, bCCKScramble, 0x1); /*turn on scramble setting*/ + + /*BB Reset*/ + write_bbreg(pAdapter, rPMAC_Reset, bBBResetB, 0x0); + write_bbreg(pAdapter, rPMAC_Reset, bBBResetB, 0x1); + } + /*Stop for dynamic set Power index*/ + write_bbreg(pAdapter, rFPGA0_XA_HSSIParameter1, bMaskDWord, 0x01000100); + write_bbreg(pAdapter, rFPGA0_XB_HSSIParameter1, bMaskDWord, 0x01000100); + } + RTW_INFO("\n MPT_ProSetCarrierSupp() is finished.\n"); +} + +u32 hal_mpt_query_phytxok(PADAPTER pAdapter) +{ + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + RT_PMAC_TX_INFO PMacTxInfo = pMptCtx->PMacTxInfo; + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + u16 count = 0; + +#ifdef PHYDM_MP_SUPPORT + struct dm_struct *dm = (struct dm_struct *)&pHalData->odmpriv; + struct phydm_mp *mp = &dm->dm_mp_table; + + if (IS_HARDWARE_TYPE_JAGUAR3(pAdapter) || IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)) { + phydm_mp_get_tx_ok(&pHalData->odmpriv, pAdapter->mppriv.rateidx); + count = mp->tx_phy_ok_cnt; + + } else +#endif + { + + if (IS_MPT_CCK_RATE(PMacTxInfo.TX_RATE)) + count = phy_query_bb_reg(pAdapter, 0xF50, bMaskLWord); /* [15:0]*/ + else + count = phy_query_bb_reg(pAdapter, 0xF50, bMaskHWord); /* [31:16]*/ + } + + if (count > 50000) { + rtw_reset_phy_trx_ok_counters(pAdapter); + pAdapter->mppriv.tx.sended += count; + count = 0; + } + + return pAdapter->mppriv.tx.sended + count; + +} + +static void mpt_StopCckContTx( + PADAPTER pAdapter +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + u8 u1bReg; + + pMptCtx->bCckContTx = FALSE; + pMptCtx->bOfdmContTx = FALSE; + + phy_set_bb_reg(pAdapter, rCCK0_System, bCCKBBMode, 0x0); /*normal mode*/ + phy_set_bb_reg(pAdapter, rCCK0_System, bCCKScramble, 0x1); /*turn on scramble setting*/ + + if (!IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter) && !IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)) { + phy_set_bb_reg(pAdapter, 0xa14, 0x300, 0x0); /* 0xa15[1:0] = 2b00*/ + phy_set_bb_reg(pAdapter, rOFDM0_TRMuxPar, 0x10000, 0x0); /* 0xc08[16] = 0*/ + + phy_set_bb_reg(pAdapter, rFPGA0_XA_HSSIParameter2, BIT14, 0); + phy_set_bb_reg(pAdapter, rFPGA0_XB_HSSIParameter2, BIT14, 0); + phy_set_bb_reg(pAdapter, 0x0B34, BIT14, 0); + } + + /*BB Reset*/ + phy_set_bb_reg(pAdapter, rPMAC_Reset, bBBResetB, 0x0); + phy_set_bb_reg(pAdapter, rPMAC_Reset, bBBResetB, 0x1); + + if (!IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter) && !IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter) + && !IS_HARDWARE_TYPE_8723D(pAdapter) && !IS_HARDWARE_TYPE_8192F(pAdapter) + && !IS_HARDWARE_TYPE_8188F(pAdapter)) { + phy_set_bb_reg(pAdapter, rFPGA0_XA_HSSIParameter1, bMaskDWord, 0x01000100); + phy_set_bb_reg(pAdapter, rFPGA0_XB_HSSIParameter1, bMaskDWord, 0x01000100); + } + + if (IS_HARDWARE_TYPE_8188E(pAdapter) || IS_HARDWARE_TYPE_8723B(pAdapter) || + IS_HARDWARE_TYPE_8703B(pAdapter) || IS_HARDWARE_TYPE_8188F(pAdapter) || + IS_HARDWARE_TYPE_8723D(pAdapter) || IS_HARDWARE_TYPE_8192F(pAdapter) || + IS_HARDWARE_TYPE_8821C(pAdapter) || IS_HARDWARE_TYPE_8188GTV(pAdapter)) { + phy_set_bb_reg(pAdapter, 0xA70, BIT(14), bDisable);/* patch Count CCK adjust Rate*/ + } + +} /* mpt_StopCckContTx */ + + +static void mpt_StopOfdmContTx( + PADAPTER pAdapter +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + u8 u1bReg; + u32 data; + + pMptCtx->bCckContTx = FALSE; + pMptCtx->bOfdmContTx = FALSE; + + if (IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter) || IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)) + phy_set_bb_reg(pAdapter, 0x914, BIT18 | BIT17 | BIT16, OFDM_ALL_OFF); + else + phy_set_bb_reg(pAdapter, rOFDM1_LSTF, BIT30 | BIT29 | BIT28, OFDM_ALL_OFF); + + rtw_mdelay_os(10); + + if (!IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter) && !IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)){ + phy_set_bb_reg(pAdapter, 0xa14, 0x300, 0x0); /* 0xa15[1:0] = 0*/ + phy_set_bb_reg(pAdapter, rOFDM0_TRMuxPar, 0x10000, 0x0); /* 0xc08[16] = 0*/ + } + + /*BB Reset*/ + phy_set_bb_reg(pAdapter, rPMAC_Reset, bBBResetB, 0x0); + phy_set_bb_reg(pAdapter, rPMAC_Reset, bBBResetB, 0x1); + + if (!IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter) && !IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter) + && !IS_HARDWARE_TYPE_8723D(pAdapter) && !IS_HARDWARE_TYPE_8192F(pAdapter) + && !IS_HARDWARE_TYPE_8188F(pAdapter)) { + phy_set_bb_reg(pAdapter, rFPGA0_XA_HSSIParameter1, bMaskDWord, 0x01000100); + phy_set_bb_reg(pAdapter, rFPGA0_XB_HSSIParameter1, bMaskDWord, 0x01000100); + } +} /* mpt_StopOfdmContTx */ + + +static void mpt_StartCckContTx( + PADAPTER pAdapter +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + u32 cckrate; + + /* 1. if CCK block on */ + if (!phy_query_bb_reg(pAdapter, rFPGA0_RFMOD, bCCKEn)) + phy_set_bb_reg(pAdapter, rFPGA0_RFMOD, bCCKEn, 1);/*set CCK block on*/ + + /*Turn Off All Test Mode*/ + if (IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter) || IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)) + phy_set_bb_reg(pAdapter, 0x914, BIT18 | BIT17 | BIT16, OFDM_ALL_OFF); + else + phy_set_bb_reg(pAdapter, rOFDM1_LSTF, BIT30 | BIT29 | BIT28, OFDM_ALL_OFF); + + cckrate = pAdapter->mppriv.rateidx; + + phy_set_bb_reg(pAdapter, rCCK0_System, bCCKTxRate, cckrate); + + phy_set_bb_reg(pAdapter, rCCK0_System, bCCKBBMode, 0x2); /*transmit mode*/ + phy_set_bb_reg(pAdapter, rCCK0_System, bCCKScramble, 0x1); /*turn on scramble setting*/ + + if (!IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter) && !IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)) { + phy_set_bb_reg(pAdapter, 0xa14, 0x300, 0x3); /* 0xa15[1:0] = 11 force cck rxiq = 0*/ + phy_set_bb_reg(pAdapter, rOFDM0_TRMuxPar, 0x10000, 0x1); /* 0xc08[16] = 1 force ofdm rxiq = ofdm txiq*/ + phy_set_bb_reg(pAdapter, rFPGA0_XA_HSSIParameter2, BIT14, 1); + phy_set_bb_reg(pAdapter, rFPGA0_XB_HSSIParameter2, BIT14, 1); + phy_set_bb_reg(pAdapter, 0x0B34, BIT14, 1); + } + + if (!IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter) && !IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter) + && !IS_HARDWARE_TYPE_8723D(pAdapter) && !IS_HARDWARE_TYPE_8192F(pAdapter) + && !IS_HARDWARE_TYPE_8188F(pAdapter)) { + phy_set_bb_reg(pAdapter, rFPGA0_XA_HSSIParameter1, bMaskDWord, 0x01000500); + phy_set_bb_reg(pAdapter, rFPGA0_XB_HSSIParameter1, bMaskDWord, 0x01000500); + } + + if (IS_HARDWARE_TYPE_8188E(pAdapter) || IS_HARDWARE_TYPE_8723B(pAdapter) || + IS_HARDWARE_TYPE_8703B(pAdapter) || IS_HARDWARE_TYPE_8188F(pAdapter) || + IS_HARDWARE_TYPE_8723D(pAdapter) || IS_HARDWARE_TYPE_8192F(pAdapter) || + IS_HARDWARE_TYPE_8821C(pAdapter) || IS_HARDWARE_TYPE_8188GTV(pAdapter)) { + if (pAdapter->mppriv.rateidx == MPT_RATE_1M) /* patch Count CCK adjust Rate*/ + phy_set_bb_reg(pAdapter, 0xA70, BIT(14), bDisable); + else + phy_set_bb_reg(pAdapter, 0xA70, BIT(14), bEnable); + } + + pMptCtx->bCckContTx = TRUE; + pMptCtx->bOfdmContTx = FALSE; + +} /* mpt_StartCckContTx */ + + +static void mpt_StartOfdmContTx( + PADAPTER pAdapter +) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + PMPT_CONTEXT pMptCtx = &(pAdapter->mppriv.mpt_ctx); + + /* 1. if OFDM block on?*/ + if (!phy_query_bb_reg(pAdapter, rFPGA0_RFMOD, bOFDMEn)) + phy_set_bb_reg(pAdapter, rFPGA0_RFMOD, bOFDMEn, 1);/*set OFDM block on*/ + + /* 2. set CCK test mode off, set to CCK normal mode*/ + phy_set_bb_reg(pAdapter, rCCK0_System, bCCKBBMode, 0); + + /* 3. turn on scramble setting*/ + phy_set_bb_reg(pAdapter, rCCK0_System, bCCKScramble, 1); + + if (!IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter) && !IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)) { + phy_set_bb_reg(pAdapter, 0xa14, 0x300, 0x3); /* 0xa15[1:0] = 2b'11*/ + phy_set_bb_reg(pAdapter, rOFDM0_TRMuxPar, 0x10000, 0x1); /* 0xc08[16] = 1*/ + } + + /* 4. Turn On Continue Tx and turn off the other test modes.*/ + if (IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter) || IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter)) + phy_set_bb_reg(pAdapter, 0x914, BIT18 | BIT17 | BIT16, OFDM_ContinuousTx); + else + phy_set_bb_reg(pAdapter, rOFDM1_LSTF, BIT30 | BIT29 | BIT28, OFDM_ContinuousTx); + + if (!IS_HARDWARE_TYPE_JAGUAR_ALL(pAdapter) && !IS_HARDWARE_TYPE_JAGUAR3_11N(pAdapter) + && !IS_HARDWARE_TYPE_8723D(pAdapter) && !IS_HARDWARE_TYPE_8192F(pAdapter) + && !IS_HARDWARE_TYPE_8188F(pAdapter)) { + phy_set_bb_reg(pAdapter, rFPGA0_XA_HSSIParameter1, bMaskDWord, 0x01000500); + phy_set_bb_reg(pAdapter, rFPGA0_XB_HSSIParameter1, bMaskDWord, 0x01000500); + } + + pMptCtx->bCckContTx = FALSE; + pMptCtx->bOfdmContTx = TRUE; +} /* mpt_StartOfdmContTx */ + +#if defined(CONFIG_RTL8814A) || defined(CONFIG_RTL8821B) || defined(CONFIG_RTL8822B) \ + || defined(CONFIG_RTL8821C) || defined(CONFIG_RTL8822C) || defined(CONFIG_RTL8814B) \ + || defined(CONFIG_RTL8723F) +#ifdef PHYDM_PMAC_TX_SETTING_SUPPORT +static void mpt_convert_phydm_txinfo_for_jaguar3( + RT_PMAC_TX_INFO *pMacTxInfo, struct phydm_pmac_info *phydmtxinfo) +{ + phydmtxinfo->en_pmac_tx = pMacTxInfo->bEnPMacTx; + phydmtxinfo->mode = pMacTxInfo->Mode; + phydmtxinfo->tx_rate = MRateToHwRate(mpt_to_mgnt_rate(pMacTxInfo->TX_RATE)); + phydmtxinfo->tx_sc = pMacTxInfo->TX_SC; + phydmtxinfo->is_short_preamble = pMacTxInfo->bSPreamble; + phydmtxinfo->ndp_sound = pMacTxInfo->NDP_sound; + phydmtxinfo->bw = pMacTxInfo->BandWidth; + phydmtxinfo->m_stbc = pMacTxInfo->m_STBC; + phydmtxinfo->packet_period = pMacTxInfo->PacketPeriod; + phydmtxinfo->packet_count = pMacTxInfo->PacketCount; + phydmtxinfo->packet_pattern = pMacTxInfo->PacketPattern; + phydmtxinfo->sfd = pMacTxInfo->SFD; + phydmtxinfo->signal_field = pMacTxInfo->SignalField; + phydmtxinfo->service_field = pMacTxInfo->ServiceField; + phydmtxinfo->length = pMacTxInfo->LENGTH; +#if defined(CONFIG_RTL8723F) + if (IS_MPT_CCK_RATE(pMacTxInfo->TX_RATE)) + phydmtxinfo->service_field_bit2= 0x1; + phydmtxinfo->packet_length = pMacTxInfo->PacketLength; +#endif + _rtw_memcpy(&phydmtxinfo->crc16,pMacTxInfo->CRC16, 2); + _rtw_memcpy(&phydmtxinfo->lsig , pMacTxInfo->LSIG,3); + _rtw_memcpy(&phydmtxinfo->ht_sig , pMacTxInfo->HT_SIG,6); + _rtw_memcpy(&phydmtxinfo->vht_sig_a , pMacTxInfo->VHT_SIG_A,6); + _rtw_memcpy(&phydmtxinfo->vht_sig_b , pMacTxInfo->VHT_SIG_B,4); + phydmtxinfo->vht_sig_b_crc = pMacTxInfo->VHT_SIG_B_CRC; + _rtw_memcpy(&phydmtxinfo->vht_delimiter,pMacTxInfo->VHT_Delimiter,4); +} +#endif + +/* for HW TX mode */ +u8 mpt_ProSetPMacTx(PADAPTER Adapter) +{ + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter); + PMPT_CONTEXT pMptCtx = &(Adapter->mppriv.mpt_ctx); + struct mp_priv *pmppriv = &Adapter->mppriv; + struct hal_spec_t *hal_spec = GET_HAL_SPEC(Adapter); + RT_PMAC_TX_INFO PMacTxInfo = pMptCtx->PMacTxInfo; + struct dm_struct *p_dm_odm; + u32 u4bTmp; + u8 status = _TRUE; + + p_dm_odm = &pHalData->odmpriv; + +#if 0 + PRINT_DATA("LSIG ", PMacTxInfo.LSIG, 3); + PRINT_DATA("HT_SIG", PMacTxInfo.HT_SIG, 6); + PRINT_DATA("VHT_SIG_A", PMacTxInfo.VHT_SIG_A, 6); + PRINT_DATA("VHT_SIG_B", PMacTxInfo.VHT_SIG_B, 4); + dbg_print("VHT_SIG_B_CRC %x\n", PMacTxInfo.VHT_SIG_B_CRC); + PRINT_DATA("VHT_Delimiter", PMacTxInfo.VHT_Delimiter, 4); + + PRINT_DATA("Src Address", Adapter->mac_addr, ETH_ALEN); + PRINT_DATA("Dest Address", PMacTxInfo.MacAddress, ETH_ALEN); +#endif + if (pmppriv->pktInterval != 0) + PMacTxInfo.PacketPeriod = pmppriv->pktInterval; + + if (pmppriv->tx.count != 0) + PMacTxInfo.PacketCount = pmppriv->tx.count; + + RTW_INFO("SGI %d bSPreamble %d bSTBC %d bLDPC %d NDP_sound %d\n", PMacTxInfo.bSGI, PMacTxInfo.bSPreamble, PMacTxInfo.bSTBC, PMacTxInfo.bLDPC, PMacTxInfo.NDP_sound); + RTW_INFO("TXSC %d BandWidth %d PacketPeriod %d PacketCount %d PacketLength %d PacketPattern %d\n", PMacTxInfo.TX_SC, PMacTxInfo.BandWidth, PMacTxInfo.PacketPeriod, PMacTxInfo.PacketCount, + PMacTxInfo.PacketLength, PMacTxInfo.PacketPattern); + + if (hal_spec->tx_nss_num < 2 && MPT_IS_2SS_RATE(PMacTxInfo.TX_RATE)) + return _FALSE; + if (hal_spec->tx_nss_num < 3 && MPT_IS_3SS_RATE(PMacTxInfo.TX_RATE)) + return _FALSE; + if (hal_spec->tx_nss_num < 4 && MPT_IS_4SS_RATE(PMacTxInfo.TX_RATE)) + return _FALSE; + if (!is_supported_vht(Adapter->registrypriv.wireless_mode) && MPT_IS_VHT_RATE(PMacTxInfo.TX_RATE)) + return _FALSE; + if (!is_supported_ht(Adapter->registrypriv.wireless_mode) && MPT_IS_HT_RATE(PMacTxInfo.TX_RATE)) + return _FALSE; + + if (PMacTxInfo.BandWidth == 1 && hal_chk_bw_cap(Adapter, BW_CAP_40M)) + PMacTxInfo.BandWidth = CHANNEL_WIDTH_40; + else if (PMacTxInfo.BandWidth == 2 && hal_chk_bw_cap(Adapter, BW_CAP_80M)) + PMacTxInfo.BandWidth = CHANNEL_WIDTH_80; + else + PMacTxInfo.BandWidth = CHANNEL_WIDTH_20; + + if (IS_HARDWARE_TYPE_JAGUAR3(Adapter) || IS_HARDWARE_TYPE_JAGUAR3_11N(Adapter)) { +#ifdef PHYDM_PMAC_TX_SETTING_SUPPORT + struct phydm_pmac_info phydm_mactxinfo; + + if (PMacTxInfo.bEnPMacTx == TRUE) { + pMptCtx->HWTxmode = PMacTxInfo.Mode; + pMptCtx->mpt_rate_index = PMacTxInfo.TX_RATE; + if (PMacTxInfo.Mode == CONTINUOUS_TX) + hal_mpt_SetTxPower(Adapter); + } else { + PMacTxInfo.Mode = pMptCtx->HWTxmode; + PMacTxInfo.TX_RATE = pMptCtx->mpt_rate_index; + pMptCtx->HWTxmode = TEST_NONE; + } + mpt_convert_phydm_txinfo_for_jaguar3(&PMacTxInfo, &phydm_mactxinfo); + phydm_set_pmac_tx(p_dm_odm, &phydm_mactxinfo, pMptCtx->mpt_rf_path); +#endif + return status; + } + + if (PMacTxInfo.bEnPMacTx == FALSE) { + if (pMptCtx->HWTxmode == CONTINUOUS_TX) { + phy_set_bb_reg(Adapter, 0xb04, 0xf, 2); /* TX Stop*/ + if (IS_MPT_CCK_RATE(pMptCtx->mpt_rate_index)) + mpt_StopCckContTx(Adapter); + else + mpt_StopOfdmContTx(Adapter); + } else if (IS_MPT_CCK_RATE(pMptCtx->mpt_rate_index)) { + u4bTmp = phy_query_bb_reg(Adapter, 0xf50, bMaskLWord); + phy_set_bb_reg(Adapter, 0xb1c, bMaskLWord, u4bTmp + 50); + phy_set_bb_reg(Adapter, 0xb04, 0xf, 2); /*TX Stop*/ + } else + phy_set_bb_reg(Adapter, 0xb04, 0xf, 2); /* TX Stop*/ + + if (pMptCtx->HWTxmode == OFDM_Single_Tone_TX) { + /* Stop HW TX -> Stop Continuous TX -> Stop RF Setting*/ + if (IS_MPT_CCK_RATE(pMptCtx->mpt_rate_index)) + mpt_StopCckContTx(Adapter); + else + mpt_StopOfdmContTx(Adapter); + + mpt_SetSingleTone_8814A(Adapter, FALSE, TRUE); + } + pMptCtx->HWTxmode = TEST_NONE; + return status; + } + + pMptCtx->mpt_rate_index = PMacTxInfo.TX_RATE; + + if (PMacTxInfo.Mode == CONTINUOUS_TX) { + pMptCtx->HWTxmode = CONTINUOUS_TX; + PMacTxInfo.PacketCount = 1; + + hal_mpt_SetTxPower(Adapter); + + if (IS_MPT_CCK_RATE(PMacTxInfo.TX_RATE)) + mpt_StartCckContTx(Adapter); + else + mpt_StartOfdmContTx(Adapter); + } else if (PMacTxInfo.Mode == OFDM_Single_Tone_TX) { + /* Continuous TX -> HW TX -> RF Setting */ + pMptCtx->HWTxmode = OFDM_Single_Tone_TX; + PMacTxInfo.PacketCount = 1; + + if (IS_MPT_CCK_RATE(PMacTxInfo.TX_RATE)) + mpt_StartCckContTx(Adapter); + else + mpt_StartOfdmContTx(Adapter); + } else if (PMacTxInfo.Mode == PACKETS_TX) { + pMptCtx->HWTxmode = PACKETS_TX; + if (IS_MPT_CCK_RATE(PMacTxInfo.TX_RATE) && PMacTxInfo.PacketCount == 0) + PMacTxInfo.PacketCount = 0xffff; + } + + if (IS_MPT_CCK_RATE(PMacTxInfo.TX_RATE)) { + /* 0xb1c[0:15] TX packet count 0xb1C[31:16] SFD*/ + u4bTmp = PMacTxInfo.PacketCount | (PMacTxInfo.SFD << 16); + phy_set_bb_reg(Adapter, 0xb1c, bMaskDWord, u4bTmp); + /* 0xb40 7:0 SIGNAL 15:8 SERVICE 31:16 LENGTH*/ + u4bTmp = PMacTxInfo.SignalField | (PMacTxInfo.ServiceField << 8) | (PMacTxInfo.LENGTH << 16); + phy_set_bb_reg(Adapter, 0xb40, bMaskDWord, u4bTmp); + u4bTmp = PMacTxInfo.CRC16[0] | (PMacTxInfo.CRC16[1] << 8); + phy_set_bb_reg(Adapter, 0xb44, bMaskLWord, u4bTmp); + + if (PMacTxInfo.bSPreamble) + phy_set_bb_reg(Adapter, 0xb0c, BIT27, 0); + else + phy_set_bb_reg(Adapter, 0xb0c, BIT27, 1); + } else { + phy_set_bb_reg(Adapter, 0xb18, 0xfffff, PMacTxInfo.PacketCount); + + u4bTmp = PMacTxInfo.LSIG[0] | ((PMacTxInfo.LSIG[1]) << 8) | ((PMacTxInfo.LSIG[2]) << 16) | ((PMacTxInfo.PacketPattern) << 24); + phy_set_bb_reg(Adapter, 0xb08, bMaskDWord, u4bTmp); /* Set 0xb08[23:0] = LSIG, 0xb08[31:24] = Data init octet*/ + + if (PMacTxInfo.PacketPattern == 0x12) + u4bTmp = 0x3000000; + else + u4bTmp = 0; + } + + if (IS_MPT_HT_RATE(PMacTxInfo.TX_RATE)) { + u4bTmp |= PMacTxInfo.HT_SIG[0] | ((PMacTxInfo.HT_SIG[1]) << 8) | ((PMacTxInfo.HT_SIG[2]) << 16); + phy_set_bb_reg(Adapter, 0xb0c, bMaskDWord, u4bTmp); + u4bTmp = PMacTxInfo.HT_SIG[3] | ((PMacTxInfo.HT_SIG[4]) << 8) | ((PMacTxInfo.HT_SIG[5]) << 16); + phy_set_bb_reg(Adapter, 0xb10, 0xffffff, u4bTmp); + } else if (IS_MPT_VHT_RATE(PMacTxInfo.TX_RATE)) { + u4bTmp |= PMacTxInfo.VHT_SIG_A[0] | ((PMacTxInfo.VHT_SIG_A[1]) << 8) | ((PMacTxInfo.VHT_SIG_A[2]) << 16); + phy_set_bb_reg(Adapter, 0xb0c, bMaskDWord, u4bTmp); + u4bTmp = PMacTxInfo.VHT_SIG_A[3] | ((PMacTxInfo.VHT_SIG_A[4]) << 8) | ((PMacTxInfo.VHT_SIG_A[5]) << 16); + phy_set_bb_reg(Adapter, 0xb10, 0xffffff, u4bTmp); + + _rtw_memcpy(&u4bTmp, PMacTxInfo.VHT_SIG_B, 4); + phy_set_bb_reg(Adapter, 0xb14, bMaskDWord, u4bTmp); + } + + if (IS_MPT_VHT_RATE(PMacTxInfo.TX_RATE)) { + u4bTmp = (PMacTxInfo.VHT_SIG_B_CRC << 24) | PMacTxInfo.PacketPeriod; /* for TX interval */ + phy_set_bb_reg(Adapter, 0xb20, bMaskDWord, u4bTmp); + + _rtw_memcpy(&u4bTmp, PMacTxInfo.VHT_Delimiter, 4); + phy_set_bb_reg(Adapter, 0xb24, bMaskDWord, u4bTmp); + + /* 0xb28 - 0xb34 24 byte Probe Request MAC Header*/ + /*& Duration & Frame control*/ + phy_set_bb_reg(Adapter, 0xb28, bMaskDWord, 0x00000040); + + /* Address1 [0:3]*/ + u4bTmp = PMacTxInfo.MacAddress[0] | (PMacTxInfo.MacAddress[1] << 8) | (PMacTxInfo.MacAddress[2] << 16) | (PMacTxInfo.MacAddress[3] << 24); + phy_set_bb_reg(Adapter, 0xb2C, bMaskDWord, u4bTmp); + + /* Address3 [3:0]*/ + phy_set_bb_reg(Adapter, 0xb38, bMaskDWord, u4bTmp); + + /* Address2[0:1] & Address1 [5:4]*/ + u4bTmp = PMacTxInfo.MacAddress[4] | (PMacTxInfo.MacAddress[5] << 8) | (Adapter->mac_addr[0] << 16) | (Adapter->mac_addr[1] << 24); + phy_set_bb_reg(Adapter, 0xb30, bMaskDWord, u4bTmp); + + /* Address2 [5:2]*/ + u4bTmp = Adapter->mac_addr[2] | (Adapter->mac_addr[3] << 8) | (Adapter->mac_addr[4] << 16) | (Adapter->mac_addr[5] << 24); + phy_set_bb_reg(Adapter, 0xb34, bMaskDWord, u4bTmp); + + /* Sequence Control & Address3 [5:4]*/ + /*u4bTmp = PMacTxInfo.MacAddress[4]|(PMacTxInfo.MacAddress[5] << 8) ;*/ + /*phy_set_bb_reg(Adapter, 0xb38, bMaskDWord, u4bTmp);*/ + } else { + phy_set_bb_reg(Adapter, 0xb20, bMaskDWord, PMacTxInfo.PacketPeriod); /* for TX interval*/ + /* & Duration & Frame control */ + phy_set_bb_reg(Adapter, 0xb24, bMaskDWord, 0x00000040); + + /* 0xb24 - 0xb38 24 byte Probe Request MAC Header*/ + /* Address1 [0:3]*/ + u4bTmp = PMacTxInfo.MacAddress[0] | (PMacTxInfo.MacAddress[1] << 8) | (PMacTxInfo.MacAddress[2] << 16) | (PMacTxInfo.MacAddress[3] << 24); + phy_set_bb_reg(Adapter, 0xb28, bMaskDWord, u4bTmp); + + /* Address3 [3:0]*/ + phy_set_bb_reg(Adapter, 0xb34, bMaskDWord, u4bTmp); + + /* Address2[0:1] & Address1 [5:4]*/ + u4bTmp = PMacTxInfo.MacAddress[4] | (PMacTxInfo.MacAddress[5] << 8) | (Adapter->mac_addr[0] << 16) | (Adapter->mac_addr[1] << 24); + phy_set_bb_reg(Adapter, 0xb2c, bMaskDWord, u4bTmp); + + /* Address2 [5:2] */ + u4bTmp = Adapter->mac_addr[2] | (Adapter->mac_addr[3] << 8) | (Adapter->mac_addr[4] << 16) | (Adapter->mac_addr[5] << 24); + phy_set_bb_reg(Adapter, 0xb30, bMaskDWord, u4bTmp); + + /* Sequence Control & Address3 [5:4]*/ + u4bTmp = PMacTxInfo.MacAddress[4] | (PMacTxInfo.MacAddress[5] << 8); + phy_set_bb_reg(Adapter, 0xb38, bMaskDWord, u4bTmp); + } + + phy_set_bb_reg(Adapter, 0xb48, bMaskByte3, PMacTxInfo.TX_RATE_HEX); + + /* 0xb4c 3:0 TXSC 5:4 BW 7:6 m_STBC 8 NDP_Sound*/ + u4bTmp = (PMacTxInfo.TX_SC) | ((PMacTxInfo.BandWidth) << 4) | ((PMacTxInfo.m_STBC - 1) << 6) | ((PMacTxInfo.NDP_sound) << 8); + phy_set_bb_reg(Adapter, 0xb4c, 0x1ff, u4bTmp); + + if (IS_HARDWARE_TYPE_JAGUAR2(Adapter)) { + u32 offset = 0xb44; + + if (IS_MPT_OFDM_RATE(PMacTxInfo.TX_RATE)) + phy_set_bb_reg(Adapter, offset, 0xc0000000, 0); + else if (IS_MPT_HT_RATE(PMacTxInfo.TX_RATE)) + phy_set_bb_reg(Adapter, offset, 0xc0000000, 1); + else if (IS_MPT_VHT_RATE(PMacTxInfo.TX_RATE)) + phy_set_bb_reg(Adapter, offset, 0xc0000000, 2); + + } else if(IS_HARDWARE_TYPE_JAGUAR(Adapter)) { + u32 offset = 0xb4c; + + if(IS_MPT_OFDM_RATE(PMacTxInfo.TX_RATE)) + phy_set_bb_reg(Adapter, offset, 0xc0000000, 0); + else if(IS_MPT_HT_RATE(PMacTxInfo.TX_RATE)) + phy_set_bb_reg(Adapter, offset, 0xc0000000, 1); + else if(IS_MPT_VHT_RATE(PMacTxInfo.TX_RATE)) + phy_set_bb_reg(Adapter, offset, 0xc0000000, 2); + } + + phy_set_bb_reg(Adapter, 0xb00, BIT8, 1); /* Turn on PMAC*/ + /* phy_set_bb_reg(Adapter, 0xb04, 0xf, 2); */ /* TX Stop */ + if (IS_MPT_CCK_RATE(PMacTxInfo.TX_RATE)) { + phy_set_bb_reg(Adapter, 0xb04, 0xf, 8); /*TX CCK ON*/ + phy_set_bb_reg(Adapter, 0xA84, BIT31, 0); + } else + phy_set_bb_reg(Adapter, 0xb04, 0xf, 4); /* TX Ofdm ON */ + + if (PMacTxInfo.Mode == OFDM_Single_Tone_TX) + mpt_SetSingleTone_8814A(Adapter, TRUE, TRUE); + + return status; +} + +#endif + +void hal_mpt_SetContinuousTx(PADAPTER pAdapter, u8 bStart) +{ + u8 Rate; + + RTW_INFO("SetContinuousTx: rate:%d\n", pAdapter->mppriv.rateidx); + Rate = HwRateToMPTRate(pAdapter->mppriv.rateidx); + pAdapter->mppriv.mpt_ctx.is_start_cont_tx = bStart; + + if (Rate <= MPT_RATE_11M) { + if (bStart) + mpt_StartCckContTx(pAdapter); + else + mpt_StopCckContTx(pAdapter); + + } else if (Rate >= MPT_RATE_6M) { + if (bStart) + mpt_StartOfdmContTx(pAdapter); + else + mpt_StopOfdmContTx(pAdapter); + } +} + +void mpt_trigger_tssi_tracking(PADAPTER pAdapter, u8 rf_path) +{ +#ifdef CONFIG_RTL8814B + HAL_DATA_TYPE *pHalData = GET_HAL_DATA(pAdapter); + struct dm_struct *pDM_Odm = &pHalData->odmpriv; + + halrf_do_tssi_8814b(pDM_Odm, rf_path); +#endif +} + +#endif /* CONFIG_MP_INCLUDE*/ diff --git a/hal/hal_phy.c b/hal/hal_phy.c new file mode 100644 index 0000000..cf5cb3b --- /dev/null +++ b/hal/hal_phy.c @@ -0,0 +1,257 @@ +/****************************************************************************** + * + * Copyright(c) 2007 - 2017 Realtek Corporation. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + *****************************************************************************/ +#define _HAL_PHY_C_ + +#include + +/** +* Function: PHY_CalculateBitShift +* +* OverView: Get shifted position of the BitMask +* +* Input: +* u32 BitMask, +* +* Output: none +* Return: u32 Return the shift bit bit position of the mask +*/ +u32 +PHY_CalculateBitShift( + u32 BitMask +) +{ + u32 i; + + for (i = 0; i <= 31; i++) { + if (((BitMask >> i) & 0x1) == 1) + break; + } + + return i; +} + + +#ifdef CONFIG_RF_SHADOW_RW +/* ******************************************************************************** + * Constant. + * ******************************************************************************** + * 2008/11/20 MH For Debug only, RF */ +static RF_SHADOW_T RF_Shadow[RF6052_MAX_PATH][RF6052_MAX_REG]; + +/* + * ==> RF shadow Operation API Code Section!!! + * + *----------------------------------------------------------------------------- + * Function: PHY_RFShadowRead + * PHY_RFShadowWrite + * PHY_RFShadowCompare + * PHY_RFShadowRecorver + * PHY_RFShadowCompareAll + * PHY_RFShadowRecorverAll + * PHY_RFShadowCompareFlagSet + * PHY_RFShadowRecorverFlagSet + * + * Overview: When we set RF register, we must write shadow at first. + * When we are running, we must compare shadow abd locate error addr. + * Decide to recorver or not. + * + * Input: NONE + * + * Output: NONE + * + * Return: NONE + * + * Revised History: + * When Who Remark + * 11/20/2008 MHC Create Version 0. + * + *---------------------------------------------------------------------------*/ +u32 +PHY_RFShadowRead( + PADAPTER Adapter, + enum rf_path eRFPath, + u32 Offset) +{ + return RF_Shadow[eRFPath][Offset].Value; + +} /* PHY_RFShadowRead */ + + +void +PHY_RFShadowWrite( + PADAPTER Adapter, + enum rf_path eRFPath, + u32 Offset, + u32 Data) +{ + RF_Shadow[eRFPath][Offset].Value = (Data & bRFRegOffsetMask); + RF_Shadow[eRFPath][Offset].Driver_Write = _TRUE; + +} /* PHY_RFShadowWrite */ + + +BOOLEAN +PHY_RFShadowCompare( + PADAPTER Adapter, + enum rf_path eRFPath, + u32 Offset) +{ + u32 reg; + /* Check if we need to check the register */ + if (RF_Shadow[eRFPath][Offset].Compare == _TRUE) { + reg = rtw_hal_read_rfreg(Adapter, eRFPath, Offset, bRFRegOffsetMask); + /* Compare shadow and real rf register for 20bits!! */ + if (RF_Shadow[eRFPath][Offset].Value != reg) { + /* Locate error position. */ + RF_Shadow[eRFPath][Offset].ErrorOrNot = _TRUE; + } + return RF_Shadow[eRFPath][Offset].ErrorOrNot ; + } + return _FALSE; +} /* PHY_RFShadowCompare */ + + +void +PHY_RFShadowRecorver( + PADAPTER Adapter, + enum rf_path eRFPath, + u32 Offset) +{ + /* Check if the address is error */ + if (RF_Shadow[eRFPath][Offset].ErrorOrNot == _TRUE) { + /* Check if we need to recorver the register. */ + if (RF_Shadow[eRFPath][Offset].Recorver == _TRUE) { + rtw_hal_write_rfreg(Adapter, eRFPath, Offset, bRFRegOffsetMask, + RF_Shadow[eRFPath][Offset].Value); + } + } + +} /* PHY_RFShadowRecorver */ + + +void +PHY_RFShadowCompareAll( + PADAPTER Adapter) +{ + enum rf_path eRFPath = RF_PATH_A; + u32 Offset = 0, maxReg = GET_RF6052_REAL_MAX_REG(Adapter); + + for (eRFPath = 0; eRFPath < RF6052_MAX_PATH; eRFPath++) { + for (Offset = 0; Offset < maxReg; Offset++) + PHY_RFShadowCompare(Adapter, eRFPath, Offset); + } + +} /* PHY_RFShadowCompareAll */ + + +void +PHY_RFShadowRecorverAll( + PADAPTER Adapter) +{ + enum rf_path eRFPath = RF_PATH_A; + u32 Offset = 0, maxReg = GET_RF6052_REAL_MAX_REG(Adapter); + + for (eRFPath = 0; eRFPath < RF6052_MAX_PATH; eRFPath++) { + for (Offset = 0; Offset < maxReg; Offset++) + PHY_RFShadowRecorver(Adapter, eRFPath, Offset); + } + +} /* PHY_RFShadowRecorverAll */ + + +void +PHY_RFShadowCompareFlagSet( + PADAPTER Adapter, + enum rf_path eRFPath, + u32 Offset, + u8 Type) +{ + /* Set True or False!!! */ + RF_Shadow[eRFPath][Offset].Compare = Type; + +} /* PHY_RFShadowCompareFlagSet */ + + +void +PHY_RFShadowRecorverFlagSet( + PADAPTER Adapter, + enum rf_path eRFPath, + u32 Offset, + u8 Type) +{ + /* Set True or False!!! */ + RF_Shadow[eRFPath][Offset].Recorver = Type; + +} /* PHY_RFShadowRecorverFlagSet */ + + +void +PHY_RFShadowCompareFlagSetAll( + PADAPTER Adapter) +{ + enum rf_path eRFPath = RF_PATH_A; + u32 Offset = 0, maxReg = GET_RF6052_REAL_MAX_REG(Adapter); + + for (eRFPath = 0; eRFPath < RF6052_MAX_PATH; eRFPath++) { + for (Offset = 0; Offset < maxReg; Offset++) { + /* 2008/11/20 MH For S3S4 test, we only check reg 26/27 now!!!! */ + if (Offset != 0x26 && Offset != 0x27) + PHY_RFShadowCompareFlagSet(Adapter, eRFPath, Offset, _FALSE); + else + PHY_RFShadowCompareFlagSet(Adapter, eRFPath, Offset, _TRUE); + } + } + +} /* PHY_RFShadowCompareFlagSetAll */ + + +void +PHY_RFShadowRecorverFlagSetAll( + PADAPTER Adapter) +{ + enum rf_path eRFPath = RF_PATH_A; + u32 Offset = 0, maxReg = GET_RF6052_REAL_MAX_REG(Adapter); + + for (eRFPath = 0; eRFPath < RF6052_MAX_PATH; eRFPath++) { + for (Offset = 0; Offset < maxReg; Offset++) { + /* 2008/11/20 MH For S3S4 test, we only check reg 26/27 now!!!! */ + if (Offset != 0x26 && Offset != 0x27) + PHY_RFShadowRecorverFlagSet(Adapter, eRFPath, Offset, _FALSE); + else + PHY_RFShadowRecorverFlagSet(Adapter, eRFPath, Offset, _TRUE); + } + } + +} /* PHY_RFShadowCompareFlagSetAll */ + +void +PHY_RFShadowRefresh( + PADAPTER Adapter) +{ + enum rf_path eRFPath = RF_PATH_A; + u32 Offset = 0, maxReg = GET_RF6052_REAL_MAX_REG(Adapter); + + for (eRFPath = 0; eRFPath < RF6052_MAX_PATH; eRFPath++) { + for (Offset = 0; Offset < maxReg; Offset++) { + RF_Shadow[eRFPath][Offset].Value = 0; + RF_Shadow[eRFPath][Offset].Compare = _FALSE; + RF_Shadow[eRFPath][Offset].Recorver = _FALSE; + RF_Shadow[eRFPath][Offset].ErrorOrNot = _FALSE; + RF_Shadow[eRFPath][Offset].Driver_Write = _FALSE; + } + } + +} /* PHY_RFShadowRead */ +#endif /*CONFIG_RF_SHADOW_RW*/