[WCNCR00146539] misc: add CFG80211_VERSION_CODE for cfg80211 API ver check

[Description]
Add CFG80211_VERSION_CODE for cfg80211 API ver check.
When compiling driver with backport package, the target
kernel version and backport kernel version are different,
CFG80211_VERSION_CODE must be specified as the backport
version

[Usage]
To build driver within backport package, we need specify
"export CFG_CFG80211_VERSION=$(BACKPORTED_KERNEL_VERSION)"
in Makefile.x86. Then, Makefile will define CFG80211_VERSION_CODE
which is aligned to backport kernel version automatically.
Default "CFG_CFG80211_VERSION" is undefined.

Change-Id: I8087dc371b68a8cf12fe3c445dc75a5e1cde1891
Feature: misc
Signed-off-by: Awk Jiang <awk.jiang@mediatek.com>
CR-Id: WCNCR00146539
diff --git a/Makefile b/Makefile
index c741963..eb58c30 100644
--- a/Makefile
+++ b/Makefile
@@ -57,6 +57,18 @@
 else
     $(error Unsuppoted HIF=$(CONFIG_MTK_COMBO_WIFI_HIF)!!)
 endif
+
+ifneq ($(CFG_CFG80211_VERSION),)
+VERSION_STR = $(subst ",,$(subst ., , $(subst -, ,$(subst v,,$(CFG_CFG80211_VERSION)))))
+$(info VERSION_STR=$(VERSION_STR))
+X = $(firstword $(VERSION_STR))
+Y = $(word 2 ,$(VERSION_STR))
+Z = $(word 3 ,$(VERSION_STR))
+VERSION := $(shell echo "$$(( $X * 65536 + $Y * 256 + $Z))" )
+ccflags-y += -DCFG_CFG80211_VERSION=$(VERSION)
+$(info DCFG_CFG80211_VERSION=$(VERSION))
+endif
+
 $(info HIF=$(CONFIG_MTK_COMBO_WIFI_HIF))
 
 ifeq ($(CONFIG_MTK_PASSPOINT_R1_SUPPORT), y)
diff --git a/Makefile.x86 b/Makefile.x86
index 6ee2142..06b846e 100644
--- a/Makefile.x86
+++ b/Makefile.x86
@@ -20,6 +20,7 @@
 export CONFIG_MTK_COMBO_COMM_UART=m
 export CONFIG_MTK_COMBO_COMM_SDIO=m
 export CONFIG_MT_WIFI_CHRDEV=m
+#export CFG_CFG80211_VERSION ?= $(BACKPORTED_KERNEL_VERSION)
 
 # pcie | sdio | usb
 ifeq ($(hif),)
diff --git a/common/wlan_lib.c b/common/wlan_lib.c
index 0c448dd..b77d452 100644
--- a/common/wlan_lib.c
+++ b/common/wlan_lib.c
@@ -8383,7 +8383,7 @@
 			} else if (prBssInfo->eCurrentOPMode == OP_MODE_INFRASTRUCTURE) {
 				if (prBssInfo->prStaRecOfAP == NULL)
 					continue;
-#if CFG_WPS_DISCONNECT || (KERNEL_VERSION(4, 4, 0) <= LINUX_VERSION_CODE)
+#if CFG_WPS_DISCONNECT || (KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE)
 				kalP2PGCIndicateConnectionStatus(prGlueInfo,
 					(UINT_8) prBssInfo->u4PrivateData,
 					NULL, NULL, 0, 0,
diff --git a/mgmt/p2p_func.c b/mgmt/p2p_func.c
index 21aa21d..47d0a7e 100644
--- a/mgmt/p2p_func.c
+++ b/mgmt/p2p_func.c
@@ -1410,7 +1410,7 @@
 		case OP_MODE_INFRASTRUCTURE:
 			/* Reset station record status. */
 			if (prP2pBssInfo->prStaRecOfAP) {
-#if CFG_WPS_DISCONNECT || (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
+#if CFG_WPS_DISCONNECT || (KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE)
 				kalP2PGCIndicateConnectionStatus(prAdapter->prGlueInfo,
 								 (UINT_8) prP2pBssInfo->u4PrivateData, NULL, NULL, 0,
 								 REASON_CODE_DEAUTH_LEAVING_BSS,
diff --git a/mgmt/p2p_role_fsm.c b/mgmt/p2p_role_fsm.c
index 74f7bce..4de226e 100644
--- a/mgmt/p2p_role_fsm.c
+++ b/mgmt/p2p_role_fsm.c
@@ -601,7 +601,7 @@
 				ASSERT(prP2pBssInfo->prStaRecOfAP == prStaRec);
 
 
-#if CFG_WPS_DISCONNECT || (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
+#if CFG_WPS_DISCONNECT || (KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE)
 /* Indicate disconnect to Host. */
 				kalP2PGCIndicateConnectionStatus(prAdapter->prGlueInfo,
 								(UINT_8) prP2pBssInfo->u4PrivateData, NULL,
@@ -691,7 +691,7 @@
 
 				u2IELength = prSwRfb->u2PacketLen - (WLAN_MAC_HEADER_LEN + REASON_CODE_FIELD_LEN);
 
-#if CFG_WPS_DISCONNECT || (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
+#if CFG_WPS_DISCONNECT || (KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE)
 				/* Indicate disconnect to Host. */
 				kalP2PGCIndicateConnectionStatus(prAdapter->prGlueInfo,
 					(UINT_8) prP2pBssInfo->u4PrivateData, NULL,
@@ -769,7 +769,7 @@
 
 		if (prP2pBssInfo->eConnectionState == PARAM_MEDIA_STATE_CONNECTED) {
 
-#if CFG_WPS_DISCONNECT || (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
+#if CFG_WPS_DISCONNECT || (KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE)
 			/* Indicate disconnect to Host. */
 			kalP2PGCIndicateConnectionStatus(prAdapter->prGlueInfo,
 					prP2pRoleFsmInfo->ucRoleIndex,
@@ -1448,7 +1448,7 @@
 					break;
 				}
 
-#if CFG_WPS_DISCONNECT || (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
+#if CFG_WPS_DISCONNECT || (KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE)
 				kalP2PGCIndicateConnectionStatus(prAdapter->prGlueInfo,
 						prP2pRoleFsmInfo->ucRoleIndex,
 						NULL, NULL, 0, 0,
@@ -1649,7 +1649,7 @@
 					if (prJoinInfo->prTargetBssDesc)
 						scanReportBss2Cfg80211(prAdapter,
 								       OP_MODE_P2P_DEVICE, prJoinInfo->prTargetBssDesc);
-#if CFG_WPS_DISCONNECT || (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
+#if CFG_WPS_DISCONNECT || (KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE)
 					kalP2PGCIndicateConnectionStatus(prAdapter->prGlueInfo,
 									prP2pRoleFsmInfo->ucRoleIndex,
 									&prP2pRoleFsmInfo->rConnReqInfo,
@@ -1685,7 +1685,7 @@
 
 						prBssDesc->fgIsConnecting = FALSE;
 
-#if CFG_WPS_DISCONNECT || (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
+#if CFG_WPS_DISCONNECT || (KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE)
 						kalP2PGCIndicateConnectionStatus(prAdapter->prGlueInfo,
 								prP2pRoleFsmInfo->ucRoleIndex,
 								&prP2pRoleFsmInfo->rConnReqInfo,
diff --git a/os/linux/gl_ate_agent.c b/os/linux/gl_ate_agent.c
index bd4596a..e80b992 100644
--- a/os/linux/gl_ate_agent.c
+++ b/os/linux/gl_ate_agent.c
@@ -81,7 +81,7 @@
 #include "gl_ate_agent.h"
 #include "gl_hook_api.h"
 #include "gl_qa_agent.h"
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
+#if KERNEL_VERSION(3, 8, 0) <= CFG80211_VERSION_CODE
 #include <uapi/linux/nl80211.h>
 #endif
 
diff --git a/os/linux/gl_cfg80211.c b/os/linux/gl_cfg80211.c
index 09c5921..840ae8a 100644
--- a/os/linux/gl_cfg80211.c
+++ b/os/linux/gl_cfg80211.c
@@ -426,7 +426,7 @@
  *         others:  failure
  */
 /*----------------------------------------------------------------------------*/
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
+#if KERNEL_VERSION(3, 16, 0) <= CFG80211_VERSION_CODE
 int mtk_cfg80211_get_station(struct wiphy *wiphy, struct net_device *ndev, const u8 *mac, struct station_info *sinfo)
 {
 	P_GLUE_INFO_T prGlueInfo = NULL;
@@ -461,7 +461,7 @@
 		rStatus = kalIoctl(prGlueInfo,
 				   wlanoidQueryLinkSpeed, &u4Rate, sizeof(u4Rate), TRUE, FALSE, FALSE, &u4BufLen);
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)
+#if KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE
 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
 #else
 		sinfo->filled |= STATION_INFO_TX_BITRATE;
@@ -489,7 +489,7 @@
 		rStatus = kalIoctl(prGlueInfo,
 				   wlanoidQueryRssi, &i4Rssi, sizeof(i4Rssi), TRUE, FALSE, FALSE, &u4BufLen);
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)
+#if KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE
 		sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
 #else
 		sinfo->filled |= STATION_INFO_SIGNAL;
@@ -510,7 +510,7 @@
 
 	if (prDevStats) {
 		/* 4. fill RX_PACKETS */
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)
+#if KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE
 		sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
 #else
 		sinfo->filled |= STATION_INFO_RX_PACKETS;
@@ -518,7 +518,7 @@
 		sinfo->rx_packets = prDevStats->rx_packets;
 
 		/* 5. fill TX_PACKETS */
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)
+#if KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE
 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
 #else
 		sinfo->filled |= STATION_INFO_TX_PACKETS;
@@ -544,7 +544,7 @@
 			u4TotalError = rQueryStaStatistics.u4TxFailCount + rQueryStaStatistics.u4TxLifeTimeoutCount;
 			prDevStats->tx_errors += u4TotalError;
 		}
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)
+#if KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE
 		sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED);
 #else
 		sinfo->filled |= STATION_INFO_TX_FAILED;
@@ -1727,7 +1727,7 @@
  *         others:  failure
  */
 /*----------------------------------------------------------------------------*/
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
+#if KERNEL_VERSION(3, 14, 0) <= CFG80211_VERSION_CODE
 int mtk_cfg80211_mgmt_tx(struct wiphy *wiphy,
 			struct wireless_dev *wdev,
 			struct cfg80211_mgmt_tx_params *params,
@@ -2314,7 +2314,7 @@
 	return i4Status;
 }
 
-#if KERNEL_VERSION(3, 12, 0) <= LINUX_VERSION_CODE
+#if KERNEL_VERSION(3, 12, 0) <= CFG80211_VERSION_CODE
 int mtk_cfg80211_testmode_cmd(struct wiphy *wiphy, struct wireless_dev *wdev,
 			      void *data, int len)
 {
@@ -2375,7 +2375,7 @@
 	if (request->ie_len > 0)
 		prSchedScanRequest->pucIE = (PUINT_8) (request->ie);
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)
+#if KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE
 	prSchedScanRequest->u2ScanInterval = (UINT_16) (request->scan_plans->interval);
 #else
 	prSchedScanRequest->u2ScanInterval = (UINT_16) (request->interval);
@@ -2577,7 +2577,7 @@
  *         others:  failure
  */
 /*----------------------------------------------------------------------------*/
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
+#if KERNEL_VERSION(3, 16, 0) <= CFG80211_VERSION_CODE
 int
 mtk_cfg80211_change_station(struct wiphy *wiphy, struct net_device *ndev, const u8 *mac,
 			    struct station_parameters *params)
@@ -2795,7 +2795,7 @@
  *         others:  failure
  */
 /*----------------------------------------------------------------------------*/
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
+#if KERNEL_VERSION(3, 16, 0) <= CFG80211_VERSION_CODE
 int mtk_cfg80211_add_station(struct wiphy *wiphy, struct net_device *ndev,
 			     const u8 *mac, struct station_parameters *params)
 {
@@ -2882,8 +2882,8 @@
  *		must implement if you have add_station().
  */
 /*----------------------------------------------------------------------------*/
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)
+#if KERNEL_VERSION(3, 16, 0) <= CFG80211_VERSION_CODE
+#if KERNEL_VERSION(3, 19, 0) <= CFG80211_VERSION_CODE
 static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 int mtk_cfg80211_del_station(struct wiphy *wiphy, struct net_device *ndev, struct station_del_parameters *params)
 {
@@ -2979,7 +2979,7 @@
 * \retval WLAN_STATUS_INVALID_LENGTH
 */
 /*----------------------------------------------------------------------------*/
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
+#if KERNEL_VERSION(3, 18, 0) <= CFG80211_VERSION_CODE
 int
 mtk_cfg80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
 		       const u8 *peer, u8 action_code, u8 dialog_token,
@@ -3013,7 +3013,7 @@
 	return 0;
 
 }
-#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
+#elif KERNEL_VERSION(3, 16, 0) <= CFG80211_VERSION_CODE
 int
 mtk_cfg80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
 		       const u8 *peer, u8 action_code, u8 dialog_token,
@@ -3098,7 +3098,7 @@
 * \retval WLAN_STATUS_INVALID_LENGTH
 */
 /*----------------------------------------------------------------------------*/
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
+#if KERNEL_VERSION(3, 16, 0) <= CFG80211_VERSION_CODE
 int mtk_cfg80211_tdls_oper(struct wiphy *wiphy, struct net_device *dev,
 			   const u8 *peer, enum nl80211_tdls_operation oper)
 {
@@ -3236,7 +3236,7 @@
 	 * Ignore the CORE's WW setting when using local data base of regulatory rules
 	 */
 	if ((pRequest->initiator == NL80211_REGDOM_SET_BY_CORE) &&
-#if KERNEL_VERSION(3, 14, 0) > LINUX_VERSION_CODE
+#if KERNEL_VERSION(3, 14, 0) > CFG80211_VERSION_CODE
 		(pWiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY))
 #else
 		(pWiphy->regulatory_flags & REGULATORY_CUSTOM_REG))
@@ -3377,7 +3377,7 @@
 	/*
 	 * clear REGULATORY_CUSTOM_REG flag
 	 */
-#if KERNEL_VERSION(3, 14, 0) > LINUX_VERSION_CODE
+#if KERNEL_VERSION(3, 14, 0) > CFG80211_VERSION_CODE
 	/*tells kernel that assign WW as default*/
 	prWiphy->flags &= ~(WIPHY_FLAG_CUSTOM_REGULATORY);
 #else
@@ -3392,7 +3392,7 @@
 	 * set REGULATORY_CUSTOM_REG flag
 	 */
 #if (CFG_SUPPORT_SINGLE_SKU_LOCAL_DB == 1)
-#if KERNEL_VERSION(3, 14, 0) > LINUX_VERSION_CODE
+#if KERNEL_VERSION(3, 14, 0) > CFG80211_VERSION_CODE
 	/*tells kernel that assign WW as default*/
 	prWiphy->flags |= (WIPHY_FLAG_CUSTOM_REGULATORY);
 #else
diff --git a/os/linux/gl_hook_api.c b/os/linux/gl_hook_api.c
index 77918c8..393dd0d 100644
--- a/os/linux/gl_hook_api.c
+++ b/os/linux/gl_hook_api.c
@@ -79,7 +79,7 @@
 #include "gl_cfg80211.h"
 #include "gl_ate_agent.h"
 #include "gl_qa_agent.h"
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
+#if KERNEL_VERSION(3, 8, 0) <= CFG80211_VERSION_CODE
 #include <uapi/linux/nl80211.h>
 #endif
 /*******************************************************************************
diff --git a/os/linux/gl_init.c b/os/linux/gl_init.c
index bb97b48..9c6bff5 100644
--- a/os/linux/gl_init.c
+++ b/os/linux/gl_init.c
@@ -390,7 +390,7 @@
 #endif
 };
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
+#if KERNEL_VERSION(3, 18, 0) <= CFG80211_VERSION_CODE
 
 static const struct wiphy_vendor_command mtk_wlan_vendor_ops[] = {
 	{
@@ -1479,7 +1479,7 @@
 	prWiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
 	prWiphy->cipher_suites = (const u32 *)mtk_cipher_suites;
 	prWiphy->n_cipher_suites = ARRAY_SIZE(mtk_cipher_suites);
-#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)
+#if KERNEL_VERSION(3, 14, 0) > CFG80211_VERSION_CODE
 	prWiphy->flags = WIPHY_FLAG_CUSTOM_REGULATORY | WIPHY_FLAG_SUPPORTS_FW_ROAM | WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
 #else
 #if (CFG_SUPPORT_DFS_MASTER == 1)
@@ -1498,7 +1498,7 @@
 	prWiphy->max_remain_on_channel_duration = 5000;
 	prWiphy->mgmt_stypes = mtk_cfg80211_ais_default_mgmt_stypes;
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
+#if KERNEL_VERSION(3, 18, 0) <= CFG80211_VERSION_CODE
 	prWiphy->vendor_commands = mtk_wlan_vendor_ops;
 	prWiphy->n_vendor_commands = sizeof(mtk_wlan_vendor_ops) / sizeof(struct wiphy_vendor_command);
 	prWiphy->vendor_events = mtk_wlan_vendor_events;
@@ -1506,7 +1506,7 @@
 #endif
 	/* 4 <1.4> wowlan support */
 #ifdef CONFIG_PM
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0)
+#if KERNEL_VERSION(3, 11, 0) <= CFG80211_VERSION_CODE
 	prWiphy->wowlan = &mtk_wlan_wowlan_support;
 #else
 	kalMemCopy(&prWiphy->wowlan, &mtk_wlan_wowlan_support, sizeof(struct wiphy_wowlan_support));
@@ -2516,7 +2516,7 @@
 
 	if (prGlueInfo->eParamMediaStateIndicated == PARAM_MEDIA_STATE_CONNECTED) {
 
-#if CFG_WPS_DISCONNECT || (KERNEL_VERSION(4, 2, 0) <= LINUX_VERSION_CODE)
+#if CFG_WPS_DISCONNECT || (KERNEL_VERSION(4, 2, 0) <= CFG80211_VERSION_CODE)
 		cfg80211_disconnected(prGlueInfo->prDevHandler, 0, NULL, 0, TRUE, GFP_KERNEL);
 #else
 		cfg80211_disconnected(prGlueInfo->prDevHandler, 0, NULL, 0, GFP_KERNEL);
diff --git a/os/linux/gl_kal.c b/os/linux/gl_kal.c
index e839b5f..da30723 100644
--- a/os/linux/gl_kal.c
+++ b/os/linux/gl_kal.c
@@ -1024,7 +1024,7 @@
 					     (&(prGlueInfo->prAdapter->rWifiVar.rAisFsmInfo)))->prTargetBssDesc;
 
 				if (prBssDesc != NULL) {
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
+#if KERNEL_VERSION(3, 18, 0) <= CFG80211_VERSION_CODE
 					bss = cfg80211_inform_bss(priv_to_wiphy(prGlueInfo),
 								prChannel,
 								CFG80211_BSS_FTYPE_PRESP,
@@ -1101,7 +1101,7 @@
 		if (prGlueInfo->fgIsRegistered == TRUE) {
 			P_BSS_INFO_T prBssInfo = prGlueInfo->prAdapter->prAisBssInfo;
 			UINT_16 u2DeauthReason = 0;
-#if CFG_WPS_DISCONNECT || (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
+#if CFG_WPS_DISCONNECT || (KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE)
 
 			if (prBssInfo)
 				u2DeauthReason = prBssInfo->u2DeauthReason;
@@ -4209,14 +4209,14 @@
 
 		i4Freq = nicChannelNum2Freq(ucChnlNum) / 1000;
 
-#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0))
+#if (KERNEL_VERSION(3, 18, 0) <= CFG80211_VERSION_CODE)
 		cfg80211_rx_mgmt(prGlueInfo->prDevHandler->ieee80211_ptr,
 				i4Freq,	/* in MHz */
 				RCPI_TO_dBm((UINT_8) nicRxGetRcpiValueFromRxv(RCPI_MODE_WF0, prSwRfb)),
 				prSwRfb->pvHeader, prSwRfb->u2PacketLen,
 				NL80211_RXMGMT_FLAG_ANSWERED);
 
-#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
+#elif (KERNEL_VERSION(3, 12, 0) <= CFG80211_VERSION_CODE)
 		cfg80211_rx_mgmt(prGlueInfo->prDevHandler->ieee80211_ptr, i4Freq,	/* in MHz */
 						RCPI_TO_dBm((UINT_8)
 						nicRxGetRcpiValueFromRxv(RCPI_MODE_WF0, prSwRfb)),
diff --git a/os/linux/gl_p2p.c b/os/linux/gl_p2p.c
index 90c4a6b..68e6dd7 100644
--- a/os/linux/gl_p2p.c
+++ b/os/linux/gl_p2p.c
@@ -167,7 +167,7 @@
 #endif
 #endif
 };
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
+#if KERNEL_VERSION(3, 18, 0) <= CFG80211_VERSION_CODE
 
 static const struct wiphy_vendor_command mtk_p2p_vendor_ops[] = {
 	{
@@ -336,7 +336,7 @@
 	},
 };
 
-#if ((CFG_SUPPORT_DFS_MASTER == 1) && (KERNEL_VERSION(3, 17, 0) > LINUX_VERSION_CODE))
+#if ((CFG_SUPPORT_DFS_MASTER == 1) && (KERNEL_VERSION(3, 17, 0) > CFG80211_VERSION_CODE))
 static const struct ieee80211_iface_limit mtk_ap_limits[] = {
 	{
 		.max = 1,
@@ -373,7 +373,7 @@
 		.limits = mtk_p2p_sta_go_limits,
 		.n_limits = ARRAY_SIZE(mtk_p2p_sta_go_limits), /* include p2p */
 	},
-#if ((CFG_SUPPORT_DFS_MASTER == 1) && (KERNEL_VERSION(3, 17, 0) > LINUX_VERSION_CODE))
+#if ((CFG_SUPPORT_DFS_MASTER == 1) && (KERNEL_VERSION(3, 17, 0) > CFG80211_VERSION_CODE))
 	/* ONLY for passing checks in cfg80211_can_use_iftype_chan before linux-3.17.0 */
 	{
 		.num_different_channels = 1,
@@ -1055,7 +1055,7 @@
 	prWiphy->max_remain_on_channel_duration = 5000;
 	prWiphy->n_cipher_suites = 5;
 	prWiphy->cipher_suites = mtk_cipher_suites;
-#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0)
+#if KERNEL_VERSION(3, 14, 0) > CFG80211_VERSION_CODE
 	prWiphy->flags = WIPHY_FLAG_CUSTOM_REGULATORY | WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL | WIPHY_FLAG_HAVE_AP_SME;
 #else
 #if (CFG_SUPPORT_DFS_MASTER == 1)
@@ -1070,13 +1070,13 @@
 	prWiphy->max_scan_ssids = MAX_SCAN_LIST_NUM;
 	prWiphy->max_scan_ie_len = MAX_SCAN_IE_LEN;
 	prWiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
+#if KERNEL_VERSION(3, 18, 0) <= CFG80211_VERSION_CODE
 	prWiphy->vendor_commands = mtk_p2p_vendor_ops;
 	prWiphy->n_vendor_commands = sizeof(mtk_p2p_vendor_ops) / sizeof(struct wiphy_vendor_command);
 #endif
 
 #ifdef CONFIG_PM
-#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0)
+#if KERNEL_VERSION(3, 9, 0) > CFG80211_VERSION_CODE
 	prWiphy->wowlan = &mtk_p2p_wowlan_support;
 #endif
 #endif
diff --git a/os/linux/gl_p2p_cfg80211.c b/os/linux/gl_p2p_cfg80211.c
index d783a11..01746b3 100644
--- a/os/linux/gl_p2p_cfg80211.c
+++ b/os/linux/gl_p2p_cfg80211.c
@@ -260,7 +260,7 @@
 
 }				/* mtk_Netdev_To_RoleIdx */
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)
+#if KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE
 struct wireless_dev *mtk_p2p_cfg80211_add_iface(struct wiphy *wiphy,
 						const char *name, unsigned char name_assign_type,
 						enum nl80211_iftype type, u32 *flags, struct vif_params *params)
@@ -790,7 +790,7 @@
 	return i4Rst;
 }
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
+#if KERNEL_VERSION(3, 16, 0) <= CFG80211_VERSION_CODE
 int mtk_p2p_cfg80211_get_station(struct wiphy *wiphy, struct net_device *ndev,
 		const u8 *mac, struct station_info *sinfo)
 {
@@ -822,7 +822,7 @@
 		p2pFuncGetStationInfo(prGlueInfo->prAdapter, (PUINT_8)mac, &rP2pStaInfo);
 
 		/* Inactive time. */
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)
+#if KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE
 		sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME);
 #else
 		sinfo->filled |= STATION_INFO_INACTIVE_TIME;
@@ -1431,7 +1431,7 @@
 	return i4Rslt;
 }
 
-#if KERNEL_VERSION(3, 15, 0) <= LINUX_VERSION_CODE
+#if KERNEL_VERSION(3, 15, 0) <= CFG80211_VERSION_CODE
 int mtk_p2p_cfg80211_start_radar_detection(struct wiphy *wiphy, struct net_device *dev,
 					struct cfg80211_chan_def *chandef, unsigned int cac_time_ms)
 {
@@ -1890,7 +1890,7 @@
 
 	return i4Rslt;
 }				/* mtk_p2p_cfg80211_cancel_remain_on_channel */
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
+#if KERNEL_VERSION(3, 14, 0) <= CFG80211_VERSION_CODE
 int mtk_p2p_cfg80211_mgmt_tx(struct wiphy *wiphy,
 				struct wireless_dev *wdev,
 				struct cfg80211_mgmt_tx_params *params,
@@ -2171,8 +2171,8 @@
 	return i4Rslt;
 }				/* mtk_p2p_cfg80211_change_bss */
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)
+#if KERNEL_VERSION(3, 16, 0) <= CFG80211_VERSION_CODE
+#if KERNEL_VERSION(3, 19, 0) <= CFG80211_VERSION_CODE
 static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 int mtk_p2p_cfg80211_del_station(struct wiphy *wiphy, struct net_device *dev, struct station_del_parameters *params)
 {
@@ -2672,7 +2672,7 @@
 
 #ifdef CONFIG_NL80211_TESTMODE
 
-#if KERNEL_VERSION(3, 12, 0) <= LINUX_VERSION_CODE
+#if KERNEL_VERSION(3, 12, 0) <= CFG80211_VERSION_CODE
 int mtk_p2p_cfg80211_testmode_cmd(struct wiphy *wiphy,
 				  struct wireless_dev *wdev, void *data,
 				  int len)
diff --git a/os/linux/gl_p2p_kal.c b/os/linux/gl_p2p_kal.c
index ecc8064..3d5ca9c 100644
--- a/os/linux/gl_p2p_kal.c
+++ b/os/linux/gl_p2p_kal.c
@@ -1130,14 +1130,14 @@
 		else
 			prNetdevice = prGlueP2pInfo->aprRoleHandler;
 
-#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0))
+#if (KERNEL_VERSION(3, 18, 0) <= CFG80211_VERSION_CODE)
 		cfg80211_rx_mgmt(prNetdevice->ieee80211_ptr,	/* struct net_device * dev, */
 				 i4Freq,
 				 RCPI_TO_dBm(nicRxGetRcpiValueFromRxv(RCPI_MODE_WF0, prSwRfb)),
 				 prSwRfb->pvHeader,
 				 prSwRfb->u2PacketLen,
 				 NL80211_RXMGMT_FLAG_ANSWERED);
-#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
+#elif (KERNEL_VERSION(3, 12, 0) <= CFG80211_VERSION_CODE)
 		cfg80211_rx_mgmt(prNetdevice->ieee80211_ptr,	/* struct net_device * dev, */
 				 i4Freq,
 				 RCPI_TO_dBm(nicRxGetRcpiValueFromRxv(RCPI_MODE_WF0, prSwRfb)),
@@ -1158,7 +1158,7 @@
 	} while (FALSE);
 
 }				/* kalP2PIndicateRxMgmtFrame */
-#if CFG_WPS_DISCONNECT || (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
+#if CFG_WPS_DISCONNECT || (KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE)
 VOID
 kalP2PGCIndicateConnectionStatus(IN P_GLUE_INFO_T prGlueInfo,
 				 IN UINT_8 ucRoleIndex,
@@ -1253,7 +1253,7 @@
 
 			kalMemZero(&rStationInfo, sizeof(rStationInfo));
 
-#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0)
+#if KERNEL_VERSION(4, 4, 0) > CFG80211_VERSION_CODE
 			rStationInfo.filled = STATION_INFO_ASSOC_REQ_IES;
 #endif
 			rStationInfo.generation = ++prP2pGlueInfo->i4Generation;
diff --git a/os/linux/gl_qa_agent.c b/os/linux/gl_qa_agent.c
index 7b2af39..2db2869 100644
--- a/os/linux/gl_qa_agent.c
+++ b/os/linux/gl_qa_agent.c
@@ -81,7 +81,7 @@
 #include "gl_ate_agent.h"
 #include "gl_qa_agent.h"
 #include "gl_hook_api.h"
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
+#if KERNEL_VERSION(3, 8, 0) <= CFG80211_VERSION_CODE
 #include <uapi/linux/nl80211.h>
 #endif
 
diff --git a/os/linux/include/gl_cfg80211.h b/os/linux/include/gl_cfg80211.h
index f20da01..097a6fe 100644
--- a/os/linux/include/gl_cfg80211.h
+++ b/os/linux/include/gl_cfg80211.h
@@ -184,7 +184,7 @@
 int
 mtk_cfg80211_set_default_key(struct wiphy *wiphy, struct net_device *ndev, u8 key_index, bool unicast, bool multicast);
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
+#if KERNEL_VERSION(3, 16, 0) <= CFG80211_VERSION_CODE
 int mtk_cfg80211_get_station(struct wiphy *wiphy, struct net_device *ndev, const u8 *mac, struct station_info *sinfo);
 #else
 int mtk_cfg80211_get_station(struct wiphy *wiphy, struct net_device *ndev, u8 *mac, struct station_info *sinfo);
@@ -218,7 +218,7 @@
 				   struct ieee80211_channel *chan, unsigned int duration, u64 *cookie);
 
 int mtk_cfg80211_cancel_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev, u64 cookie);
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
+#if KERNEL_VERSION(3, 14, 0) <= CFG80211_VERSION_CODE
 int mtk_cfg80211_mgmt_tx(struct wiphy *wiphy,
 			struct wireless_dev *wdev,
 			struct cfg80211_mgmt_tx_params *params,
@@ -243,7 +243,7 @@
 
 int mtk_cfg80211_testmode_get_scan_done(IN struct wiphy *wiphy, IN void *data, IN int len, IN P_GLUE_INFO_T prGlueInfo);
 
-#if KERNEL_VERSION(3, 12, 0) <= LINUX_VERSION_CODE
+#if KERNEL_VERSION(3, 12, 0) <= CFG80211_VERSION_CODE
 int mtk_cfg80211_testmode_cmd(struct wiphy *wiphy, struct wireless_dev *wdev,
 			      void *data, int len);
 #else
@@ -275,7 +275,7 @@
 
 int mtk_cfg80211_assoc(struct wiphy *wiphy, struct net_device *ndev, struct cfg80211_assoc_request *req);
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
+#if KERNEL_VERSION(3, 16, 0) <= CFG80211_VERSION_CODE
 int
 mtk_cfg80211_change_station(struct wiphy *wiphy, struct net_device *ndev,
 			    const u8 *mac, struct station_parameters *params);
@@ -283,13 +283,13 @@
 int mtk_cfg80211_add_station(struct wiphy *wiphy, struct net_device *ndev,
 			     const u8 *mac, struct station_parameters *params);
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)
+#if KERNEL_VERSION(3, 19, 0) <= CFG80211_VERSION_CODE
 int mtk_cfg80211_del_station(struct wiphy *wiphy, struct net_device *ndev, struct station_del_parameters *params);
 #else
 int mtk_cfg80211_del_station(struct wiphy *wiphy, struct net_device *ndev,
 		const u8 *mac);
 #endif
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0)
+#if KERNEL_VERSION(3, 18, 0) <= CFG80211_VERSION_CODE
 int mtk_cfg80211_tdls_mgmt(struct wiphy *wiphy, struct net_device *dev,
 			   const u8 *peer, u8 action_code, u8 dialog_token,
 			   u16 status_code, u32 peer_capability,
diff --git a/os/linux/include/gl_kal.h b/os/linux/include/gl_kal.h
index 78701b2..09adf2e 100644
--- a/os/linux/include/gl_kal.h
+++ b/os/linux/include/gl_kal.h
@@ -429,7 +429,7 @@
 /*----------------------------------------------------------------------------*/
 /* Macros for kernel related defines                      */
 /*----------------------------------------------------------------------------*/
-#if KERNEL_VERSION(3, 14, 0) > LINUX_VERSION_CODE
+#if KERNEL_VERSION(3, 14, 0) > CFG80211_VERSION_CODE
 #define IEEE80211_CHAN_PASSIVE_FLAG	IEEE80211_CHAN_PASSIVE_SCAN
 #define IEEE80211_CHAN_PASSIVE_STR		"PASSIVE"
 #else
diff --git a/os/linux/include/gl_os.h b/os/linux/include/gl_os.h
index 758830a..690b515 100644
--- a/os/linux/include/gl_os.h
+++ b/os/linux/include/gl_os.h
@@ -178,6 +178,12 @@
 #include <net/iw_handler.h>
 #endif
 
+#ifdef CFG_CFG80211_VERSION
+#define CFG80211_VERSION_CODE CFG_CFG80211_VERSION
+#else
+#define CFG80211_VERSION_CODE LINUX_VERSION_CODE
+#endif
+
 #include "version.h"
 #include "config.h"
 
@@ -200,7 +206,7 @@
 #include <net/addrconf.h>
 #endif /* CFG_SUPPORT_PASSPOINT */
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
+#if KERNEL_VERSION(3, 8, 0) <= CFG80211_VERSION_CODE
 #include <uapi/linux/nl80211.h>
 #endif
 
@@ -881,7 +887,7 @@
 	static u16 ieee8021d_to_queue[8] = { 1, 0, 0, 1, 2, 2, 3, 3 };
 
 	/* cfg80211_classify8021d returns 0~7 */
-#if KERNEL_VERSION(3, 14, 0) > LINUX_VERSION_CODE
+#if KERNEL_VERSION(3, 14, 0) > CFG80211_VERSION_CODE
 	skb->priority = cfg80211_classify8021d(skb);
 #else
 	skb->priority = cfg80211_classify8021d(skb, NULL);
diff --git a/os/linux/include/gl_p2p_ioctl.h b/os/linux/include/gl_p2p_ioctl.h
index 7496f2b..9711aa4 100644
--- a/os/linux/include/gl_p2p_ioctl.h
+++ b/os/linux/include/gl_p2p_ioctl.h
@@ -369,7 +369,7 @@
 
 #if (CFG_ENABLE_WIFI_DIRECT_CFG_80211 != 0)
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)
+#if KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE
 struct wireless_dev *mtk_p2p_cfg80211_add_iface(struct wiphy *wiphy,
 						const char *name, unsigned char name_assign_type,
 						enum nl80211_iftype type, u32 *flags, struct vif_params *params);
@@ -405,7 +405,7 @@
 int
 mtk_p2p_cfg80211_set_default_key(struct wiphy *wiphy,
 				 struct net_device *netdev, u8 key_index, bool unicast, bool multicast);
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
+#if KERNEL_VERSION(3, 16, 0) <= CFG80211_VERSION_CODE
 int mtk_p2p_cfg80211_get_station(struct wiphy *wiphy, struct net_device *ndev,
 				const u8 *mac, struct station_info *sinfo);
 #else
@@ -438,7 +438,7 @@
 
 #if (CFG_SUPPORT_DFS_MASTER == 1)
 
-#if KERNEL_VERSION(3, 15, 0) <= LINUX_VERSION_CODE
+#if KERNEL_VERSION(3, 15, 0) <= CFG80211_VERSION_CODE
 int mtk_p2p_cfg80211_start_radar_detection(struct wiphy *wiphy,
 						struct net_device *dev,
 						struct cfg80211_chan_def *chandef,
@@ -462,7 +462,7 @@
 int mtk_p2p_cfg80211_start_ap(struct wiphy *wiphy, struct net_device *dev, struct cfg80211_ap_settings *settings);
 
 int mtk_p2p_cfg80211_change_beacon(struct wiphy *wiphy, struct net_device *dev, struct cfg80211_beacon_data *info);
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
+#if KERNEL_VERSION(3, 14, 0) <= CFG80211_VERSION_CODE
 int mtk_p2p_cfg80211_mgmt_tx(struct wiphy *wiphy,
 			     struct wireless_dev *wdev,
 			     struct cfg80211_mgmt_tx_params *params,
@@ -475,9 +475,9 @@
 			     bool no_cck, bool dont_wait_for_ack, u64 *cookie);
 #endif
 
-#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 19, 0)
+#if KERNEL_VERSION(3, 19, 0) <= CFG80211_VERSION_CODE
 int mtk_p2p_cfg80211_del_station(struct wiphy *wiphy, struct net_device *dev, struct station_del_parameters *params);
-#elif LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0)
+#elif KERNEL_VERSION(3, 16, 0) <= CFG80211_VERSION_CODE
 int mtk_p2p_cfg80211_del_station(struct wiphy *wiphy, struct net_device *dev, const u8 *mac);
 #else
 int mtk_p2p_cfg80211_del_station(struct wiphy *wiphy, struct net_device *dev, u8 *mac);
@@ -498,7 +498,7 @@
 				  IN const u8 *peer, IN const struct cfg80211_bitrate_mask *mask);
 
 #ifdef CONFIG_NL80211_TESTMODE
-#if KERNEL_VERSION(3, 12, 0) <= LINUX_VERSION_CODE
+#if KERNEL_VERSION(3, 12, 0) <= CFG80211_VERSION_CODE
 int mtk_p2p_cfg80211_testmode_cmd(struct wiphy *wiphy,
 				  struct wireless_dev *wdev, void *data,
 				  int len);
diff --git a/os/linux/include/gl_p2p_kal.h b/os/linux/include/gl_p2p_kal.h
index ff8f5e3..e1e696b 100644
--- a/os/linux/include/gl_p2p_kal.h
+++ b/os/linux/include/gl_p2p_kal.h
@@ -230,7 +230,7 @@
 			     IN UINT_64 u8SeqNum,
 			     IN UINT_32 u4ChannelNum, IN ENUM_BAND_T eBand, IN ENUM_CHNL_EXT_T eSco);
 
-#if CFG_WPS_DISCONNECT  || (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
+#if CFG_WPS_DISCONNECT  || (KERNEL_VERSION(4, 4, 0) <= CFG80211_VERSION_CODE)
 VOID
 kalP2PGCIndicateConnectionStatus(IN P_GLUE_INFO_T prGlueInfo,
 				 IN UINT_8 ucRoleIndex,