Merge 3.18-rc3 into staging-next
[cascardo/linux.git] / drivers / staging / rtl8723au / hal / rtl8723a_bt-coexist.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  *published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  ******************************************************************************/
15 #include <drv_types.h>
16 #include <rtl8723a_hal.h>
17 #include <usb_ops_linux.h>
18
19 #define DIS_PS_RX_BCN
20
21 u32 BTCoexDbgLevel = _bt_dbg_off_;
22
23 #define RTPRINT(_Comp, _Level, Fmt)\
24 do {\
25         if ((BTCoexDbgLevel == _bt_dbg_on_)) {\
26                 printk Fmt;\
27         }                                       \
28 } while (0)
29
30 #define RTPRINT_ADDR(dbgtype, dbgflag, printstr, _Ptr)\
31 if ((BTCoexDbgLevel == _bt_dbg_on_)) {\
32         u32 __i;                                                \
33         u8 *ptr = (u8 *)_Ptr;   \
34         printk printstr;                                \
35         printk(" ");                                    \
36         for (__i = 0; __i < 6; __i++)           \
37                 printk("%02X%s", ptr[__i], (__i == 5)?"":"-");          \
38         printk("\n");                                                   \
39 }
40 #define RTPRINT_DATA(dbgtype, dbgflag, _TitleString, _HexData, _HexDataLen)\
41 if ((BTCoexDbgLevel == _bt_dbg_on_)) {\
42         u32 __i;                                                \
43         u8 *ptr = (u8 *)_HexData;                               \
44         printk(_TitleString);                                   \
45         for (__i = 0; __i < (u32)_HexDataLen; __i++) {          \
46                 printk("%02X%s", ptr[__i], (((__i + 1) % 4) == 0)?"  ":" ");\
47                 if (((__i + 1) % 16) == 0)                      \
48                         printk("\n");                           \
49         }                                                               \
50         printk("\n");                                                   \
51 }
52 /*  Added by Annie, 2005-11-22. */
53 #define MAX_STR_LEN     64
54 /*  I want to see ASCII 33 to 126 only. Otherwise, I print '?'. */
55 #define PRINTABLE(_ch)  (_ch >= ' ' && _ch <= '~')
56 #define RT_PRINT_STR(_Comp, _Level, _TitleString, _Ptr, _Len)           \
57         {                                                               \
58                 u32 __i;                                                \
59                 u8 buffer[MAX_STR_LEN];                                 \
60                 u32 length = (_Len < MAX_STR_LEN) ? _Len : (MAX_STR_LEN-1);\
61                 memset(buffer, 0, MAX_STR_LEN);                         \
62                 memcpy(buffer, (u8 *)_Ptr, length);                     \
63                 for (__i = 0; __i < length; __i++) {                    \
64                         if (!PRINTABLE(buffer[__i]))                    \
65                                 buffer[__i] = '?';                      \
66                 }                                                       \
67                 buffer[length] = '\0';                                  \
68                 printk(_TitleString);                                   \
69                 printk(": %d, <%s>\n", _Len, buffer);                   \
70         }
71
72 #define DCMD_Printf(...)
73 #define RT_ASSERT(...)
74
75 #define rsprintf snprintf
76
77 #define GetDefaultAdapter(padapter)     padapter
78
79 #define PlatformZeroMemory(ptr, sz)     memset(ptr, 0, sz)
80
81 #define PlatformProcessHCICommands(...)
82 #define PlatformTxBTQueuedPackets(...)
83 #define PlatformIndicateBTACLData(...)  (RT_STATUS_SUCCESS)
84 #define PlatformAcquireSpinLock(padapter, type)
85 #define PlatformReleaseSpinLock(padapter, type)
86
87 #define GET_UNDECORATED_AVERAGE_RSSI(padapter)  \
88                         (GET_HAL_DATA(padapter)->dmpriv.EntryMinUndecoratedSmoothedPWDB)
89 #define RT_RF_CHANGE_SOURCE u32
90
91 enum {
92         RT_JOIN_INFRA   = 1,
93         RT_JOIN_IBSS  = 2,
94         RT_START_IBSS = 3,
95         RT_NO_ACTION  = 4,
96 };
97
98 /*  power saving */
99
100 /*  ===== Below this line is sync from SD7 driver COMMOM/BT.c ===== */
101
102 static u8 BT_Operation(struct rtw_adapter *padapter)
103 {
104         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
105         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
106
107         if (pBtMgnt->BtOperationOn)
108                 return true;
109         else
110                 return false;
111 }
112
113 static u8 BT_IsLegalChannel(struct rtw_adapter *padapter, u8 channel)
114 {
115         struct rt_channel_info *pChanneList = NULL;
116         u8 channelLen, i;
117
118         pChanneList = padapter->mlmeextpriv.channel_set;
119         channelLen = padapter->mlmeextpriv.max_chan_nums;
120
121         for (i = 0; i < channelLen; i++) {
122                 RTPRINT(FIOCTL, IOCTL_STATE,
123                         ("Check if chnl(%d) in channel plan contains bt target chnl(%d) for BT connection\n",
124                          pChanneList[i].ChannelNum, channel));
125                 if ((channel == pChanneList[i].ChannelNum) ||
126                     (channel == pChanneList[i].ChannelNum + 2))
127                         return channel;
128         }
129         return 0;
130 }
131
132 void BT_SignalCompensation(struct rtw_adapter *padapter, u8 *rssi_wifi, u8 *rssi_bt)
133 {
134         BTDM_SignalCompensation(padapter, rssi_wifi, rssi_bt);
135 }
136
137 void rtl8723a_BT_wifiscan_notify(struct rtw_adapter *padapter, u8 scanType)
138 {
139         BTHCI_WifiScanNotify(padapter, scanType);
140         BTDM_CheckAntSelMode(padapter);
141         BTDM_WifiScanNotify(padapter, scanType);
142 }
143
144 void rtl8723a_BT_wifiassociate_notify(struct rtw_adapter *padapter, u8 action)
145 {
146         /*  action : */
147         /*  true = associate start */
148         /*  false = associate finished */
149         if (action)
150                 BTDM_CheckAntSelMode(padapter);
151
152         BTDM_WifiAssociateNotify(padapter, action);
153 }
154
155 void BT_HaltProcess(struct rtw_adapter *padapter)
156 {
157         BTDM_ForHalt(padapter);
158 }
159
160 /*  ===== End of sync from SD7 driver COMMOM/BT.c ===== */
161
162 #define i64fmt          "ll"
163 #define UINT64_C(v)  (v)
164
165 #define FillOctetString(_os, _octet, _len)              \
166         (_os).Octet = (u8 *)(_octet);                   \
167         (_os).Length = (_len);
168
169 static enum rt_status PlatformIndicateBTEvent(
170         struct rtw_adapter *padapter,
171         void                                            *pEvntData,
172         u32                                             dataLen
173         )
174 {
175         enum rt_status  rt_status = RT_STATUS_FAILURE;
176
177         RTPRINT(FIOCTL, IOCTL_BT_EVENT_DETAIL, ("BT event start, %d bytes data to Transferred!!\n", dataLen));
178         RTPRINT_DATA(FIOCTL, IOCTL_BT_EVENT_DETAIL, "To transfer Hex Data :\n",
179                 pEvntData, dataLen);
180
181         BT_EventParse(padapter, pEvntData, dataLen);
182
183         printk(KERN_WARNING "%s: Linux has no way to report BT event!!\n", __func__);
184
185         RTPRINT(FIOCTL, IOCTL_BT_EVENT_DETAIL, ("BT event end, %s\n",
186                 (rt_status == RT_STATUS_SUCCESS) ? "SUCCESS" : "FAIL"));
187
188         return rt_status;
189 }
190
191 /*  ===== Below this line is sync from SD7 driver COMMOM/bt_hci.c ===== */
192
193 static u8 bthci_GetLocalChannel(struct rtw_adapter *padapter)
194 {
195         return padapter->mlmeextpriv.cur_channel;
196 }
197
198 static u8 bthci_GetCurrentEntryNum(struct rtw_adapter *padapter, u8 PhyHandle)
199 {
200         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
201         u8 i;
202
203         for (i = 0; i < MAX_BT_ASOC_ENTRY_NUM; i++) {
204                 if ((pBTInfo->BtAsocEntry[i].bUsed) &&
205                     (pBTInfo->BtAsocEntry[i].PhyLinkCmdData.BtPhyLinkhandle == PhyHandle))
206                         return i;
207         }
208
209         return 0xFF;
210 }
211
212 static void bthci_DecideBTChannel(struct rtw_adapter *padapter, u8 EntryNum)
213 {
214 /*PMGNT_INFO pMgntInfo = &padapter->MgntInfo; */
215         struct mlme_priv *pmlmepriv;
216         struct bt_30info *pBTInfo;
217         struct bt_mgnt *pBtMgnt;
218         struct bt_hci_info *pBtHciInfo;
219         struct chnl_txpower_triple *pTriple_subband = NULL;
220         struct common_triple *pTriple;
221         u8 i, j, localchnl, firstRemoteLegalChnlInTriplet = 0;
222         u8 regulatory_skipLen = 0;
223         u8 subbandTripletCnt = 0;
224
225         pmlmepriv = &padapter->mlmepriv;
226         pBTInfo = GET_BT_INFO(padapter);
227         pBtMgnt = &pBTInfo->BtMgnt;
228         pBtHciInfo = &pBTInfo->BtHciInfo;
229
230         pBtMgnt->CheckChnlIsSuit = true;
231         localchnl = bthci_GetLocalChannel(padapter);
232
233         pTriple = (struct common_triple *)
234                 &pBtHciInfo->BTPreChnllist[COUNTRY_STR_LEN];
235
236         /*  contains country string, len is 3 */
237         for (i = 0; i < (pBtHciInfo->BtPreChnlListLen-COUNTRY_STR_LEN); i += 3, pTriple++) {
238                 /*  */
239                 /*  check every triplet, an triplet may be */
240                 /*  regulatory extension identifier or sub-band triplet */
241                 /*  */
242                 if (pTriple->byte_1st == 0xc9) {
243                         /*  Regulatory Extension Identifier, skip it */
244                         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO),
245                                 ("Find Regulatory ID, regulatory class = %d\n", pTriple->byte_2nd));
246                         regulatory_skipLen += 3;
247                         pTriple_subband = NULL;
248                         continue;
249                 } else {        /*  Sub-band triplet */
250                         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Find Sub-band triplet \n"));
251                         subbandTripletCnt++;
252                         pTriple_subband = (struct chnl_txpower_triple *)pTriple;
253                         /*  if remote first legal channel not found, then find first remote channel */
254                         /*  and it's legal for our channel plan. */
255
256                         /*  search the sub-band triplet and find if remote channel is legal to our channel plan. */
257                         for (j = pTriple_subband->FirstChnl; j < (pTriple_subband->FirstChnl+pTriple_subband->NumChnls); j++) {
258                                 RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), (" Check if chnl(%d) is legal\n", j));
259                                 if (BT_IsLegalChannel(padapter, j)) {
260                                         /*  remote channel is legal for our channel plan. */
261                                         firstRemoteLegalChnlInTriplet = j;
262                                         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO),
263                                                 ("Find first remote legal channel : %d\n",
264                                                 firstRemoteLegalChnlInTriplet));
265
266                                         /*  If we find a remote legal channel in the sub-band triplet */
267                                         /*  and only BT connection is established(local not connect to any AP or IBSS), */
268                                         /*  then we just switch channel to remote channel. */
269                                         if (!(check_fwstate(pmlmepriv, WIFI_ASOC_STATE|WIFI_ADHOC_STATE|WIFI_AP_STATE) ||
270                                             BTHCI_HsConnectionEstablished(padapter))) {
271                                                 pBtMgnt->BTChannel = firstRemoteLegalChnlInTriplet;
272                                                 RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Remote legal channel (%d) is selected, Local not connect to any!!\n", pBtMgnt->BTChannel));
273                                                 return;
274                                         } else {
275                                                 if ((localchnl >= firstRemoteLegalChnlInTriplet) &&
276                                                     (localchnl < (pTriple_subband->FirstChnl+pTriple_subband->NumChnls))) {
277                                                         pBtMgnt->BTChannel = localchnl;
278                                                         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Local channel (%d) is selected, wifi or BT connection exists\n", pBtMgnt->BTChannel));
279                                                         return;
280                                                 }
281                                         }
282                                         break;
283                                 }
284                         }
285                 }
286         }
287
288         if (subbandTripletCnt) {
289                 /* if any preferred channel triplet exists */
290                 RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("There are %d sub band triplet exists, ", subbandTripletCnt));
291                 if (firstRemoteLegalChnlInTriplet == 0) {
292                         /* no legal channel is found, reject the connection. */
293                         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("no legal channel is found!!\n"));
294                 } else {
295                         /*  Remote Legal channel is found but not match to local */
296                         /* wifi connection exists), so reject the connection. */
297                         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO),
298                                 ("Remote Legal channel is found but not match to local(wifi connection exists)!!\n"));
299                 }
300                 pBtMgnt->CheckChnlIsSuit = false;
301         } else {
302                 /*  There are not any preferred channel triplet exists */
303                 /*  Use current legal channel as the bt channel. */
304                 RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("No sub band triplet exists!!\n"));
305         }
306         pBtMgnt->BTChannel = localchnl;
307         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Local channel (%d) is selected!!\n", pBtMgnt->BTChannel));
308 }
309
310 /* Success:return true */
311 /* Fail:return false */
312 static u8 bthci_GetAssocInfo(struct rtw_adapter *padapter, u8 EntryNum)
313 {
314         struct bt_30info *pBTInfo;
315         struct bt_hci_info *pBtHciInfo;
316         u8 tempBuf[256];
317         u8 i = 0;
318         u8 BaseMemoryShift = 0;
319         u16     TotalLen = 0;
320         struct amp_assoc_structure *pAmpAsoc;
321
322         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("GetAssocInfo start\n"));
323         pBTInfo = GET_BT_INFO(padapter);
324         pBtHciInfo = &pBTInfo->BtHciInfo;
325
326         if (pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.LenSoFar == 0) {
327                 if (pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.AMPAssocRemLen < (MAX_AMP_ASSOC_FRAG_LEN))
328                         TotalLen = pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.AMPAssocRemLen;
329                 else if (pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.AMPAssocRemLen == (MAX_AMP_ASSOC_FRAG_LEN))
330                         TotalLen = MAX_AMP_ASSOC_FRAG_LEN;
331         } else if (pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.LenSoFar > 0)
332                 TotalLen = pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.LenSoFar;
333
334         while ((pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.LenSoFar >= BaseMemoryShift) || TotalLen > BaseMemoryShift) {
335                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_DETAIL, ("GetAssocInfo, TotalLen =%d, BaseMemoryShift =%d\n", TotalLen, BaseMemoryShift));
336                 memcpy(tempBuf,
337                         (u8 *)pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.AMPAssocfragment+BaseMemoryShift,
338                         TotalLen-BaseMemoryShift);
339                 RTPRINT_DATA(FIOCTL, IOCTL_BT_HCICMD_DETAIL, "GetAssocInfo :\n",
340                         tempBuf, TotalLen-BaseMemoryShift);
341
342                 pAmpAsoc = (struct amp_assoc_structure *)tempBuf;
343                 pAmpAsoc->Length = le16_to_cpu(pAmpAsoc->Length);
344                 BaseMemoryShift += 3 + pAmpAsoc->Length;
345
346                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("TypeID = 0x%x, ", pAmpAsoc->TypeID));
347                 RTPRINT_DATA(FIOCTL, IOCTL_BT_HCICMD, "Hex Data: \n", pAmpAsoc->Data, pAmpAsoc->Length);
348                 switch (pAmpAsoc->TypeID) {
349                 case AMP_MAC_ADDR:
350                         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("==> AMP_MAC_ADDR\n"));
351                         if (pAmpAsoc->Length > 6)
352                                 return false;
353                         memcpy(pBTInfo->BtAsocEntry[EntryNum].BTRemoteMACAddr, pAmpAsoc->Data, 6);
354                         RTPRINT_ADDR(FIOCTL, IOCTL_BT_HCICMD, ("Remote Mac address \n"), pBTInfo->BtAsocEntry[EntryNum].BTRemoteMACAddr);
355                         break;
356                 case AMP_PREFERRED_CHANNEL_LIST:
357                         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("==> AMP_PREFERRED_CHANNEL_LIST\n"));
358                         pBtHciInfo->BtPreChnlListLen = pAmpAsoc->Length;
359                         memcpy(pBtHciInfo->BTPreChnllist,
360                                 pAmpAsoc->Data,
361                                 pBtHciInfo->BtPreChnlListLen);
362                         RTPRINT_DATA(FIOCTL, IOCTL_BT_HCICMD, "Preferred channel list : \n", pBtHciInfo->BTPreChnllist, pBtHciInfo->BtPreChnlListLen);
363                         bthci_DecideBTChannel(padapter, EntryNum);
364                         break;
365                 case AMP_CONNECTED_CHANNEL:
366                         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("==> AMP_CONNECTED_CHANNEL\n"));
367                         pBtHciInfo->BTConnectChnlListLen = pAmpAsoc->Length;
368                         memcpy(pBtHciInfo->BTConnectChnllist,
369                                 pAmpAsoc->Data,
370                                 pBtHciInfo->BTConnectChnlListLen);
371                         break;
372                 case AMP_80211_PAL_CAP_LIST:
373                         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("==> AMP_80211_PAL_CAP_LIST\n"));
374                         pBTInfo->BtAsocEntry[EntryNum].BTCapability = *(u32 *)(pAmpAsoc->Data);
375                         if (pBTInfo->BtAsocEntry[EntryNum].BTCapability & 0x00000001) {
376                                 /*  TODO: */
377
378                                 /* Signifies PAL capable of utilizing received activity reports. */
379                         }
380                         if (pBTInfo->BtAsocEntry[EntryNum].BTCapability & 0x00000002) {
381                                 /*  TODO: */
382                                 /* Signifies PAL is capable of utilizing scheduling information received in an activity reports. */
383                         }
384                         break;
385                 case AMP_80211_PAL_VISION:
386                         pBtHciInfo->BTPalVersion = *(u8 *)(pAmpAsoc->Data);
387                         pBtHciInfo->BTPalCompanyID = *(u16 *)(((u8 *)(pAmpAsoc->Data))+1);
388                         pBtHciInfo->BTPalsubversion = *(u16 *)(((u8 *)(pAmpAsoc->Data))+3);
389                         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("==> AMP_80211_PAL_VISION PalVersion  0x%x, PalCompanyID  0x%x, Palsubversion 0x%x\n",
390                                 pBtHciInfo->BTPalVersion,
391                                 pBtHciInfo->BTPalCompanyID,
392                                 pBtHciInfo->BTPalsubversion));
393                         break;
394                 default:
395                         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("==> Unsupport TypeID !!\n"));
396                         break;
397                 }
398                 i++;
399         }
400         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("GetAssocInfo end\n"));
401
402         return true;
403 }
404
405 static u8 bthci_AddEntry(struct rtw_adapter *padapter)
406 {
407         struct bt_30info *pBTInfo;
408         struct bt_mgnt *pBtMgnt;
409         u8 i;
410
411         pBTInfo = GET_BT_INFO(padapter);
412         pBtMgnt = &pBTInfo->BtMgnt;
413
414         for (i = 0; i < MAX_BT_ASOC_ENTRY_NUM; i++) {
415                 if (pBTInfo->BtAsocEntry[i].bUsed == false) {
416                         pBTInfo->BtAsocEntry[i].bUsed = true;
417                         pBtMgnt->CurrentConnectEntryNum = i;
418                         break;
419                 }
420         }
421
422         if (i == MAX_BT_ASOC_ENTRY_NUM) {
423                 RTPRINT(FIOCTL, IOCTL_STATE, ("bthci_AddEntry(), Add entry fail!!\n"));
424                 return false;
425         }
426         return true;
427 }
428
429 static u8 bthci_DiscardTxPackets(struct rtw_adapter *padapter, u16 LLH)
430 {
431         return false;
432 }
433
434 static u8
435 bthci_CheckLogLinkBehavior(
436         struct rtw_adapter *padapter,
437         struct hci_flow_spec                    TxFlowSpec
438         )
439 {
440         u8 ID = TxFlowSpec.Identifier;
441         u8 ServiceType = TxFlowSpec.ServiceType;
442         u16     MaxSDUSize = TxFlowSpec.MaximumSDUSize;
443         u32     SDUInterArrivatime = TxFlowSpec.SDUInterArrivalTime;
444         u8 match = false;
445
446         switch (ID) {
447         case 1:
448                 if (ServiceType == BT_LL_BE) {
449                         match = true;
450                         RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("Logical Link Type =  TX best effort flowspec\n"));
451                 } else if ((ServiceType == BT_LL_GU) && (MaxSDUSize == 0xffff)) {
452                         match = true;
453                         RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("Logical Link Type =  RX guaranteed latency flowspec\n"));
454                 } else if ((ServiceType == BT_LL_GU) && (MaxSDUSize == 2500)) {
455                         RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("Logical Link Type =  RX guaranteed Large latency flowspec\n"));
456                 }
457                 break;
458         case 2:
459                 if (ServiceType == BT_LL_BE) {
460                         match = true;
461                         RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("Logical Link Type =  RX best effort flowspec\n"));
462
463                 }
464                 break;
465         case 3:
466                 if ((ServiceType == BT_LL_GU) && (MaxSDUSize == 1492)) {
467                         match = true;
468                         RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("Logical Link Type =  TX guaranteed latency flowspec\n"));
469                 } else if ((ServiceType == BT_LL_GU) && (MaxSDUSize == 2500)) {
470                         RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("Logical Link Type =  TX guaranteed Large latency flowspec\n"));
471                 }
472                 break;
473         case 4:
474                 if (ServiceType == BT_LL_BE) {
475                         if ((SDUInterArrivatime == 0xffffffff) && (ServiceType == BT_LL_BE) && (MaxSDUSize == 1492)) {
476                                 match = true;
477                                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("Logical Link Type =  TX/RX aggregated best effort flowspec\n"));
478                         }
479                 } else if (ServiceType == BT_LL_GU) {
480                         if (SDUInterArrivatime == 100) {
481                                 match = true;
482                                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("Logical Link Type =  TX/RX guaranteed bandwidth flowspec\n"));
483                         }
484                 }
485                 break;
486         default:
487                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("Logical Link Type =  Unknow Type !!!!!!!!\n"));
488                 break;
489         }
490
491         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO),
492                 ("ID = 0x%x, ServiceType = 0x%x, MaximumSDUSize = 0x%x, SDUInterArrivalTime = 0x%x, AccessLatency = 0x%x, FlushTimeout = 0x%x\n",
493                 TxFlowSpec.Identifier, TxFlowSpec.ServiceType, MaxSDUSize,
494                 SDUInterArrivatime, TxFlowSpec.AccessLatency, TxFlowSpec.FlushTimeout));
495         return match;
496 }
497
498 static u16 bthci_AssocMACAddr(struct rtw_adapter *padapter, void        *pbuf)
499 {
500         struct amp_assoc_structure *pAssoStrc = (struct amp_assoc_structure *)pbuf;
501         pAssoStrc->TypeID = AMP_MAC_ADDR;
502         pAssoStrc->Length = 0x06;
503         memcpy(&pAssoStrc->Data[0], padapter->eeprompriv.mac_addr, 6);
504         RTPRINT_DATA(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO),
505                      ("AssocMACAddr : \n"), pAssoStrc, pAssoStrc->Length+3);
506
507         return pAssoStrc->Length + 3;
508 }
509
510 static u16
511 bthci_PALCapabilities(
512         struct rtw_adapter *padapter,
513         void    *pbuf
514         )
515 {
516         struct amp_assoc_structure *pAssoStrc = (struct amp_assoc_structure *)pbuf;
517
518         pAssoStrc->TypeID = AMP_80211_PAL_CAP_LIST;
519         pAssoStrc->Length = 0x04;
520
521         pAssoStrc->Data[0] = 0x00;
522         pAssoStrc->Data[1] = 0x00;
523
524         RTPRINT_DATA(FIOCTL, IOCTL_BT_HCICMD_DETAIL, ("PALCapabilities:\n"), pAssoStrc, pAssoStrc->Length+3);
525         RTPRINT(FIOCTL, IOCTL_BT_LOGO, ("PALCapabilities \n"));
526
527         RTPRINT(FIOCTL, IOCTL_BT_LOGO, (" TypeID = 0x%x,\n Length = 0x%x,\n Content = 0x0000\n",
528                 pAssoStrc->TypeID,
529                 pAssoStrc->Length));
530
531         return pAssoStrc->Length + 3;
532 }
533
534 static u16 bthci_AssocPreferredChannelList(struct rtw_adapter *padapter,
535                                            void *pbuf, u8 EntryNum)
536 {
537         struct bt_30info *pBTInfo;
538         struct amp_assoc_structure *pAssoStrc;
539         struct amp_pref_chnl_regulatory *pReg;
540         struct chnl_txpower_triple *pTriple;
541         char ctrString[3] = {'X', 'X', 'X'};
542         u32 len = 0;
543         u8 preferredChnl;
544
545         pBTInfo = GET_BT_INFO(padapter);
546         pAssoStrc = (struct amp_assoc_structure *)pbuf;
547         pReg = (struct amp_pref_chnl_regulatory *)&pAssoStrc->Data[3];
548
549         preferredChnl = bthci_GetLocalChannel(padapter);
550         pAssoStrc->TypeID = AMP_PREFERRED_CHANNEL_LIST;
551
552         /*  locale unknown */
553         memcpy(&pAssoStrc->Data[0], &ctrString[0], 3);
554         pReg->reXId = 201;
555         pReg->regulatoryClass = 254;
556         pReg->coverageClass = 0;
557         len += 6;
558         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD | IOCTL_BT_LOGO), ("PREFERRED_CHNL_LIST\n"));
559         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD | IOCTL_BT_LOGO), ("XXX, 201, 254, 0\n"));
560         /*  at the following, chnl 1~11 should be contained */
561         pTriple = (struct chnl_txpower_triple *)&pAssoStrc->Data[len];
562
563         /*  (1) if any wifi or bt HS connection exists */
564         if ((pBTInfo->BtAsocEntry[EntryNum].AMPRole == AMP_BTAP_CREATOR) ||
565             (check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE |
566                            WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE |
567                            WIFI_AP_STATE)) ||
568             BTHCI_HsConnectionEstablished(padapter)) {
569                 pTriple->FirstChnl = preferredChnl;
570                 pTriple->NumChnls = 1;
571                 pTriple->MaxTxPowerInDbm = 20;
572                 len += 3;
573                 RTPRINT(FIOCTL, (IOCTL_BT_HCICMD | IOCTL_BT_LOGO), ("First Channel = %d, Channel Num = %d, MaxDbm = %d\n",
574                         pTriple->FirstChnl,
575                         pTriple->NumChnls,
576                         pTriple->MaxTxPowerInDbm));
577         }
578
579         pAssoStrc->Length = (u16)len;
580         RTPRINT_DATA(FIOCTL, IOCTL_BT_HCICMD, ("AssocPreferredChannelList : \n"), pAssoStrc, pAssoStrc->Length+3);
581
582         return pAssoStrc->Length + 3;
583 }
584
585 static u16 bthci_AssocPALVer(struct rtw_adapter *padapter, void *pbuf)
586 {
587         struct amp_assoc_structure *pAssoStrc = (struct amp_assoc_structure *)pbuf;
588         u8 *pu1Tmp;
589         u16     *pu2Tmp;
590
591         pAssoStrc->TypeID = AMP_80211_PAL_VISION;
592         pAssoStrc->Length = 0x5;
593         pu1Tmp = &pAssoStrc->Data[0];
594         *pu1Tmp = 0x1;  /*  PAL Version */
595         pu2Tmp = (u16 *)&pAssoStrc->Data[1];
596         *pu2Tmp = 0x5D; /*  SIG Company identifier of 802.11 PAL vendor */
597         pu2Tmp = (u16 *)&pAssoStrc->Data[3];
598         *pu2Tmp = 0x1;  /*  PAL Sub-version specifier */
599
600         RTPRINT_DATA(FIOCTL, IOCTL_BT_HCICMD_DETAIL, ("AssocPALVer : \n"), pAssoStrc, pAssoStrc->Length+3);
601         RTPRINT(FIOCTL, IOCTL_BT_LOGO, ("AssocPALVer \n"));
602
603         RTPRINT(FIOCTL, IOCTL_BT_LOGO, (" TypeID = 0x%x,\n Length = 0x%x,\n PAL Version = 0x01,\n PAL vendor = 0x01,\n PAL Sub-version specifier = 0x01\n",
604                 pAssoStrc->TypeID,
605                 pAssoStrc->Length));
606         return pAssoStrc->Length + 3;
607 }
608
609 static u8 bthci_CheckRfStateBeforeConnect(struct rtw_adapter *padapter)
610 {
611         struct bt_30info *pBTInfo;
612         enum rt_rf_power_state          RfState;
613
614         pBTInfo = GET_BT_INFO(padapter);
615
616         RfState = padapter->pwrctrlpriv.rf_pwrstate;
617
618         if (RfState != rf_on) {
619                 mod_timer(&pBTInfo->BTPsDisableTimer,
620                           jiffies + msecs_to_jiffies(50));
621                 return false;
622         }
623         return true;
624 }
625
626 static void bthci_ResponderStartToScan(struct rtw_adapter *padapter)
627 {
628 }
629
630 static u8 bthci_PhyLinkConnectionInProgress(struct rtw_adapter *padapter, u8 PhyLinkHandle)
631 {
632         struct bt_30info *pBTInfo;
633         struct bt_mgnt *pBtMgnt;
634
635         pBTInfo = GET_BT_INFO(padapter);
636         pBtMgnt = &pBTInfo->BtMgnt;
637
638         if (pBtMgnt->bPhyLinkInProgress &&
639                 (pBtMgnt->BtCurrentPhyLinkhandle == PhyLinkHandle))
640                 return true;
641         return false;
642 }
643
644 static void bthci_ResetFlowSpec(struct rtw_adapter *padapter, u8 EntryNum, u8 index)
645 {
646         struct bt_30info *pBTinfo;
647
648         pBTinfo = GET_BT_INFO(padapter);
649
650         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].BtLogLinkhandle = 0;
651         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].BtPhyLinkhandle = 0;
652         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].bLLCompleteEventIsSet = false;
653         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].bLLCancelCMDIsSetandComplete = false;
654         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].BtTxFlowSpecID = 0;
655         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].TxPacketCount = 0;
656
657         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].Tx_Flow_Spec.Identifier = 0x01;
658         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].Tx_Flow_Spec.ServiceType = SERVICE_BEST_EFFORT;
659         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].Tx_Flow_Spec.MaximumSDUSize = 0xffff;
660         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].Tx_Flow_Spec.SDUInterArrivalTime = 0xffffffff;
661         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].Tx_Flow_Spec.AccessLatency = 0xffffffff;
662         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].Tx_Flow_Spec.FlushTimeout = 0xffffffff;
663
664         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].Rx_Flow_Spec.Identifier = 0x01;
665         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].Rx_Flow_Spec.ServiceType = SERVICE_BEST_EFFORT;
666         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].Rx_Flow_Spec.MaximumSDUSize = 0xffff;
667         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].Rx_Flow_Spec.SDUInterArrivalTime = 0xffffffff;
668         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].Rx_Flow_Spec.AccessLatency = 0xffffffff;
669         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[index].Rx_Flow_Spec.FlushTimeout = 0xffffffff;
670 }
671
672 static void bthci_ResetEntry(struct rtw_adapter *padapter, u8 EntryNum)
673 {
674         struct bt_30info *pBTinfo;
675         struct bt_mgnt *pBtMgnt;
676         u8 j;
677
678         pBTinfo = GET_BT_INFO(padapter);
679         pBtMgnt = &pBTinfo->BtMgnt;
680
681         pBTinfo->BtAsocEntry[EntryNum].bUsed = false;
682         pBTinfo->BtAsocEntry[EntryNum].BtCurrentState = HCI_STATE_DISCONNECTED;
683         pBTinfo->BtAsocEntry[EntryNum].BtNextState = HCI_STATE_DISCONNECTED;
684
685         pBTinfo->BtAsocEntry[EntryNum].AmpAsocCmdData.AMPAssocRemLen = 0;
686         pBTinfo->BtAsocEntry[EntryNum].AmpAsocCmdData.BtPhyLinkhandle = 0;
687         if (pBTinfo->BtAsocEntry[EntryNum].AmpAsocCmdData.AMPAssocfragment != NULL)
688                 memset(pBTinfo->BtAsocEntry[EntryNum].AmpAsocCmdData.AMPAssocfragment, 0, TOTAL_ALLOCIATE_ASSOC_LEN);
689         pBTinfo->BtAsocEntry[EntryNum].AmpAsocCmdData.LenSoFar = 0;
690
691         pBTinfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtAMPKeyType = 0;
692         pBTinfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle = 0;
693         memset(pBTinfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtAMPKey, 0,
694                pBTinfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtAMPKeyLen);
695         pBTinfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtAMPKeyLen = 0;
696
697         /* 0x640; 0.625ms*1600 = 1000ms, 0.625ms*16000 = 10000ms */
698         pBTinfo->BtAsocEntry[EntryNum].PhyLinkCmdData.LinkSuperversionTimeout = 0x3e80;
699
700         pBTinfo->BtAsocEntry[EntryNum].AMPRole = AMP_BTAP_NONE;
701
702         pBTinfo->BtAsocEntry[EntryNum].mAssoc = false;
703         pBTinfo->BtAsocEntry[EntryNum].b4waySuccess = false;
704
705         /*  Reset BT WPA */
706         pBTinfo->BtAsocEntry[EntryNum].KeyReplayCounter = 0;
707         pBTinfo->BtAsocEntry[EntryNum].BTWPAAuthState = STATE_WPA_AUTH_UNINITIALIZED;
708
709         pBTinfo->BtAsocEntry[EntryNum].bSendSupervisionPacket = false;
710         pBTinfo->BtAsocEntry[EntryNum].NoRxPktCnt = 0;
711         pBTinfo->BtAsocEntry[EntryNum].ShortRangeMode = 0;
712         pBTinfo->BtAsocEntry[EntryNum].rxSuvpPktCnt = 0;
713
714         for (j = 0; j < MAX_LOGICAL_LINK_NUM; j++)
715                 bthci_ResetFlowSpec(padapter, EntryNum, j);
716
717         pBtMgnt->BTAuthCount = 0;
718         pBtMgnt->BTAsocCount = 0;
719         pBtMgnt->BTCurrentConnectType = BT_DISCONNECT;
720         pBtMgnt->BTReceiveConnectPkt = BT_DISCONNECT;
721
722         HALBT_RemoveKey(padapter, EntryNum);
723 }
724
725 static void bthci_RemoveEntryByEntryNum(struct rtw_adapter *padapter, u8 EntryNum)
726 {
727         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
728         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
729
730         bthci_ResetEntry(padapter, EntryNum);
731
732         if (pBtMgnt->CurrentBTConnectionCnt > 0)
733                 pBtMgnt->CurrentBTConnectionCnt--;
734
735         RTPRINT(FIOCTL, IOCTL_STATE, ("[BT Flag], CurrentBTConnectionCnt = %d!!\n",
736                 pBtMgnt->CurrentBTConnectionCnt));
737
738         if (pBtMgnt->CurrentBTConnectionCnt > 0) {
739                 pBtMgnt->BtOperationOn = true;
740         } else {
741                 pBtMgnt->BtOperationOn = false;
742                 RTPRINT(FIOCTL, IOCTL_STATE, ("[BT Flag], Bt Operation OFF!!\n"));
743         }
744
745         if (!pBtMgnt->BtOperationOn) {
746                 del_timer_sync(&pBTInfo->BTHCIDiscardAclDataTimer);
747                 del_timer_sync(&pBTInfo->BTBeaconTimer);
748                 pBtMgnt->bStartSendSupervisionPkt = false;
749         }
750 }
751
752 static u8
753 bthci_CommandCompleteHeader(
754         u8 *pbuf,
755         u16             OGF,
756         u16             OCF,
757         enum hci_status status
758         )
759 {
760         struct packet_irp_hcievent_data *PPacketIrpEvent = (struct packet_irp_hcievent_data *)pbuf;
761         u8 NumHCI_Comm = 0x1;
762
763         PPacketIrpEvent->EventCode = HCI_EVENT_COMMAND_COMPLETE;
764         PPacketIrpEvent->Data[0] = NumHCI_Comm; /* packet # */
765         PPacketIrpEvent->Data[1] = HCIOPCODELOW(OCF, OGF);
766         PPacketIrpEvent->Data[2] = HCIOPCODEHIGHT(OCF, OGF);
767
768         if (OGF == OGF_EXTENSION) {
769                 if (OCF == HCI_SET_RSSI_VALUE) {
770                         RTPRINT(FIOCTL, (IOCTL_BT_EVENT_PERIODICAL),
771                                 ("[BT event], CommandComplete, Num_HCI_Comm = 0x%x, Opcode = 0x%02x%02x, status = 0x%x, OGF = 0x%x, OCF = 0x%x\n",
772                                 NumHCI_Comm, (HCIOPCODEHIGHT(OCF, OGF)), (HCIOPCODELOW(OCF, OGF)), status, OGF, OCF));
773                 } else {
774                         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD_EXT),
775                                 ("[BT event], CommandComplete, Num_HCI_Comm = 0x%x, Opcode = 0x%02x%02x, status = 0x%x, OGF = 0x%x, OCF = 0x%x\n",
776                                 NumHCI_Comm, (HCIOPCODEHIGHT(OCF, OGF)), (HCIOPCODELOW(OCF, OGF)), status, OGF, OCF));
777                 }
778         } else {
779                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO),
780                         ("[BT event], CommandComplete, Num_HCI_Comm = 0x%x, Opcode = 0x%02x%02x, status = 0x%x, OGF = 0x%x, OCF = 0x%x\n",
781                         NumHCI_Comm, (HCIOPCODEHIGHT(OCF, OGF)), (HCIOPCODELOW(OCF, OGF)), status, OGF, OCF));
782         }
783         return 3;
784 }
785
786 static u8 bthci_ExtensionEventHeaderRtk(u8 *pbuf, u8 extensionEvent)
787 {
788         struct packet_irp_hcievent_data *PPacketIrpEvent = (struct packet_irp_hcievent_data *)pbuf;
789         PPacketIrpEvent->EventCode = HCI_EVENT_EXTENSION_RTK;
790         PPacketIrpEvent->Data[0] = extensionEvent;      /* extension event code */
791
792         return 1;
793 }
794
795 static enum rt_status
796 bthci_IndicateEvent(
797         struct rtw_adapter *padapter,
798         void            *pEvntData,
799         u32             dataLen
800         )
801 {
802         enum rt_status  rt_status;
803
804         rt_status = PlatformIndicateBTEvent(padapter, pEvntData, dataLen);
805
806         return rt_status;
807 }
808
809 static void
810 bthci_EventWriteRemoteAmpAssoc(
811         struct rtw_adapter *padapter,
812         enum hci_status status,
813         u8 PLHandle
814         )
815 {
816         u8 localBuf[TmpLocalBufSize] = "";
817         u8 *pRetPar;
818         u8 len = 0;
819         struct packet_irp_hcievent_data *PPacketIrpEvent;
820
821         PlatformZeroMemory(&localBuf[0], TmpLocalBufSize);
822         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
823
824         len += bthci_CommandCompleteHeader(&localBuf[0],
825                 OGF_STATUS_PARAMETERS,
826                 HCI_WRITE_REMOTE_AMP_ASSOC,
827                 status);
828         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("PhyLinkHandle = 0x%x, status = %d\n", PLHandle, status));
829         /*  Return parameters starts from here */
830         pRetPar = &PPacketIrpEvent->Data[len];
831         pRetPar[0] = status;            /* status */
832         pRetPar[1] = PLHandle;
833         len += 2;
834         PPacketIrpEvent->Length = len;
835
836         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
837 }
838
839 static void
840 bthci_EventEnhancedFlushComplete(
841         struct rtw_adapter *padapter,
842         u16                                     LLH
843         )
844 {
845         u8 localBuf[4] = "";
846         struct packet_irp_hcievent_data *PPacketIrpEvent;
847
848         RTPRINT(FIOCTL, IOCTL_BT_EVENT, ("EventEnhancedFlushComplete, LLH = 0x%x\n", LLH));
849
850         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
851         PPacketIrpEvent->EventCode = HCI_EVENT_ENHANCED_FLUSH_COMPLETE;
852         PPacketIrpEvent->Length = 2;
853         /* Logical link handle */
854         PPacketIrpEvent->Data[0] = TWOBYTE_LOWBYTE(LLH);
855         PPacketIrpEvent->Data[1] = TWOBYTE_HIGHTBYTE(LLH);
856
857         bthci_IndicateEvent(padapter, PPacketIrpEvent, 4);
858 }
859
860 static void
861 bthci_EventShortRangeModeChangeComplete(
862         struct rtw_adapter *padapter,
863         enum hci_status                         HciStatus,
864         u8              ShortRangeState,
865         u8              EntryNum
866         )
867 {
868         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
869         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
870         u8 localBuf[5] = "";
871         struct packet_irp_hcievent_data *PPacketIrpEvent;
872
873         if (!(pBtHciInfo->BTEventMaskPage2 & EMP2_HCI_EVENT_SHORT_RANGE_MODE_CHANGE_COMPLETE)) {
874                 RTPRINT(FIOCTL, IOCTL_BT_EVENT,
875                         ("[BT event], Short Range Mode Change Complete, Ignore to send this event due to event mask page 2\n"));
876                 return;
877         }
878         RTPRINT(FIOCTL, IOCTL_BT_EVENT, ("[BT event], Short Range Mode Change Complete, Status = %d\n , PLH = 0x%x\n, Short_Range_Mode_State = 0x%x\n",
879                 HciStatus, pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle, ShortRangeState));
880
881         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
882         PPacketIrpEvent->EventCode = HCI_EVENT_SHORT_RANGE_MODE_CHANGE_COMPLETE;
883         PPacketIrpEvent->Length = 3;
884         PPacketIrpEvent->Data[0] = HciStatus;
885         PPacketIrpEvent->Data[1] = pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle;
886         PPacketIrpEvent->Data[2] = ShortRangeState;
887         bthci_IndicateEvent(padapter, PPacketIrpEvent, 5);
888 }
889
890 static void bthci_EventSendFlowSpecModifyComplete(struct rtw_adapter *padapter,
891                                                   enum hci_status HciStatus,
892                                                   u16 logicHandle)
893 {
894         u8 localBuf[5] = "";
895         struct packet_irp_hcievent_data *PPacketIrpEvent;
896         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
897         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
898
899         if (!(pBtHciInfo->BTEventMaskPage2 & EMP2_HCI_EVENT_FLOW_SPEC_MODIFY_COMPLETE)) {
900                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO),
901                         ("[BT event], Flow Spec Modify Complete, Ignore to send this event due to event mask page 2\n"));
902                 return;
903         }
904         RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO),
905                 ("[BT event], Flow Spec Modify Complete, status = 0x%x, LLH = 0x%x\n", HciStatus, logicHandle));
906         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
907         PPacketIrpEvent->EventCode = HCI_EVENT_FLOW_SPEC_MODIFY_COMPLETE;
908         PPacketIrpEvent->Length = 3;
909
910         PPacketIrpEvent->Data[0] = HciStatus;
911         /* Logical link handle */
912         PPacketIrpEvent->Data[1] = TWOBYTE_LOWBYTE(logicHandle);
913         PPacketIrpEvent->Data[2] = TWOBYTE_HIGHTBYTE(logicHandle);
914
915         bthci_IndicateEvent(padapter, PPacketIrpEvent, 5);
916 }
917
918 static void
919 bthci_EventExtWifiScanNotify(
920         struct rtw_adapter *padapter,
921         u8                      scanType
922         )
923 {
924         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
925         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
926         u8 len = 0;
927         u8 localBuf[7] = "";
928         u8 *pRetPar;
929         u8 *pu1Temp;
930         struct packet_irp_hcievent_data *PPacketIrpEvent;
931
932         if (!pBtMgnt->BtOperationOn)
933                 return;
934
935         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
936
937         len += bthci_ExtensionEventHeaderRtk(&localBuf[0], HCI_EVENT_EXT_WIFI_SCAN_NOTIFY);
938
939         /*  Return parameters starts from here */
940         pRetPar = &PPacketIrpEvent->Data[len];
941         pu1Temp = (u8 *)&pRetPar[0];
942         *pu1Temp = scanType;
943         len += 1;
944
945         PPacketIrpEvent->Length = len;
946
947         if (bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2) == RT_STATUS_SUCCESS) {
948                 RTPRINT(FIOCTL, IOCTL_BT_EVENT, ("[BT event], Wifi scan notify, scan type = %d\n",
949                         scanType));
950         }
951 }
952
953 static void
954 bthci_EventAMPReceiverReport(
955         struct rtw_adapter *padapter,
956         u8 Reason
957         )
958 {
959         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
960         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
961
962         if (pBtHciInfo->bTestNeedReport) {
963                 u8 localBuf[20] = "";
964                 u32     *pu4Temp;
965                 u16     *pu2Temp;
966                 struct packet_irp_hcievent_data *PPacketIrpEvent;
967
968                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), (" HCI_EVENT_AMP_RECEIVER_REPORT\n"));
969                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
970                 PPacketIrpEvent->EventCode = HCI_EVENT_AMP_RECEIVER_REPORT;
971                 PPacketIrpEvent->Length = 2;
972
973                 PPacketIrpEvent->Data[0] = pBtHciInfo->TestCtrType;
974
975                 PPacketIrpEvent->Data[1] = Reason;
976
977                 pu4Temp = (u32 *)&PPacketIrpEvent->Data[2];
978                 *pu4Temp = pBtHciInfo->TestEventType;
979
980                 pu2Temp = (u16 *)&PPacketIrpEvent->Data[6];
981                 *pu2Temp = pBtHciInfo->TestNumOfFrame;
982
983                 pu2Temp = (u16 *)&PPacketIrpEvent->Data[8];
984                 *pu2Temp = pBtHciInfo->TestNumOfErrFrame;
985
986                 pu4Temp = (u32 *)&PPacketIrpEvent->Data[10];
987                 *pu4Temp = pBtHciInfo->TestNumOfBits;
988
989                 pu4Temp = (u32 *)&PPacketIrpEvent->Data[14];
990                 *pu4Temp = pBtHciInfo->TestNumOfErrBits;
991
992                 bthci_IndicateEvent(padapter, PPacketIrpEvent, 20);
993
994                 /* Return to Idel state with RX and TX off. */
995
996         }
997
998         pBtHciInfo->TestNumOfFrame = 0x00;
999 }
1000
1001 static void
1002 bthci_EventChannelSelected(
1003         struct rtw_adapter *padapter,
1004         u8      EntryNum
1005         )
1006 {
1007         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
1008         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
1009         u8 localBuf[3] = "";
1010         struct packet_irp_hcievent_data *PPacketIrpEvent;
1011
1012         if (!(pBtHciInfo->BTEventMaskPage2 & EMP2_HCI_EVENT_CHANNEL_SELECT)) {
1013                 RTPRINT(FIOCTL, IOCTL_BT_EVENT,
1014                         ("[BT event], Channel Selected, Ignore to send this event due to event mask page 2\n"));
1015                 return;
1016         }
1017
1018         RTPRINT(FIOCTL, IOCTL_BT_EVENT|IOCTL_STATE,
1019                 ("[BT event], Channel Selected, PhyLinkHandle %d\n",
1020                 pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle));
1021
1022         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
1023         PPacketIrpEvent->EventCode = HCI_EVENT_CHANNEL_SELECT;
1024         PPacketIrpEvent->Length = 1;
1025         PPacketIrpEvent->Data[0] = pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle;
1026         bthci_IndicateEvent(padapter, PPacketIrpEvent, 3);
1027 }
1028
1029 static void
1030 bthci_EventDisconnectPhyLinkComplete(
1031         struct rtw_adapter *padapter,
1032         enum hci_status                         HciStatus,
1033         enum hci_status                         Reason,
1034         u8              EntryNum
1035         )
1036 {
1037         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
1038         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
1039         u8 localBuf[5] = "";
1040         struct packet_irp_hcievent_data *PPacketIrpEvent;
1041
1042         if (!(pBtHciInfo->BTEventMaskPage2 & EMP2_HCI_EVENT_DISCONNECT_PHY_LINK_COMPLETE)) {
1043                 RTPRINT(FIOCTL, IOCTL_BT_EVENT,
1044                         ("[BT event], Disconnect Physical Link Complete, Ignore to send this event due to event mask page 2\n"));
1045                 return;
1046         }
1047         RTPRINT(FIOCTL, IOCTL_BT_EVENT,
1048                 ("[BT event], Disconnect Physical Link Complete, Status = 0x%x, PLH = 0x%x Reason = 0x%x\n",
1049                 HciStatus, pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle, Reason));
1050         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
1051         PPacketIrpEvent->EventCode = HCI_EVENT_DISCONNECT_PHY_LINK_COMPLETE;
1052         PPacketIrpEvent->Length = 3;
1053         PPacketIrpEvent->Data[0] = HciStatus;
1054         PPacketIrpEvent->Data[1] = pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle;
1055         PPacketIrpEvent->Data[2] = Reason;
1056         bthci_IndicateEvent(padapter, PPacketIrpEvent, 5);
1057 }
1058
1059 static void
1060 bthci_EventPhysicalLinkComplete(
1061         struct rtw_adapter *padapter,
1062         enum hci_status                         HciStatus,
1063         u8              EntryNum,
1064         u8              PLHandle
1065         )
1066 {
1067         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
1068         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
1069         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
1070         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
1071         u8 localBuf[4] = "";
1072         struct packet_irp_hcievent_data *PPacketIrpEvent;
1073         u8 PL_handle;
1074
1075         pBtMgnt->bPhyLinkInProgress = false;
1076         pBtDbg->dbgHciInfo.hciCmdPhyLinkStatus = HciStatus;
1077         if (!(pBtHciInfo->BTEventMaskPage2 & EMP2_HCI_EVENT_PHY_LINK_COMPLETE)) {
1078                 RTPRINT(FIOCTL, IOCTL_BT_EVENT,
1079                         ("[BT event], Physical Link Complete, Ignore to send this event due to event mask page 2\n"));
1080                 return;
1081         }
1082
1083         if (EntryNum == 0xff) {
1084                 /*  connection not started yet, just use the input physical link handle to response. */
1085                 PL_handle = PLHandle;
1086         } else {
1087                 /*  connection is under progress, use the phy link handle we recorded. */
1088                 PL_handle  = pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle;
1089                 pBTInfo->BtAsocEntry[EntryNum].bNeedPhysLinkCompleteEvent = false;
1090         }
1091
1092         RTPRINT(FIOCTL, IOCTL_BT_EVENT, ("[BT event], Physical Link Complete, Status = 0x%x PhyLinkHandle = 0x%x\n", HciStatus,
1093                 PL_handle));
1094
1095         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
1096         PPacketIrpEvent->EventCode = HCI_EVENT_PHY_LINK_COMPLETE;
1097         PPacketIrpEvent->Length = 2;
1098
1099         PPacketIrpEvent->Data[0] = HciStatus;
1100         PPacketIrpEvent->Data[1] = PL_handle;
1101         bthci_IndicateEvent(padapter, PPacketIrpEvent, 4);
1102
1103 }
1104
1105 static void
1106 bthci_EventCommandStatus(
1107         struct rtw_adapter *padapter,
1108         u8              OGF,
1109         u16                                     OCF,
1110         enum hci_status                         HciStatus
1111         )
1112 {
1113
1114         u8 localBuf[6] = "";
1115         struct packet_irp_hcievent_data *PPacketIrpEvent;
1116         u8 Num_Hci_Comm = 0x1;
1117         RTPRINT(FIOCTL, IOCTL_BT_EVENT,
1118                 ("[BT event], CommandStatus, Opcode = 0x%02x%02x, OGF = 0x%x,  OCF = 0x%x, Status = 0x%x, Num_HCI_COMM = 0x%x\n",
1119                 (HCIOPCODEHIGHT(OCF, OGF)), (HCIOPCODELOW(OCF, OGF)), OGF, OCF, HciStatus, Num_Hci_Comm));
1120
1121         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
1122         PPacketIrpEvent->EventCode = HCI_EVENT_COMMAND_STATUS;
1123         PPacketIrpEvent->Length = 4;
1124         PPacketIrpEvent->Data[0] = HciStatus;   /* current pending */
1125         PPacketIrpEvent->Data[1] = Num_Hci_Comm;        /* packet # */
1126         PPacketIrpEvent->Data[2] = HCIOPCODELOW(OCF, OGF);
1127         PPacketIrpEvent->Data[3] = HCIOPCODEHIGHT(OCF, OGF);
1128
1129         bthci_IndicateEvent(padapter, PPacketIrpEvent, 6);
1130
1131 }
1132
1133 static void
1134 bthci_EventLogicalLinkComplete(
1135         struct rtw_adapter *padapter,
1136         enum hci_status                         HciStatus,
1137         u8              PhyLinkHandle,
1138         u16                                     LogLinkHandle,
1139         u8              LogLinkIndex,
1140         u8              EntryNum
1141         )
1142 {
1143 /*PMGNT_INFO pMgntInfo = &padapter->MgntInfo; */
1144         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
1145         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
1146         u8 localBuf[7] = "";
1147         struct packet_irp_hcievent_data *PPacketIrpEvent;
1148
1149         if (!(pBtHciInfo->BTEventMaskPage2 & EMP2_HCI_EVENT_LOGICAL_LINK_COMPLETE)) {
1150                 RTPRINT(FIOCTL, IOCTL_BT_EVENT,
1151                         ("[BT event], Logical Link Complete, Ignore to send this event due to event mask page 2\n"));
1152                 return;
1153         }
1154         RTPRINT(FIOCTL, IOCTL_BT_EVENT, ("[BT event], Logical Link Complete, PhyLinkHandle = 0x%x,  LogLinkHandle = 0x%x, Status = 0x%x\n",
1155                 PhyLinkHandle, LogLinkHandle, HciStatus));
1156
1157         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
1158         PPacketIrpEvent->EventCode = HCI_EVENT_LOGICAL_LINK_COMPLETE;
1159         PPacketIrpEvent->Length = 5;
1160
1161         PPacketIrpEvent->Data[0] = HciStatus;/* status code */
1162         /* Logical link handle */
1163         PPacketIrpEvent->Data[1] = TWOBYTE_LOWBYTE(LogLinkHandle);
1164         PPacketIrpEvent->Data[2] = TWOBYTE_HIGHTBYTE(LogLinkHandle);
1165         /* Physical link handle */
1166         PPacketIrpEvent->Data[3] = TWOBYTE_LOWBYTE(PhyLinkHandle);
1167         /* corresponding Tx flow spec ID */
1168         if (HciStatus == HCI_STATUS_SUCCESS) {
1169                 PPacketIrpEvent->Data[4] =
1170                         pBTInfo->BtAsocEntry[EntryNum].LogLinkCmdData[LogLinkIndex].Tx_Flow_Spec.Identifier;
1171         } else {
1172                 PPacketIrpEvent->Data[4] = 0x0;
1173         }
1174
1175         bthci_IndicateEvent(padapter, PPacketIrpEvent, 7);
1176 }
1177
1178 static void
1179 bthci_EventDisconnectLogicalLinkComplete(
1180         struct rtw_adapter *padapter,
1181         enum hci_status                         HciStatus,
1182         u16                                     LogLinkHandle,
1183         enum hci_status                         Reason
1184         )
1185 {
1186         u8 localBuf[6] = "";
1187         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
1188         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
1189         struct packet_irp_hcievent_data *PPacketIrpEvent;
1190
1191         if (!(pBtHciInfo->BTEventMaskPage2 & EMP2_HCI_EVENT_DISCONNECT_LOGICAL_LINK_COMPLETE)) {
1192                 RTPRINT(FIOCTL, IOCTL_BT_EVENT, ("[BT event], Disconnect Logical Link Complete, Ignore to send this event due to event mask page 2\n"));
1193                 return;
1194         }
1195         RTPRINT(FIOCTL, IOCTL_BT_EVENT, ("[BT event], Disconnect Logical Link Complete, Status = 0x%x, LLH = 0x%x Reason = 0x%x\n", HciStatus, LogLinkHandle, Reason));
1196
1197         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
1198         PPacketIrpEvent->EventCode = HCI_EVENT_DISCONNECT_LOGICAL_LINK_COMPLETE;
1199         PPacketIrpEvent->Length = 4;
1200
1201         PPacketIrpEvent->Data[0] = HciStatus;
1202         /* Logical link handle */
1203         PPacketIrpEvent->Data[1] = TWOBYTE_LOWBYTE(LogLinkHandle);
1204         PPacketIrpEvent->Data[2] = TWOBYTE_HIGHTBYTE(LogLinkHandle);
1205         /* Disconnect reason */
1206         PPacketIrpEvent->Data[3] = Reason;
1207
1208         bthci_IndicateEvent(padapter, PPacketIrpEvent, 6);
1209 }
1210
1211 static void
1212 bthci_EventFlushOccurred(
1213         struct rtw_adapter *padapter,
1214         u16                                     LogLinkHandle
1215         )
1216 {
1217         u8 localBuf[4] = "";
1218         struct packet_irp_hcievent_data *PPacketIrpEvent;
1219         RTPRINT(FIOCTL, IOCTL_BT_EVENT, ("bthci_EventFlushOccurred(), LLH = 0x%x\n", LogLinkHandle));
1220
1221         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
1222         PPacketIrpEvent->EventCode = HCI_EVENT_FLUSH_OCCRUED;
1223         PPacketIrpEvent->Length = 2;
1224         /* Logical link handle */
1225         PPacketIrpEvent->Data[0] = TWOBYTE_LOWBYTE(LogLinkHandle);
1226         PPacketIrpEvent->Data[1] = TWOBYTE_HIGHTBYTE(LogLinkHandle);
1227
1228         bthci_IndicateEvent(padapter, PPacketIrpEvent, 4);
1229 }
1230
1231 static enum hci_status
1232 bthci_BuildPhysicalLink(
1233         struct rtw_adapter *padapter,
1234         struct packet_irp_hcicmd_data *pHciCmd,
1235         u16     OCF
1236 )
1237 {
1238         enum hci_status         status = HCI_STATUS_SUCCESS;
1239         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
1240         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
1241         u8 EntryNum, PLH;
1242
1243         /* Send HCI Command status event to AMP. */
1244         bthci_EventCommandStatus(padapter,
1245                         LINK_CONTROL_COMMANDS,
1246                         OCF,
1247                         HCI_STATUS_SUCCESS);
1248
1249         PLH = *((u8 *)pHciCmd->Data);
1250
1251         /*  Check if resource or bt connection is under progress, if yes, reject the link creation. */
1252         if (!bthci_AddEntry(padapter)) {
1253                 status = HCI_STATUS_CONNECT_RJT_LIMIT_RESOURCE;
1254                 bthci_EventPhysicalLinkComplete(padapter, status, INVALID_ENTRY_NUM, PLH);
1255                 return status;
1256         }
1257
1258         EntryNum = pBtMgnt->CurrentConnectEntryNum;
1259         pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle = PLH;
1260         pBtMgnt->BtCurrentPhyLinkhandle = PLH;
1261
1262         if (pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.AMPAssocfragment == NULL) {
1263                 RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Create/Accept PhysicalLink, AMP controller is busy\n"));
1264                 status = HCI_STATUS_CONTROLLER_BUSY;
1265                 bthci_EventPhysicalLinkComplete(padapter, status, INVALID_ENTRY_NUM, PLH);
1266                 return status;
1267         }
1268
1269         /*  Record Key and the info */
1270         pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtAMPKeyLen = (*((u8 *)pHciCmd->Data+1));
1271         pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtAMPKeyType = (*((u8 *)pHciCmd->Data+2));
1272         memcpy(pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtAMPKey,
1273                 (((u8 *)pHciCmd->Data+3)), pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtAMPKeyLen);
1274         memcpy(pBTInfo->BtAsocEntry[EntryNum].PMK, pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtAMPKey, PMK_LEN);
1275         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("BuildPhysicalLink, EntryNum = %d, PLH = 0x%x  KeyLen = 0x%x, KeyType = 0x%x\n",
1276                 EntryNum, pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle,
1277                 pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtAMPKeyLen,
1278                 pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtAMPKeyType));
1279         RTPRINT_DATA(FIOCTL, (IOCTL_BT_LOGO|IOCTL_BT_HCICMD), ("BtAMPKey\n"), pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtAMPKey,
1280                 pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtAMPKeyLen);
1281         RTPRINT_DATA(FIOCTL, (IOCTL_BT_LOGO|IOCTL_BT_HCICMD), ("PMK\n"), pBTInfo->BtAsocEntry[EntryNum].PMK,
1282                 PMK_LEN);
1283
1284         if (OCF == HCI_CREATE_PHYSICAL_LINK) {
1285                 /* These macros require braces */
1286                 BTHCI_SM_WITH_INFO(padapter, HCI_STATE_DISCONNECTED, STATE_CMD_CREATE_PHY_LINK, EntryNum);
1287         } else if (OCF == HCI_ACCEPT_PHYSICAL_LINK) {
1288                 BTHCI_SM_WITH_INFO(padapter, HCI_STATE_DISCONNECTED, STATE_CMD_ACCEPT_PHY_LINK, EntryNum);
1289         }
1290
1291         return status;
1292 }
1293
1294 static void
1295 bthci_BuildLogicalLink(
1296         struct rtw_adapter *padapter,
1297         struct packet_irp_hcicmd_data *pHciCmd,
1298         u16 OCF
1299         )
1300 {
1301         enum hci_status status = HCI_STATUS_SUCCESS;
1302         struct bt_30info *pBTinfo = GET_BT_INFO(padapter);
1303         struct bt_mgnt *pBtMgnt = &pBTinfo->BtMgnt;
1304         u8 PhyLinkHandle, EntryNum;
1305         static u16 AssignLogHandle = 1;
1306
1307         struct hci_flow_spec    TxFlowSpec;
1308         struct hci_flow_spec    RxFlowSpec;
1309         u32     MaxSDUSize, ArriveTime, Bandwidth;
1310
1311         PhyLinkHandle = *((u8 *)pHciCmd->Data);
1312
1313         EntryNum = bthci_GetCurrentEntryNum(padapter, PhyLinkHandle);
1314
1315         memcpy(&TxFlowSpec,
1316                 &pHciCmd->Data[1], sizeof(struct hci_flow_spec));
1317         memcpy(&RxFlowSpec,
1318                 &pHciCmd->Data[17], sizeof(struct hci_flow_spec));
1319
1320         MaxSDUSize = TxFlowSpec.MaximumSDUSize;
1321         ArriveTime = TxFlowSpec.SDUInterArrivalTime;
1322
1323         if (bthci_CheckLogLinkBehavior(padapter, TxFlowSpec) && bthci_CheckLogLinkBehavior(padapter, RxFlowSpec))
1324                 Bandwidth = BTTOTALBANDWIDTH;
1325         else if (MaxSDUSize == 0xffff && ArriveTime == 0xffffffff)
1326                 Bandwidth = BTTOTALBANDWIDTH;
1327         else
1328                 Bandwidth = MaxSDUSize*8*1000/(ArriveTime+244);
1329
1330         RTPRINT(FIOCTL, IOCTL_BT_HCICMD,
1331                 ("BuildLogicalLink, PhyLinkHandle = 0x%x, MaximumSDUSize = 0x%x, SDUInterArrivalTime = 0x%x, Bandwidth = 0x%x\n",
1332                 PhyLinkHandle, MaxSDUSize, ArriveTime, Bandwidth));
1333
1334         if (EntryNum == 0xff) {
1335                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("Invalid Physical Link handle = 0x%x, status = HCI_STATUS_UNKNOW_CONNECT_ID, return\n", PhyLinkHandle));
1336                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
1337
1338                 /* When we receive Create/Accept logical link command, we should send command status event first. */
1339                 bthci_EventCommandStatus(padapter,
1340                         LINK_CONTROL_COMMANDS,
1341                         OCF,
1342                         status);
1343                 return;
1344         }
1345
1346         if (!pBtMgnt->bLogLinkInProgress) {
1347                 if (bthci_PhyLinkConnectionInProgress(padapter, PhyLinkHandle)) {
1348                         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("Physical link connection in progress, status = HCI_STATUS_CMD_DISALLOW, return\n"));
1349                         status = HCI_STATUS_CMD_DISALLOW;
1350
1351                         pBtMgnt->bPhyLinkInProgressStartLL = true;
1352                         /* When we receive Create/Accept logical link command, we should send command status event first. */
1353                         bthci_EventCommandStatus(padapter,
1354                                 LINK_CONTROL_COMMANDS,
1355                                 OCF,
1356                                 status);
1357
1358                         return;
1359                 }
1360
1361                 if (Bandwidth > BTTOTALBANDWIDTH) {
1362                         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("status = HCI_STATUS_QOS_REJECT, Bandwidth = 0x%x, return\n", Bandwidth));
1363                         status = HCI_STATUS_QOS_REJECT;
1364
1365                         /* When we receive Create/Accept logical link command, we should send command status event first. */
1366                         bthci_EventCommandStatus(padapter,
1367                                 LINK_CONTROL_COMMANDS,
1368                                 OCF,
1369                                 status);
1370                 } else {
1371                         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("status = HCI_STATUS_SUCCESS\n"));
1372                         status = HCI_STATUS_SUCCESS;
1373
1374                         /* When we receive Create/Accept logical link command, we should send command status event first. */
1375                         bthci_EventCommandStatus(padapter,
1376                                 LINK_CONTROL_COMMANDS,
1377                                 OCF,
1378                                 status);
1379
1380                 }
1381
1382                 if (pBTinfo->BtAsocEntry[EntryNum].BtCurrentState != HCI_STATE_CONNECTED) {
1383                         bthci_EventLogicalLinkComplete(padapter,
1384                                 HCI_STATUS_CMD_DISALLOW, 0, 0, 0, EntryNum);
1385                 } else {
1386                         u8 i, find = 0;
1387
1388                         pBtMgnt->bLogLinkInProgress = true;
1389
1390                         /*  find an unused logical link index and copy the data */
1391                         for (i = 0; i < MAX_LOGICAL_LINK_NUM; i++) {
1392                                 if (pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[i].BtLogLinkhandle == 0) {
1393                                         enum hci_status LogCompEventstatus = HCI_STATUS_SUCCESS;
1394
1395                                         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[i].BtPhyLinkhandle = *((u8 *)pHciCmd->Data);
1396                                         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[i].BtLogLinkhandle = AssignLogHandle;
1397                                         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("BuildLogicalLink, EntryNum = %d, physical link handle = 0x%x, logical link handle = 0x%x\n",
1398                                                 EntryNum, pBTinfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle,
1399                                                                   pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[i].BtLogLinkhandle));
1400                                         memcpy(&pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[i].Tx_Flow_Spec,
1401                                                 &TxFlowSpec, sizeof(struct hci_flow_spec));
1402                                         memcpy(&pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[i].Rx_Flow_Spec,
1403                                                 &RxFlowSpec, sizeof(struct hci_flow_spec));
1404
1405                                         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[i].bLLCompleteEventIsSet = false;
1406
1407                                         if (pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[i].bLLCancelCMDIsSetandComplete)
1408                                                 LogCompEventstatus = HCI_STATUS_UNKNOW_CONNECT_ID;
1409                                         bthci_EventLogicalLinkComplete(padapter,
1410                                                 LogCompEventstatus,
1411                                                 pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[i].BtPhyLinkhandle,
1412                                                 pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[i].BtLogLinkhandle, i, EntryNum);
1413
1414                                         pBTinfo->BtAsocEntry[EntryNum].LogLinkCmdData[i].bLLCompleteEventIsSet = true;
1415
1416                                         find = 1;
1417                                         pBtMgnt->BtCurrentLogLinkhandle = AssignLogHandle;
1418                                         AssignLogHandle++;
1419                                         break;
1420                                 }
1421                         }
1422
1423                         if (!find) {
1424                                 bthci_EventLogicalLinkComplete(padapter,
1425                                         HCI_STATUS_CONNECT_RJT_LIMIT_RESOURCE, 0, 0, 0, EntryNum);
1426                         }
1427                         pBtMgnt->bLogLinkInProgress = false;
1428                 }
1429         } else {
1430                 bthci_EventLogicalLinkComplete(padapter,
1431                         HCI_STATUS_CONTROLLER_BUSY, 0, 0, 0, EntryNum);
1432         }
1433
1434 }
1435
1436 static void
1437 bthci_StartBeaconAndConnect(
1438         struct rtw_adapter *padapter,
1439         struct packet_irp_hcicmd_data *pHciCmd,
1440         u8 CurrentAssocNum
1441         )
1442 {
1443 /*PMGNT_INFO pMgntInfo = &padapter->MgntInfo; */
1444         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
1445         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
1446
1447         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("StartBeaconAndConnect, CurrentAssocNum =%d, AMPRole =%d\n",
1448                 CurrentAssocNum,
1449                 pBTInfo->BtAsocEntry[CurrentAssocNum].AMPRole));
1450
1451         if (!pBtMgnt->CheckChnlIsSuit) {
1452                 bthci_EventPhysicalLinkComplete(padapter, HCI_STATUS_CONNECT_REJ_NOT_SUIT_CHNL_FOUND, CurrentAssocNum, INVALID_PL_HANDLE);
1453                 bthci_RemoveEntryByEntryNum(padapter, CurrentAssocNum);
1454                 return;
1455         }
1456
1457         if (pBTInfo->BtAsocEntry[CurrentAssocNum].AMPRole == AMP_BTAP_CREATOR) {
1458                 rsprintf((char *)pBTInfo->BtAsocEntry[CurrentAssocNum].BTSsidBuf, 32, "AMP-%02x-%02x-%02x-%02x-%02x-%02x",
1459                 padapter->eeprompriv.mac_addr[0],
1460                 padapter->eeprompriv.mac_addr[1],
1461                 padapter->eeprompriv.mac_addr[2],
1462                 padapter->eeprompriv.mac_addr[3],
1463                 padapter->eeprompriv.mac_addr[4],
1464                 padapter->eeprompriv.mac_addr[5]);
1465         } else if (pBTInfo->BtAsocEntry[CurrentAssocNum].AMPRole == AMP_BTAP_JOINER) {
1466                 rsprintf((char *)pBTInfo->BtAsocEntry[CurrentAssocNum].BTSsidBuf, 32, "AMP-%02x-%02x-%02x-%02x-%02x-%02x",
1467                 pBTInfo->BtAsocEntry[CurrentAssocNum].BTRemoteMACAddr[0],
1468                 pBTInfo->BtAsocEntry[CurrentAssocNum].BTRemoteMACAddr[1],
1469                 pBTInfo->BtAsocEntry[CurrentAssocNum].BTRemoteMACAddr[2],
1470                 pBTInfo->BtAsocEntry[CurrentAssocNum].BTRemoteMACAddr[3],
1471                 pBTInfo->BtAsocEntry[CurrentAssocNum].BTRemoteMACAddr[4],
1472                 pBTInfo->BtAsocEntry[CurrentAssocNum].BTRemoteMACAddr[5]);
1473         }
1474
1475         FillOctetString(pBTInfo->BtAsocEntry[CurrentAssocNum].BTSsid, pBTInfo->BtAsocEntry[CurrentAssocNum].BTSsidBuf, 21);
1476         pBTInfo->BtAsocEntry[CurrentAssocNum].BTSsid.Length = 21;
1477
1478         /* To avoid set the start ap or connect twice, or the original connection will be disconnected. */
1479         if (!pBtMgnt->bBTConnectInProgress) {
1480                 pBtMgnt->bBTConnectInProgress = true;
1481                 RTPRINT(FIOCTL, IOCTL_STATE, ("[BT Flag], BT Connect in progress ON!!\n"));
1482                 BTHCI_SM_WITH_INFO(padapter, HCI_STATE_STARTING, STATE_CMD_MAC_START_COMPLETE, CurrentAssocNum);
1483
1484                 /*  20100325 Joseph: Check RF ON/OFF. */
1485                 /*  If RF OFF, it reschedule connecting operation after 50ms. */
1486                 if (!bthci_CheckRfStateBeforeConnect(padapter))
1487                         return;
1488
1489                 if (pBTInfo->BtAsocEntry[CurrentAssocNum].AMPRole == AMP_BTAP_CREATOR) {
1490                         /* These macros need braces */
1491                         BTHCI_SM_WITH_INFO(padapter, HCI_STATE_CONNECTING, STATE_CMD_MAC_CONNECT_COMPLETE, CurrentAssocNum);
1492                 } else if (pBTInfo->BtAsocEntry[CurrentAssocNum].AMPRole == AMP_BTAP_JOINER) {
1493                         bthci_ResponderStartToScan(padapter);
1494                 }
1495         }
1496         RT_PRINT_STR(_module_rtl871x_mlme_c_, _drv_notice_,
1497                      "StartBeaconAndConnect, SSID:\n",
1498                      pBTInfo->BtAsocEntry[pBtMgnt->CurrentConnectEntryNum].BTSsid.Octet,
1499                      pBTInfo->BtAsocEntry[pBtMgnt->CurrentConnectEntryNum].BTSsid.Length);
1500 }
1501
1502 static void bthci_ResetBtMgnt(struct bt_mgnt *pBtMgnt)
1503 {
1504         pBtMgnt->BtOperationOn = false;
1505         pBtMgnt->bBTConnectInProgress = false;
1506         pBtMgnt->bLogLinkInProgress = false;
1507         pBtMgnt->bPhyLinkInProgress = false;
1508         pBtMgnt->bPhyLinkInProgressStartLL = false;
1509         pBtMgnt->DisconnectEntryNum = 0xff;
1510         pBtMgnt->bStartSendSupervisionPkt = false;
1511         pBtMgnt->JoinerNeedSendAuth = false;
1512         pBtMgnt->CurrentBTConnectionCnt = 0;
1513         pBtMgnt->BTCurrentConnectType = BT_DISCONNECT;
1514         pBtMgnt->BTReceiveConnectPkt = BT_DISCONNECT;
1515         pBtMgnt->BTAuthCount = 0;
1516         pBtMgnt->btLogoTest = 0;
1517 }
1518
1519 static void bthci_ResetBtHciInfo(struct bt_hci_info *pBtHciInfo)
1520 {
1521         pBtHciInfo->BTEventMask = 0;
1522         pBtHciInfo->BTEventMaskPage2 = 0;
1523         pBtHciInfo->ConnAcceptTimeout =  10000;
1524         pBtHciInfo->PageTimeout  =  0x30;
1525         pBtHciInfo->LocationDomainAware = 0x0;
1526         pBtHciInfo->LocationDomain = 0x5858;
1527         pBtHciInfo->LocationDomainOptions = 0x58;
1528         pBtHciInfo->LocationOptions = 0x0;
1529         pBtHciInfo->FlowControlMode = 0x1;      /*  0:Packet based data flow control mode(BR/EDR), 1: Data block based data flow control mode(AMP). */
1530
1531         pBtHciInfo->enFlush_LLH = 0;
1532         pBtHciInfo->FLTO_LLH = 0;
1533
1534         /* Test command only */
1535         pBtHciInfo->bTestIsEnd = true;
1536         pBtHciInfo->bInTestMode = false;
1537         pBtHciInfo->bTestNeedReport = false;
1538         pBtHciInfo->TestScenario = 0xff;
1539         pBtHciInfo->TestReportInterval = 0x01;
1540         pBtHciInfo->TestCtrType = 0x5d;
1541         pBtHciInfo->TestEventType = 0x00;
1542         pBtHciInfo->TestNumOfFrame = 0;
1543         pBtHciInfo->TestNumOfErrFrame = 0;
1544         pBtHciInfo->TestNumOfBits = 0;
1545         pBtHciInfo->TestNumOfErrBits = 0;
1546 }
1547
1548 static void bthci_ResetBtSec(struct rtw_adapter *padapter, struct bt_security *pBtSec)
1549 {
1550 /*PMGNT_INFO    pMgntInfo = &padapter->MgntInfo; */
1551
1552         /*  Set BT used HW or SW encrypt !! */
1553         if (GET_HAL_DATA(padapter)->bBTMode)
1554                 pBtSec->bUsedHwEncrypt = true;
1555         else
1556                 pBtSec->bUsedHwEncrypt = false;
1557         RT_TRACE(_module_rtl871x_security_c_, _drv_info_, ("%s: bUsedHwEncrypt =%d\n", __func__, pBtSec->bUsedHwEncrypt));
1558
1559         pBtSec->RSNIE.Octet = pBtSec->RSNIEBuf;
1560 }
1561
1562 static void bthci_ResetBtExtInfo(struct bt_mgnt *pBtMgnt)
1563 {
1564         u8 i;
1565
1566         for (i = 0; i < MAX_BT_ASOC_ENTRY_NUM; i++) {
1567                 pBtMgnt->ExtConfig.linkInfo[i].ConnectHandle = 0;
1568                 pBtMgnt->ExtConfig.linkInfo[i].IncomingTrafficMode = 0;
1569                 pBtMgnt->ExtConfig.linkInfo[i].OutgoingTrafficMode = 0;
1570                 pBtMgnt->ExtConfig.linkInfo[i].BTProfile = BT_PROFILE_NONE;
1571                 pBtMgnt->ExtConfig.linkInfo[i].BTCoreSpec = BT_SPEC_2_1_EDR;
1572                 pBtMgnt->ExtConfig.linkInfo[i].BT_RSSI = 0;
1573                 pBtMgnt->ExtConfig.linkInfo[i].TrafficProfile = BT_PROFILE_NONE;
1574                 pBtMgnt->ExtConfig.linkInfo[i].linkRole = BT_LINK_MASTER;
1575         }
1576
1577         pBtMgnt->ExtConfig.CurrentConnectHandle = 0;
1578         pBtMgnt->ExtConfig.CurrentIncomingTrafficMode = 0;
1579         pBtMgnt->ExtConfig.CurrentOutgoingTrafficMode = 0;
1580         pBtMgnt->ExtConfig.MIN_BT_RSSI = 0;
1581         pBtMgnt->ExtConfig.NumberOfHandle = 0;
1582         pBtMgnt->ExtConfig.NumberOfSCO = 0;
1583         pBtMgnt->ExtConfig.CurrentBTStatus = 0;
1584         pBtMgnt->ExtConfig.HCIExtensionVer = 0;
1585
1586         pBtMgnt->ExtConfig.bManualControl = false;
1587         pBtMgnt->ExtConfig.bBTBusy = false;
1588         pBtMgnt->ExtConfig.bBTA2DPBusy = false;
1589 }
1590
1591 static enum hci_status bthci_CmdReset(struct rtw_adapter *_padapter, u8 bNeedSendEvent)
1592 {
1593         enum hci_status status = HCI_STATUS_SUCCESS;
1594         struct rtw_adapter *padapter;
1595 /*PMGNT_INFO pMgntInfo = &padapter->MgntInfo; */
1596         struct bt_30info *pBTInfo;
1597         struct bt_mgnt *pBtMgnt;
1598         struct bt_hci_info *pBtHciInfo;
1599         struct bt_security *pBtSec;
1600         struct bt_dgb *pBtDbg;
1601         u8 i;
1602
1603         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("bthci_CmdReset()\n"));
1604
1605         padapter = GetDefaultAdapter(_padapter);
1606         pBTInfo = GET_BT_INFO(padapter);
1607         pBtMgnt = &pBTInfo->BtMgnt;
1608         pBtHciInfo = &pBTInfo->BtHciInfo;
1609         pBtSec = &pBTInfo->BtSec;
1610         pBtDbg = &pBTInfo->BtDbg;
1611
1612         pBTInfo->padapter = padapter;
1613
1614         for (i = 0; i < MAX_BT_ASOC_ENTRY_NUM; i++)
1615                 bthci_ResetEntry(padapter, i);
1616
1617         bthci_ResetBtMgnt(pBtMgnt);
1618         bthci_ResetBtHciInfo(pBtHciInfo);
1619         bthci_ResetBtSec(padapter, pBtSec);
1620
1621         pBtMgnt->BTChannel = BT_Default_Chnl;
1622         pBtMgnt->CheckChnlIsSuit = true;
1623
1624         pBTInfo->BTBeaconTmrOn = false;
1625
1626         pBtMgnt->bCreateSpportQos = true;
1627
1628         del_timer_sync(&pBTInfo->BTHCIDiscardAclDataTimer);
1629         del_timer_sync(&pBTInfo->BTBeaconTimer);
1630
1631         HALBT_SetRtsCtsNoLenLimit(padapter);
1632         /*  */
1633         /*  Maybe we need to take care Group != AES case !! */
1634         /*  now we Pairwise and Group all used AES !! */
1635
1636         bthci_ResetBtExtInfo(pBtMgnt);
1637
1638         /* send command complete event here when all data are received. */
1639         if (bNeedSendEvent) {
1640                 u8 localBuf[6] = "";
1641                 u8 *pRetPar;
1642                 u8 len = 0;
1643                 struct packet_irp_hcievent_data *PPacketIrpEvent;
1644
1645                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
1646
1647                 len += bthci_CommandCompleteHeader(&localBuf[0],
1648                         OGF_SET_EVENT_MASK_COMMAND,
1649                         HCI_RESET,
1650                         status);
1651
1652                 /*  Return parameters starts from here */
1653                 pRetPar = &PPacketIrpEvent->Data[len];
1654                 pRetPar[0] = status;            /* status */
1655                 len += 1;
1656                 PPacketIrpEvent->Length = len;
1657
1658                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
1659         }
1660
1661         return status;
1662 }
1663
1664 static enum hci_status
1665 bthci_CmdWriteRemoteAMPAssoc(
1666         struct rtw_adapter *padapter,
1667         struct packet_irp_hcicmd_data *pHciCmd
1668         )
1669 {
1670         enum hci_status status = HCI_STATUS_SUCCESS;
1671         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
1672         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
1673         u8 CurrentAssocNum;
1674         u8 PhyLinkHandle;
1675
1676         pBtDbg->dbgHciInfo.hciCmdCntWriteRemoteAmpAssoc++;
1677         PhyLinkHandle = *((u8 *)pHciCmd->Data);
1678         CurrentAssocNum = bthci_GetCurrentEntryNum(padapter, PhyLinkHandle);
1679
1680         if (CurrentAssocNum == 0xff) {
1681                 RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("WriteRemoteAMPAssoc, No such Handle in the Entry\n"));
1682                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
1683                 bthci_EventWriteRemoteAmpAssoc(padapter, status, PhyLinkHandle);
1684                 return status;
1685         }
1686
1687         if (pBTInfo->BtAsocEntry[CurrentAssocNum].AmpAsocCmdData.AMPAssocfragment == NULL) {
1688                 RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("WriteRemoteAMPAssoc, AMP controller is busy\n"));
1689                 status = HCI_STATUS_CONTROLLER_BUSY;
1690                 bthci_EventWriteRemoteAmpAssoc(padapter, status, PhyLinkHandle);
1691                 return status;
1692         }
1693
1694         pBTInfo->BtAsocEntry[CurrentAssocNum].AmpAsocCmdData.BtPhyLinkhandle = PhyLinkHandle;/* u8 *)pHciCmd->Data); */
1695         pBTInfo->BtAsocEntry[CurrentAssocNum].AmpAsocCmdData.LenSoFar = *((u16 *)((u8 *)pHciCmd->Data+1));
1696         pBTInfo->BtAsocEntry[CurrentAssocNum].AmpAsocCmdData.AMPAssocRemLen = *((u16 *)((u8 *)pHciCmd->Data+3));
1697
1698         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO), ("WriteRemoteAMPAssoc, LenSoFar = 0x%x, AssocRemLen = 0x%x\n",
1699                 pBTInfo->BtAsocEntry[CurrentAssocNum].AmpAsocCmdData.LenSoFar,
1700                 pBTInfo->BtAsocEntry[CurrentAssocNum].AmpAsocCmdData.AMPAssocRemLen));
1701
1702         RTPRINT_DATA(FIOCTL, (IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO),
1703                      ("WriteRemoteAMPAssoc fragment \n"),
1704                      pHciCmd->Data,
1705                      pBTInfo->BtAsocEntry[CurrentAssocNum].AmpAsocCmdData.AMPAssocRemLen+5);
1706         if ((pBTInfo->BtAsocEntry[CurrentAssocNum].AmpAsocCmdData.AMPAssocRemLen) > MAX_AMP_ASSOC_FRAG_LEN) {
1707                 memcpy(((u8 *)pBTInfo->BtAsocEntry[CurrentAssocNum].AmpAsocCmdData.AMPAssocfragment+(pBTInfo->BtAsocEntry[CurrentAssocNum].AmpAsocCmdData.LenSoFar*(sizeof(u8)))),
1708                         (u8 *)pHciCmd->Data+5,
1709                         MAX_AMP_ASSOC_FRAG_LEN);
1710         } else {
1711                 memcpy((u8 *)(pBTInfo->BtAsocEntry[CurrentAssocNum].AmpAsocCmdData.AMPAssocfragment)+(pBTInfo->BtAsocEntry[CurrentAssocNum].AmpAsocCmdData.LenSoFar*(sizeof(u8))),
1712                         ((u8 *)pHciCmd->Data+5),
1713                         (pBTInfo->BtAsocEntry[CurrentAssocNum].AmpAsocCmdData.AMPAssocRemLen));
1714
1715                 RTPRINT_DATA(FIOCTL, (IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO), "WriteRemoteAMPAssoc :\n",
1716                         pHciCmd->Data+5, pBTInfo->BtAsocEntry[CurrentAssocNum].AmpAsocCmdData.AMPAssocRemLen);
1717
1718                 if (!bthci_GetAssocInfo(padapter, CurrentAssocNum))
1719                         status = HCI_STATUS_INVALID_HCI_CMD_PARA_VALUE;
1720
1721                 bthci_EventWriteRemoteAmpAssoc(padapter, status, PhyLinkHandle);
1722
1723                 bthci_StartBeaconAndConnect(padapter, pHciCmd, CurrentAssocNum);
1724         }
1725
1726         return status;
1727 }
1728
1729 /* 7.3.13 */
1730 static enum hci_status bthci_CmdReadConnectionAcceptTimeout(struct rtw_adapter *padapter)
1731 {
1732         enum hci_status         status = HCI_STATUS_SUCCESS;
1733 /*PMGNT_INFO            pMgntInfo = &padapter->MgntInfo; */
1734         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
1735         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
1736         u8 localBuf[8] = "";
1737         u8 *pRetPar;
1738         u8 len = 0;
1739         struct packet_irp_hcievent_data *PPacketIrpEvent;
1740         u16 *pu2Temp;
1741
1742         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
1743
1744         len += bthci_CommandCompleteHeader(&localBuf[0],
1745                 OGF_SET_EVENT_MASK_COMMAND,
1746                 HCI_READ_CONNECTION_ACCEPT_TIMEOUT,
1747                 status);
1748
1749         /*  Return parameters starts from here */
1750         pRetPar = &PPacketIrpEvent->Data[len];
1751         pRetPar[0] = status;            /* status */
1752         pu2Temp = (u16 *)&pRetPar[1];           /*  Conn_Accept_Timeout */
1753         *pu2Temp = pBtHciInfo->ConnAcceptTimeout;
1754         len += 3;
1755         PPacketIrpEvent->Length = len;
1756
1757         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
1758
1759         return status;
1760 }
1761
1762 /* 7.3.3 */
1763 static enum hci_status
1764 bthci_CmdSetEventFilter(
1765         struct rtw_adapter *padapter,
1766         struct packet_irp_hcicmd_data *pHciCmd
1767         )
1768 {
1769         return HCI_STATUS_SUCCESS;
1770 }
1771
1772 /* 7.3.14 */
1773 static enum hci_status
1774 bthci_CmdWriteConnectionAcceptTimeout(
1775         struct rtw_adapter *padapter,
1776         struct packet_irp_hcicmd_data *pHciCmd
1777         )
1778 {
1779         enum hci_status         status = HCI_STATUS_SUCCESS;
1780         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
1781         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
1782         u16     *pu2Temp;
1783         u8 localBuf[6] = "";
1784         u8 *pRetPar;
1785         u8 len = 0;
1786         struct packet_irp_hcievent_data *PPacketIrpEvent;
1787
1788         pu2Temp = (u16 *)&pHciCmd->Data[0];
1789         pBtHciInfo->ConnAcceptTimeout = *pu2Temp;
1790         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_DETAIL, ("ConnAcceptTimeout = 0x%x",
1791                 pBtHciInfo->ConnAcceptTimeout));
1792
1793         /* send command complete event here when all data are received. */
1794         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
1795
1796         len += bthci_CommandCompleteHeader(&localBuf[0],
1797                 OGF_SET_EVENT_MASK_COMMAND,
1798                 HCI_WRITE_CONNECTION_ACCEPT_TIMEOUT,
1799                 status);
1800
1801         /*  Return parameters starts from here */
1802         pRetPar = &PPacketIrpEvent->Data[len];
1803         pRetPar[0] = status;            /* status */
1804         len += 1;
1805         PPacketIrpEvent->Length = len;
1806
1807         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
1808
1809         return status;
1810 }
1811
1812 static enum hci_status
1813 bthci_CmdReadPageTimeout(
1814         struct rtw_adapter *padapter,
1815         struct packet_irp_hcicmd_data *pHciCmd
1816         )
1817 {
1818         enum hci_status         status = HCI_STATUS_SUCCESS;
1819         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
1820         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
1821         u8 localBuf[8] = "";
1822         u8 *pRetPar;
1823         u8 len = 0;
1824         struct packet_irp_hcievent_data *PPacketIrpEvent;
1825         u16 *pu2Temp;
1826
1827         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
1828
1829         len += bthci_CommandCompleteHeader(&localBuf[0],
1830                 OGF_SET_EVENT_MASK_COMMAND,
1831                 HCI_READ_PAGE_TIMEOUT,
1832                 status);
1833
1834         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("Read PageTimeout = 0x%x\n", pBtHciInfo->PageTimeout));
1835         /*  Return parameters starts from here */
1836         pRetPar = &PPacketIrpEvent->Data[len];
1837         pRetPar[0] = status;            /* status */
1838         pu2Temp = (u16 *)&pRetPar[1];           /*  Page_Timeout */
1839         *pu2Temp = pBtHciInfo->PageTimeout;
1840         len += 3;
1841         PPacketIrpEvent->Length = len;
1842
1843         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
1844
1845         return status;
1846 }
1847
1848 static enum hci_status
1849 bthci_CmdWritePageTimeout(
1850         struct rtw_adapter *padapter,
1851         struct packet_irp_hcicmd_data *pHciCmd
1852         )
1853 {
1854         enum hci_status         status = HCI_STATUS_SUCCESS;
1855         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
1856         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
1857         u16     *pu2Temp;
1858
1859         pu2Temp = (u16 *)&pHciCmd->Data[0];
1860         pBtHciInfo->PageTimeout = *pu2Temp;
1861         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("Write PageTimeout = 0x%x\n",
1862                 pBtHciInfo->PageTimeout));
1863
1864         /* send command complete event here when all data are received. */
1865         {
1866                 u8 localBuf[6] = "";
1867                 u8 *pRetPar;
1868                 u8 len = 0;
1869                 struct packet_irp_hcievent_data *PPacketIrpEvent;
1870
1871                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
1872
1873                 len += bthci_CommandCompleteHeader(&localBuf[0],
1874                         OGF_SET_EVENT_MASK_COMMAND,
1875                         HCI_WRITE_PAGE_TIMEOUT,
1876                         status);
1877
1878                 /*  Return parameters starts from here */
1879                 pRetPar = &PPacketIrpEvent->Data[len];
1880                 pRetPar[0] = status;            /* status */
1881                 len += 1;
1882                 PPacketIrpEvent->Length = len;
1883
1884                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
1885         }
1886
1887         return status;
1888 }
1889
1890 static enum hci_status
1891 bthci_CmdReadLinkSupervisionTimeout(
1892         struct rtw_adapter *padapter,
1893         struct packet_irp_hcicmd_data *pHciCmd
1894         )
1895 {
1896         enum hci_status status = HCI_STATUS_SUCCESS;
1897         struct bt_30info *pBTinfo = GET_BT_INFO(padapter);
1898         u8 physicalLinkHandle, EntryNum;
1899
1900         physicalLinkHandle = *((u8 *)pHciCmd->Data);
1901
1902         EntryNum = bthci_GetCurrentEntryNum(padapter, physicalLinkHandle);
1903
1904         if (EntryNum == 0xff) {
1905                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("ReadLinkSupervisionTimeout, No such Handle in the Entry\n"));
1906                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
1907                 return status;
1908         }
1909
1910         if (pBTinfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle != physicalLinkHandle)
1911                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
1912
1913         {
1914                 u8 localBuf[10] = "";
1915                 u8 *pRetPar;
1916                 u8 len = 0;
1917                 struct packet_irp_hcievent_data *PPacketIrpEvent;
1918                 u16 *pu2Temp;
1919
1920                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
1921
1922                 len += bthci_CommandCompleteHeader(&localBuf[0],
1923                         OGF_SET_EVENT_MASK_COMMAND,
1924                         HCI_READ_LINK_SUPERVISION_TIMEOUT,
1925                         status);
1926
1927                 /*  Return parameters starts from here */
1928                 pRetPar = &PPacketIrpEvent->Data[len];
1929                 pRetPar[0] = status;
1930                 pRetPar[1] = pBTinfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle;
1931                 pRetPar[2] = 0;
1932                 pu2Temp = (u16 *)&pRetPar[3];           /*  Conn_Accept_Timeout */
1933                 *pu2Temp = pBTinfo->BtAsocEntry[EntryNum].PhyLinkCmdData.LinkSuperversionTimeout;
1934                 len += 5;
1935                 PPacketIrpEvent->Length = len;
1936
1937                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
1938         }
1939
1940         return status;
1941 }
1942
1943 static enum hci_status
1944 bthci_CmdWriteLinkSupervisionTimeout(
1945         struct rtw_adapter *padapter,
1946         struct packet_irp_hcicmd_data *pHciCmd
1947         )
1948 {
1949         enum hci_status status = HCI_STATUS_SUCCESS;
1950         struct bt_30info *pBTinfo = GET_BT_INFO(padapter);
1951         u8 physicalLinkHandle, EntryNum;
1952
1953         physicalLinkHandle = *((u8 *)pHciCmd->Data);
1954
1955         EntryNum = bthci_GetCurrentEntryNum(padapter, physicalLinkHandle);
1956
1957         if (EntryNum == 0xff) {
1958                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("WriteLinkSupervisionTimeout, No such Handle in the Entry\n"));
1959                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
1960         } else {
1961                 if (pBTinfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle != physicalLinkHandle) {
1962                         status = HCI_STATUS_UNKNOW_CONNECT_ID;
1963                 } else {
1964                         pBTinfo->BtAsocEntry[EntryNum].PhyLinkCmdData.LinkSuperversionTimeout = *((u16 *)(((u8 *)pHciCmd->Data)+2));
1965                         RTPRINT(FIOCTL, IOCTL_STATE, ("BT Write LinkSuperversionTimeout[%d] = 0x%x\n",
1966                                 EntryNum, pBTinfo->BtAsocEntry[EntryNum].PhyLinkCmdData.LinkSuperversionTimeout));
1967                 }
1968         }
1969
1970         {
1971                 u8 localBuf[8] = "";
1972                 u8 *pRetPar;
1973                 u8 len = 0;
1974                 struct packet_irp_hcievent_data *PPacketIrpEvent;
1975
1976                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
1977
1978                 len += bthci_CommandCompleteHeader(&localBuf[0],
1979                         OGF_SET_EVENT_MASK_COMMAND,
1980                         HCI_WRITE_LINK_SUPERVISION_TIMEOUT,
1981                         status);
1982
1983                 /*  Return parameters starts from here */
1984                 pRetPar = &PPacketIrpEvent->Data[len];
1985                 pRetPar[0] = status;
1986                 pRetPar[1] = pBTinfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle;
1987                 pRetPar[2] = 0;
1988                 len += 3;
1989                 PPacketIrpEvent->Length = len;
1990
1991                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
1992         }
1993
1994         return status;
1995 }
1996
1997 static enum hci_status
1998 bthci_CmdEnhancedFlush(
1999         struct rtw_adapter *padapter,
2000         struct packet_irp_hcicmd_data *pHciCmd
2001         )
2002 {
2003         enum hci_status         status = HCI_STATUS_SUCCESS;
2004         struct bt_30info *pBTinfo = GET_BT_INFO(padapter);
2005         struct bt_hci_info *pBtHciInfo = &pBTinfo->BtHciInfo;
2006         u16             logicHandle;
2007         u8 Packet_Type;
2008
2009         logicHandle = *((u16 *)&pHciCmd->Data[0]);
2010         Packet_Type = pHciCmd->Data[2];
2011
2012         if (Packet_Type != 0)
2013                 status = HCI_STATUS_INVALID_HCI_CMD_PARA_VALUE;
2014         else
2015                 pBtHciInfo->enFlush_LLH = logicHandle;
2016
2017         if (bthci_DiscardTxPackets(padapter, pBtHciInfo->enFlush_LLH))
2018                 bthci_EventFlushOccurred(padapter, pBtHciInfo->enFlush_LLH);
2019
2020         /*  should send command status event */
2021         bthci_EventCommandStatus(padapter,
2022                         OGF_SET_EVENT_MASK_COMMAND,
2023                         HCI_ENHANCED_FLUSH,
2024                         status);
2025
2026         if (pBtHciInfo->enFlush_LLH) {
2027                 bthci_EventEnhancedFlushComplete(padapter, pBtHciInfo->enFlush_LLH);
2028                 pBtHciInfo->enFlush_LLH = 0;
2029         }
2030
2031         return status;
2032 }
2033
2034 static enum hci_status
2035 bthci_CmdReadLogicalLinkAcceptTimeout(
2036         struct rtw_adapter *padapter,
2037         struct packet_irp_hcicmd_data *pHciCmd
2038         )
2039 {
2040         enum hci_status         status = HCI_STATUS_SUCCESS;
2041 /*PMGNT_INFO            pMgntInfo = &padapter->MgntInfo; */
2042         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2043         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
2044         u8 localBuf[8] = "";
2045         u8 *pRetPar;
2046         u8 len = 0;
2047         struct packet_irp_hcievent_data *PPacketIrpEvent;
2048         u16 *pu2Temp;
2049
2050         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2051
2052         len += bthci_CommandCompleteHeader(&localBuf[0],
2053                 OGF_SET_EVENT_MASK_COMMAND,
2054                 HCI_READ_LOGICAL_LINK_ACCEPT_TIMEOUT,
2055                 status);
2056
2057         /*  Return parameters starts from here */
2058         pRetPar = &PPacketIrpEvent->Data[len];
2059         pRetPar[0] = status;
2060
2061         pu2Temp = (u16 *)&pRetPar[1];           /*  Conn_Accept_Timeout */
2062         *pu2Temp = pBtHciInfo->LogicalAcceptTimeout;
2063         len += 3;
2064         PPacketIrpEvent->Length = len;
2065
2066         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2067
2068         return status;
2069 }
2070
2071 static enum hci_status
2072 bthci_CmdWriteLogicalLinkAcceptTimeout(
2073         struct rtw_adapter *padapter,
2074         struct packet_irp_hcicmd_data *pHciCmd
2075         )
2076 {
2077         enum hci_status         status = HCI_STATUS_SUCCESS;
2078 /*PMGNT_INFO            pMgntInfo = &padapter->MgntInfo; */
2079         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2080         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
2081         u8 localBuf[6] = "";
2082         u8 *pRetPar;
2083         u8 len = 0;
2084         struct packet_irp_hcievent_data *PPacketIrpEvent;
2085
2086         pBtHciInfo->LogicalAcceptTimeout = *((u16 *)pHciCmd->Data);
2087
2088         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2089
2090         len += bthci_CommandCompleteHeader(&localBuf[0],
2091                 OGF_SET_EVENT_MASK_COMMAND,
2092                 HCI_WRITE_LOGICAL_LINK_ACCEPT_TIMEOUT,
2093                 status);
2094
2095         /*  Return parameters starts from here */
2096         pRetPar = &PPacketIrpEvent->Data[len];
2097         pRetPar[0] = status;
2098
2099         len += 1;
2100         PPacketIrpEvent->Length = len;
2101
2102         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2103         return status;
2104 }
2105
2106 static enum hci_status
2107 bthci_CmdSetEventMask(
2108         struct rtw_adapter *padapter,
2109         struct packet_irp_hcicmd_data *pHciCmd
2110         )
2111 {
2112         enum hci_status         status = HCI_STATUS_SUCCESS;
2113 /*PMGNT_INFO            pMgntInfo = &padapter->MgntInfo; */
2114         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2115         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
2116         u8 *pu8Temp;
2117         u8 localBuf[6] = "";
2118         u8 *pRetPar;
2119         u8 len = 0;
2120         struct packet_irp_hcievent_data *PPacketIrpEvent;
2121
2122         pu8Temp = (u8 *)&pHciCmd->Data[0];
2123         pBtHciInfo->BTEventMask = *pu8Temp;
2124         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_DETAIL, ("BTEventMask = 0x%"i64fmt"x\n",
2125                 pBtHciInfo->BTEventMask));
2126
2127         /* send command complete event here when all data are received. */
2128         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2129
2130         len += bthci_CommandCompleteHeader(&localBuf[0],
2131                 OGF_SET_EVENT_MASK_COMMAND,
2132                 HCI_SET_EVENT_MASK,
2133                 status);
2134
2135         /*  Return parameters starts from here */
2136         pRetPar = &PPacketIrpEvent->Data[len];
2137         pRetPar[0] = status;            /* status */
2138         len += 1;
2139         PPacketIrpEvent->Length = len;
2140
2141         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2142
2143         return status;
2144 }
2145
2146 /*  7.3.69 */
2147 static enum hci_status
2148 bthci_CmdSetEventMaskPage2(
2149         struct rtw_adapter *padapter,
2150         struct packet_irp_hcicmd_data *pHciCmd
2151         )
2152 {
2153         enum hci_status         status = HCI_STATUS_SUCCESS;
2154         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2155         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
2156         u8 *pu8Temp;
2157         u8 localBuf[6] = "";
2158         u8 *pRetPar;
2159         u8 len = 0;
2160         struct packet_irp_hcievent_data *PPacketIrpEvent;
2161
2162         pu8Temp = (u8 *)&pHciCmd->Data[0];
2163         pBtHciInfo->BTEventMaskPage2 = *pu8Temp;
2164         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO), ("BTEventMaskPage2 = 0x%"i64fmt"x\n",
2165                 pBtHciInfo->BTEventMaskPage2));
2166
2167         /* send command complete event here when all data are received. */
2168         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2169
2170         len += bthci_CommandCompleteHeader(&localBuf[0],
2171                 OGF_SET_EVENT_MASK_COMMAND,
2172                 HCI_SET_EVENT_MASK_PAGE_2,
2173                 status);
2174
2175         /*  Return parameters starts from here */
2176         pRetPar = &PPacketIrpEvent->Data[len];
2177         pRetPar[0] = status;            /* status */
2178         len += 1;
2179         PPacketIrpEvent->Length = len;
2180
2181         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2182
2183         return status;
2184 }
2185
2186 static enum hci_status
2187 bthci_CmdReadLocationData(
2188         struct rtw_adapter *padapter,
2189         struct packet_irp_hcicmd_data *pHciCmd
2190         )
2191 {
2192         enum hci_status         status = HCI_STATUS_SUCCESS;
2193         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2194         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
2195         u8 localBuf[12] = "";
2196         u8 *pRetPar;
2197         u8 len = 0;
2198         struct packet_irp_hcievent_data *PPacketIrpEvent;
2199         u16 *pu2Temp;
2200
2201         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2202
2203         len += bthci_CommandCompleteHeader(&localBuf[0],
2204                 OGF_SET_EVENT_MASK_COMMAND,
2205                 HCI_READ_LOCATION_DATA,
2206                 status);
2207         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("DomainAware = 0x%x\n", pBtHciInfo->LocationDomainAware));
2208         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("Domain = 0x%x\n", pBtHciInfo->LocationDomain));
2209         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("DomainOptions = 0x%x\n", pBtHciInfo->LocationDomainOptions));
2210         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("Options = 0x%x\n", pBtHciInfo->LocationOptions));
2211
2212         /*  Return parameters starts from here */
2213         pRetPar = &PPacketIrpEvent->Data[len];
2214         pRetPar[0] = status;
2215
2216         pRetPar[1] = pBtHciInfo->LocationDomainAware;   /* 0x0;  Location_Domain_Aware */
2217         pu2Temp = (u16 *)&pRetPar[2];                                   /*  Location_Domain */
2218         *pu2Temp = pBtHciInfo->LocationDomain;          /* 0x5858; */
2219         pRetPar[4] = pBtHciInfo->LocationDomainOptions; /* 0x58;        Location_Domain_Options */
2220         pRetPar[5] = pBtHciInfo->LocationOptions;               /* 0x0; Location_Options */
2221         len += 6;
2222         PPacketIrpEvent->Length = len;
2223
2224         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2225         return status;
2226 }
2227
2228 static enum hci_status
2229 bthci_CmdWriteLocationData(
2230         struct rtw_adapter *padapter,
2231         struct packet_irp_hcicmd_data *pHciCmd
2232         )
2233 {
2234         enum hci_status status = HCI_STATUS_SUCCESS;
2235         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2236         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
2237         u16     *pu2Temp;
2238         u8 localBuf[6] = "";
2239         u8 *pRetPar;
2240         u8 len = 0;
2241         struct packet_irp_hcievent_data *PPacketIrpEvent;
2242
2243         pBtHciInfo->LocationDomainAware = pHciCmd->Data[0];
2244         pu2Temp = (u16 *)&pHciCmd->Data[1];
2245         pBtHciInfo->LocationDomain = *pu2Temp;
2246         pBtHciInfo->LocationDomainOptions = pHciCmd->Data[3];
2247         pBtHciInfo->LocationOptions = pHciCmd->Data[4];
2248         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("DomainAware = 0x%x\n", pBtHciInfo->LocationDomainAware));
2249         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("Domain = 0x%x\n", pBtHciInfo->LocationDomain));
2250         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("DomainOptions = 0x%x\n", pBtHciInfo->LocationDomainOptions));
2251         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("Options = 0x%x\n", pBtHciInfo->LocationOptions));
2252
2253         /* send command complete event here when all data are received. */
2254         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2255
2256         len += bthci_CommandCompleteHeader(&localBuf[0],
2257                 OGF_SET_EVENT_MASK_COMMAND,
2258                 HCI_WRITE_LOCATION_DATA,
2259                 status);
2260
2261         /*  Return parameters starts from here */
2262         pRetPar = &PPacketIrpEvent->Data[len];
2263         pRetPar[0] = status;            /* status */
2264         len += 1;
2265         PPacketIrpEvent->Length = len;
2266
2267         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2268
2269         return status;
2270 }
2271
2272 static enum hci_status
2273 bthci_CmdReadFlowControlMode(
2274         struct rtw_adapter *padapter,
2275         struct packet_irp_hcicmd_data *pHciCmd
2276         )
2277 {
2278         enum hci_status status = HCI_STATUS_SUCCESS;
2279         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2280         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
2281         u8 localBuf[7] = "";
2282         u8 *pRetPar;
2283         u8 len = 0;
2284         struct packet_irp_hcievent_data *PPacketIrpEvent;
2285
2286         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2287
2288         len += bthci_CommandCompleteHeader(&localBuf[0],
2289                 OGF_SET_EVENT_MASK_COMMAND,
2290                 HCI_READ_FLOW_CONTROL_MODE,
2291                 status);
2292
2293         /*  Return parameters starts from here */
2294         pRetPar = &PPacketIrpEvent->Data[len];
2295         pRetPar[0] = status;
2296         pRetPar[1] = pBtHciInfo->FlowControlMode;       /*  Flow Control Mode */
2297         len += 2;
2298         PPacketIrpEvent->Length = len;
2299
2300         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2301         return status;
2302 }
2303
2304 static enum hci_status
2305 bthci_CmdWriteFlowControlMode(
2306         struct rtw_adapter *padapter,
2307         struct packet_irp_hcicmd_data *pHciCmd
2308         )
2309 {
2310         enum hci_status status = HCI_STATUS_SUCCESS;
2311         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2312         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
2313         u8 localBuf[6] = "";
2314         u8 *pRetPar;
2315         u8 len = 0;
2316         struct packet_irp_hcievent_data *PPacketIrpEvent;
2317
2318         pBtHciInfo->FlowControlMode = pHciCmd->Data[0];
2319
2320         /* send command complete event here when all data are received. */
2321         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2322
2323         len += bthci_CommandCompleteHeader(&localBuf[0],
2324                 OGF_SET_EVENT_MASK_COMMAND,
2325                 HCI_WRITE_FLOW_CONTROL_MODE,
2326                 status);
2327
2328         /*  Return parameters starts from here */
2329         pRetPar = &PPacketIrpEvent->Data[len];
2330         pRetPar[0] = status;            /* status */
2331         len += 1;
2332         PPacketIrpEvent->Length = len;
2333
2334         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2335
2336         return status;
2337 }
2338
2339 static enum hci_status
2340 bthci_CmdReadBestEffortFlushTimeout(
2341         struct rtw_adapter *padapter,
2342         struct packet_irp_hcicmd_data *pHciCmd
2343         )
2344 {
2345         enum hci_status status = HCI_STATUS_SUCCESS;
2346         struct bt_30info *pBTinfo = GET_BT_INFO(padapter);
2347         u16 i, j, logicHandle;
2348         u32 BestEffortFlushTimeout = 0xffffffff;
2349         u8 find = 0;
2350
2351         logicHandle = *((u16 *)pHciCmd->Data);
2352         /*  find an matched logical link index and copy the data */
2353         for (j = 0; j < MAX_BT_ASOC_ENTRY_NUM; j++) {
2354                 for (i = 0; i < MAX_LOGICAL_LINK_NUM; i++) {
2355                         if (pBTinfo->BtAsocEntry[j].LogLinkCmdData[i].BtLogLinkhandle == logicHandle) {
2356                                 BestEffortFlushTimeout = pBTinfo->BtAsocEntry[j].LogLinkCmdData[i].BestEffortFlushTimeout;
2357                                 find = 1;
2358                                 break;
2359                         }
2360                 }
2361         }
2362
2363         if (!find)
2364                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
2365
2366         {
2367                 u8 localBuf[10] = "";
2368                 u8 *pRetPar;
2369                 u8 len = 0;
2370                 struct packet_irp_hcievent_data *PPacketIrpEvent;
2371                 u32 *pu4Temp;
2372
2373                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2374
2375                 len += bthci_CommandCompleteHeader(&localBuf[0],
2376                         OGF_SET_EVENT_MASK_COMMAND,
2377                         HCI_READ_BEST_EFFORT_FLUSH_TIMEOUT,
2378                         status);
2379
2380                 /*  Return parameters starts from here */
2381                 pRetPar = &PPacketIrpEvent->Data[len];
2382                 pRetPar[0] = status;
2383                 pu4Temp = (u32 *)&pRetPar[1];   /*  Best_Effort_Flush_Timeout */
2384                 *pu4Temp = BestEffortFlushTimeout;
2385                 len += 5;
2386                 PPacketIrpEvent->Length = len;
2387
2388                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2389         }
2390         return status;
2391 }
2392
2393 static enum hci_status
2394 bthci_CmdWriteBestEffortFlushTimeout(
2395         struct rtw_adapter *padapter,
2396         struct packet_irp_hcicmd_data *pHciCmd
2397         )
2398 {
2399         enum hci_status status = HCI_STATUS_SUCCESS;
2400         struct bt_30info *pBTinfo = GET_BT_INFO(padapter);
2401         u16 i, j, logicHandle;
2402         u32 BestEffortFlushTimeout = 0xffffffff;
2403         u8 find = 0;
2404
2405         logicHandle = *((u16 *)pHciCmd->Data);
2406         BestEffortFlushTimeout = *((u32 *)(pHciCmd->Data+1));
2407
2408         /*  find an matched logical link index and copy the data */
2409         for (j = 0; j < MAX_BT_ASOC_ENTRY_NUM; j++) {
2410                 for (i = 0; i < MAX_LOGICAL_LINK_NUM; i++) {
2411                         if (pBTinfo->BtAsocEntry[j].LogLinkCmdData[i].BtLogLinkhandle == logicHandle) {
2412                                 pBTinfo->BtAsocEntry[j].LogLinkCmdData[i].BestEffortFlushTimeout = BestEffortFlushTimeout;
2413                                 find = 1;
2414                                 break;
2415                         }
2416                 }
2417         }
2418
2419         if (!find)
2420                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
2421
2422         {
2423                 u8 localBuf[6] = "";
2424                 u8 *pRetPar;
2425                 u8 len = 0;
2426                 struct packet_irp_hcievent_data *PPacketIrpEvent;
2427
2428                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2429
2430                 len += bthci_CommandCompleteHeader(&localBuf[0],
2431                         OGF_SET_EVENT_MASK_COMMAND,
2432                         HCI_WRITE_BEST_EFFORT_FLUSH_TIMEOUT,
2433                         status);
2434
2435                 /*  Return parameters starts from here */
2436                 pRetPar = &PPacketIrpEvent->Data[len];
2437                 pRetPar[0] = status;
2438                 len += 1;
2439                 PPacketIrpEvent->Length = len;
2440
2441                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2442         }
2443         return status;
2444 }
2445
2446 static enum hci_status
2447 bthci_CmdShortRangeMode(
2448         struct rtw_adapter *padapter,
2449         struct packet_irp_hcicmd_data *pHciCmd
2450         )
2451 {
2452         enum hci_status status = HCI_STATUS_SUCCESS;
2453         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2454         u8 PhyLinkHandle, EntryNum, ShortRangeMode;
2455
2456         PhyLinkHandle = pHciCmd->Data[0];
2457         ShortRangeMode = pHciCmd->Data[1];
2458         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("PLH = 0x%x, Short_Range_Mode = 0x%x\n", PhyLinkHandle, ShortRangeMode));
2459
2460         EntryNum = bthci_GetCurrentEntryNum(padapter, PhyLinkHandle);
2461         if (EntryNum != 0xff) {
2462                 pBTInfo->BtAsocEntry[EntryNum].ShortRangeMode = ShortRangeMode;
2463         } else {
2464                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("No such PLH(0x%x)\n", PhyLinkHandle));
2465                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
2466         }
2467
2468         bthci_EventCommandStatus(padapter,
2469                         OGF_SET_EVENT_MASK_COMMAND,
2470                         HCI_SHORT_RANGE_MODE,
2471                         status);
2472
2473         bthci_EventShortRangeModeChangeComplete(padapter, status, ShortRangeMode, EntryNum);
2474
2475         return status;
2476 }
2477
2478 static enum hci_status bthci_CmdReadLocalSupportedCommands(struct rtw_adapter *padapter)
2479 {
2480         enum hci_status status = HCI_STATUS_SUCCESS;
2481         u8 localBuf[TmpLocalBufSize] = "";
2482         u8 *pRetPar, *pSupportedCmds;
2483         u8 len = 0;
2484         struct packet_irp_hcievent_data *PPacketIrpEvent;
2485
2486         /*  send command complete event here when all data are received. */
2487         PlatformZeroMemory(&localBuf[0], TmpLocalBufSize);
2488         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2489
2490         len += bthci_CommandCompleteHeader(&localBuf[0],
2491                 OGF_INFORMATIONAL_PARAMETERS,
2492                 HCI_READ_LOCAL_SUPPORTED_COMMANDS,
2493                 status);
2494
2495         /*  Return parameters starts from here */
2496         pRetPar = &PPacketIrpEvent->Data[len];
2497         pRetPar[0] = status;            /* status */
2498         len += 1;
2499         pSupportedCmds = &pRetPar[1];
2500         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Octet[5]= 0xc0\nBit [6]= Set Event Mask, [7]= Reset\n"));
2501         pSupportedCmds[5] = 0xc0;
2502         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Octet[6]= 0x01\nBit [0]= Set Event Filter\n"));
2503         pSupportedCmds[6] = 0x01;
2504         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Octet[7]= 0x0c\nBit [2]= Read Connection Accept Timeout, [3]= Write Connection Accept Timeout\n"));
2505         pSupportedCmds[7] = 0x0c;
2506         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Octet[10]= 0x80\nBit [7]= Host Number Of Completed Packets\n"));
2507         pSupportedCmds[10] = 0x80;
2508         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Octet[11]= 0x03\nBit [0]= Read Link Supervision Timeout, [1]= Write Link Supervision Timeout\n"));
2509         pSupportedCmds[11] = 0x03;
2510         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Octet[14]= 0xa8\nBit [3]= Read Local Version Information, [5]= Read Local Supported Features, [7]= Read Buffer Size\n"));
2511         pSupportedCmds[14] = 0xa8;
2512         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Octet[15]= 0x1c\nBit [2]= Read Failed Contact Count, [3]= Reset Failed Contact Count, [4]= Get Link Quality\n"));
2513         pSupportedCmds[15] = 0x1c;
2514         /* pSupportedCmds[16] = 0x04; */
2515         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Octet[19]= 0x40\nBit [6]= Enhanced Flush\n"));
2516         pSupportedCmds[19] = 0x40;
2517         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Octet[21]= 0xff\nBit [0]= Create Physical Link, [1]= Accept Physical Link, [2]= Disconnect Physical Link, [3]= Create Logical Link\n"));
2518         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("     [4]= Accept Logical Link, [5]= Disconnect Logical Link, [6]= Logical Link Cancel, [7]= Flow Spec Modify\n"));
2519         pSupportedCmds[21] = 0xff;
2520         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Octet[22]= 0xff\nBit [0]= Read Logical Link Accept Timeout, [1]= Write Logical Link Accept Timeout, [2]= Set Event Mask Page 2, [3]= Read Location Data\n"));
2521         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("     [4]= Write Location Data, [5]= Read Local AMP Info, [6]= Read Local AMP_ASSOC, [7]= Write Remote AMP_ASSOC\n"));
2522         pSupportedCmds[22] = 0xff;
2523         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Octet[23]= 0x07\nBit [0]= Read Flow Control Mode, [1]= Write Flow Control Mode, [2]= Read Data Block Size\n"));
2524         pSupportedCmds[23] = 0x07;
2525         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD|IOCTL_BT_LOGO), ("Octet[24]= 0x1c\nBit [2]= Read Best Effort Flush Timeout, [3]= Write Best Effort Flush Timeout, [4]= Short Range Mode\n"));
2526         pSupportedCmds[24] = 0x1c;
2527         len += 64;
2528         PPacketIrpEvent->Length = len;
2529
2530         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2531
2532         return status;
2533 }
2534
2535 static enum hci_status bthci_CmdReadLocalSupportedFeatures(struct rtw_adapter *padapter)
2536 {
2537         enum hci_status status = HCI_STATUS_SUCCESS;
2538         u8 localBuf[TmpLocalBufSize] = "";
2539         u8 *pRetPar;
2540         u8 len = 0;
2541         struct packet_irp_hcievent_data *PPacketIrpEvent;
2542
2543         /* send command complete event here when all data are received. */
2544         PlatformZeroMemory(&localBuf[0], TmpLocalBufSize);
2545         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2546
2547         len += bthci_CommandCompleteHeader(&localBuf[0],
2548                 OGF_INFORMATIONAL_PARAMETERS,
2549                 HCI_READ_LOCAL_SUPPORTED_FEATURES,
2550                 status);
2551
2552         /*  Return parameters starts from here */
2553         pRetPar = &PPacketIrpEvent->Data[len];
2554         pRetPar[0] = status;            /* status */
2555         len += 9;
2556         PPacketIrpEvent->Length = len;
2557
2558         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2559         return status;
2560 }
2561
2562 static enum hci_status bthci_CmdReadLocalAMPAssoc(struct rtw_adapter *padapter,
2563         struct packet_irp_hcicmd_data *pHciCmd)
2564 {
2565         enum hci_status status = HCI_STATUS_SUCCESS;
2566         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2567         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
2568         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
2569         u8 PhyLinkHandle, EntryNum;
2570
2571         pBtDbg->dbgHciInfo.hciCmdCntReadLocalAmpAssoc++;
2572         PhyLinkHandle = *((u8 *)pHciCmd->Data);
2573         EntryNum = bthci_GetCurrentEntryNum(padapter, PhyLinkHandle);
2574
2575         if ((EntryNum == 0xff) && PhyLinkHandle != 0) {
2576                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("ReadLocalAMPAssoc, EntryNum = %d  !!!!!, physical link handle = 0x%x\n",
2577                 EntryNum, PhyLinkHandle));
2578                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
2579         } else if (pBtMgnt->bPhyLinkInProgressStartLL) {
2580                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
2581                 pBtMgnt->bPhyLinkInProgressStartLL = false;
2582         } else {
2583                 pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.BtPhyLinkhandle = *((u8 *)pHciCmd->Data);
2584                 pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.LenSoFar = *((u16 *)((u8 *)pHciCmd->Data+1));
2585                 pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.MaxRemoteASSOCLen = *((u16 *)((u8 *)pHciCmd->Data+3));
2586                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_DETAIL, ("ReadLocalAMPAssoc, LenSoFar =%d, MaxRemoteASSOCLen =%d\n",
2587                         pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.LenSoFar,
2588                         pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.MaxRemoteASSOCLen));
2589         }
2590
2591         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("ReadLocalAMPAssoc, EntryNum = %d  !!!!!, physical link handle = 0x%x, LengthSoFar = %x  \n",
2592                 EntryNum, PhyLinkHandle, pBTInfo->BtAsocEntry[EntryNum].AmpAsocCmdData.LenSoFar));
2593
2594         /* send command complete event here when all data are received. */
2595         {
2596                 struct packet_irp_hcievent_data *PPacketIrpEvent;
2597
2598                 /* PVOID buffer = padapter->IrpHCILocalbuf.Ptr; */
2599                 u8 localBuf[TmpLocalBufSize] = "";
2600                 u16     *pRemainLen;
2601                 u32     totalLen = 0;
2602                 u16     typeLen = 0, remainLen = 0, ret_index = 0;
2603                 u8 *pRetPar;
2604
2605                 PlatformZeroMemory(&localBuf[0], TmpLocalBufSize);
2606                 /* PPacketIrpEvent = (struct packet_irp_hcievent_data *)(buffer); */
2607                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2608
2609                 totalLen += bthci_CommandCompleteHeader(&localBuf[0],
2610                         OGF_STATUS_PARAMETERS,
2611                         HCI_READ_LOCAL_AMP_ASSOC,
2612                         status);
2613                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("ReadLocalAMPAssoc, Remaining_Len =%d  \n", remainLen));
2614                 /*  Return parameters starts from here */
2615                 pRetPar = &PPacketIrpEvent->Data[totalLen];
2616                 pRetPar[0] = status;            /* status */
2617                 pRetPar[1] = *((u8 *)pHciCmd->Data);
2618                 pRemainLen = (u16 *)&pRetPar[2];        /* AMP_ASSOC_Remaining_Length */
2619                 totalLen += 4;  /* 0]~[3] */
2620                 ret_index = 4;
2621
2622                 typeLen = bthci_AssocMACAddr(padapter, &pRetPar[ret_index]);
2623                 totalLen += typeLen;
2624                 remainLen += typeLen;
2625                 ret_index += typeLen;
2626                 typeLen = bthci_AssocPreferredChannelList(padapter, &pRetPar[ret_index], EntryNum);
2627                 totalLen += typeLen;
2628                 remainLen += typeLen;
2629                 ret_index += typeLen;
2630                 typeLen = bthci_PALCapabilities(padapter, &pRetPar[ret_index]);
2631                 totalLen += typeLen;
2632                 remainLen += typeLen;
2633                 ret_index += typeLen;
2634                 typeLen = bthci_AssocPALVer(padapter, &pRetPar[ret_index]);
2635                 totalLen += typeLen;
2636                 remainLen += typeLen;
2637                 PPacketIrpEvent->Length = (u8)totalLen;
2638                 *pRemainLen = remainLen;        /*  AMP_ASSOC_Remaining_Length */
2639                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("ReadLocalAMPAssoc, Remaining_Len =%d  \n", remainLen));
2640                 RTPRINT_DATA(FIOCTL, (IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO), ("AMP_ASSOC_fragment : \n"), PPacketIrpEvent->Data, totalLen);
2641
2642                 bthci_IndicateEvent(padapter, PPacketIrpEvent, totalLen+2);
2643         }
2644
2645         return status;
2646 }
2647
2648 static enum hci_status bthci_CmdReadFailedContactCounter(struct rtw_adapter *padapter,
2649                        struct packet_irp_hcicmd_data *pHciCmd)
2650 {
2651
2652         enum hci_status         status = HCI_STATUS_SUCCESS;
2653         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2654         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
2655         u8 localBuf[TmpLocalBufSize] = "";
2656         u8 *pRetPar;
2657         u8 len = 0;
2658         struct packet_irp_hcievent_data *PPacketIrpEvent;
2659         u16 handle;
2660
2661         handle = *((u16 *)pHciCmd->Data);
2662         /* send command complete event here when all data are received. */
2663         PlatformZeroMemory(&localBuf[0], TmpLocalBufSize);
2664         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2665
2666         len += bthci_CommandCompleteHeader(&localBuf[0],
2667                 OGF_STATUS_PARAMETERS,
2668                 HCI_READ_FAILED_CONTACT_COUNTER,
2669                 status);
2670
2671         /*  Return parameters starts from here */
2672         pRetPar = &PPacketIrpEvent->Data[len];
2673         pRetPar[0] = status;            /* status */
2674         pRetPar[1] = TWOBYTE_LOWBYTE(handle);
2675         pRetPar[2] = TWOBYTE_HIGHTBYTE(handle);
2676         pRetPar[3] = TWOBYTE_LOWBYTE(pBtHciInfo->FailContactCount);
2677         pRetPar[4] = TWOBYTE_HIGHTBYTE(pBtHciInfo->FailContactCount);
2678         len += 5;
2679         PPacketIrpEvent->Length = len;
2680
2681         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2682
2683         return status;
2684 }
2685
2686 static enum hci_status
2687 bthci_CmdResetFailedContactCounter(
2688         struct rtw_adapter *padapter,
2689         struct packet_irp_hcicmd_data *pHciCmd
2690         )
2691 {
2692         enum hci_status         status = HCI_STATUS_SUCCESS;
2693 /*PMGNT_INFO            pMgntInfo = &padapter->MgntInfo; */
2694         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2695         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
2696         u16             handle;
2697         u8 localBuf[TmpLocalBufSize] = "";
2698         u8 *pRetPar;
2699         u8 len = 0;
2700         struct packet_irp_hcievent_data *PPacketIrpEvent;
2701
2702         handle = *((u16 *)pHciCmd->Data);
2703         pBtHciInfo->FailContactCount = 0;
2704
2705         /* send command complete event here when all data are received. */
2706         PlatformZeroMemory(&localBuf[0], TmpLocalBufSize);
2707         /* PPacketIrpEvent = (struct packet_irp_hcievent_data *)(buffer); */
2708         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2709
2710         len += bthci_CommandCompleteHeader(&localBuf[0],
2711                 OGF_STATUS_PARAMETERS,
2712                 HCI_RESET_FAILED_CONTACT_COUNTER,
2713                 status);
2714
2715         /*  Return parameters starts from here */
2716         pRetPar = &PPacketIrpEvent->Data[len];
2717         pRetPar[0] = status;            /* status */
2718         pRetPar[1] = TWOBYTE_LOWBYTE(handle);
2719         pRetPar[2] = TWOBYTE_HIGHTBYTE(handle);
2720         len += 3;
2721         PPacketIrpEvent->Length = len;
2722
2723         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2724         return status;
2725 }
2726
2727 /*  */
2728 /*  BT 3.0+HS [Vol 2] 7.4.1 */
2729 /*  */
2730 static enum hci_status
2731 bthci_CmdReadLocalVersionInformation(
2732         struct rtw_adapter *padapter
2733         )
2734 {
2735         enum hci_status status = HCI_STATUS_SUCCESS;
2736         /* send command complete event here when all data are received. */
2737         u8 localBuf[TmpLocalBufSize] = "";
2738         u8 *pRetPar;
2739         u8 len = 0;
2740         struct packet_irp_hcievent_data *PPacketIrpEvent;
2741         u16 *pu2Temp;
2742
2743         PlatformZeroMemory(&localBuf[0], TmpLocalBufSize);
2744         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2745
2746         len += bthci_CommandCompleteHeader(&localBuf[0],
2747                 OGF_INFORMATIONAL_PARAMETERS,
2748                 HCI_READ_LOCAL_VERSION_INFORMATION,
2749                 status);
2750
2751         /*  Return parameters starts from here */
2752         pRetPar = &PPacketIrpEvent->Data[len];
2753         pRetPar[0] = status;            /* status */
2754         pRetPar[1] = 0x05;                      /*  HCI_Version */
2755         pu2Temp = (u16 *)&pRetPar[2];           /*  HCI_Revision */
2756         *pu2Temp = 0x0001;
2757         pRetPar[4] = 0x05;                      /*  LMP/PAL_Version */
2758         pu2Temp = (u16 *)&pRetPar[5];           /*  Manufacturer_Name */
2759         *pu2Temp = 0x005d;
2760         pu2Temp = (u16 *)&pRetPar[7];           /*  LMP/PAL_Subversion */
2761         *pu2Temp = 0x0001;
2762         len += 9;
2763         PPacketIrpEvent->Length = len;
2764
2765         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO), ("LOCAL_VERSION_INFORMATION\n"));
2766         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO), ("Status  %x\n", status));
2767         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO), ("HCI_Version = 0x05\n"));
2768         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO), ("HCI_Revision = 0x0001\n"));
2769         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO), ("LMP/PAL_Version = 0x05\n"));
2770         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO), ("Manufacturer_Name = 0x0001\n"));
2771         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO), ("LMP/PAL_Subversion = 0x0001\n"));
2772
2773         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2774
2775         return status;
2776 }
2777
2778 /* 7.4.7 */
2779 static enum hci_status bthci_CmdReadDataBlockSize(struct rtw_adapter *padapter)
2780 {
2781         enum hci_status                 status = HCI_STATUS_SUCCESS;
2782         u8 localBuf[TmpLocalBufSize] = "";
2783         u8 *pRetPar;
2784         u8 len = 0;
2785         struct packet_irp_hcievent_data *PPacketIrpEvent;
2786         u16 *pu2Temp;
2787
2788         PlatformZeroMemory(&localBuf[0], TmpLocalBufSize);
2789         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2790
2791         len += bthci_CommandCompleteHeader(&localBuf[0],
2792                 OGF_INFORMATIONAL_PARAMETERS,
2793                 HCI_READ_DATA_BLOCK_SIZE,
2794                 status);
2795
2796         /*  Return parameters starts from here */
2797         pRetPar = &PPacketIrpEvent->Data[len];
2798         pRetPar[0] = HCI_STATUS_SUCCESS;        /* status */
2799         pu2Temp = (u16 *)&pRetPar[1];           /*  Max_ACL_Data_Packet_Length */
2800         *pu2Temp = Max80211PALPDUSize;
2801
2802         pu2Temp = (u16 *)&pRetPar[3];           /*  Data_Block_Length */
2803         *pu2Temp = Max80211PALPDUSize;
2804         pu2Temp = (u16 *)&pRetPar[5];           /*  Total_Num_Data_Blocks */
2805         *pu2Temp = BTTotalDataBlockNum;
2806         len += 7;
2807         PPacketIrpEvent->Length = len;
2808
2809         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2810
2811         return status;
2812 }
2813
2814 /*  7.4.5 */
2815 static enum hci_status bthci_CmdReadBufferSize(struct rtw_adapter *padapter)
2816 {
2817         enum hci_status status = HCI_STATUS_SUCCESS;
2818         u8 localBuf[TmpLocalBufSize] = "";
2819         u8 *pRetPar;
2820         u8 len = 0;
2821         struct packet_irp_hcievent_data *PPacketIrpEvent;
2822         u16 *pu2Temp;
2823
2824         PlatformZeroMemory(&localBuf[0], TmpLocalBufSize);
2825         /* PPacketIrpEvent = (struct packet_irp_hcievent_data *)(buffer); */
2826         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2827
2828         len += bthci_CommandCompleteHeader(&localBuf[0],
2829                 OGF_INFORMATIONAL_PARAMETERS,
2830                 HCI_READ_BUFFER_SIZE,
2831                 status);
2832         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("Synchronous_Data_Packet_Length = 0x%x\n", BTSynDataPacketLength));
2833         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("Total_Num_ACL_Data_Packets = 0x%x\n", BTTotalDataBlockNum));
2834         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("Total_Num_Synchronous_Data_Packets = 0x%x\n", BTTotalDataBlockNum));
2835         /*  Return parameters starts from here */
2836         pRetPar = &PPacketIrpEvent->Data[len];
2837         pRetPar[0] = status;            /* status */
2838         pu2Temp = (u16 *)&pRetPar[1];           /*  HC_ACL_Data_Packet_Length */
2839         *pu2Temp = Max80211PALPDUSize;
2840
2841         pRetPar[3] = BTSynDataPacketLength;     /*  HC_Synchronous_Data_Packet_Length */
2842         pu2Temp = (u16 *)&pRetPar[4];           /*  HC_Total_Num_ACL_Data_Packets */
2843         *pu2Temp = BTTotalDataBlockNum;
2844         pu2Temp = (u16 *)&pRetPar[6];           /*  HC_Total_Num_Synchronous_Data_Packets */
2845         *pu2Temp = BTTotalDataBlockNum;
2846         len += 8;
2847         PPacketIrpEvent->Length = len;
2848
2849         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2850
2851         return status;
2852 }
2853
2854 static enum hci_status bthci_CmdReadLocalAMPInfo(struct rtw_adapter *padapter)
2855 {
2856         enum hci_status status = HCI_STATUS_SUCCESS;
2857         struct pwrctrl_priv *ppwrctrl = &padapter->pwrctrlpriv;
2858         u8 localBuf[TmpLocalBufSize] = "";
2859         u8 *pRetPar;
2860         u8 len = 0;
2861         struct packet_irp_hcievent_data *PPacketIrpEvent;
2862         u16 *pu2Temp;
2863         u32 *pu4Temp;
2864         u32     TotalBandwidth = BTTOTALBANDWIDTH, MaxBandGUBandwidth = BTMAXBANDGUBANDWIDTH;
2865         u8 ControlType = 0x01, AmpStatus = 0x01;
2866         u32     MaxFlushTimeout = 10000, BestEffortFlushTimeout = 5000;
2867         u16 MaxPDUSize = Max80211PALPDUSize, PalCap = 0x1, AmpAssocLen = Max80211AMPASSOCLen, MinLatency = 20;
2868
2869         if ((ppwrctrl->rfoff_reason & RF_CHANGE_BY_HW) ||
2870             (ppwrctrl->rfoff_reason & RF_CHANGE_BY_SW)) {
2871                 AmpStatus = AMP_STATUS_NO_CAPACITY_FOR_BT;
2872         }
2873
2874         PlatformZeroMemory(&localBuf[0], TmpLocalBufSize);
2875         /* PPacketIrpEvent = (struct packet_irp_hcievent_data *)(buffer); */
2876         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2877
2878         len += bthci_CommandCompleteHeader(&localBuf[0],
2879                 OGF_STATUS_PARAMETERS,
2880                 HCI_READ_LOCAL_AMP_INFO,
2881                 status);
2882
2883         /*  Return parameters starts from here */
2884         pRetPar = &PPacketIrpEvent->Data[len];
2885         pRetPar[0] = status;                    /* status */
2886         pRetPar[1] = AmpStatus;                 /*  AMP_Status */
2887         pu4Temp = (u32 *)&pRetPar[2];           /*  Total_Bandwidth */
2888         *pu4Temp = TotalBandwidth;              /* 0x19bfcc00;0x7530; */
2889         pu4Temp = (u32 *)&pRetPar[6];           /*  Max_Guaranteed_Bandwidth */
2890         *pu4Temp = MaxBandGUBandwidth;          /* 0x19bfcc00;0x4e20; */
2891         pu4Temp = (u32 *)&pRetPar[10];          /*  Min_Latency */
2892         *pu4Temp = MinLatency;                  /* 150; */
2893         pu4Temp = (u32 *)&pRetPar[14];          /*  Max_PDU_Size */
2894         *pu4Temp = MaxPDUSize;
2895         pRetPar[18] = ControlType;              /*  Controller_Type */
2896         pu2Temp = (u16 *)&pRetPar[19];          /*  PAL_Capabilities */
2897         *pu2Temp = PalCap;
2898         pu2Temp = (u16 *)&pRetPar[21];          /*  AMP_ASSOC_Length */
2899         *pu2Temp = AmpAssocLen;
2900         pu4Temp = (u32 *)&pRetPar[23];          /*  Max_Flush_Timeout */
2901         *pu4Temp = MaxFlushTimeout;
2902         pu4Temp = (u32 *)&pRetPar[27];          /*  Best_Effort_Flush_Timeout */
2903         *pu4Temp = BestEffortFlushTimeout;
2904         len += 31;
2905         PPacketIrpEvent->Length = len;
2906         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("AmpStatus = 0x%x\n",
2907                 AmpStatus));
2908         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("TotalBandwidth = 0x%x, MaxBandGUBandwidth = 0x%x, MinLatency = 0x%x, \n MaxPDUSize = 0x%x, ControlType = 0x%x\n",
2909                 TotalBandwidth, MaxBandGUBandwidth, MinLatency, MaxPDUSize, ControlType));
2910         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("PalCap = 0x%x, AmpAssocLen = 0x%x, MaxFlushTimeout = 0x%x, BestEffortFlushTimeout = 0x%x\n",
2911                 PalCap, AmpAssocLen, MaxFlushTimeout, BestEffortFlushTimeout));
2912         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2913         return status;
2914 }
2915
2916 static enum hci_status
2917 bthci_CmdCreatePhysicalLink(
2918         struct rtw_adapter *padapter,
2919         struct packet_irp_hcicmd_data *pHciCmd
2920         )
2921 {
2922         enum hci_status status;
2923         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2924         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
2925
2926         pBtDbg->dbgHciInfo.hciCmdCntCreatePhyLink++;
2927
2928         status = bthci_BuildPhysicalLink(padapter,
2929                 pHciCmd, HCI_CREATE_PHYSICAL_LINK);
2930
2931         return status;
2932 }
2933
2934 static enum hci_status
2935 bthci_CmdReadLinkQuality(
2936         struct rtw_adapter *padapter,
2937         struct packet_irp_hcicmd_data *pHciCmd
2938         )
2939 {
2940         enum hci_status                 status = HCI_STATUS_SUCCESS;
2941         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2942         u16                             PLH;
2943         u8      EntryNum, LinkQuality = 0x55;
2944
2945         PLH = *((u16 *)&pHciCmd->Data[0]);
2946         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("PLH = 0x%x\n", PLH));
2947
2948         EntryNum = bthci_GetCurrentEntryNum(padapter, (u8)PLH);
2949         if (EntryNum == 0xff) {
2950                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("No such PLH(0x%x)\n", PLH));
2951                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
2952         }
2953
2954         {
2955                 u8 localBuf[11] = "";
2956                 u8 *pRetPar;
2957                 u8 len = 0;
2958                 struct packet_irp_hcievent_data *PPacketIrpEvent;
2959
2960                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
2961
2962                 len += bthci_CommandCompleteHeader(&localBuf[0],
2963                         OGF_STATUS_PARAMETERS,
2964                         HCI_READ_LINK_QUALITY,
2965                         status);
2966
2967                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, (" PLH = 0x%x\n Link Quality = 0x%x\n", PLH, LinkQuality));
2968
2969                 /*  Return parameters starts from here */
2970                 pRetPar = &PPacketIrpEvent->Data[len];
2971                 pRetPar[0] = status;                    /* status */
2972                 *((u16 *)&pRetPar[1]) = pBTInfo->BtAsocEntry[EntryNum].PhyLinkCmdData.BtPhyLinkhandle;  /*  Handle */
2973                 pRetPar[3] = 0x55;      /* Link Quailty */
2974                 len += 4;
2975                 PPacketIrpEvent->Length = len;
2976
2977                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
2978         }
2979
2980         return status;
2981 }
2982
2983 static enum hci_status bthci_CmdReadRSSI(struct rtw_adapter *padapter)
2984 {
2985         return HCI_STATUS_SUCCESS;
2986 }
2987
2988 static enum hci_status
2989 bthci_CmdCreateLogicalLink(
2990         struct rtw_adapter *padapter,
2991         struct packet_irp_hcicmd_data *pHciCmd
2992         )
2993 {
2994         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
2995         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
2996
2997         pBtDbg->dbgHciInfo.hciCmdCntCreateLogLink++;
2998
2999         bthci_BuildLogicalLink(padapter, pHciCmd,
3000                 HCI_CREATE_LOGICAL_LINK);
3001
3002         return HCI_STATUS_SUCCESS;
3003 }
3004
3005 static enum hci_status
3006 bthci_CmdAcceptLogicalLink(
3007         struct rtw_adapter *padapter,
3008         struct packet_irp_hcicmd_data *pHciCmd
3009         )
3010 {
3011         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3012         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
3013
3014         pBtDbg->dbgHciInfo.hciCmdCntAcceptLogLink++;
3015
3016         bthci_BuildLogicalLink(padapter, pHciCmd,
3017                 HCI_ACCEPT_LOGICAL_LINK);
3018
3019         return HCI_STATUS_SUCCESS;
3020 }
3021
3022 static enum hci_status
3023 bthci_CmdDisconnectLogicalLink(
3024         struct rtw_adapter *padapter,
3025         struct packet_irp_hcicmd_data *pHciCmd
3026         )
3027 {
3028         enum hci_status status = HCI_STATUS_SUCCESS;
3029 /*PMGNT_INFO pMgntInfo = &padapter->MgntInfo; */
3030         struct bt_30info *pBTinfo = GET_BT_INFO(padapter);
3031         struct bt_mgnt *pBtMgnt = &pBTinfo->BtMgnt;
3032         struct bt_dgb *pBtDbg = &pBTinfo->BtDbg;
3033         u16     logicHandle;
3034         u8 i, j, find = 0, LogLinkCount = 0;
3035
3036         pBtDbg->dbgHciInfo.hciCmdCntDisconnectLogLink++;
3037
3038         logicHandle = *((u16 *)pHciCmd->Data);
3039         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("DisconnectLogicalLink, logicHandle = 0x%x\n", logicHandle));
3040
3041         /*  find an created logical link index and clear the data */
3042         for (j = 0; j < MAX_BT_ASOC_ENTRY_NUM; j++) {
3043                 for (i = 0; i < MAX_LOGICAL_LINK_NUM; i++) {
3044                         if (pBTinfo->BtAsocEntry[j].LogLinkCmdData[i].BtLogLinkhandle == logicHandle) {
3045                                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("DisconnectLogicalLink, logicHandle is matched  0x%x\n", logicHandle));
3046                                 bthci_ResetFlowSpec(padapter, j, i);
3047                                 find = 1;
3048                                 pBtMgnt->DisconnectEntryNum = j;
3049                                 break;
3050                         }
3051                 }
3052         }
3053
3054         if (!find)
3055                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
3056
3057         /*  To check each */
3058         for (i = 0; i < MAX_LOGICAL_LINK_NUM; i++) {
3059                 if (pBTinfo->BtAsocEntry[pBtMgnt->DisconnectEntryNum].LogLinkCmdData[i].BtLogLinkhandle != 0)
3060                         LogLinkCount++;
3061         }
3062
3063         /* When we receive Create logical link command, we should send command status event first. */
3064         bthci_EventCommandStatus(padapter,
3065                         LINK_CONTROL_COMMANDS,
3066                         HCI_DISCONNECT_LOGICAL_LINK,
3067                         status);
3068         /*  */
3069         /* When we determines the logical link is established, we should send command complete event. */
3070         /*  */
3071         if (status == HCI_STATUS_SUCCESS) {
3072                 bthci_EventDisconnectLogicalLinkComplete(padapter, status,
3073                         logicHandle, HCI_STATUS_CONNECT_TERMINATE_LOCAL_HOST);
3074         }
3075
3076         if (LogLinkCount == 0)
3077                 mod_timer(&pBTinfo->BTDisconnectPhyLinkTimer,
3078                           jiffies + msecs_to_jiffies(100));
3079
3080         return status;
3081 }
3082
3083 static enum hci_status
3084 bthci_CmdLogicalLinkCancel(struct rtw_adapter *padapter,
3085                            struct packet_irp_hcicmd_data *pHciCmd)
3086 {
3087         enum hci_status status = HCI_STATUS_SUCCESS;
3088         struct bt_30info *pBTinfo = GET_BT_INFO(padapter);
3089         struct bt_mgnt *pBtMgnt = &pBTinfo->BtMgnt;
3090         u8 CurrentEntryNum, CurrentLogEntryNum;
3091
3092         u8 physicalLinkHandle, TxFlowSpecID, i;
3093         u16     CurrentLogicalHandle;
3094
3095         physicalLinkHandle = *((u8 *)pHciCmd->Data);
3096         TxFlowSpecID = *(((u8 *)pHciCmd->Data)+1);
3097
3098         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("LogicalLinkCancel, physicalLinkHandle = 0x%x, TxFlowSpecID = 0x%x\n",
3099                 physicalLinkHandle, TxFlowSpecID));
3100
3101         CurrentEntryNum = pBtMgnt->CurrentConnectEntryNum;
3102         CurrentLogicalHandle = pBtMgnt->BtCurrentLogLinkhandle;
3103
3104         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("CurrentEntryNum = 0x%x, CurrentLogicalHandle = 0x%x\n",
3105                 CurrentEntryNum, CurrentLogicalHandle));
3106
3107         CurrentLogEntryNum = 0xff;
3108         for (i = 0; i < MAX_LOGICAL_LINK_NUM; i++) {
3109                 if ((CurrentLogicalHandle == pBTinfo->BtAsocEntry[CurrentEntryNum].LogLinkCmdData[i].BtLogLinkhandle) &&
3110                         (physicalLinkHandle == pBTinfo->BtAsocEntry[CurrentEntryNum].LogLinkCmdData[i].BtPhyLinkhandle)) {
3111                         CurrentLogEntryNum = i;
3112                         break;
3113                 }
3114         }
3115
3116         if (CurrentLogEntryNum == 0xff) {
3117                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("LogicalLinkCancel, CurrentLogEntryNum == 0xff !!!!\n"));
3118                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
3119                 return status;
3120         } else {
3121                 if (pBTinfo->BtAsocEntry[CurrentEntryNum].LogLinkCmdData[CurrentLogEntryNum].bLLCompleteEventIsSet) {
3122                         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("LogicalLinkCancel, LLCompleteEventIsSet!!!!\n"));
3123                         status = HCI_STATUS_ACL_CONNECT_EXISTS;
3124                 }
3125         }
3126
3127         {
3128                 u8 localBuf[8] = "";
3129                 u8 *pRetPar;
3130                 u8 len = 0;
3131                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3132
3133                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3134
3135                 len += bthci_CommandCompleteHeader(&localBuf[0],
3136                         LINK_CONTROL_COMMANDS,
3137                         HCI_LOGICAL_LINK_CANCEL,
3138                         status);
3139
3140                 /*  Return parameters starts from here */
3141                 pRetPar = &PPacketIrpEvent->Data[len];
3142                 pRetPar[0] = status;            /* status */
3143                 pRetPar[1] = pBTinfo->BtAsocEntry[CurrentEntryNum].LogLinkCmdData[CurrentLogEntryNum].BtPhyLinkhandle;
3144                 pRetPar[2] = pBTinfo->BtAsocEntry[CurrentEntryNum].LogLinkCmdData[CurrentLogEntryNum].BtTxFlowSpecID;
3145                 len += 3;
3146                 PPacketIrpEvent->Length = len;
3147
3148                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
3149         }
3150
3151         pBTinfo->BtAsocEntry[CurrentEntryNum].LogLinkCmdData[CurrentLogEntryNum].bLLCancelCMDIsSetandComplete = true;
3152
3153         return status;
3154 }
3155
3156 static enum hci_status
3157 bthci_CmdFlowSpecModify(struct rtw_adapter *padapter,
3158                         struct packet_irp_hcicmd_data *pHciCmd)
3159 {
3160         enum hci_status status = HCI_STATUS_SUCCESS;
3161 /*PMGNT_INFO pMgntInfo = &padapter->MgntInfo; */
3162         struct bt_30info *pBTinfo = GET_BT_INFO(padapter);
3163         u8 i, j, find = 0;
3164         u16 logicHandle;
3165
3166         logicHandle = *((u16 *)pHciCmd->Data);
3167         /*  find an matched logical link index and copy the data */
3168         for (j = 0; j < MAX_BT_ASOC_ENTRY_NUM; j++) {
3169                 for (i = 0; i < MAX_LOGICAL_LINK_NUM; i++) {
3170                         if (pBTinfo->BtAsocEntry[j].LogLinkCmdData[i].BtLogLinkhandle == logicHandle) {
3171                                 memcpy(&pBTinfo->BtAsocEntry[j].LogLinkCmdData[i].Tx_Flow_Spec,
3172                                         &pHciCmd->Data[2], sizeof(struct hci_flow_spec));
3173                                 memcpy(&pBTinfo->BtAsocEntry[j].LogLinkCmdData[i].Rx_Flow_Spec,
3174                                         &pHciCmd->Data[18], sizeof(struct hci_flow_spec));
3175
3176                                 bthci_CheckLogLinkBehavior(padapter, pBTinfo->BtAsocEntry[j].LogLinkCmdData[i].Tx_Flow_Spec);
3177                                 find = 1;
3178                                 break;
3179                         }
3180                 }
3181         }
3182         RTPRINT(FIOCTL, IOCTL_BT_LOGO, ("FlowSpecModify, LLH = 0x%x, \n", logicHandle));
3183
3184         /* When we receive Flow Spec Modify command, we should send command status event first. */
3185         bthci_EventCommandStatus(padapter,
3186                 LINK_CONTROL_COMMANDS,
3187                 HCI_FLOW_SPEC_MODIFY,
3188                 HCI_STATUS_SUCCESS);
3189
3190         if (!find)
3191                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
3192
3193         bthci_EventSendFlowSpecModifyComplete(padapter, status, logicHandle);
3194
3195         return status;
3196 }
3197
3198 static enum hci_status
3199 bthci_CmdAcceptPhysicalLink(struct rtw_adapter *padapter,
3200                             struct packet_irp_hcicmd_data *pHciCmd)
3201 {
3202         enum hci_status status;
3203         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3204         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
3205
3206         pBtDbg->dbgHciInfo.hciCmdCntAcceptPhyLink++;
3207
3208         status = bthci_BuildPhysicalLink(padapter,
3209                 pHciCmd, HCI_ACCEPT_PHYSICAL_LINK);
3210
3211         return status;
3212 }
3213
3214 static enum hci_status
3215 bthci_CmdDisconnectPhysicalLink(struct rtw_adapter *padapter,
3216                                 struct packet_irp_hcicmd_data *pHciCmd)
3217 {
3218         enum hci_status status = HCI_STATUS_SUCCESS;
3219         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3220         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
3221         u8 PLH, CurrentEntryNum, PhysLinkDisconnectReason;
3222
3223         pBtDbg->dbgHciInfo.hciCmdCntDisconnectPhyLink++;
3224
3225         PLH = *((u8 *)pHciCmd->Data);
3226         PhysLinkDisconnectReason = (*((u8 *)pHciCmd->Data+1));
3227         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_DISCONNECT_PHYSICAL_LINK  PhyHandle = 0x%x, Reason = 0x%x\n",
3228                 PLH, PhysLinkDisconnectReason));
3229
3230         CurrentEntryNum = bthci_GetCurrentEntryNum(padapter, PLH);
3231
3232         if (CurrentEntryNum == 0xff) {
3233                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD,
3234                         ("DisconnectPhysicalLink, No such Handle in the Entry\n"));
3235                 status = HCI_STATUS_UNKNOW_CONNECT_ID;
3236         } else {
3237                 pBTInfo->BtAsocEntry[CurrentEntryNum].PhyLinkDisconnectReason =
3238                         (enum hci_status)PhysLinkDisconnectReason;
3239         }
3240         /* Send HCI Command status event to AMP. */
3241         bthci_EventCommandStatus(padapter, LINK_CONTROL_COMMANDS,
3242                                  HCI_DISCONNECT_PHYSICAL_LINK, status);
3243
3244         if (status != HCI_STATUS_SUCCESS)
3245                 return status;
3246
3247         /* The macros below require { and } in the if statement */
3248         if (pBTInfo->BtAsocEntry[CurrentEntryNum].BtCurrentState == HCI_STATE_DISCONNECTED) {
3249                 BTHCI_SM_WITH_INFO(padapter, HCI_STATE_DISCONNECTED, STATE_CMD_DISCONNECT_PHY_LINK, CurrentEntryNum);
3250         } else {
3251                 BTHCI_SM_WITH_INFO(padapter, HCI_STATE_DISCONNECTING, STATE_CMD_DISCONNECT_PHY_LINK, CurrentEntryNum);
3252         }
3253         return status;
3254 }
3255
3256 static enum hci_status
3257 bthci_CmdSetACLLinkDataFlowMode(struct rtw_adapter *padapter,
3258                                 struct packet_irp_hcicmd_data *pHciCmd)
3259 {
3260         enum hci_status status = HCI_STATUS_SUCCESS;
3261         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3262         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
3263         u8 localBuf[8] = "";
3264         u8 *pRetPar;
3265         u8 len = 0;
3266         struct packet_irp_hcievent_data *PPacketIrpEvent;
3267         u16 *pu2Temp;
3268
3269         pBtMgnt->ExtConfig.CurrentConnectHandle = *((u16 *)pHciCmd->Data);
3270         pBtMgnt->ExtConfig.CurrentIncomingTrafficMode = *((u8 *)pHciCmd->Data)+2;
3271         pBtMgnt->ExtConfig.CurrentOutgoingTrafficMode = *((u8 *)pHciCmd->Data)+3;
3272         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("Connection Handle = 0x%x, Incoming Traffic mode = 0x%x, Outgoing Traffic mode = 0x%x",
3273                 pBtMgnt->ExtConfig.CurrentConnectHandle,
3274                 pBtMgnt->ExtConfig.CurrentIncomingTrafficMode,
3275                 pBtMgnt->ExtConfig.CurrentOutgoingTrafficMode));
3276
3277
3278         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3279
3280         len += bthci_CommandCompleteHeader(&localBuf[0],
3281                 OGF_EXTENSION,
3282                 HCI_SET_ACL_LINK_DATA_FLOW_MODE,
3283                 status);
3284
3285         /*  Return parameters starts from here */
3286         pRetPar = &PPacketIrpEvent->Data[len];
3287         pRetPar[0] = status;            /* status */
3288
3289         pu2Temp = (u16 *)&pRetPar[1];
3290         *pu2Temp = pBtMgnt->ExtConfig.CurrentConnectHandle;
3291         len += 3;
3292         PPacketIrpEvent->Length = len;
3293
3294         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
3295         return status;
3296 }
3297
3298 static enum hci_status
3299 bthci_CmdSetACLLinkStatus(struct rtw_adapter *padapter,
3300                           struct packet_irp_hcicmd_data *pHciCmd)
3301 {
3302         enum hci_status status = HCI_STATUS_SUCCESS;
3303         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3304         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
3305         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
3306         u8 i;
3307         u8 *pTriple;
3308
3309         pBtDbg->dbgHciInfo.hciCmdCntSetAclLinkStatus++;
3310         RTPRINT_DATA(FIOCTL, IOCTL_BT_HCICMD_EXT, "SetACLLinkStatus, Hex Data :\n",
3311                         &pHciCmd->Data[0], pHciCmd->Length);
3312
3313         /*  Only Core Stack v251 and later version support this command. */
3314         pBtMgnt->bSupportProfile = true;
3315
3316         pBtMgnt->ExtConfig.NumberOfHandle = *((u8 *)pHciCmd->Data);
3317         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("NumberOfHandle = 0x%x\n", pBtMgnt->ExtConfig.NumberOfHandle));
3318
3319         pTriple = &pHciCmd->Data[1];
3320         for (i = 0; i < pBtMgnt->ExtConfig.NumberOfHandle; i++) {
3321                 pBtMgnt->ExtConfig.linkInfo[i].ConnectHandle = *((u16 *)&pTriple[0]);
3322                 pBtMgnt->ExtConfig.linkInfo[i].IncomingTrafficMode = pTriple[2];
3323                 pBtMgnt->ExtConfig.linkInfo[i].OutgoingTrafficMode = pTriple[3];
3324                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT,
3325                         ("Connection_Handle = 0x%x, Incoming Traffic mode = 0x%x, Outgoing Traffic Mode = 0x%x\n",
3326                         pBtMgnt->ExtConfig.linkInfo[i].ConnectHandle,
3327                         pBtMgnt->ExtConfig.linkInfo[i].IncomingTrafficMode,
3328                         pBtMgnt->ExtConfig.linkInfo[i].OutgoingTrafficMode));
3329                 pTriple += 4;
3330         }
3331
3332         {
3333                 u8 localBuf[6] = "";
3334                 u8 *pRetPar;
3335                 u8 len = 0;
3336                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3337
3338                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3339
3340                 len += bthci_CommandCompleteHeader(&localBuf[0],
3341                         OGF_EXTENSION,
3342                         HCI_SET_ACL_LINK_STATUS,
3343                         status);
3344
3345                 /*  Return parameters starts from here */
3346                 pRetPar = &PPacketIrpEvent->Data[len];
3347                 pRetPar[0] = status;            /* status */
3348
3349                 len += 1;
3350                 PPacketIrpEvent->Length = len;
3351
3352                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
3353         }
3354
3355         return status;
3356 }
3357
3358 static enum hci_status
3359 bthci_CmdSetSCOLinkStatus(
3360         struct rtw_adapter *padapter,
3361         struct packet_irp_hcicmd_data *pHciCmd
3362         )
3363 {
3364         enum hci_status status = HCI_STATUS_SUCCESS;
3365         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3366         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
3367         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
3368
3369         pBtDbg->dbgHciInfo.hciCmdCntSetScoLinkStatus++;
3370         pBtMgnt->ExtConfig.NumberOfSCO = *((u8 *)pHciCmd->Data);
3371         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("NumberOfSCO = 0x%x\n",
3372                 pBtMgnt->ExtConfig.NumberOfSCO));
3373
3374         {
3375                 u8 localBuf[6] = "";
3376                 u8 *pRetPar;
3377                 u8 len = 0;
3378                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3379
3380                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3381
3382                 len += bthci_CommandCompleteHeader(&localBuf[0],
3383                         OGF_EXTENSION,
3384                         HCI_SET_SCO_LINK_STATUS,
3385                         status);
3386
3387                 /*  Return parameters starts from here */
3388                 pRetPar = &PPacketIrpEvent->Data[len];
3389                 pRetPar[0] = status;            /* status */
3390
3391                 len += 1;
3392                 PPacketIrpEvent->Length = len;
3393
3394                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
3395         }
3396
3397         return status;
3398 }
3399
3400 static enum hci_status
3401 bthci_CmdSetRSSIValue(
3402         struct rtw_adapter *padapter,
3403         struct packet_irp_hcicmd_data *pHciCmd
3404         )
3405 {
3406         enum hci_status status = HCI_STATUS_SUCCESS;
3407         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3408         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
3409         s8              min_bt_rssi = 0;
3410         u8 i;
3411         for (i = 0; i < pBtMgnt->ExtConfig.NumberOfHandle; i++) {
3412                 if (pBtMgnt->ExtConfig.linkInfo[i].ConnectHandle == *((u16 *)&pHciCmd->Data[0])) {
3413                         pBtMgnt->ExtConfig.linkInfo[i].BT_RSSI = (s8)(pHciCmd->Data[2]);
3414                         RTPRINT(FIOCTL, IOCTL_BT_EVENT_PERIODICAL,
3415                         ("Connection_Handle = 0x%x, RSSI = %d \n",
3416                         pBtMgnt->ExtConfig.linkInfo[i].ConnectHandle,
3417                         pBtMgnt->ExtConfig.linkInfo[i].BT_RSSI));
3418                 }
3419                 /*  get the minimum bt rssi value */
3420                 if (pBtMgnt->ExtConfig.linkInfo[i].BT_RSSI <= min_bt_rssi)
3421                         min_bt_rssi = pBtMgnt->ExtConfig.linkInfo[i].BT_RSSI;
3422         }
3423
3424         pBtMgnt->ExtConfig.MIN_BT_RSSI = min_bt_rssi;
3425         RTPRINT(FBT, BT_TRACE, ("[bt rssi], the min rssi is %d\n", min_bt_rssi));
3426
3427         {
3428                 u8 localBuf[6] = "";
3429                 u8 *pRetPar;
3430                 u8 len = 0;
3431                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3432
3433                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3434
3435                 len += bthci_CommandCompleteHeader(&localBuf[0],
3436                         OGF_EXTENSION,
3437                         HCI_SET_RSSI_VALUE,
3438                         status);
3439
3440                 /*  Return parameters starts from here */
3441                 pRetPar = &PPacketIrpEvent->Data[len];
3442                 pRetPar[0] = status;            /* status */
3443
3444                 len += 1;
3445                 PPacketIrpEvent->Length = len;
3446
3447                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
3448         }
3449
3450         return status;
3451 }
3452
3453 static enum hci_status
3454 bthci_CmdSetCurrentBluetoothStatus(
3455         struct rtw_adapter *padapter,
3456         struct packet_irp_hcicmd_data *pHciCmd
3457         )
3458 {
3459         enum hci_status status = HCI_STATUS_SUCCESS;
3460 /*PMGNT_INFO    pMgntInfo = &padapter->MgntInfo; */
3461         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3462         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
3463
3464         pBtMgnt->ExtConfig.CurrentBTStatus = *((u8 *)&pHciCmd->Data[0]);
3465         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("SetCurrentBluetoothStatus, CurrentBTStatus = 0x%x\n",
3466                 pBtMgnt->ExtConfig.CurrentBTStatus));
3467
3468         {
3469                 u8 localBuf[6] = "";
3470                 u8 *pRetPar;
3471                 u8 len = 0;
3472                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3473
3474                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3475
3476                 len += bthci_CommandCompleteHeader(&localBuf[0],
3477                         OGF_EXTENSION,
3478                         HCI_SET_CURRENT_BLUETOOTH_STATUS,
3479                         status);
3480
3481                 /*  Return parameters starts from here */
3482                 pRetPar = &PPacketIrpEvent->Data[len];
3483                 pRetPar[0] = status;            /* status */
3484                 len += 1;
3485
3486                 PPacketIrpEvent->Length = len;
3487
3488                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
3489         }
3490
3491         return status;
3492 }
3493
3494 static enum hci_status
3495 bthci_CmdExtensionVersionNotify(
3496         struct rtw_adapter *padapter,
3497         struct packet_irp_hcicmd_data *pHciCmd
3498         )
3499 {
3500         enum hci_status status = HCI_STATUS_SUCCESS;
3501         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3502         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
3503         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
3504
3505         pBtDbg->dbgHciInfo.hciCmdCntExtensionVersionNotify++;
3506         RTPRINT_DATA(FIOCTL, IOCTL_BT_HCICMD_EXT, "ExtensionVersionNotify, Hex Data :\n",
3507                         &pHciCmd->Data[0], pHciCmd->Length);
3508
3509         pBtMgnt->ExtConfig.HCIExtensionVer = *((u16 *)&pHciCmd->Data[0]);
3510         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("HCIExtensionVer = 0x%x\n", pBtMgnt->ExtConfig.HCIExtensionVer));
3511
3512         {
3513                 u8 localBuf[6] = "";
3514                 u8 *pRetPar;
3515                 u8 len = 0;
3516                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3517
3518                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3519
3520                 len += bthci_CommandCompleteHeader(&localBuf[0],
3521                         OGF_EXTENSION,
3522                         HCI_EXTENSION_VERSION_NOTIFY,
3523                         status);
3524
3525                 /*  Return parameters starts from here */
3526                 pRetPar = &PPacketIrpEvent->Data[len];
3527                 pRetPar[0] = status;            /* status */
3528
3529                 len += 1;
3530                 PPacketIrpEvent->Length = len;
3531
3532                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
3533         }
3534
3535         return status;
3536 }
3537
3538 static enum hci_status
3539 bthci_CmdLinkStatusNotify(
3540         struct rtw_adapter *padapter,
3541         struct packet_irp_hcicmd_data *pHciCmd
3542         )
3543 {
3544         enum hci_status status = HCI_STATUS_SUCCESS;
3545         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3546         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
3547         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
3548         u8 i;
3549         u8 *pTriple;
3550
3551         pBtDbg->dbgHciInfo.hciCmdCntLinkStatusNotify++;
3552         RTPRINT_DATA(FIOCTL, IOCTL_BT_HCICMD_EXT, "LinkStatusNotify, Hex Data :\n",
3553                         &pHciCmd->Data[0], pHciCmd->Length);
3554
3555         /*  Current only RTL8723 support this command. */
3556         pBtMgnt->bSupportProfile = true;
3557
3558         pBtMgnt->ExtConfig.NumberOfHandle = *((u8 *)pHciCmd->Data);
3559         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("NumberOfHandle = 0x%x\n", pBtMgnt->ExtConfig.NumberOfHandle));
3560         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("HCIExtensionVer = %d\n", pBtMgnt->ExtConfig.HCIExtensionVer));
3561
3562         pTriple = &pHciCmd->Data[1];
3563         for (i = 0; i < pBtMgnt->ExtConfig.NumberOfHandle; i++) {
3564                 if (pBtMgnt->ExtConfig.HCIExtensionVer < 1) {
3565                         pBtMgnt->ExtConfig.linkInfo[i].ConnectHandle = *((u16 *)&pTriple[0]);
3566                         pBtMgnt->ExtConfig.linkInfo[i].BTProfile = pTriple[2];
3567                         pBtMgnt->ExtConfig.linkInfo[i].BTCoreSpec = pTriple[3];
3568                         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT,
3569                                 ("Connection_Handle = 0x%x, BTProfile =%d, BTSpec =%d\n",
3570                                 pBtMgnt->ExtConfig.linkInfo[i].ConnectHandle,
3571                                 pBtMgnt->ExtConfig.linkInfo[i].BTProfile,
3572                                 pBtMgnt->ExtConfig.linkInfo[i].BTCoreSpec));
3573                         pTriple += 4;
3574                 } else if (pBtMgnt->ExtConfig.HCIExtensionVer >= 1) {
3575                         pBtMgnt->ExtConfig.linkInfo[i].ConnectHandle = *((u16 *)&pTriple[0]);
3576                         pBtMgnt->ExtConfig.linkInfo[i].BTProfile = pTriple[2];
3577                         pBtMgnt->ExtConfig.linkInfo[i].BTCoreSpec = pTriple[3];
3578                         pBtMgnt->ExtConfig.linkInfo[i].linkRole = pTriple[4];
3579                         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT,
3580                                 ("Connection_Handle = 0x%x, BTProfile =%d, BTSpec =%d, LinkRole =%d\n",
3581                                 pBtMgnt->ExtConfig.linkInfo[i].ConnectHandle,
3582                                 pBtMgnt->ExtConfig.linkInfo[i].BTProfile,
3583                                 pBtMgnt->ExtConfig.linkInfo[i].BTCoreSpec,
3584                                 pBtMgnt->ExtConfig.linkInfo[i].linkRole));
3585                         pTriple += 5;
3586                 }
3587
3588         }
3589         BTHCI_UpdateBTProfileRTKToMoto(padapter);
3590         {
3591                 u8 localBuf[6] = "";
3592                 u8 *pRetPar;
3593                 u8 len = 0;
3594                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3595
3596                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3597
3598                 len += bthci_CommandCompleteHeader(&localBuf[0],
3599                         OGF_EXTENSION,
3600                         HCI_LINK_STATUS_NOTIFY,
3601                         status);
3602
3603                 /*  Return parameters starts from here */
3604                 pRetPar = &PPacketIrpEvent->Data[len];
3605                 pRetPar[0] = status;            /* status */
3606
3607                 len += 1;
3608                 PPacketIrpEvent->Length = len;
3609
3610                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
3611         }
3612
3613         return status;
3614 }
3615
3616 static enum hci_status
3617 bthci_CmdBtOperationNotify(
3618         struct rtw_adapter *padapter,
3619         struct packet_irp_hcicmd_data *pHciCmd
3620         )
3621 {
3622         enum hci_status status = HCI_STATUS_SUCCESS;
3623         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3624         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
3625
3626         RTPRINT_DATA(FIOCTL, IOCTL_BT_HCICMD_EXT, "Bt Operation notify, Hex Data :\n",
3627                         &pHciCmd->Data[0], pHciCmd->Length);
3628
3629         pBtMgnt->ExtConfig.btOperationCode = *((u8 *)pHciCmd->Data);
3630         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("btOperationCode = 0x%x\n", pBtMgnt->ExtConfig.btOperationCode));
3631         switch (pBtMgnt->ExtConfig.btOperationCode) {
3632         case HCI_BT_OP_NONE:
3633                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("[bt operation] : Operation None!!\n"));
3634                 break;
3635         case HCI_BT_OP_INQUIRY_START:
3636                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("[bt operation] : Inquire start!!\n"));
3637                 break;
3638         case HCI_BT_OP_INQUIRY_FINISH:
3639                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("[bt operation] : Inquire finished!!\n"));
3640                 break;
3641         case HCI_BT_OP_PAGING_START:
3642                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("[bt operation] : Paging is started!!\n"));
3643                 break;
3644         case HCI_BT_OP_PAGING_SUCCESS:
3645                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("[bt operation] : Paging complete successfully!!\n"));
3646                 break;
3647         case HCI_BT_OP_PAGING_UNSUCCESS:
3648                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("[bt operation] : Paging complete unsuccessfully!!\n"));
3649                 break;
3650         case HCI_BT_OP_PAIRING_START:
3651                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("[bt operation] : Pairing start!!\n"));
3652                 break;
3653         case HCI_BT_OP_PAIRING_FINISH:
3654                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("[bt operation] : Pairing finished!!\n"));
3655                 break;
3656         case HCI_BT_OP_BT_DEV_ENABLE:
3657                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("[bt operation] : BT Device is enabled!!\n"));
3658                 break;
3659         case HCI_BT_OP_BT_DEV_DISABLE:
3660                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("[bt operation] : BT Device is disabled!!\n"));
3661                 break;
3662         default:
3663                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("[bt operation] : Unknown, error!!\n"));
3664                 break;
3665         }
3666         BTDM_AdjustForBtOperation(padapter);
3667         {
3668                 u8 localBuf[6] = "";
3669                 u8 *pRetPar;
3670                 u8 len = 0;
3671                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3672
3673                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3674
3675                 len += bthci_CommandCompleteHeader(&localBuf[0],
3676                         OGF_EXTENSION,
3677                         HCI_BT_OPERATION_NOTIFY,
3678                         status);
3679
3680                 /*  Return parameters starts from here */
3681                 pRetPar = &PPacketIrpEvent->Data[len];
3682                 pRetPar[0] = status;            /* status */
3683
3684                 len += 1;
3685                 PPacketIrpEvent->Length = len;
3686
3687                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
3688         }
3689
3690         return status;
3691 }
3692
3693 static enum hci_status
3694 bthci_CmdEnableWifiScanNotify(struct rtw_adapter *padapter,
3695                               struct packet_irp_hcicmd_data *pHciCmd)
3696 {
3697         enum hci_status status = HCI_STATUS_SUCCESS;
3698         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3699         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
3700
3701         RTPRINT_DATA(FIOCTL, IOCTL_BT_HCICMD_EXT, "Enable Wifi scan notify, Hex Data :\n",
3702                         &pHciCmd->Data[0], pHciCmd->Length);
3703
3704         pBtMgnt->ExtConfig.bEnableWifiScanNotify = *((u8 *)pHciCmd->Data);
3705         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("bEnableWifiScanNotify = %d\n", pBtMgnt->ExtConfig.bEnableWifiScanNotify));
3706
3707         {
3708                 u8 localBuf[6] = "";
3709                 u8 *pRetPar;
3710                 u8 len = 0;
3711                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3712
3713                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3714
3715                 len += bthci_CommandCompleteHeader(&localBuf[0],
3716                         OGF_EXTENSION,
3717                         HCI_ENABLE_WIFI_SCAN_NOTIFY,
3718                         status);
3719
3720                 /*  Return parameters starts from here */
3721                 pRetPar = &PPacketIrpEvent->Data[len];
3722                 pRetPar[0] = status;            /* status */
3723
3724                 len += 1;
3725                 PPacketIrpEvent->Length = len;
3726
3727                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
3728         }
3729
3730         return status;
3731 }
3732
3733 static enum hci_status
3734 bthci_CmdWIFICurrentChannel(struct rtw_adapter *padapter,
3735                             struct packet_irp_hcicmd_data *pHciCmd)
3736 {
3737         enum hci_status status = HCI_STATUS_SUCCESS;
3738         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
3739         u8 chnl = pmlmeext->cur_channel;
3740
3741         if (pmlmeext->cur_bwmode == HT_CHANNEL_WIDTH_40) {
3742                 if (pmlmeext->cur_ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER)
3743                         chnl += 2;
3744                 else if (pmlmeext->cur_ch_offset == HAL_PRIME_CHNL_OFFSET_LOWER)
3745                         chnl -= 2;
3746         }
3747
3748         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("Current Channel  = 0x%x\n", chnl));
3749
3750         {
3751                 u8 localBuf[8] = "";
3752                 u8 *pRetPar;
3753                 u8 len = 0;
3754                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3755
3756                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3757
3758                 len += bthci_CommandCompleteHeader(&localBuf[0],
3759                         OGF_EXTENSION,
3760                         HCI_WIFI_CURRENT_CHANNEL,
3761                         status);
3762
3763                 /*  Return parameters starts from here */
3764                 pRetPar = &PPacketIrpEvent->Data[len];
3765                 pRetPar[0] = status;            /* status */
3766                 pRetPar[1] = chnl;                      /* current channel */
3767                 len += 2;
3768                 PPacketIrpEvent->Length = len;
3769
3770                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
3771         }
3772
3773         return status;
3774 }
3775
3776 static enum hci_status
3777 bthci_CmdWIFICurrentBandwidth(struct rtw_adapter *padapter,
3778                               struct packet_irp_hcicmd_data *pHciCmd)
3779 {
3780         enum hci_status status = HCI_STATUS_SUCCESS;
3781         enum ht_channel_width bw;
3782         u8 CurrentBW = 0;
3783
3784         bw = padapter->mlmeextpriv.cur_bwmode;
3785
3786         if (bw == HT_CHANNEL_WIDTH_20)
3787                 CurrentBW = 0;
3788         else if (bw == HT_CHANNEL_WIDTH_40)
3789                 CurrentBW = 1;
3790
3791         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("Current BW = 0x%x\n",
3792                 CurrentBW));
3793
3794         {
3795                 u8 localBuf[8] = "";
3796                 u8 *pRetPar;
3797                 u8 len = 0;
3798                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3799
3800                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3801
3802                 len += bthci_CommandCompleteHeader(&localBuf[0],
3803                         OGF_EXTENSION,
3804                         HCI_WIFI_CURRENT_BANDWIDTH,
3805                         status);
3806
3807                 /*  Return parameters starts from here */
3808                 pRetPar = &PPacketIrpEvent->Data[len];
3809                 pRetPar[0] = status;            /* status */
3810                 pRetPar[1] = CurrentBW;         /* current BW */
3811                 len += 2;
3812                 PPacketIrpEvent->Length = len;
3813
3814                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
3815         }
3816
3817         return status;
3818 }
3819
3820 static enum hci_status
3821 bthci_CmdWIFIConnectionStatus(
3822         struct rtw_adapter *padapter,
3823         struct packet_irp_hcicmd_data *pHciCmd
3824         )
3825 {
3826         enum hci_status status = HCI_STATUS_SUCCESS;
3827         u8 connectStatus = HCI_WIFI_NOT_CONNECTED;
3828
3829         if (check_fwstate(&padapter->mlmepriv, WIFI_AP_STATE)) {
3830                 if (padapter->stapriv.asoc_sta_count >= 3)
3831                         connectStatus = HCI_WIFI_CONNECTED;
3832                 else
3833                         connectStatus = HCI_WIFI_NOT_CONNECTED;
3834         } else if (check_fwstate(&padapter->mlmepriv, WIFI_ADHOC_STATE|WIFI_ADHOC_MASTER_STATE|WIFI_ASOC_STATE)) {
3835                 connectStatus = HCI_WIFI_CONNECTED;
3836         } else if (check_fwstate(&padapter->mlmepriv, WIFI_UNDER_LINKING)) {
3837                 connectStatus = HCI_WIFI_CONNECT_IN_PROGRESS;
3838         } else {
3839                 connectStatus = HCI_WIFI_NOT_CONNECTED;
3840         }
3841
3842         {
3843                 u8 localBuf[8] = "";
3844                 u8 *pRetPar;
3845                 u8 len = 0;
3846                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3847
3848                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3849
3850                 len += bthci_CommandCompleteHeader(&localBuf[0],
3851                         OGF_EXTENSION,
3852                         HCI_WIFI_CONNECTION_STATUS,
3853                         status);
3854
3855                 /*  Return parameters starts from here */
3856                 pRetPar = &PPacketIrpEvent->Data[len];
3857                 pRetPar[0] = status;                    /* status */
3858                 pRetPar[1] = connectStatus;     /* connect status */
3859                 len += 2;
3860                 PPacketIrpEvent->Length = len;
3861
3862                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
3863         }
3864
3865         return status;
3866 }
3867
3868 static enum hci_status
3869 bthci_CmdEnableDeviceUnderTestMode(
3870         struct rtw_adapter *padapter,
3871         struct packet_irp_hcicmd_data *pHciCmd
3872         )
3873 {
3874         enum hci_status status = HCI_STATUS_SUCCESS;
3875         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3876         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
3877
3878         pBtHciInfo->bInTestMode = true;
3879         pBtHciInfo->bTestIsEnd = false;
3880
3881         /* send command complete event here when all data are received. */
3882         {
3883                 u8 localBuf[6] = "";
3884                 u8 *pRetPar;
3885                 u8 len = 0;
3886                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3887
3888                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3889
3890                 len += bthci_CommandCompleteHeader(&localBuf[0],
3891                         OGF_TESTING_COMMANDS,
3892                         HCI_ENABLE_DEVICE_UNDER_TEST_MODE,
3893                         status);
3894
3895                 /*  Return parameters starts from here */
3896                 pRetPar = &PPacketIrpEvent->Data[len];
3897                 pRetPar[0] = status;            /* status */
3898                 len += 1;
3899                 PPacketIrpEvent->Length = len;
3900
3901                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
3902         }
3903
3904         return status;
3905 }
3906
3907 static enum hci_status
3908 bthci_CmdAMPTestEnd(struct rtw_adapter *padapter,
3909                     struct packet_irp_hcicmd_data *pHciCmd)
3910 {
3911         enum hci_status status = HCI_STATUS_SUCCESS;
3912         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3913         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
3914
3915         if (!pBtHciInfo->bInTestMode) {
3916                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("Not in Test mode, return status = HCI_STATUS_CMD_DISALLOW\n"));
3917                 status = HCI_STATUS_CMD_DISALLOW;
3918                 return status;
3919         }
3920
3921         pBtHciInfo->bTestIsEnd = true;
3922
3923         del_timer_sync(&pBTInfo->BTTestSendPacketTimer);
3924
3925         rtl8723a_check_bssid(padapter, true);
3926
3927         /* send command complete event here when all data are received. */
3928         {
3929                 u8 localBuf[4] = "";
3930                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3931
3932                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("AMP Test End Event \n"));
3933                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3934                 PPacketIrpEvent->EventCode = HCI_EVENT_AMP_TEST_END;
3935                 PPacketIrpEvent->Length = 2;
3936
3937                 PPacketIrpEvent->Data[0] = status;
3938                 PPacketIrpEvent->Data[1] = pBtHciInfo->TestScenario;
3939
3940                 bthci_IndicateEvent(padapter, PPacketIrpEvent, 4);
3941         }
3942
3943         bthci_EventAMPReceiverReport(padapter, 0x01);
3944
3945         return status;
3946 }
3947
3948 static enum hci_status
3949 bthci_CmdAMPTestCommand(struct rtw_adapter *padapter,
3950                         struct packet_irp_hcicmd_data *pHciCmd)
3951 {
3952         enum hci_status status = HCI_STATUS_SUCCESS;
3953         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
3954         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
3955
3956         if (!pBtHciInfo->bInTestMode) {
3957                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("Not in Test mode, return status = HCI_STATUS_CMD_DISALLOW\n"));
3958                 status = HCI_STATUS_CMD_DISALLOW;
3959                 return status;
3960         }
3961
3962         pBtHciInfo->TestScenario = *((u8 *)pHciCmd->Data);
3963
3964         if (pBtHciInfo->TestScenario == 0x01)
3965                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("TX Single Test \n"));
3966         else if (pBtHciInfo->TestScenario == 0x02)
3967                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("Receive Frame Test \n"));
3968         else
3969                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("No Such Test !!!!!!!!!!!!!!!!!! \n"));
3970
3971         if (pBtHciInfo->bTestIsEnd) {
3972                 u8 localBuf[5] = "";
3973                 struct packet_irp_hcievent_data *PPacketIrpEvent;
3974
3975                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("AMP Test End Event \n"));
3976                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
3977                 PPacketIrpEvent->EventCode = HCI_EVENT_AMP_TEST_END;
3978                 PPacketIrpEvent->Length = 2;
3979
3980                 PPacketIrpEvent->Data[0] = status;
3981                 PPacketIrpEvent->Data[1] = pBtHciInfo->TestScenario ;
3982
3983                 bthci_IndicateEvent(padapter, PPacketIrpEvent, 4);
3984
3985                 /* Return to Idel state with RX and TX off. */
3986
3987                 return status;
3988         }
3989
3990         /*  should send command status event */
3991         bthci_EventCommandStatus(padapter,
3992                         OGF_TESTING_COMMANDS,
3993                         HCI_AMP_TEST_COMMAND,
3994                         status);
3995
3996         /* The HCI_AMP_Start Test Event shall be generated when the */
3997         /* HCI_AMP_Test_Command has completed and the first data is ready to be sent */
3998         /* or received. */
3999
4000         {
4001                 u8 localBuf[5] = "";
4002                 struct packet_irp_hcievent_data *PPacketIrpEvent;
4003
4004                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), (" HCI_AMP_Start Test Event \n"));
4005                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
4006                 PPacketIrpEvent->EventCode = HCI_EVENT_AMP_START_TEST;
4007                 PPacketIrpEvent->Length = 2;
4008
4009                 PPacketIrpEvent->Data[0] = status;
4010                 PPacketIrpEvent->Data[1] = pBtHciInfo->TestScenario ;
4011
4012                 bthci_IndicateEvent(padapter, PPacketIrpEvent, 4);
4013
4014                 /* Return to Idel state with RX and TX off. */
4015         }
4016
4017         if (pBtHciInfo->TestScenario == 0x01) {
4018                 /*
4019                         When in a transmitter test scenario and the frames/bursts count have been
4020                         transmitted the HCI_AMP_Test_End event shall be sent.
4021                 */
4022                 mod_timer(&pBTInfo->BTTestSendPacketTimer,
4023                           jiffies + msecs_to_jiffies(50));
4024                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("TX Single Test \n"));
4025         } else if (pBtHciInfo->TestScenario == 0x02) {
4026                 rtl8723a_check_bssid(padapter, false);
4027                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_BT_LOGO), ("Receive Frame Test \n"));
4028         }
4029
4030         return status;
4031 }
4032
4033 static enum hci_status
4034 bthci_CmdEnableAMPReceiverReports(struct rtw_adapter *padapter,
4035                                   struct packet_irp_hcicmd_data *pHciCmd)
4036 {
4037         enum hci_status status = HCI_STATUS_SUCCESS;
4038         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
4039         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
4040
4041         if (!pBtHciInfo->bInTestMode) {
4042                 status = HCI_STATUS_CMD_DISALLOW;
4043                 /* send command complete event here when all data are received. */
4044                 {
4045                         u8 localBuf[6] = "";
4046                         u8 *pRetPar;
4047                         u8 len = 0;
4048                         struct packet_irp_hcievent_data *PPacketIrpEvent;
4049
4050                         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
4051
4052                         len += bthci_CommandCompleteHeader(&localBuf[0],
4053                                 OGF_TESTING_COMMANDS,
4054                                 HCI_ENABLE_AMP_RECEIVER_REPORTS,
4055                                 status);
4056
4057                         /*  Return parameters starts from here */
4058                         pRetPar = &PPacketIrpEvent->Data[len];
4059                         pRetPar[0] = status;            /* status */
4060                         len += 1;
4061                         PPacketIrpEvent->Length = len;
4062
4063                         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
4064                 }
4065                 return status;
4066         }
4067
4068         pBtHciInfo->bTestNeedReport = *((u8 *)pHciCmd->Data);
4069         pBtHciInfo->TestReportInterval = (*((u8 *)pHciCmd->Data+2));
4070
4071         bthci_EventAMPReceiverReport(padapter, 0x00);
4072
4073         /* send command complete event here when all data are received. */
4074         {
4075                 u8 localBuf[6] = "";
4076                 u8 *pRetPar;
4077                 u8 len = 0;
4078                 struct packet_irp_hcievent_data *PPacketIrpEvent;
4079
4080                 PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
4081
4082                 len += bthci_CommandCompleteHeader(&localBuf[0],
4083                         OGF_TESTING_COMMANDS,
4084                         HCI_ENABLE_AMP_RECEIVER_REPORTS,
4085                         status);
4086
4087                 /*  Return parameters starts from here */
4088                 pRetPar = &PPacketIrpEvent->Data[len];
4089                 pRetPar[0] = status;            /* status */
4090                 len += 1;
4091                 PPacketIrpEvent->Length = len;
4092
4093                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
4094         }
4095
4096         return status;
4097 }
4098
4099 static enum hci_status
4100 bthci_CmdHostBufferSize(struct rtw_adapter *padapter,
4101                         struct packet_irp_hcicmd_data *pHciCmd)
4102 {
4103         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
4104         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
4105         struct packet_irp_hcievent_data *PPacketIrpEvent;
4106         enum hci_status status = HCI_STATUS_SUCCESS;
4107         u8 localBuf[6] = "";
4108         u8 *pRetPar;
4109         u8 len = 0;
4110
4111         pBTInfo->BtAsocEntry[pBtMgnt->CurrentConnectEntryNum].ACLPacketsData.ACLDataPacketLen = *((u16 *)pHciCmd->Data);
4112         pBTInfo->BtAsocEntry[pBtMgnt->CurrentConnectEntryNum].SyncDataPacketLen = *((u8 *)(pHciCmd->Data+2));
4113         pBTInfo->BtAsocEntry[pBtMgnt->CurrentConnectEntryNum].TotalNumACLDataPackets = *((u16 *)(pHciCmd->Data+3));
4114         pBTInfo->BtAsocEntry[pBtMgnt->CurrentConnectEntryNum].TotalSyncNumDataPackets = *((u16 *)(pHciCmd->Data+5));
4115
4116         /* send command complete event here when all data are received. */
4117         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
4118
4119         len += bthci_CommandCompleteHeader(&localBuf[0],
4120                 OGF_SET_EVENT_MASK_COMMAND,
4121                 HCI_HOST_BUFFER_SIZE,
4122                 status);
4123
4124         /*  Return parameters starts from here */
4125         pRetPar = &PPacketIrpEvent->Data[len];
4126         pRetPar[0] = status;            /* status */
4127         len += 1;
4128         PPacketIrpEvent->Length = len;
4129
4130         bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
4131
4132         return status;
4133 }
4134
4135 static enum hci_status
4136 bthci_CmdHostNumberOfCompletedPackets(struct rtw_adapter *padapter,
4137                                       struct packet_irp_hcicmd_data *pHciCmd)
4138 {
4139         return HCI_STATUS_SUCCESS;
4140 }
4141
4142 static enum hci_status
4143 bthci_UnknownCMD(struct rtw_adapter *padapter, struct packet_irp_hcicmd_data *pHciCmd)
4144 {
4145         enum hci_status status = HCI_STATUS_UNKNOW_HCI_CMD;
4146         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
4147         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
4148
4149         pBtDbg->dbgHciInfo.hciCmdCntUnknown++;
4150         bthci_EventCommandStatus(padapter,
4151                         (u8)pHciCmd->OGF,
4152                         pHciCmd->OCF,
4153                         status);
4154
4155         return status;
4156 }
4157
4158 static enum hci_status
4159 bthci_HandleOGFInformationalParameters(struct rtw_adapter *padapter,
4160                                        struct packet_irp_hcicmd_data *pHciCmd)
4161 {
4162         enum hci_status status = HCI_STATUS_SUCCESS;
4163
4164         switch (pHciCmd->OCF) {
4165         case HCI_READ_LOCAL_VERSION_INFORMATION:
4166                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_LOCAL_VERSION_INFORMATION\n"));
4167                 status = bthci_CmdReadLocalVersionInformation(padapter);
4168                 break;
4169         case HCI_READ_LOCAL_SUPPORTED_COMMANDS:
4170                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_LOCAL_SUPPORTED_COMMANDS\n"));
4171                 status = bthci_CmdReadLocalSupportedCommands(padapter);
4172                 break;
4173         case HCI_READ_LOCAL_SUPPORTED_FEATURES:
4174                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_LOCAL_SUPPORTED_FEATURES\n"));
4175                 status = bthci_CmdReadLocalSupportedFeatures(padapter);
4176                 break;
4177         case HCI_READ_BUFFER_SIZE:
4178                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_BUFFER_SIZE\n"));
4179                 status = bthci_CmdReadBufferSize(padapter);
4180                 break;
4181         case HCI_READ_DATA_BLOCK_SIZE:
4182                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_DATA_BLOCK_SIZE\n"));
4183                 status = bthci_CmdReadDataBlockSize(padapter);
4184                 break;
4185         default:
4186                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("bthci_HandleOGFInformationalParameters(), Unknown case = 0x%x\n", pHciCmd->OCF));
4187                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_UNKNOWN_COMMAND\n"));
4188                 status = bthci_UnknownCMD(padapter, pHciCmd);
4189                 break;
4190         }
4191         return status;
4192 }
4193
4194 static enum hci_status
4195 bthci_HandleOGFSetEventMaskCMD(struct rtw_adapter *padapter,
4196                                struct packet_irp_hcicmd_data *pHciCmd)
4197 {
4198         enum hci_status status = HCI_STATUS_SUCCESS;
4199
4200         switch (pHciCmd->OCF) {
4201         case HCI_SET_EVENT_MASK:
4202                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_SET_EVENT_MASK\n"));
4203                 status = bthci_CmdSetEventMask(padapter, pHciCmd);
4204                 break;
4205         case HCI_RESET:
4206                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_RESET\n"));
4207                 status = bthci_CmdReset(padapter, true);
4208                 break;
4209         case HCI_READ_CONNECTION_ACCEPT_TIMEOUT:
4210                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_CONNECTION_ACCEPT_TIMEOUT\n"));
4211                 status = bthci_CmdReadConnectionAcceptTimeout(padapter);
4212                 break;
4213         case HCI_SET_EVENT_FILTER:
4214                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_SET_EVENT_FILTER\n"));
4215                 status = bthci_CmdSetEventFilter(padapter, pHciCmd);
4216                 break;
4217         case HCI_WRITE_CONNECTION_ACCEPT_TIMEOUT:
4218                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_WRITE_CONNECTION_ACCEPT_TIMEOUT\n"));
4219                 status = bthci_CmdWriteConnectionAcceptTimeout(padapter, pHciCmd);
4220                 break;
4221         case HCI_READ_PAGE_TIMEOUT:
4222                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_PAGE_TIMEOUT\n"));
4223                 status = bthci_CmdReadPageTimeout(padapter, pHciCmd);
4224                 break;
4225         case HCI_WRITE_PAGE_TIMEOUT:
4226                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_WRITE_PAGE_TIMEOUT\n"));
4227                 status = bthci_CmdWritePageTimeout(padapter, pHciCmd);
4228                 break;
4229         case HCI_HOST_NUMBER_OF_COMPLETED_PACKETS:
4230                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_HOST_NUMBER_OF_COMPLETED_PACKETS\n"));
4231                 status = bthci_CmdHostNumberOfCompletedPackets(padapter, pHciCmd);
4232                 break;
4233         case HCI_READ_LINK_SUPERVISION_TIMEOUT:
4234                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_LINK_SUPERVISION_TIMEOUT\n"));
4235                 status = bthci_CmdReadLinkSupervisionTimeout(padapter, pHciCmd);
4236                 break;
4237         case HCI_WRITE_LINK_SUPERVISION_TIMEOUT:
4238                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_WRITE_LINK_SUPERVISION_TIMEOUT\n"));
4239                 status = bthci_CmdWriteLinkSupervisionTimeout(padapter, pHciCmd);
4240                 break;
4241         case HCI_ENHANCED_FLUSH:
4242                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_ENHANCED_FLUSH\n"));
4243                 status = bthci_CmdEnhancedFlush(padapter, pHciCmd);
4244                 break;
4245         case HCI_READ_LOGICAL_LINK_ACCEPT_TIMEOUT:
4246                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_LOGICAL_LINK_ACCEPT_TIMEOUT\n"));
4247                 status = bthci_CmdReadLogicalLinkAcceptTimeout(padapter, pHciCmd);
4248                 break;
4249         case HCI_WRITE_LOGICAL_LINK_ACCEPT_TIMEOUT:
4250                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_WRITE_LOGICAL_LINK_ACCEPT_TIMEOUT\n"));
4251                 status = bthci_CmdWriteLogicalLinkAcceptTimeout(padapter, pHciCmd);
4252                 break;
4253         case HCI_SET_EVENT_MASK_PAGE_2:
4254                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_SET_EVENT_MASK_PAGE_2\n"));
4255                 status = bthci_CmdSetEventMaskPage2(padapter, pHciCmd);
4256                 break;
4257         case HCI_READ_LOCATION_DATA:
4258                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_LOCATION_DATA\n"));
4259                 status = bthci_CmdReadLocationData(padapter, pHciCmd);
4260                 break;
4261         case HCI_WRITE_LOCATION_DATA:
4262                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_WRITE_LOCATION_DATA\n"));
4263                 status = bthci_CmdWriteLocationData(padapter, pHciCmd);
4264                 break;
4265         case HCI_READ_FLOW_CONTROL_MODE:
4266                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_FLOW_CONTROL_MODE\n"));
4267                 status = bthci_CmdReadFlowControlMode(padapter, pHciCmd);
4268                 break;
4269         case HCI_WRITE_FLOW_CONTROL_MODE:
4270                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_WRITE_FLOW_CONTROL_MODE\n"));
4271                 status = bthci_CmdWriteFlowControlMode(padapter, pHciCmd);
4272                 break;
4273         case HCI_READ_BEST_EFFORT_FLUSH_TIMEOUT:
4274                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_BEST_EFFORT_FLUSH_TIMEOUT\n"));
4275                 status = bthci_CmdReadBestEffortFlushTimeout(padapter, pHciCmd);
4276                 break;
4277         case HCI_WRITE_BEST_EFFORT_FLUSH_TIMEOUT:
4278                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_WRITE_BEST_EFFORT_FLUSH_TIMEOUT\n"));
4279                 status = bthci_CmdWriteBestEffortFlushTimeout(padapter, pHciCmd);
4280                 break;
4281         case HCI_SHORT_RANGE_MODE:
4282                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_SHORT_RANGE_MODE\n"));
4283                 status = bthci_CmdShortRangeMode(padapter, pHciCmd);
4284                 break;
4285         case HCI_HOST_BUFFER_SIZE:
4286                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_HOST_BUFFER_SIZE\n"));
4287                 status = bthci_CmdHostBufferSize(padapter, pHciCmd);
4288                 break;
4289         default:
4290                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("bthci_HandleOGFSetEventMaskCMD(), Unknown case = 0x%x\n", pHciCmd->OCF));
4291                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_UNKNOWN_COMMAND\n"));
4292                 status = bthci_UnknownCMD(padapter, pHciCmd);
4293                 break;
4294         }
4295         return status;
4296 }
4297
4298 static enum hci_status
4299 bthci_HandleOGFStatusParameters(struct rtw_adapter *padapter,
4300                                 struct packet_irp_hcicmd_data *pHciCmd)
4301 {
4302         enum hci_status status = HCI_STATUS_SUCCESS;
4303
4304         switch (pHciCmd->OCF) {
4305         case HCI_READ_FAILED_CONTACT_COUNTER:
4306                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_FAILED_CONTACT_COUNTER\n"));
4307                 status = bthci_CmdReadFailedContactCounter(padapter, pHciCmd);
4308                 break;
4309         case HCI_RESET_FAILED_CONTACT_COUNTER:
4310                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_RESET_FAILED_CONTACT_COUNTER\n"));
4311                 status = bthci_CmdResetFailedContactCounter(padapter, pHciCmd);
4312                 break;
4313         case HCI_READ_LINK_QUALITY:
4314                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_LINK_QUALITY\n"));
4315                 status = bthci_CmdReadLinkQuality(padapter, pHciCmd);
4316                 break;
4317         case HCI_READ_RSSI:
4318                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_RSSI\n"));
4319                 status = bthci_CmdReadRSSI(padapter);
4320                 break;
4321         case HCI_READ_LOCAL_AMP_INFO:
4322                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_LOCAL_AMP_INFO\n"));
4323                 status = bthci_CmdReadLocalAMPInfo(padapter);
4324                 break;
4325         case HCI_READ_LOCAL_AMP_ASSOC:
4326                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_READ_LOCAL_AMP_ASSOC\n"));
4327                 status = bthci_CmdReadLocalAMPAssoc(padapter, pHciCmd);
4328                 break;
4329         case HCI_WRITE_REMOTE_AMP_ASSOC:
4330                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_WRITE_REMOTE_AMP_ASSOC\n"));
4331                 status = bthci_CmdWriteRemoteAMPAssoc(padapter, pHciCmd);
4332                 break;
4333         default:
4334                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("bthci_HandleOGFStatusParameters(), Unknown case = 0x%x\n", pHciCmd->OCF));
4335                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_UNKNOWN_COMMAND\n"));
4336                 status = bthci_UnknownCMD(padapter, pHciCmd);
4337                 break;
4338         }
4339         return status;
4340 }
4341
4342 static enum hci_status
4343 bthci_HandleOGFLinkControlCMD(struct rtw_adapter *padapter,
4344                               struct packet_irp_hcicmd_data *pHciCmd)
4345 {
4346         enum hci_status status = HCI_STATUS_SUCCESS;
4347
4348         switch (pHciCmd->OCF) {
4349         case HCI_CREATE_PHYSICAL_LINK:
4350                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_CREATE_PHYSICAL_LINK\n"));
4351                 status = bthci_CmdCreatePhysicalLink(padapter, pHciCmd);
4352                 break;
4353         case HCI_ACCEPT_PHYSICAL_LINK:
4354                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_ACCEPT_PHYSICAL_LINK\n"));
4355                 status = bthci_CmdAcceptPhysicalLink(padapter, pHciCmd);
4356                 break;
4357         case HCI_DISCONNECT_PHYSICAL_LINK:
4358                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_DISCONNECT_PHYSICAL_LINK\n"));
4359                 status = bthci_CmdDisconnectPhysicalLink(padapter, pHciCmd);
4360                 break;
4361         case HCI_CREATE_LOGICAL_LINK:
4362                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_CREATE_LOGICAL_LINK\n"));
4363                 status = bthci_CmdCreateLogicalLink(padapter, pHciCmd);
4364                 break;
4365         case HCI_ACCEPT_LOGICAL_LINK:
4366                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_ACCEPT_LOGICAL_LINK\n"));
4367                 status = bthci_CmdAcceptLogicalLink(padapter, pHciCmd);
4368                 break;
4369         case HCI_DISCONNECT_LOGICAL_LINK:
4370                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_DISCONNECT_LOGICAL_LINK\n"));
4371                 status = bthci_CmdDisconnectLogicalLink(padapter, pHciCmd);
4372                 break;
4373         case HCI_LOGICAL_LINK_CANCEL:
4374                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_LOGICAL_LINK_CANCEL\n"));
4375                 status = bthci_CmdLogicalLinkCancel(padapter, pHciCmd);
4376                 break;
4377         case HCI_FLOW_SPEC_MODIFY:
4378                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_FLOW_SPEC_MODIFY\n"));
4379                 status = bthci_CmdFlowSpecModify(padapter, pHciCmd);
4380                 break;
4381         default:
4382                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("bthci_HandleOGFLinkControlCMD(), Unknown case = 0x%x\n", pHciCmd->OCF));
4383                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_UNKNOWN_COMMAND\n"));
4384                 status = bthci_UnknownCMD(padapter, pHciCmd);
4385                 break;
4386         }
4387         return status;
4388 }
4389
4390 static enum hci_status
4391 bthci_HandleOGFTestingCMD(struct rtw_adapter *padapter,
4392                           struct packet_irp_hcicmd_data *pHciCmd)
4393 {
4394         enum hci_status status = HCI_STATUS_SUCCESS;
4395         switch (pHciCmd->OCF) {
4396         case HCI_ENABLE_DEVICE_UNDER_TEST_MODE:
4397                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_ENABLE_DEVICE_UNDER_TEST_MODE\n"));
4398                 bthci_CmdEnableDeviceUnderTestMode(padapter, pHciCmd);
4399                 break;
4400         case HCI_AMP_TEST_END:
4401                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_AMP_TEST_END\n"));
4402                 bthci_CmdAMPTestEnd(padapter, pHciCmd);
4403                 break;
4404         case HCI_AMP_TEST_COMMAND:
4405                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_AMP_TEST_COMMAND\n"));
4406                 bthci_CmdAMPTestCommand(padapter, pHciCmd);
4407                 break;
4408         case HCI_ENABLE_AMP_RECEIVER_REPORTS:
4409                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_ENABLE_AMP_RECEIVER_REPORTS\n"));
4410                 bthci_CmdEnableAMPReceiverReports(padapter, pHciCmd);
4411                 break;
4412         default:
4413                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_UNKNOWN_COMMAND\n"));
4414                 status = bthci_UnknownCMD(padapter, pHciCmd);
4415                 break;
4416         }
4417         return status;
4418 }
4419
4420 static enum hci_status
4421 bthci_HandleOGFExtension(struct rtw_adapter *padapter,
4422                          struct packet_irp_hcicmd_data *pHciCmd)
4423 {
4424         enum hci_status status = HCI_STATUS_SUCCESS;
4425         switch (pHciCmd->OCF) {
4426         case HCI_SET_ACL_LINK_DATA_FLOW_MODE:
4427                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("HCI_SET_ACL_LINK_DATA_FLOW_MODE\n"));
4428                 status = bthci_CmdSetACLLinkDataFlowMode(padapter, pHciCmd);
4429                 break;
4430         case HCI_SET_ACL_LINK_STATUS:
4431                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("HCI_SET_ACL_LINK_STATUS\n"));
4432                 status = bthci_CmdSetACLLinkStatus(padapter, pHciCmd);
4433                 break;
4434         case HCI_SET_SCO_LINK_STATUS:
4435                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("HCI_SET_SCO_LINK_STATUS\n"));
4436                 status = bthci_CmdSetSCOLinkStatus(padapter, pHciCmd);
4437                 break;
4438         case HCI_SET_RSSI_VALUE:
4439                 RTPRINT(FIOCTL, IOCTL_BT_EVENT_PERIODICAL, ("HCI_SET_RSSI_VALUE\n"));
4440                 status = bthci_CmdSetRSSIValue(padapter, pHciCmd);
4441                 break;
4442         case HCI_SET_CURRENT_BLUETOOTH_STATUS:
4443                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("HCI_SET_CURRENT_BLUETOOTH_STATUS\n"));
4444                 status = bthci_CmdSetCurrentBluetoothStatus(padapter, pHciCmd);
4445                 break;
4446         /* The following is for RTK8723 */
4447
4448         case HCI_EXTENSION_VERSION_NOTIFY:
4449                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("HCI_EXTENSION_VERSION_NOTIFY\n"));
4450                 status = bthci_CmdExtensionVersionNotify(padapter, pHciCmd);
4451                 break;
4452         case HCI_LINK_STATUS_NOTIFY:
4453                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("HCI_LINK_STATUS_NOTIFY\n"));
4454                 status = bthci_CmdLinkStatusNotify(padapter, pHciCmd);
4455                 break;
4456         case HCI_BT_OPERATION_NOTIFY:
4457                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("HCI_BT_OPERATION_NOTIFY\n"));
4458                 status = bthci_CmdBtOperationNotify(padapter, pHciCmd);
4459                 break;
4460         case HCI_ENABLE_WIFI_SCAN_NOTIFY:
4461                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("HCI_ENABLE_WIFI_SCAN_NOTIFY\n"));
4462                 status = bthci_CmdEnableWifiScanNotify(padapter, pHciCmd);
4463                 break;
4464
4465         /* The following is for IVT */
4466         case HCI_WIFI_CURRENT_CHANNEL:
4467                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("HCI_WIFI_CURRENT_CHANNEL\n"));
4468                 status = bthci_CmdWIFICurrentChannel(padapter, pHciCmd);
4469                 break;
4470         case HCI_WIFI_CURRENT_BANDWIDTH:
4471                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("HCI_WIFI_CURRENT_BANDWIDTH\n"));
4472                 status = bthci_CmdWIFICurrentBandwidth(padapter, pHciCmd);
4473                 break;
4474         case HCI_WIFI_CONNECTION_STATUS:
4475                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("HCI_WIFI_CONNECTION_STATUS\n"));
4476                 status = bthci_CmdWIFIConnectionStatus(padapter, pHciCmd);
4477                 break;
4478
4479         default:
4480                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("HCI_UNKNOWN_COMMAND\n"));
4481                 status = bthci_UnknownCMD(padapter, pHciCmd);
4482                 break;
4483         }
4484         return status;
4485 }
4486
4487 static void
4488 bthci_StateStarting(struct rtw_adapter *padapter,
4489                     enum hci_state_with_cmd StateCmd, u8 EntryNum)
4490 {
4491         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
4492         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
4493
4494         RTPRINT(FIOCTL, IOCTL_STATE, ("[BT state], [Starting], "));
4495         switch (StateCmd) {
4496         case STATE_CMD_CONNECT_ACCEPT_TIMEOUT:
4497                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_CONNECT_ACCEPT_TIMEOUT\n"));
4498                 pBTInfo->BtAsocEntry[EntryNum].PhysLinkCompleteStatus = HCI_STATUS_CONNECT_ACCEPT_TIMEOUT;
4499                 pBtMgnt->bNeedNotifyAMPNoCap = true;
4500                 BTHCI_DisconnectPeer(padapter, EntryNum);
4501                 break;
4502         case STATE_CMD_DISCONNECT_PHY_LINK:
4503                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_DISCONNECT_PHY_LINK\n"));
4504
4505                 bthci_EventDisconnectPhyLinkComplete(padapter,
4506                 HCI_STATUS_SUCCESS,
4507                 pBTInfo->BtAsocEntry[EntryNum].PhyLinkDisconnectReason,
4508                 EntryNum);
4509
4510                 del_timer_sync(&pBTInfo->BTHCIJoinTimeoutTimer);
4511
4512                 pBTInfo->BtAsocEntry[EntryNum].PhysLinkCompleteStatus = HCI_STATUS_UNKNOW_CONNECT_ID;
4513
4514                 BTHCI_DisconnectPeer(padapter, EntryNum);
4515                 break;
4516         case STATE_CMD_MAC_START_COMPLETE:
4517                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_MAC_START_COMPLETE\n"));
4518                 if (pBTInfo->BtAsocEntry[EntryNum].AMPRole == AMP_BTAP_CREATOR)
4519                         bthci_EventChannelSelected(padapter, EntryNum);
4520                 break;
4521         default:
4522                 RTPRINT(FIOCTL, IOCTL_STATE, ("State command(%d) is Wrong !!!\n", StateCmd));
4523                 break;
4524         }
4525 }
4526
4527 static void
4528 bthci_StateConnecting(struct rtw_adapter *padapter,
4529                       enum hci_state_with_cmd StateCmd, u8 EntryNum)
4530 {
4531         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
4532         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
4533
4534         RTPRINT(FIOCTL, IOCTL_STATE, ("[BT state], [Connecting], "));
4535         switch (StateCmd) {
4536         case STATE_CMD_CONNECT_ACCEPT_TIMEOUT:
4537                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_CONNECT_ACCEPT_TIMEOUT\n"));
4538                 pBTInfo->BtAsocEntry[EntryNum].PhysLinkCompleteStatus = HCI_STATUS_CONNECT_ACCEPT_TIMEOUT;
4539                 pBtMgnt->bNeedNotifyAMPNoCap = true;
4540                 BTHCI_DisconnectPeer(padapter, EntryNum);
4541                 break;
4542         case STATE_CMD_MAC_CONNECT_COMPLETE:
4543                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_MAC_CONNECT_COMPLETE\n"));
4544
4545                 if (pBTInfo->BtAsocEntry[EntryNum].AMPRole == AMP_BTAP_JOINER) {
4546                         RT_TRACE(_module_rtl871x_security_c_,
4547                                  _drv_info_, ("StateConnecting \n"));
4548                 }
4549                 break;
4550         case STATE_CMD_DISCONNECT_PHY_LINK:
4551                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_DISCONNECT_PHY_LINK\n"));
4552
4553                 bthci_EventDisconnectPhyLinkComplete(padapter,
4554                 HCI_STATUS_SUCCESS,
4555                 pBTInfo->BtAsocEntry[EntryNum].PhyLinkDisconnectReason,
4556                 EntryNum);
4557
4558                 pBTInfo->BtAsocEntry[EntryNum].PhysLinkCompleteStatus = HCI_STATUS_UNKNOW_CONNECT_ID;
4559
4560                 del_timer_sync(&pBTInfo->BTHCIJoinTimeoutTimer);
4561
4562                 BTHCI_DisconnectPeer(padapter, EntryNum);
4563
4564                 break;
4565         case STATE_CMD_MAC_CONNECT_CANCEL_INDICATE:
4566                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_MAC_CONNECT_CANCEL_INDICATE\n"));
4567                 pBTInfo->BtAsocEntry[EntryNum].PhysLinkCompleteStatus = HCI_STATUS_CONTROLLER_BUSY;
4568                 /*  Because this state cmd is caused by the BTHCI_EventAMPStatusChange(), */
4569                 /*  we don't need to send event in the following BTHCI_DisconnectPeer() again. */
4570                 pBtMgnt->bNeedNotifyAMPNoCap = false;
4571                 BTHCI_DisconnectPeer(padapter, EntryNum);
4572                 break;
4573         default:
4574                 RTPRINT(FIOCTL, IOCTL_STATE, ("State command(%d) is Wrong !!!\n", StateCmd));
4575                 break;
4576         }
4577 }
4578
4579 static void
4580 bthci_StateConnected(struct rtw_adapter *padapter,
4581                      enum hci_state_with_cmd StateCmd, u8 EntryNum)
4582 {
4583 /*PMGNT_INFO pMgntInfo = &padapter->MgntInfo; */
4584         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
4585         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
4586         u8 i;
4587         u16 logicHandle = 0;
4588
4589         RTPRINT(FIOCTL, IOCTL_STATE, ("[BT state], [Connected], "));
4590         switch (StateCmd) {
4591         case STATE_CMD_DISCONNECT_PHY_LINK:
4592                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_DISCONNECT_PHY_LINK\n"));
4593
4594                 /* When we are trying to disconnect the phy link, we should disconnect log link first, */
4595                 for (i = 0; i < MAX_LOGICAL_LINK_NUM; i++) {
4596                         if (pBTInfo->BtAsocEntry[EntryNum].LogLinkCmdData->BtLogLinkhandle != 0) {
4597                                 logicHandle = pBTInfo->BtAsocEntry[EntryNum].LogLinkCmdData->BtLogLinkhandle;
4598
4599                                 bthci_EventDisconnectLogicalLinkComplete(padapter, HCI_STATUS_SUCCESS,
4600                                         logicHandle, pBTInfo->BtAsocEntry[EntryNum].PhyLinkDisconnectReason);
4601
4602                                 pBTInfo->BtAsocEntry[EntryNum].LogLinkCmdData->BtLogLinkhandle = 0;
4603                         }
4604                 }
4605
4606                 bthci_EventDisconnectPhyLinkComplete(padapter,
4607                 HCI_STATUS_SUCCESS,
4608                 pBTInfo->BtAsocEntry[EntryNum].PhyLinkDisconnectReason,
4609                 EntryNum);
4610
4611                 del_timer_sync(&pBTInfo->BTHCIJoinTimeoutTimer);
4612
4613                 BTHCI_DisconnectPeer(padapter, EntryNum);
4614                 break;
4615
4616         case STATE_CMD_MAC_DISCONNECT_INDICATE:
4617                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_MAC_DISCONNECT_INDICATE\n"));
4618
4619                 bthci_EventDisconnectPhyLinkComplete(padapter,
4620                 HCI_STATUS_SUCCESS,
4621                 /*  TODO: Remote Host not local host */
4622                 HCI_STATUS_CONNECT_TERMINATE_LOCAL_HOST,
4623                 EntryNum);
4624                 BTHCI_DisconnectPeer(padapter, EntryNum);
4625
4626                 break;
4627         case STATE_CMD_ENTER_STATE:
4628                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_ENTER_STATE\n"));
4629
4630                 if (pBtMgnt->bBTConnectInProgress) {
4631                         pBtMgnt->bBTConnectInProgress = false;
4632                         RTPRINT(FIOCTL, IOCTL_STATE, ("[BT Flag], BT Connect in progress OFF!!\n"));
4633                 }
4634                 pBTInfo->BtAsocEntry[EntryNum].BtCurrentState = HCI_STATE_CONNECTED;
4635                 pBTInfo->BtAsocEntry[EntryNum].b4waySuccess = true;
4636                 pBtMgnt->bStartSendSupervisionPkt = true;
4637
4638                 /*  for rate adaptive */
4639
4640                 rtl8723a_update_ramask(padapter,
4641                                        MAX_FW_SUPPORT_MACID_NUM-1-EntryNum, 0);
4642
4643                 HalSetBrateCfg23a(padapter, padapter->mlmepriv.cur_network.network.SupportedRates);
4644                 BTDM_SetFwChnlInfo(padapter, RT_MEDIA_CONNECT);
4645                 break;
4646         default:
4647                 RTPRINT(FIOCTL, IOCTL_STATE, ("State command(%d) is Wrong !!!\n", StateCmd));
4648                 break;
4649         }
4650 }
4651
4652 static void
4653 bthci_StateAuth(struct rtw_adapter *padapter, enum hci_state_with_cmd StateCmd,
4654                 u8 EntryNum)
4655 {
4656         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
4657         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
4658
4659         RTPRINT(FIOCTL, IOCTL_STATE, ("[BT state], [Authenticating], "));
4660         switch (StateCmd) {
4661         case STATE_CMD_CONNECT_ACCEPT_TIMEOUT:
4662                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_CONNECT_ACCEPT_TIMEOUT\n"));
4663                 pBTInfo->BtAsocEntry[EntryNum].PhysLinkCompleteStatus = HCI_STATUS_CONNECT_ACCEPT_TIMEOUT;
4664                 pBtMgnt->bNeedNotifyAMPNoCap = true;
4665                 BTHCI_DisconnectPeer(padapter, EntryNum);
4666                 break;
4667         case STATE_CMD_DISCONNECT_PHY_LINK:
4668                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_DISCONNECT_PHY_LINK\n"));
4669                 bthci_EventDisconnectPhyLinkComplete(padapter,
4670                 HCI_STATUS_SUCCESS,
4671                 pBTInfo->BtAsocEntry[EntryNum].PhyLinkDisconnectReason,
4672                 EntryNum);
4673
4674                 pBTInfo->BtAsocEntry[EntryNum].PhysLinkCompleteStatus = HCI_STATUS_UNKNOW_CONNECT_ID;
4675
4676                 del_timer_sync(&pBTInfo->BTHCIJoinTimeoutTimer);
4677
4678                 BTHCI_DisconnectPeer(padapter, EntryNum);
4679                 break;
4680         case STATE_CMD_4WAY_FAILED:
4681                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_4WAY_FAILED\n"));
4682
4683                 pBTInfo->BtAsocEntry[EntryNum].PhysLinkCompleteStatus = HCI_STATUS_AUTH_FAIL;
4684                 pBtMgnt->bNeedNotifyAMPNoCap = true;
4685
4686                 BTHCI_DisconnectPeer(padapter, EntryNum);
4687
4688                 del_timer_sync(&pBTInfo->BTHCIJoinTimeoutTimer);
4689                 break;
4690         case STATE_CMD_4WAY_SUCCESSED:
4691                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_4WAY_SUCCESSED\n"));
4692
4693                 bthci_EventPhysicalLinkComplete(padapter, HCI_STATUS_SUCCESS, EntryNum, INVALID_PL_HANDLE);
4694
4695                 del_timer_sync(&pBTInfo->BTHCIJoinTimeoutTimer);
4696
4697                 BTHCI_SM_WITH_INFO(padapter, HCI_STATE_CONNECTED, STATE_CMD_ENTER_STATE, EntryNum);
4698                 break;
4699         default:
4700                 RTPRINT(FIOCTL, IOCTL_STATE, ("State command(%d) is Wrong !!!\n", StateCmd));
4701                 break;
4702         }
4703 }
4704
4705 static void
4706 bthci_StateDisconnecting(struct rtw_adapter *padapter,
4707                          enum hci_state_with_cmd StateCmd, u8 EntryNum)
4708 {
4709         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
4710         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
4711
4712         RTPRINT(FIOCTL, IOCTL_STATE, ("[BT state], [Disconnecting], "));
4713         switch (StateCmd) {
4714         case STATE_CMD_MAC_CONNECT_CANCEL_INDICATE:
4715                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_MAC_CONNECT_CANCEL_INDICATE\n"));
4716                 if (pBTInfo->BtAsocEntry[EntryNum].bNeedPhysLinkCompleteEvent) {
4717                         bthci_EventPhysicalLinkComplete(padapter,
4718                                 pBTInfo->BtAsocEntry[EntryNum].PhysLinkCompleteStatus,
4719                                 EntryNum, INVALID_PL_HANDLE);
4720                 }
4721
4722                 if (pBtMgnt->bBTConnectInProgress) {
4723                         pBtMgnt->bBTConnectInProgress = false;
4724                         RTPRINT(FIOCTL, IOCTL_STATE, ("[BT Flag], BT Connect in progress OFF!!\n"));
4725                 }
4726
4727                 BTHCI_SM_WITH_INFO(padapter, HCI_STATE_DISCONNECTED, STATE_CMD_ENTER_STATE, EntryNum);
4728                 break;
4729         case STATE_CMD_DISCONNECT_PHY_LINK:
4730                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_DISCONNECT_PHY_LINK\n"));
4731
4732                 bthci_EventDisconnectPhyLinkComplete(padapter,
4733                 HCI_STATUS_SUCCESS,
4734                 pBTInfo->BtAsocEntry[EntryNum].PhyLinkDisconnectReason,
4735                 EntryNum);
4736
4737                 del_timer_sync(&pBTInfo->BTHCIJoinTimeoutTimer);
4738
4739                 BTHCI_DisconnectPeer(padapter, EntryNum);
4740                 break;
4741         default:
4742                 RTPRINT(FIOCTL, IOCTL_STATE, ("State command(%d) is Wrong !!!\n", StateCmd));
4743                 break;
4744         }
4745 }
4746
4747 static void
4748 bthci_StateDisconnected(struct rtw_adapter *padapter,
4749                         enum hci_state_with_cmd StateCmd, u8 EntryNum)
4750 {
4751 /*PMGNT_INFO pMgntInfo = &padapter->MgntInfo; */
4752         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
4753         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
4754         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
4755
4756         RTPRINT(FIOCTL, IOCTL_STATE, ("[BT state], [Disconnected], "));
4757         switch (StateCmd) {
4758         case STATE_CMD_CREATE_PHY_LINK:
4759         case STATE_CMD_ACCEPT_PHY_LINK:
4760                 if (StateCmd == STATE_CMD_CREATE_PHY_LINK)
4761                         RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_CREATE_PHY_LINK\n"));
4762                 else
4763                         RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_ACCEPT_PHY_LINK\n"));
4764
4765                 RTPRINT(FIOCTL, IOCTL_STATE, ("[BT PS], Disable IPS and LPS\n"));
4766                 ips_leave23a(padapter);
4767                 LPS_Leave23a(padapter);
4768
4769                 pBtMgnt->bPhyLinkInProgress = true;
4770                 pBtMgnt->BTCurrentConnectType = BT_DISCONNECT;
4771                 pBtMgnt->CurrentBTConnectionCnt++;
4772                 RTPRINT(FIOCTL, IOCTL_STATE, ("[BT Flag], CurrentBTConnectionCnt = %d\n",
4773                         pBtMgnt->CurrentBTConnectionCnt));
4774                 pBtMgnt->BtOperationOn = true;
4775                 RTPRINT(FIOCTL, IOCTL_STATE, ("[BT Flag], Bt Operation ON!! CurrentConnectEntryNum = %d\n",
4776                         pBtMgnt->CurrentConnectEntryNum));
4777
4778                 if (pBtMgnt->bBTConnectInProgress) {
4779                         bthci_EventPhysicalLinkComplete(padapter, HCI_STATUS_CONTROLLER_BUSY, INVALID_ENTRY_NUM, pBtMgnt->BtCurrentPhyLinkhandle);
4780                         bthci_RemoveEntryByEntryNum(padapter, EntryNum);
4781                         return;
4782                 }
4783
4784                 if (StateCmd == STATE_CMD_CREATE_PHY_LINK)
4785                         pBTInfo->BtAsocEntry[EntryNum].AMPRole = AMP_BTAP_CREATOR;
4786                 else
4787                         pBTInfo->BtAsocEntry[EntryNum].AMPRole = AMP_BTAP_JOINER;
4788
4789                 /*  1. MAC not yet in selected channel */
4790                 while (check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE|WIFI_SITE_MONITOR)) {
4791                         RTPRINT(FIOCTL, IOCTL_STATE, ("Scan/Roaming/Wifi Link is in Progress, wait 200 ms\n"));
4792                         mdelay(200);
4793                 }
4794                 /*  2. MAC already in selected channel */
4795                 RTPRINT(FIOCTL, IOCTL_STATE, ("Channel is Ready\n"));
4796                 mod_timer(&pBTInfo->BTHCIJoinTimeoutTimer,
4797                           jiffies + msecs_to_jiffies(pBtHciInfo->ConnAcceptTimeout));
4798
4799                 pBTInfo->BtAsocEntry[EntryNum].bNeedPhysLinkCompleteEvent = true;
4800                 break;
4801         case STATE_CMD_DISCONNECT_PHY_LINK:
4802                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_DISCONNECT_PHY_LINK\n"));
4803
4804                 del_timer_sync(&pBTInfo->BTHCIJoinTimeoutTimer);
4805
4806                 bthci_EventDisconnectPhyLinkComplete(padapter,
4807                 HCI_STATUS_SUCCESS,
4808                 pBTInfo->BtAsocEntry[EntryNum].PhyLinkDisconnectReason,
4809                 EntryNum);
4810
4811                 if (pBTInfo->BtAsocEntry[EntryNum].bNeedPhysLinkCompleteEvent) {
4812                         bthci_EventPhysicalLinkComplete(padapter,
4813                                 HCI_STATUS_UNKNOW_CONNECT_ID,
4814                                 EntryNum, INVALID_PL_HANDLE);
4815                 }
4816
4817                 if (pBtMgnt->bBTConnectInProgress) {
4818                         pBtMgnt->bBTConnectInProgress = false;
4819                         RTPRINT(FIOCTL, IOCTL_STATE, ("[BT Flag], BT Connect in progress OFF!!\n"));
4820                 }
4821                 BTHCI_SM_WITH_INFO(padapter, HCI_STATE_DISCONNECTED, STATE_CMD_ENTER_STATE, EntryNum);
4822                 bthci_RemoveEntryByEntryNum(padapter, EntryNum);
4823                 break;
4824         case STATE_CMD_ENTER_STATE:
4825                 RTPRINT(FIOCTL, IOCTL_STATE, ("STATE_CMD_ENTER_STATE\n"));
4826                 break;
4827         default:
4828                 RTPRINT(FIOCTL, IOCTL_STATE, ("State command(%d) is Wrong !!!\n", StateCmd));
4829                 break;
4830         }
4831 }
4832
4833 void BTHCI_EventParse(struct rtw_adapter *padapter, void *pEvntData, u32 dataLen)
4834 {
4835 }
4836
4837 u8 BTHCI_HsConnectionEstablished(struct rtw_adapter *padapter)
4838 {
4839         u8 bBtConnectionExist = false;
4840         struct bt_30info *pBtinfo = GET_BT_INFO(padapter);
4841         u8 i;
4842
4843         for (i = 0; i < MAX_BT_ASOC_ENTRY_NUM; i++) {
4844                 if (pBtinfo->BtAsocEntry[i].b4waySuccess) {
4845                         bBtConnectionExist = true;
4846                         break;
4847                 }
4848         }
4849
4850 /*RTPRINT(FIOCTL, IOCTL_STATE, (" BTHCI_HsConnectionEstablished(), connection exist = %d\n", bBtConnectionExist)); */
4851
4852         return bBtConnectionExist;
4853 }
4854
4855 static u8
4856 BTHCI_CheckProfileExist(struct rtw_adapter *padapter,
4857                         enum bt_traffic_mode_profile Profile)
4858 {
4859         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
4860         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
4861         u8 IsPRofile = false;
4862         u8 i = 0;
4863
4864         for (i = 0; i < pBtMgnt->ExtConfig.NumberOfHandle; i++) {
4865                 if (pBtMgnt->ExtConfig.linkInfo[i].TrafficProfile == Profile) {
4866                         IsPRofile = true;
4867                         break;
4868                 }
4869         }
4870
4871         return IsPRofile;
4872 }
4873
4874 void BTHCI_UpdateBTProfileRTKToMoto(struct rtw_adapter *padapter)
4875 {
4876         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
4877         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
4878         u8 i = 0;
4879
4880         pBtMgnt->ExtConfig.NumberOfSCO = 0;
4881
4882         for (i = 0; i < pBtMgnt->ExtConfig.NumberOfHandle; i++) {
4883                 pBtMgnt->ExtConfig.linkInfo[i].TrafficProfile = BT_PROFILE_NONE;
4884
4885                 if (pBtMgnt->ExtConfig.linkInfo[i].BTProfile == BT_PROFILE_SCO)
4886                         pBtMgnt->ExtConfig.NumberOfSCO++;
4887
4888                 pBtMgnt->ExtConfig.linkInfo[i].TrafficProfile = pBtMgnt->ExtConfig.linkInfo[i].BTProfile;
4889                 switch (pBtMgnt->ExtConfig.linkInfo[i].TrafficProfile) {
4890                 case BT_PROFILE_SCO:
4891                         break;
4892                 case BT_PROFILE_PAN:
4893                         pBtMgnt->ExtConfig.linkInfo[i].IncomingTrafficMode = BT_MOTOR_EXT_BE;
4894                         pBtMgnt->ExtConfig.linkInfo[i].OutgoingTrafficMode = BT_MOTOR_EXT_BE;
4895                         break;
4896                 case BT_PROFILE_A2DP:
4897                         pBtMgnt->ExtConfig.linkInfo[i].IncomingTrafficMode = BT_MOTOR_EXT_GULB;
4898                         pBtMgnt->ExtConfig.linkInfo[i].OutgoingTrafficMode = BT_MOTOR_EXT_GULB;
4899                         break;
4900                 case BT_PROFILE_HID:
4901                         pBtMgnt->ExtConfig.linkInfo[i].IncomingTrafficMode = BT_MOTOR_EXT_GUL;
4902                         pBtMgnt->ExtConfig.linkInfo[i].OutgoingTrafficMode = BT_MOTOR_EXT_BE;
4903                         break;
4904                 default:
4905                         break;
4906                 }
4907         }
4908
4909         RTPRINT(FBT, BT_TRACE, ("[DM][BT], RTK, NumberOfHandle = %d, NumberOfSCO = %d\n",
4910                 pBtMgnt->ExtConfig.NumberOfHandle, pBtMgnt->ExtConfig.NumberOfSCO));
4911 }
4912
4913 void BTHCI_WifiScanNotify(struct rtw_adapter *padapter, u8 scanType)
4914 {
4915         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
4916         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
4917
4918         if (pBtMgnt->ExtConfig.bEnableWifiScanNotify)
4919                 bthci_EventExtWifiScanNotify(padapter, scanType);
4920 }
4921
4922 void
4923 BTHCI_StateMachine(
4924         struct rtw_adapter *padapter,
4925         u8              StateToEnter,
4926         enum hci_state_with_cmd         StateCmd,
4927         u8              EntryNum
4928         )
4929 {
4930         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
4931         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
4932
4933         if (EntryNum == 0xff) {
4934                 RTPRINT(FIOCTL, IOCTL_STATE, (" StateMachine, error EntryNum = 0x%x \n", EntryNum));
4935                 return;
4936         }
4937         RTPRINT(FIOCTL, IOCTL_STATE, (" StateMachine, EntryNum = 0x%x, CurrentState = 0x%x, BtNextState = 0x%x,  StateCmd = 0x%x , StateToEnter = 0x%x\n",
4938                 EntryNum, pBTInfo->BtAsocEntry[EntryNum].BtCurrentState, pBTInfo->BtAsocEntry[EntryNum].BtNextState, StateCmd, StateToEnter));
4939
4940         if (pBTInfo->BtAsocEntry[EntryNum].BtNextState & StateToEnter) {
4941                 pBTInfo->BtAsocEntry[EntryNum].BtCurrentState = StateToEnter;
4942
4943                 switch (StateToEnter) {
4944                 case HCI_STATE_STARTING:
4945                         pBTInfo->BtAsocEntry[EntryNum].BtNextState = HCI_STATE_DISCONNECTING | HCI_STATE_CONNECTING;
4946                         bthci_StateStarting(padapter, StateCmd, EntryNum);
4947                         break;
4948                 case HCI_STATE_CONNECTING:
4949                         pBTInfo->BtAsocEntry[EntryNum].BtNextState = HCI_STATE_CONNECTING | HCI_STATE_DISCONNECTING | HCI_STATE_AUTHENTICATING;
4950                         bthci_StateConnecting(padapter, StateCmd, EntryNum);
4951                         break;
4952                 case HCI_STATE_AUTHENTICATING:
4953                         pBTInfo->BtAsocEntry[EntryNum].BtNextState = HCI_STATE_DISCONNECTING | HCI_STATE_CONNECTED;
4954                         bthci_StateAuth(padapter, StateCmd, EntryNum);
4955                         break;
4956                 case HCI_STATE_CONNECTED:
4957                         pBTInfo->BtAsocEntry[EntryNum].BtNextState = HCI_STATE_CONNECTED | HCI_STATE_DISCONNECTING;
4958                         bthci_StateConnected(padapter, StateCmd, EntryNum);
4959                         break;
4960                 case HCI_STATE_DISCONNECTING:
4961                         pBTInfo->BtAsocEntry[EntryNum].BtNextState = HCI_STATE_DISCONNECTED | HCI_STATE_DISCONNECTING;
4962                         bthci_StateDisconnecting(padapter, StateCmd, EntryNum);
4963                         break;
4964                 case HCI_STATE_DISCONNECTED:
4965                         pBTInfo->BtAsocEntry[EntryNum].BtNextState = HCI_STATE_DISCONNECTED | HCI_STATE_STARTING | HCI_STATE_CONNECTING;
4966                         bthci_StateDisconnected(padapter, StateCmd, EntryNum);
4967                         break;
4968                 default:
4969                         RTPRINT(FIOCTL, IOCTL_STATE, (" StateMachine, Unknown state to enter!!!\n"));
4970                         break;
4971                 }
4972         } else {
4973                 RTPRINT(FIOCTL, IOCTL_STATE, (" StateMachine, Wrong state to enter\n"));
4974         }
4975
4976         /*  20100325 Joseph: Disable/Enable IPS/LPS according to BT status. */
4977         if (!pBtMgnt->bBTConnectInProgress && !pBtMgnt->BtOperationOn) {
4978                 RTPRINT(FIOCTL, IOCTL_STATE, ("[BT PS], ips_enter23a()\n"));
4979                 ips_enter23a(padapter);
4980         }
4981 }
4982
4983 void BTHCI_DisconnectPeer(struct rtw_adapter *padapter, u8 EntryNum)
4984 {
4985         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
4986         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
4987
4988         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, (" BTHCI_DisconnectPeer()\n"));
4989
4990         BTHCI_SM_WITH_INFO(padapter, HCI_STATE_DISCONNECTING, STATE_CMD_MAC_CONNECT_CANCEL_INDICATE, EntryNum);
4991
4992         if (pBTInfo->BtAsocEntry[EntryNum].bUsed) {
4993 /*BTPKT_SendDeauthentication(padapter, pBTInfo->BtAsocEntry[EntryNum].BTRemoteMACAddr, unspec_reason); not porting yet */
4994         }
4995
4996         if (pBtMgnt->bBTConnectInProgress) {
4997                 pBtMgnt->bBTConnectInProgress = false;
4998                 RTPRINT(FIOCTL, IOCTL_STATE, ("[BT Flag], BT Connect in progress OFF!!\n"));
4999         }
5000
5001         bthci_RemoveEntryByEntryNum(padapter, EntryNum);
5002
5003         if (pBtMgnt->bNeedNotifyAMPNoCap) {
5004                 RTPRINT(FIOCTL, IOCTL_STATE, ("[BT AMPStatus], set to invalid in BTHCI_DisconnectPeer()\n"));
5005                 BTHCI_EventAMPStatusChange(padapter, AMP_STATUS_NO_CAPACITY_FOR_BT);
5006         }
5007 }
5008
5009 void BTHCI_EventNumOfCompletedDataBlocks(struct rtw_adapter *padapter)
5010 {
5011 /*PMGNT_INFO pMgntInfo = &padapter->MgntInfo; */
5012         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
5013         struct bt_hci_info *pBtHciInfo = &pBTInfo->BtHciInfo;
5014         u8 localBuf[TmpLocalBufSize] = "";
5015         u8 *pRetPar, *pTriple;
5016         u8 len = 0, i, j, handleNum = 0;
5017         struct packet_irp_hcievent_data *PPacketIrpEvent;
5018         u16 *pu2Temp, *pPackets, *pHandle, *pDblocks;
5019         u8 sent = 0;
5020
5021         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
5022
5023         if (!(pBtHciInfo->BTEventMaskPage2 & EMP2_HCI_EVENT_NUM_OF_COMPLETE_DATA_BLOCKS)) {
5024                 RTPRINT(FIOCTL, IOCTL_BT_EVENT, ("[BT event], Num Of Completed DataBlocks, Ignore to send NumOfCompletedDataBlocksEvent due to event mask page 2\n"));
5025                 return;
5026         }
5027
5028         /*  Return parameters starts from here */
5029         pRetPar = &PPacketIrpEvent->Data[0];
5030         pTriple = &pRetPar[3];
5031         for (j = 0; j < MAX_BT_ASOC_ENTRY_NUM; j++) {
5032
5033                 for (i = 0; i < MAX_LOGICAL_LINK_NUM; i++) {
5034                         if (pBTInfo->BtAsocEntry[j].LogLinkCmdData[i].BtLogLinkhandle) {
5035                                 handleNum++;
5036                                 pHandle = (u16 *)&pTriple[0];   /*  Handle[i] */
5037                                 pPackets = (u16 *)&pTriple[2];  /*  Num_Of_Completed_Packets[i] */
5038                                 pDblocks = (u16 *)&pTriple[4];  /*  Num_Of_Completed_Blocks[i] */
5039                                 *pHandle = pBTInfo->BtAsocEntry[j].LogLinkCmdData[i].BtLogLinkhandle;
5040                                 *pPackets = (u16)pBTInfo->BtAsocEntry[j].LogLinkCmdData[i].TxPacketCount;
5041                                 *pDblocks = (u16)pBTInfo->BtAsocEntry[j].LogLinkCmdData[i].TxPacketCount;
5042                                 if (pBTInfo->BtAsocEntry[j].LogLinkCmdData[i].TxPacketCount) {
5043                                         sent = 1;
5044                                         RTPRINT(FIOCTL, IOCTL_BT_EVENT_DETAIL,
5045                                                 ("[BT event], Num Of Completed DataBlocks, Handle = 0x%x, Num_Of_Completed_Packets = 0x%x, Num_Of_Completed_Blocks = 0x%x\n",
5046                                         *pHandle, *pPackets, *pDblocks));
5047                                 }
5048                                 pBTInfo->BtAsocEntry[j].LogLinkCmdData[i].TxPacketCount = 0;
5049                                 len += 6;
5050                                 pTriple += len;
5051                         }
5052                 }
5053         }
5054
5055         pRetPar[2] = handleNum;                         /*  Number_of_Handles */
5056         len += 1;
5057         pu2Temp = (u16 *)&pRetPar[0];
5058         *pu2Temp = BTTotalDataBlockNum;
5059         len += 2;
5060
5061         PPacketIrpEvent->EventCode = HCI_EVENT_NUM_OF_COMPLETE_DATA_BLOCKS;
5062         PPacketIrpEvent->Length = len;
5063         if (handleNum && sent)
5064                 bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2);
5065 }
5066
5067 void BTHCI_EventAMPStatusChange(struct rtw_adapter *padapter, u8 AMP_Status)
5068 {
5069         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
5070         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
5071         struct packet_irp_hcievent_data *PPacketIrpEvent;
5072         u8 len = 0;
5073         u8 localBuf[7] = "";
5074         u8 *pRetPar;
5075
5076         if (AMP_Status == AMP_STATUS_NO_CAPACITY_FOR_BT) {
5077                 pBtMgnt->BTNeedAMPStatusChg = true;
5078                 pBtMgnt->bNeedNotifyAMPNoCap = false;
5079
5080                 BTHCI_DisconnectAll(padapter);
5081         } else if (AMP_Status == AMP_STATUS_FULL_CAPACITY_FOR_BT) {
5082                 pBtMgnt->BTNeedAMPStatusChg = false;
5083         }
5084
5085         PPacketIrpEvent = (struct packet_irp_hcievent_data *)(&localBuf[0]);
5086         /*  Return parameters starts from here */
5087         pRetPar = &PPacketIrpEvent->Data[0];
5088
5089         pRetPar[0] = 0; /*  Status */
5090         len += 1;
5091         pRetPar[1] = AMP_Status;        /*  AMP_Status */
5092         len += 1;
5093
5094         PPacketIrpEvent->EventCode = HCI_EVENT_AMP_STATUS_CHANGE;
5095         PPacketIrpEvent->Length = len;
5096         if (bthci_IndicateEvent(padapter, PPacketIrpEvent, len+2) == RT_STATUS_SUCCESS)
5097                 RTPRINT(FIOCTL, (IOCTL_BT_EVENT|IOCTL_STATE), ("[BT event], AMP Status Change, AMP_Status = %d\n", AMP_Status));
5098 }
5099
5100 void BTHCI_DisconnectAll(struct rtw_adapter *padapter)
5101 {
5102         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
5103         u8 i;
5104
5105         RTPRINT(FIOCTL, IOCTL_STATE, (" DisconnectALL()\n"));
5106
5107         for (i = 0; i < MAX_BT_ASOC_ENTRY_NUM; i++) {
5108                 if (pBTInfo->BtAsocEntry[i].b4waySuccess) {
5109                         BTHCI_SM_WITH_INFO(padapter, HCI_STATE_CONNECTED, STATE_CMD_DISCONNECT_PHY_LINK, i);
5110                 } else if (pBTInfo->BtAsocEntry[i].bUsed) {
5111                         if (pBTInfo->BtAsocEntry[i].BtCurrentState == HCI_STATE_CONNECTING) {
5112                                 BTHCI_SM_WITH_INFO(padapter, HCI_STATE_CONNECTING, STATE_CMD_MAC_CONNECT_CANCEL_INDICATE, i);
5113                         } else if (pBTInfo->BtAsocEntry[i].BtCurrentState == HCI_STATE_DISCONNECTING) {
5114                                 BTHCI_SM_WITH_INFO(padapter, HCI_STATE_DISCONNECTING, STATE_CMD_MAC_CONNECT_CANCEL_INDICATE, i);
5115                         }
5116                 }
5117         }
5118 }
5119
5120 enum hci_status
5121 BTHCI_HandleHCICMD(
5122         struct rtw_adapter *padapter,
5123         struct packet_irp_hcicmd_data *pHciCmd
5124         )
5125 {
5126         enum hci_status status = HCI_STATUS_SUCCESS;
5127         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
5128         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
5129
5130         RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("\n"));
5131         RTPRINT(FIOCTL, (IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO), ("HCI Command start, OGF = 0x%x, OCF = 0x%x, Length = 0x%x\n",
5132                 pHciCmd->OGF, pHciCmd->OCF, pHciCmd->Length));
5133         if (pHciCmd->Length) {
5134                 RTPRINT_DATA(FIOCTL, (IOCTL_BT_HCICMD_DETAIL|IOCTL_BT_LOGO), "HCI Command, Hex Data :\n",
5135                         &pHciCmd->Data[0], pHciCmd->Length);
5136         }
5137         if (pHciCmd->OGF == OGF_EXTENSION) {
5138                 if (pHciCmd->OCF == HCI_SET_RSSI_VALUE)
5139                         RTPRINT(FIOCTL, IOCTL_BT_EVENT_PERIODICAL, ("[BT cmd], "));
5140                 else
5141                         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_EXT, ("[BT cmd], "));
5142         } else {
5143                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("[BT cmd], "));
5144         }
5145
5146         pBtDbg->dbgHciInfo.hciCmdCnt++;
5147
5148         switch (pHciCmd->OGF) {
5149         case LINK_CONTROL_COMMANDS:
5150                 status = bthci_HandleOGFLinkControlCMD(padapter, pHciCmd);
5151                 break;
5152         case HOLD_MODE_COMMAND:
5153                 break;
5154         case OGF_SET_EVENT_MASK_COMMAND:
5155                 status = bthci_HandleOGFSetEventMaskCMD(padapter, pHciCmd);
5156                 break;
5157         case OGF_INFORMATIONAL_PARAMETERS:
5158                 status = bthci_HandleOGFInformationalParameters(padapter, pHciCmd);
5159                 break;
5160         case OGF_STATUS_PARAMETERS:
5161                 status = bthci_HandleOGFStatusParameters(padapter, pHciCmd);
5162                 break;
5163         case OGF_TESTING_COMMANDS:
5164                 status = bthci_HandleOGFTestingCMD(padapter, pHciCmd);
5165                 break;
5166         case OGF_EXTENSION:
5167                 status = bthci_HandleOGFExtension(padapter, pHciCmd);
5168                 break;
5169         default:
5170                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI Command(), Unknown OGF = 0x%x\n", pHciCmd->OGF));
5171                 RTPRINT(FIOCTL, IOCTL_BT_HCICMD, ("HCI_UNKNOWN_COMMAND\n"));
5172                 status = bthci_UnknownCMD(padapter, pHciCmd);
5173                 break;
5174         }
5175         RTPRINT(FIOCTL, IOCTL_BT_HCICMD_DETAIL, ("HCI Command execution end!!\n"));
5176
5177         return status;
5178 }
5179
5180 /*  ===== End of sync from SD7 driver COMMOM/bt_hci.c ===== */
5181
5182 static const char *const BtStateString[] = {
5183         "BT_DISABLED",
5184         "BT_NO_CONNECTION",
5185         "BT_CONNECT_IDLE",
5186         "BT_INQ_OR_PAG",
5187         "BT_ACL_ONLY_BUSY",
5188         "BT_SCO_ONLY_BUSY",
5189         "BT_ACL_SCO_BUSY",
5190         "BT_ACL_INQ_OR_PAG",
5191         "BT_STATE_NOT_DEFINED"
5192 };
5193
5194 /*  ===== Below this line is sync from SD7 driver HAL/BTCoexist/HalBtc87231Ant.c ===== */
5195
5196 static void btdm_SetFwIgnoreWlanAct(struct rtw_adapter *padapter, u8 bEnable)
5197 {
5198         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
5199         u8 H2C_Parameter[1] = {0};
5200
5201         if (bEnable) {
5202                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], BT Ignore Wlan_Act !!\n"));
5203                 H2C_Parameter[0] |= BIT(0);             /*  function enable */
5204                 pHalData->bt_coexist.bFWCoexistAllOff = false;
5205         } else {
5206                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], BT don't ignore Wlan_Act !!\n"));
5207         }
5208
5209         RTPRINT(FBT, BT_TRACE, ("[BTCoex], set FW for BT Ignore Wlan_Act, write 0x25 = 0x%02x\n",
5210                 H2C_Parameter[0]));
5211
5212         FillH2CCmd(padapter, BT_IGNORE_WLAN_ACT_EID, 1, H2C_Parameter);
5213 }
5214
5215 static void btdm_NotifyFwScan(struct rtw_adapter *padapter, u8 scanType)
5216 {
5217         u8 H2C_Parameter[1] = {0};
5218
5219         if (scanType == true)
5220                 H2C_Parameter[0] = 0x1;
5221
5222         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Notify FW for wifi scan, write 0x3b = 0x%02x\n",
5223                 H2C_Parameter[0]));
5224
5225         FillH2CCmd(padapter, 0x3b, 1, H2C_Parameter);
5226 }
5227
5228 static void btdm_1AntSetPSMode(struct rtw_adapter *padapter,
5229                                u8 enable, u8 smartps, u8 mode)
5230 {
5231         struct pwrctrl_priv *pwrctrl;
5232
5233         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Current LPS(%s, %d), smartps =%d\n", enable == true?"ON":"OFF", mode, smartps));
5234
5235         pwrctrl = &padapter->pwrctrlpriv;
5236
5237         if (enable == true) {
5238                 rtw_set_ps_mode23a(padapter, PS_MODE_MIN, smartps, mode);
5239         } else {
5240                 rtw_set_ps_mode23a(padapter, PS_MODE_ACTIVE, 0, 0);
5241                 LPS_RF_ON_check23a(padapter, 100);
5242         }
5243 }
5244
5245 static void btdm_1AntTSFSwitch(struct rtw_adapter *padapter, u8 enable)
5246 {
5247         u8 oldVal, newVal;
5248
5249         oldVal = rtl8723au_read8(padapter, 0x550);
5250
5251         if (enable)
5252                 newVal = oldVal | EN_BCN_FUNCTION;
5253         else
5254                 newVal = oldVal & ~EN_BCN_FUNCTION;
5255
5256         if (oldVal != newVal)
5257                 rtl8723au_write8(padapter, 0x550, newVal);
5258 }
5259
5260 static u8 btdm_Is1AntPsTdmaStateChange(struct rtw_adapter *padapter)
5261 {
5262         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
5263         struct btdm_8723a_1ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm1Ant;
5264
5265         if ((pBtdm8723->bPrePsTdmaOn != pBtdm8723->bCurPsTdmaOn) ||
5266                 (pBtdm8723->prePsTdma != pBtdm8723->curPsTdma))
5267                 return true;
5268         else
5269                 return false;
5270 }
5271
5272 /*  Before enter TDMA, make sure Power Saving is enable! */
5273 static void
5274 btdm_1AntPsTdma(
5275         struct rtw_adapter *padapter,
5276         u8 bTurnOn,
5277         u8 type
5278         )
5279 {
5280         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
5281         struct btdm_8723a_1ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm1Ant;
5282
5283         pBtdm8723->bCurPsTdmaOn = bTurnOn;
5284         pBtdm8723->curPsTdma = type;
5285         if (bTurnOn) {
5286                 switch (type) {
5287                 case 1: /*  A2DP Level-1 or FTP/OPP */
5288                 default:
5289                         if (btdm_Is1AntPsTdmaStateChange(padapter)) {
5290                                 /*  wide duration for WiFi */
5291                                 BTDM_SetFw3a(padapter, 0xd3, 0x1a, 0x1a, 0x0, 0x58);
5292                         }
5293                         break;
5294                 case 2: /*  A2DP Level-2 */
5295                         if (btdm_Is1AntPsTdmaStateChange(padapter)) {
5296                                 /*  normal duration for WiFi */
5297                                 BTDM_SetFw3a(padapter, 0xd3, 0x12, 0x12, 0x0, 0x58);
5298                         }
5299                         break;
5300                 case 3: /*  BT FTP/OPP */
5301                         if (btdm_Is1AntPsTdmaStateChange(padapter)) {
5302                                 /*  normal duration for WiFi */
5303                                 BTDM_SetFw3a(padapter, 0xd3, 0x30, 0x03, 0x10, 0x58);
5304
5305                         }
5306                         break;
5307                 case 4: /*  for wifi scan & BT is connected */
5308                         if (btdm_Is1AntPsTdmaStateChange(padapter)) {
5309                                 /*  protect 3 beacons in 3-beacon period & no Tx pause at BT slot */
5310                                 BTDM_SetFw3a(padapter, 0x93, 0x15, 0x03, 0x14, 0x0);
5311                         }
5312                         break;
5313                 case 5: /*  for WiFi connected-busy & BT is Non-Connected-Idle */
5314                         if (btdm_Is1AntPsTdmaStateChange(padapter)) {
5315                                 /*  SCO mode, Ant fixed at WiFi, WLAN_Act toggle */
5316                                 BTDM_SetFw3a(padapter, 0x61, 0x15, 0x03, 0x31, 0x00);
5317                         }
5318                         break;
5319                 case 9: /*  ACL high-retry type - 2 */
5320                         if (btdm_Is1AntPsTdmaStateChange(padapter)) {
5321                                 /*  narrow duration for WiFi */
5322                                 BTDM_SetFw3a(padapter, 0xd3, 0xa, 0xa, 0x0, 0x58); /* narrow duration for WiFi */
5323                         }
5324                         break;
5325                 case 10: /*  for WiFi connect idle & BT ACL busy or WiFi Connected-Busy & BT is Inquiry */
5326                         if (btdm_Is1AntPsTdmaStateChange(padapter))
5327                                 BTDM_SetFw3a(padapter, 0x13, 0xa, 0xa, 0x0, 0x40);
5328                         break;
5329                 case 11: /*  ACL high-retry type - 3 */
5330                         if (btdm_Is1AntPsTdmaStateChange(padapter)) {
5331                                 /*  narrow duration for WiFi */
5332                                 BTDM_SetFw3a(padapter, 0xd3, 0x05, 0x05, 0x00, 0x58);
5333                         }
5334                         break;
5335                 case 12: /*  for WiFi Connected-Busy & BT is Connected-Idle */
5336                         if (btdm_Is1AntPsTdmaStateChange(padapter)) {
5337                                 /*  Allow High-Pri BT */
5338                                 BTDM_SetFw3a(padapter, 0xeb, 0x0a, 0x03, 0x31, 0x18);
5339                         }
5340                         break;
5341                 case 20: /*  WiFi only busy , TDMA mode for power saving */
5342                         if (btdm_Is1AntPsTdmaStateChange(padapter))
5343                                 BTDM_SetFw3a(padapter, 0x13, 0x25, 0x25, 0x00, 0x00);
5344                         break;
5345                 case 27: /*  WiFi DHCP/Site Survey & BT SCO busy */
5346                         if (btdm_Is1AntPsTdmaStateChange(padapter))
5347                                 BTDM_SetFw3a(padapter, 0xa3, 0x25, 0x03, 0x31, 0x98);
5348                         break;
5349                 case 28: /*  WiFi DHCP/Site Survey & BT idle */
5350                         if (btdm_Is1AntPsTdmaStateChange(padapter))
5351                                 BTDM_SetFw3a(padapter, 0x69, 0x25, 0x03, 0x31, 0x00);
5352                         break;
5353                 case 29: /*  WiFi DHCP/Site Survey & BT ACL busy */
5354                         if (btdm_Is1AntPsTdmaStateChange(padapter)) {
5355                                 BTDM_SetFw3a(padapter, 0xeb, 0x1a, 0x1a, 0x01, 0x18);
5356                                 rtl8723au_write32(padapter, 0x6c0, 0x5afa5afa);
5357                                 rtl8723au_write32(padapter, 0x6c4, 0x5afa5afa);
5358                         }
5359                         break;
5360                 case 30: /*  WiFi idle & BT Inquiry */
5361                         if (btdm_Is1AntPsTdmaStateChange(padapter))
5362                                 BTDM_SetFw3a(padapter, 0x93, 0x15, 0x03, 0x14, 0x00);
5363                         break;
5364                 case 31:  /*  BT HID */
5365                         if (btdm_Is1AntPsTdmaStateChange(padapter))
5366                                 BTDM_SetFw3a(padapter, 0xd3, 0x1a, 0x1a, 0x00, 0x58);
5367                         break;
5368                 case 32:  /*  BT SCO & Inquiry */
5369                         if (btdm_Is1AntPsTdmaStateChange(padapter))
5370                                 BTDM_SetFw3a(padapter, 0xab, 0x0a, 0x03, 0x11, 0x98);
5371                         break;
5372                 case 33:  /*  BT SCO & WiFi site survey */
5373                         if (btdm_Is1AntPsTdmaStateChange(padapter))
5374                                 BTDM_SetFw3a(padapter, 0xa3, 0x25, 0x03, 0x30, 0x98);
5375                         break;
5376                 case 34:  /*  BT HID & WiFi site survey */
5377                         if (btdm_Is1AntPsTdmaStateChange(padapter))
5378                                 BTDM_SetFw3a(padapter, 0xd3, 0x1a, 0x1a, 0x00, 0x18);
5379                         break;
5380                 case 35:  /*  BT HID & WiFi Connecting */
5381                         if (btdm_Is1AntPsTdmaStateChange(padapter))
5382                                 BTDM_SetFw3a(padapter, 0xe3, 0x1a, 0x1a, 0x00, 0x18);
5383                         break;
5384                 }
5385         } else {
5386                 /*  disable PS-TDMA */
5387                 switch (type) {
5388                 case 8:
5389                         if (btdm_Is1AntPsTdmaStateChange(padapter)) {
5390                                 /*  Antenna control by PTA, 0x870 = 0x310 */
5391                                 BTDM_SetFw3a(padapter, 0x8, 0x0, 0x0, 0x0, 0x0);
5392                         }
5393                         break;
5394                 case 0:
5395                 default:
5396                         if (btdm_Is1AntPsTdmaStateChange(padapter)) {
5397                                 /*  Antenna control by PTA, 0x870 = 0x310 */
5398                                 BTDM_SetFw3a(padapter, 0x0, 0x0, 0x0, 0x8, 0x0);
5399                         }
5400                         /*  Switch Antenna to BT */
5401                         rtl8723au_write16(padapter, 0x860, 0x210);
5402                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], 0x860 = 0x210, Switch Antenna to BT\n"));
5403                         break;
5404                 case 9:
5405                         if (btdm_Is1AntPsTdmaStateChange(padapter)) {
5406                                 /*  Antenna control by PTA, 0x870 = 0x310 */
5407                                 BTDM_SetFw3a(padapter, 0x0, 0x0, 0x0, 0x8, 0x0);
5408                         }
5409                         /*  Switch Antenna to WiFi */
5410                         rtl8723au_write16(padapter, 0x860, 0x110);
5411                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], 0x860 = 0x110, Switch Antenna to WiFi\n"));
5412                         break;
5413                 }
5414         }
5415
5416         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Current TDMA(%s, %d)\n",
5417                 pBtdm8723->bCurPsTdmaOn?"ON":"OFF", pBtdm8723->curPsTdma));
5418
5419         /*  update pre state */
5420         pBtdm8723->bPrePsTdmaOn = pBtdm8723->bCurPsTdmaOn;
5421         pBtdm8723->prePsTdma = pBtdm8723->curPsTdma;
5422 }
5423
5424 static void
5425 _btdm_1AntSetPSTDMA(struct rtw_adapter *padapter, u8 bPSEn, u8 smartps,
5426                     u8 psOption, u8 bTDMAOn, u8 tdmaType)
5427 {
5428         struct pwrctrl_priv *pwrctrl;
5429         struct hal_data_8723a *pHalData;
5430         struct btdm_8723a_1ant *pBtdm8723;
5431         u8 psMode;
5432         u8 bSwitchPS;
5433
5434         if (!check_fwstate(&padapter->mlmepriv, WIFI_STATION_STATE) &&
5435             (get_fwstate(&padapter->mlmepriv) != WIFI_NULL_STATE)) {
5436                 btdm_1AntPsTdma(padapter, bTDMAOn, tdmaType);
5437                 return;
5438         }
5439         psOption &= ~BIT(0);
5440
5441         RTPRINT(FBT, BT_TRACE,
5442                 ("[BTCoex], Set LPS(%s, %d) TDMA(%s, %d)\n",
5443                 bPSEn == true?"ON":"OFF", psOption,
5444                 bTDMAOn == true?"ON":"OFF", tdmaType));
5445
5446         pwrctrl = &padapter->pwrctrlpriv;
5447         pHalData = GET_HAL_DATA(padapter);
5448         pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm1Ant;
5449
5450         if (bPSEn) {
5451                 if (pBtdm8723->bWiFiHalt) {
5452                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Enable PS Fail, WiFi in Halt!!\n"));
5453                         return;
5454                 }
5455
5456                 if (pwrctrl->bInSuspend) {
5457                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Enable PS Fail, WiFi in Suspend!!\n"));
5458                         return;
5459                 }
5460
5461                 if (padapter->bDriverStopped) {
5462                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Enable PS Fail, WiFi driver stopped!!\n"));
5463                         return;
5464                 }
5465
5466                 if (padapter->bSurpriseRemoved) {
5467                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Enable PS Fail, WiFi Surprise Removed!!\n"));
5468                         return;
5469                 }
5470
5471                 psMode = PS_MODE_MIN;
5472         } else {
5473                 psMode = PS_MODE_ACTIVE;
5474                 psOption = 0;
5475         }
5476
5477         if (psMode != pwrctrl->pwr_mode) {
5478                 bSwitchPS = true;
5479         } else if (psMode != PS_MODE_ACTIVE) {
5480                 if (psOption != pwrctrl->bcn_ant_mode)
5481                         bSwitchPS = true;
5482                 else if (smartps != pwrctrl->smart_ps)
5483                         bSwitchPS = true;
5484                 else
5485                         bSwitchPS = false;
5486         } else {
5487                 bSwitchPS = false;
5488         }
5489
5490         if (bSwitchPS) {
5491                 /*  disable TDMA */
5492                 if (pBtdm8723->bCurPsTdmaOn) {
5493                         if (!bTDMAOn) {
5494                                 btdm_1AntPsTdma(padapter, false, tdmaType);
5495                         } else {
5496                                 if (!rtl8723a_BT_enabled(padapter) ||
5497                                     (pHalData->bt_coexist.halCoex8723.c2hBtInfo == BT_INFO_STATE_NO_CONNECTION) ||
5498                                     (pHalData->bt_coexist.halCoex8723.c2hBtInfo == BT_INFO_STATE_CONNECT_IDLE) ||
5499                                     (tdmaType == 29))
5500                                         btdm_1AntPsTdma(padapter, false, 9);
5501                                 else
5502                                         btdm_1AntPsTdma(padapter, false, 0);
5503                         }
5504                 }
5505
5506                 /*  change Power Save State */
5507                 btdm_1AntSetPSMode(padapter, bPSEn, smartps, psOption);
5508         }
5509
5510         btdm_1AntPsTdma(padapter, bTDMAOn, tdmaType);
5511 }
5512
5513 static void
5514 btdm_1AntSetPSTDMA(struct rtw_adapter *padapter, u8 bPSEn,
5515                    u8 psOption, u8 bTDMAOn, u8 tdmaType)
5516 {
5517         _btdm_1AntSetPSTDMA(padapter, bPSEn, 0, psOption, bTDMAOn, tdmaType);
5518 }
5519
5520 static void btdm_1AntWifiParaAdjust(struct rtw_adapter *padapter, u8 bEnable)
5521 {
5522         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
5523         struct btdm_8723a_1ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm1Ant;
5524
5525         if (bEnable) {
5526                 pBtdm8723->curWifiPara = 1;
5527                 if (pBtdm8723->preWifiPara != pBtdm8723->curWifiPara)
5528                         BTDM_SetSwPenaltyTxRateAdaptive(padapter, BT_TX_RATE_ADAPTIVE_LOW_PENALTY);
5529         } else {
5530                 pBtdm8723->curWifiPara = 2;
5531                 if (pBtdm8723->preWifiPara != pBtdm8723->curWifiPara)
5532                         BTDM_SetSwPenaltyTxRateAdaptive(padapter, BT_TX_RATE_ADAPTIVE_NORMAL);
5533         }
5534
5535 }
5536
5537 static void btdm_1AntPtaParaReload(struct rtw_adapter *padapter)
5538 {
5539         /*  PTA parameter */
5540         rtl8723au_write8(padapter, 0x6cc, 0x0);         /*  1-Ant coex */
5541         rtl8723au_write32(padapter, 0x6c8, 0xffff);     /*  wifi break table */
5542         rtl8723au_write32(padapter, 0x6c4, 0x55555555); /*  coex table */
5543
5544         /*  Antenna switch control parameter */
5545         rtl8723au_write32(padapter, 0x858, 0xaaaaaaaa);
5546         if (IS_8723A_A_CUT(GET_HAL_DATA(padapter)->VersionID)) {
5547                 /*  SPDT(connected with TRSW) control by hardware PTA */
5548                 rtl8723au_write32(padapter, 0x870, 0x0);
5549                 rtl8723au_write8(padapter, 0x40, 0x24);
5550         } else {
5551                 rtl8723au_write8(padapter, 0x40, 0x20);
5552                 /*  set antenna at bt side if ANTSW is software control */
5553                 rtl8723au_write16(padapter, 0x860, 0x210);
5554                 /*  SPDT(connected with TRSW) control by hardware PTA */
5555                 rtl8723au_write32(padapter, 0x870, 0x300);
5556                 /*  ANTSW keep by GNT_BT */
5557                 rtl8723au_write32(padapter, 0x874, 0x22804000);
5558         }
5559
5560         /*  coexistence parameters */
5561         rtl8723au_write8(padapter, 0x778, 0x1); /*  enable RTK mode PTA */
5562
5563         /*  BT don't ignore WLAN_Act */
5564         btdm_SetFwIgnoreWlanAct(padapter, false);
5565 }
5566
5567 /*
5568  * Return
5569  *1: upgrade (add WiFi duration time)
5570  *0: keep
5571  *-1: downgrade (add BT duration time)
5572  */
5573 static s8 btdm_1AntTdmaJudgement(struct rtw_adapter *padapter, u8 retry)
5574 {
5575         struct hal_data_8723a *pHalData;
5576         struct btdm_8723a_1ant *pBtdm8723;
5577         static s8 up, dn, m = 1, n = 3, WaitCount;
5578         s8 ret;
5579
5580         pHalData = GET_HAL_DATA(padapter);
5581         pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm1Ant;
5582         ret = 0;
5583
5584         if (pBtdm8723->psTdmaMonitorCnt == 0) {
5585                 up = 0;
5586                 dn = 0;
5587                 m = 1;
5588                 n = 3;
5589                 WaitCount = 0;
5590         } else {
5591                 WaitCount++;
5592         }
5593
5594         if (retry == 0) {
5595         /*  no retry in the last 2-second duration */
5596                 up++;
5597                 dn--;
5598                 if (dn < 0)
5599                         dn = 0;
5600                 if (up >= 3*m) {
5601                         /*  retry = 0 in consecutive 3m*(2s), add WiFi duration */
5602                         ret = 1;
5603
5604                         n = 3;
5605                         up = 0;
5606                         dn = 0;
5607                         WaitCount = 0;
5608                 }
5609         } else if (retry <= 3) {
5610                 /*  retry<= 3 in the last 2-second duration */
5611                 up--;
5612                 dn++;
5613                 if (up < 0)
5614                         up = 0;
5615
5616                 if (dn == 2) {
5617                         /*  retry<= 3 in consecutive 2*(2s), minus WiFi duration (add BT duration) */
5618                         ret = -1;
5619
5620                         /*  record how many time downgrad WiFi duration */
5621                         if (WaitCount <= 2)
5622                                 m++;
5623                         else
5624                                 m = 1;
5625                         /*  the max number of m is 20 */
5626                         /*  the longest time of upgrade WiFi duration is 20*3*2s = 120s */
5627                         if (m >= 20)
5628                                 m = 20;
5629                         up = 0;
5630                         dn = 0;
5631                         WaitCount = 0;
5632                 }
5633         } else {
5634                 /*  retry count > 3 */
5635                 /*  retry>3, minus WiFi duration (add BT duration) */
5636                 ret = -1;
5637
5638                 /*  record how many time downgrad WiFi duration */
5639                 if (WaitCount == 1)
5640                         m++;
5641                 else
5642                         m = 1;
5643                 if (m >= 20)
5644                         m = 20;
5645
5646                 up = 0;
5647                 dn = 0;
5648                 WaitCount = 0;
5649         }
5650         return ret;
5651 }
5652
5653 static void btdm_1AntTdmaDurationAdjustForACL(struct rtw_adapter *padapter)
5654 {
5655         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
5656         struct btdm_8723a_1ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm1Ant;
5657
5658         if (pBtdm8723->psTdmaGlobalCnt != pBtdm8723->psTdmaMonitorCnt) {
5659                 pBtdm8723->psTdmaMonitorCnt = 0;
5660                 pBtdm8723->psTdmaGlobalCnt = 0;
5661         }
5662         if (pBtdm8723->psTdmaMonitorCnt == 0) {
5663                 btdm_1AntSetPSTDMA(padapter, true, 0, true, 2);
5664                 pBtdm8723->psTdmaDuAdjType = 2;
5665         } else {
5666                 /*  Now we only have 4 level Ps Tdma, */
5667                 /*  if that's not the following 4 level(will changed by wifi scan, dhcp...), */
5668                 /*  then we have to adjust it back to the previous record one. */
5669                 if ((pBtdm8723->curPsTdma != 1) &&
5670                     (pBtdm8723->curPsTdma != 2) &&
5671                     (pBtdm8723->curPsTdma != 9) &&
5672                     (pBtdm8723->curPsTdma != 11)) {
5673                         btdm_1AntSetPSTDMA(padapter, true, 0, true, pBtdm8723->psTdmaDuAdjType);
5674                 } else {
5675                         s32 judge = 0;
5676
5677                         judge = btdm_1AntTdmaJudgement(padapter, pHalData->bt_coexist.halCoex8723.btRetryCnt);
5678                         if (judge == -1) {
5679                                 if (pBtdm8723->curPsTdma == 1) {
5680                                         /*  Decrease WiFi duration for high BT retry */
5681                                         if (pHalData->bt_coexist.halCoex8723.btInfoExt)
5682                                                 pBtdm8723->psTdmaDuAdjType = 9;
5683                                         else
5684                                                 pBtdm8723->psTdmaDuAdjType = 2;
5685                                         btdm_1AntSetPSTDMA(padapter, true, 0, true, pBtdm8723->psTdmaDuAdjType);
5686                                 } else if (pBtdm8723->curPsTdma == 2) {
5687                                         btdm_1AntSetPSTDMA(padapter, true, 0, true, 9);
5688                                         pBtdm8723->psTdmaDuAdjType = 9;
5689                                 } else if (pBtdm8723->curPsTdma == 9) {
5690                                         btdm_1AntSetPSTDMA(padapter, true, 0, true, 11);
5691                                         pBtdm8723->psTdmaDuAdjType = 11;
5692                                 }
5693                         } else if (judge == 1) {
5694                                 if (pBtdm8723->curPsTdma == 11) {
5695                                         btdm_1AntSetPSTDMA(padapter, true, 0, true, 9);
5696                                         pBtdm8723->psTdmaDuAdjType = 9;
5697                                 } else if (pBtdm8723->curPsTdma == 9) {
5698                                         if (pHalData->bt_coexist.halCoex8723.btInfoExt)
5699                                                 pBtdm8723->psTdmaDuAdjType = 9;
5700                                         else
5701                                                 pBtdm8723->psTdmaDuAdjType = 2;
5702                                         btdm_1AntSetPSTDMA(padapter, true, 0, true, pBtdm8723->psTdmaDuAdjType);
5703                                 } else if (pBtdm8723->curPsTdma == 2) {
5704                                         if (pHalData->bt_coexist.halCoex8723.btInfoExt)
5705                                                 pBtdm8723->psTdmaDuAdjType = 9;
5706                                         else
5707                                                 pBtdm8723->psTdmaDuAdjType = 1;
5708                                         btdm_1AntSetPSTDMA(padapter, true, 0, true, pBtdm8723->psTdmaDuAdjType);
5709                                 }
5710                         }
5711                 }
5712                 RTPRINT(FBT, BT_TRACE,
5713                         ("[BTCoex], ACL current TDMA(%s, %d)\n",
5714                         (pBtdm8723->bCurPsTdmaOn ? "ON" : "OFF"), pBtdm8723->curPsTdma));
5715         }
5716         pBtdm8723->psTdmaMonitorCnt++;
5717 }
5718
5719 static void btdm_1AntCoexProcessForWifiConnect(struct rtw_adapter *padapter)
5720 {
5721         struct mlme_priv *pmlmepriv;
5722         struct hal_data_8723a *pHalData;
5723         struct bt_coexist_8723a *pBtCoex;
5724         struct btdm_8723a_1ant *pBtdm8723;
5725         u8 BtState;
5726
5727         pmlmepriv = &padapter->mlmepriv;
5728         pHalData = GET_HAL_DATA(padapter);
5729         pBtCoex = &pHalData->bt_coexist.halCoex8723;
5730         pBtdm8723 = &pBtCoex->btdm1Ant;
5731         BtState = pBtCoex->c2hBtInfo;
5732
5733         RTPRINT(FBT, BT_TRACE, ("[BTCoex], WiFi is %s\n",
5734                                 BTDM_IsWifiBusy(padapter)?"Busy":"IDLE"));
5735         RTPRINT(FBT, BT_TRACE, ("[BTCoex], BT is %s\n",
5736                                 BtStateString[BtState]));
5737
5738         padapter->pwrctrlpriv.btcoex_rfon = false;
5739
5740         if (!BTDM_IsWifiBusy(padapter) &&
5741             !check_fwstate(&padapter->mlmepriv, WIFI_AP_STATE) &&
5742             (BtState == BT_INFO_STATE_NO_CONNECTION ||
5743              BtState == BT_INFO_STATE_CONNECT_IDLE)) {
5744                 switch (BtState) {
5745                 case BT_INFO_STATE_NO_CONNECTION:
5746                         _btdm_1AntSetPSTDMA(padapter, true, 2, 0x26, false, 9);
5747                         break;
5748                 case BT_INFO_STATE_CONNECT_IDLE:
5749                         _btdm_1AntSetPSTDMA(padapter, true, 2, 0x26, false, 0);
5750                         break;
5751                 }
5752         } else {
5753                 switch (BtState) {
5754                 case BT_INFO_STATE_NO_CONNECTION:
5755                 case BT_INFO_STATE_CONNECT_IDLE:
5756                         /*  WiFi is Busy */
5757                         btdm_1AntSetPSTDMA(padapter, false, 0, true, 5);
5758                         rtl8723au_write32(padapter, 0x6c0, 0x5a5a5a5a);
5759                         rtl8723au_write32(padapter, 0x6c4, 0x5a5a5a5a);
5760                         break;
5761                 case BT_INFO_STATE_ACL_INQ_OR_PAG:
5762                         RTPRINT(FBT, BT_TRACE,
5763                                 ("[BTCoex], BT PROFILE is "
5764                                  "BT_INFO_STATE_ACL_INQ_OR_PAG\n"));
5765                 case BT_INFO_STATE_INQ_OR_PAG:
5766                         padapter->pwrctrlpriv.btcoex_rfon = true;
5767                         btdm_1AntSetPSTDMA(padapter, true, 0, true, 30);
5768                         break;
5769                 case BT_INFO_STATE_SCO_ONLY_BUSY:
5770                 case BT_INFO_STATE_ACL_SCO_BUSY:
5771                         if (true == pBtCoex->bC2hBtInquiryPage)
5772                                 btdm_1AntSetPSTDMA(padapter, false, 0,
5773                                                    true, 32);
5774                         else {
5775 #ifdef BTCOEX_CMCC_TEST
5776                                 btdm_1AntSetPSTDMA(padapter, false, 0,
5777                                                    true, 23);
5778 #else /*  !BTCOEX_CMCC_TEST */
5779                                 btdm_1AntSetPSTDMA(padapter, false, 0,
5780                                                    false, 8);
5781                                 rtl8723au_write32(padapter, 0x6c0, 0x5a5a5a5a);
5782                                 rtl8723au_write32(padapter, 0x6c4, 0x5a5a5a5a);
5783 #endif /*  !BTCOEX_CMCC_TEST */
5784                         }
5785                         break;
5786                 case BT_INFO_STATE_ACL_ONLY_BUSY:
5787                         padapter->pwrctrlpriv.btcoex_rfon = true;
5788                         if (pBtCoex->c2hBtProfile == BT_INFO_HID) {
5789                                 RTPRINT(FBT, BT_TRACE,
5790                                         ("[BTCoex], BT PROFILE is HID\n"));
5791                                 btdm_1AntSetPSTDMA(padapter, true, 0, true, 31);
5792                         } else if (pBtCoex->c2hBtProfile == BT_INFO_FTP) {
5793                                 RTPRINT(FBT, BT_TRACE,
5794                                         ("[BTCoex], BT PROFILE is FTP/OPP\n"));
5795                                 btdm_1AntSetPSTDMA(padapter, true, 0, true, 3);
5796                         } else if (pBtCoex->c2hBtProfile == (BT_INFO_A2DP|BT_INFO_FTP)) {
5797                                 RTPRINT(FBT, BT_TRACE,
5798                                         ("[BTCoex], BT PROFILE is A2DP_FTP\n"));
5799                                 btdm_1AntSetPSTDMA(padapter, true, 0, true, 11);
5800                         } else {
5801                                 if (pBtCoex->c2hBtProfile == BT_INFO_A2DP)
5802                                         RTPRINT(FBT, BT_TRACE,
5803                                                 ("[BTCoex], BT PROFILE is "
5804                                                  "A2DP\n"));
5805                                 else
5806                                         RTPRINT(FBT, BT_TRACE,
5807                                                 ("[BTCoex], BT PROFILE is "
5808                                                  "UNKNOWN(0x%02X)! Use A2DP "
5809                                                  "Profile\n",
5810                                                  pBtCoex->c2hBtProfile));
5811                                 btdm_1AntTdmaDurationAdjustForACL(padapter);
5812                         }
5813                         break;
5814                 }
5815         }
5816
5817         pBtdm8723->psTdmaGlobalCnt++;
5818 }
5819
5820 static void
5821 btdm_1AntUpdateHalRAMask(struct rtw_adapter *padapter, u32 mac_id, u32 filter)
5822 {
5823         u8 init_rate = 0;
5824         u8 raid;
5825         u32 mask;
5826         u8 shortGIrate = false;
5827         int supportRateNum = 0;
5828         struct sta_info *psta;
5829         struct hal_data_8723a *pHalData;
5830         struct dm_priv *pdmpriv;
5831         struct mlme_ext_priv *pmlmeext;
5832         struct mlme_ext_info *pmlmeinfo;
5833         struct wlan_bssid_ex *cur_network;
5834
5835         RTPRINT(FBT, BT_TRACE, ("[BTCoex], %s, MACID =%d, filter = 0x%08x!!\n",
5836                                 __func__, mac_id, filter));
5837
5838         pHalData = GET_HAL_DATA(padapter);
5839         pdmpriv = &pHalData->dmpriv;
5840         pmlmeext = &padapter->mlmeextpriv;
5841         pmlmeinfo = &pmlmeext->mlmext_info;
5842         cur_network = &pmlmeinfo->network;
5843
5844         if (mac_id >= NUM_STA) { /* CAM_SIZE */
5845                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], %s, MACID =%d illegal!!\n",
5846                                         __func__, mac_id));
5847                 return;
5848         }
5849
5850         psta = pmlmeinfo->FW_sta_info[mac_id].psta;
5851         if (!psta) {
5852                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], %s, Can't find station!!\n",
5853                                         __func__));
5854                 return;
5855         }
5856
5857         raid = psta->raid;
5858
5859         switch (mac_id) {
5860         case 0:/*  for infra mode */
5861                 supportRateNum =
5862                         rtw_get_rateset_len23a(cur_network->SupportedRates);
5863                 mask = update_supported_rate23a(cur_network->SupportedRates,
5864                                                 supportRateNum);
5865                 mask |= (pmlmeinfo->HT_enable) ?
5866                         update_MSC_rate23a(&pmlmeinfo->ht_cap):0;
5867                 if (support_short_GI23a(padapter, &pmlmeinfo->ht_cap))
5868                         shortGIrate = true;
5869                 break;
5870         case 1:/* for broadcast/multicast */
5871                 supportRateNum = rtw_get_rateset_len23a(
5872                         pmlmeinfo->FW_sta_info[mac_id].SupportedRates);
5873                 mask = update_basic_rate23a(cur_network->SupportedRates,
5874                                             supportRateNum);
5875                 break;
5876         default: /* for each sta in IBSS */
5877                 supportRateNum = rtw_get_rateset_len23a(
5878                         pmlmeinfo->FW_sta_info[mac_id].SupportedRates);
5879                 mask = update_supported_rate23a(cur_network->SupportedRates,
5880                                                 supportRateNum);
5881                 break;
5882         }
5883         mask |= ((raid<<28)&0xf0000000);
5884         mask &= 0xffffffff;
5885         mask &= ~filter;
5886         init_rate = get_highest_rate_idx23a(mask)&0x3f;
5887
5888         if (pHalData->fw_ractrl) {
5889                 u8 arg = 0;
5890
5891                 arg = mac_id&0x1f;/* MACID */
5892                 arg |= BIT(7);
5893                 if (true == shortGIrate)
5894                         arg |= BIT(5);
5895
5896                 RTPRINT(FBT, BT_TRACE,
5897                         ("[BTCoex], Update FW RAID entry, MASK = 0x%08x, "
5898                          "arg = 0x%02x\n", mask, arg));
5899
5900                 rtl8723a_set_raid_cmd(padapter, mask, arg);
5901         } else {
5902                 if (shortGIrate)
5903                         init_rate |= BIT(6);
5904
5905                 rtl8723au_write8(padapter, REG_INIDATA_RATE_SEL + mac_id,
5906                                  init_rate);
5907         }
5908
5909         psta->init_rate = init_rate;
5910         pdmpriv->INIDATA_RATE[mac_id] = init_rate;
5911 }
5912
5913 static void
5914 btdm_1AntUpdateHalRAMaskForSCO(struct rtw_adapter *padapter, u8 forceUpdate)
5915 {
5916         struct btdm_8723a_1ant *pBtdm8723;
5917         struct sta_priv *pstapriv;
5918         struct wlan_bssid_ex *cur_network;
5919         struct sta_info *psta;
5920         u32 macid;
5921         u32 filter = 0;
5922
5923         pBtdm8723 = &GET_HAL_DATA(padapter)->bt_coexist.halCoex8723.btdm1Ant;
5924
5925         if (pBtdm8723->bRAChanged == true && forceUpdate == false)
5926                 return;
5927
5928         pstapriv = &padapter->stapriv;
5929         cur_network = &padapter->mlmeextpriv.mlmext_info.network;
5930         psta = rtw_get_stainfo23a(pstapriv, cur_network->MacAddress);
5931         macid = psta->mac_id;
5932
5933         filter |= BIT(_1M_RATE_);
5934         filter |= BIT(_2M_RATE_);
5935         filter |= BIT(_5M_RATE_);
5936         filter |= BIT(_11M_RATE_);
5937         filter |= BIT(_6M_RATE_);
5938         filter |= BIT(_9M_RATE_);
5939
5940         btdm_1AntUpdateHalRAMask(padapter, macid, filter);
5941
5942         pBtdm8723->bRAChanged = true;
5943 }
5944
5945 static void btdm_1AntRecoverHalRAMask(struct rtw_adapter *padapter)
5946 {
5947         struct btdm_8723a_1ant *pBtdm8723;
5948         struct sta_priv *pstapriv;
5949         struct wlan_bssid_ex *cur_network;
5950         struct sta_info *psta;
5951
5952         pBtdm8723 = &GET_HAL_DATA(padapter)->bt_coexist.halCoex8723.btdm1Ant;
5953
5954         if (pBtdm8723->bRAChanged == false)
5955                 return;
5956
5957         pstapriv = &padapter->stapriv;
5958         cur_network = &padapter->mlmeextpriv.mlmext_info.network;
5959         psta = rtw_get_stainfo23a(pstapriv, cur_network->MacAddress);
5960
5961         Update_RA_Entry23a(padapter, psta);
5962
5963         pBtdm8723->bRAChanged = false;
5964 }
5965
5966 static void
5967 btdm_1AntBTStateChangeHandler(struct rtw_adapter *padapter,
5968                               enum bt_state_1ant oldState,
5969                               enum bt_state_1ant newState)
5970 {
5971         struct hal_data_8723a *phaldata;
5972         RTPRINT(FBT, BT_TRACE, ("[BTCoex], BT state change, %s => %s\n",
5973                                 BtStateString[oldState],
5974                                 BtStateString[newState]));
5975
5976         /*  BT default ignore wlan active, */
5977         /*  WiFi MUST disable this when BT is enable */
5978         if (newState > BT_INFO_STATE_DISABLED)
5979                 btdm_SetFwIgnoreWlanAct(padapter, false);
5980
5981         if ((check_fwstate(&padapter->mlmepriv, WIFI_STATION_STATE)) &&
5982             (BTDM_IsWifiConnectionExist(padapter))) {
5983                 if ((newState == BT_INFO_STATE_SCO_ONLY_BUSY) ||
5984                     (newState == BT_INFO_STATE_ACL_SCO_BUSY)) {
5985                         btdm_1AntUpdateHalRAMaskForSCO(padapter, false);
5986                 } else {
5987                         /*  Recover original RA setting */
5988                         btdm_1AntRecoverHalRAMask(padapter);
5989                 }
5990         } else {
5991                 phaldata = GET_HAL_DATA(padapter);
5992                 phaldata->bt_coexist.halCoex8723.btdm1Ant.bRAChanged = false;
5993         }
5994
5995         if (oldState == newState)
5996                 return;
5997
5998         if (oldState == BT_INFO_STATE_ACL_ONLY_BUSY) {
5999                 struct hal_data_8723a *Hal = GET_HAL_DATA(padapter);
6000                 Hal->bt_coexist.halCoex8723.btdm1Ant.psTdmaMonitorCnt = 0;
6001                 Hal->bt_coexist.halCoex8723.btdm1Ant.psTdmaMonitorCntForSCO = 0;
6002         }
6003
6004         if ((oldState == BT_INFO_STATE_SCO_ONLY_BUSY) ||
6005             (oldState == BT_INFO_STATE_ACL_SCO_BUSY)) {
6006                 struct hal_data_8723a *Hal = GET_HAL_DATA(padapter);
6007                 Hal->bt_coexist.halCoex8723.btdm1Ant.psTdmaMonitorCntForSCO = 0;
6008         }
6009
6010         /*  Active 2Ant mechanism when BT Connected */
6011         if ((oldState == BT_INFO_STATE_DISABLED) ||
6012             (oldState == BT_INFO_STATE_NO_CONNECTION)) {
6013                 if ((newState != BT_INFO_STATE_DISABLED) &&
6014                     (newState != BT_INFO_STATE_NO_CONNECTION)) {
6015                         BTDM_SetSwRfRxLpfCorner(padapter,
6016                                                 BT_RF_RX_LPF_CORNER_SHRINK);
6017                         BTDM_AGCTable(padapter, BT_AGCTABLE_ON);
6018                         BTDM_BBBackOffLevel(padapter, BT_BB_BACKOFF_ON);
6019                 }
6020         } else {
6021                 if ((newState == BT_INFO_STATE_DISABLED) ||
6022                     (newState == BT_INFO_STATE_NO_CONNECTION)) {
6023                         BTDM_SetSwRfRxLpfCorner(padapter,
6024                                                 BT_RF_RX_LPF_CORNER_RESUME);
6025                         BTDM_AGCTable(padapter, BT_AGCTABLE_OFF);
6026                         BTDM_BBBackOffLevel(padapter, BT_BB_BACKOFF_OFF);
6027                 }
6028         }
6029 }
6030
6031 static void btdm_1AntBtCoexistHandler(struct rtw_adapter *padapter)
6032 {
6033         struct hal_data_8723a *pHalData;
6034         struct bt_coexist_8723a *pBtCoex8723;
6035         struct btdm_8723a_1ant *pBtdm8723;
6036
6037         pHalData = GET_HAL_DATA(padapter);
6038         pBtCoex8723 = &pHalData->bt_coexist.halCoex8723;
6039         pBtdm8723 = &pBtCoex8723->btdm1Ant;
6040         padapter->pwrctrlpriv.btcoex_rfon = false;
6041         if (!rtl8723a_BT_enabled(padapter)) {
6042                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], BT is disabled\n"));
6043
6044                 if (BTDM_IsWifiConnectionExist(padapter)) {
6045                         RTPRINT(FBT, BT_TRACE,
6046                                 ("[BTCoex], wifi is connected\n"));
6047
6048                         if (BTDM_IsWifiBusy(padapter)) {
6049                                 RTPRINT(FBT, BT_TRACE,
6050                                         ("[BTCoex], Wifi is busy\n"));
6051                                 btdm_1AntSetPSTDMA(padapter, false, 0,
6052                                                    false, 9);
6053                         } else {
6054                                 RTPRINT(FBT, BT_TRACE,
6055                                         ("[BTCoex], Wifi is idle\n"));
6056                                 _btdm_1AntSetPSTDMA(padapter, true, 2, 1,
6057                                                     false, 9);
6058                         }
6059                 } else {
6060                         RTPRINT(FBT, BT_TRACE,
6061                                 ("[BTCoex], wifi is disconnected\n"));
6062
6063                         btdm_1AntSetPSTDMA(padapter, false, 0, false, 9);
6064                 }
6065         } else {
6066                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], BT is enabled\n"));
6067
6068                 if (BTDM_IsWifiConnectionExist(padapter)) {
6069                         RTPRINT(FBT, BT_TRACE,
6070                                 ("[BTCoex], wifi is connected\n"));
6071
6072                         btdm_1AntWifiParaAdjust(padapter, true);
6073                         btdm_1AntCoexProcessForWifiConnect(padapter);
6074                 } else {
6075                         RTPRINT(FBT, BT_TRACE,
6076                                 ("[BTCoex], wifi is disconnected\n"));
6077
6078                         /*  Antenna switch at BT side(0x870 = 0x300,
6079                             0x860 = 0x210) after PSTDMA off */
6080                         btdm_1AntWifiParaAdjust(padapter, false);
6081                         btdm_1AntSetPSTDMA(padapter, false, 0, false, 0);
6082                 }
6083         }
6084
6085         btdm_1AntBTStateChangeHandler(padapter, pBtCoex8723->prec2hBtInfo,
6086                                       pBtCoex8723->c2hBtInfo);
6087         pBtCoex8723->prec2hBtInfo = pBtCoex8723->c2hBtInfo;
6088 }
6089
6090 void BTDM_1AntSignalCompensation(struct rtw_adapter *padapter,
6091                                  u8 *rssi_wifi, u8 *rssi_bt)
6092 {
6093         struct hal_data_8723a *pHalData;
6094         struct btdm_8723a_1ant *pBtdm8723;
6095         u8 RSSI_WiFi_Cmpnstn, RSSI_BT_Cmpnstn;
6096
6097         pHalData = GET_HAL_DATA(padapter);
6098         pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm1Ant;
6099         RSSI_WiFi_Cmpnstn = 0;
6100         RSSI_BT_Cmpnstn = 0;
6101
6102         switch (pBtdm8723->curPsTdma) {
6103         case 1: /*  WiFi 52ms */
6104                 RSSI_WiFi_Cmpnstn = 11; /*  22*0.48 */
6105                 break;
6106         case 2: /*  WiFi 36ms */
6107                 RSSI_WiFi_Cmpnstn = 14; /*  22*0.64 */
6108                 break;
6109         case 9: /*  WiFi 20ms */
6110                 RSSI_WiFi_Cmpnstn = 18; /*  22*0.80 */
6111                 break;
6112         case 11: /*  WiFi 10ms */
6113                 RSSI_WiFi_Cmpnstn = 20; /*  22*0.90 */
6114                 break;
6115         case 4: /*  WiFi 21ms */
6116                 RSSI_WiFi_Cmpnstn = 17; /*  22*0.79 */
6117                 break;
6118         case 16: /*  WiFi 24ms */
6119                 RSSI_WiFi_Cmpnstn = 18; /*  22*0.76 */
6120                 break;
6121         case 18: /*  WiFi 37ms */
6122                 RSSI_WiFi_Cmpnstn = 14; /*  22*0.64 */
6123                 break;
6124         case 23: /* Level-1, Antenna switch to BT at all time */
6125         case 24: /* Level-2, Antenna switch to BT at all time */
6126         case 25: /* Level-3a, Antenna switch to BT at all time */
6127         case 26: /* Level-3b, Antenna switch to BT at all time */
6128         case 27: /* Level-3b, Antenna switch to BT at all time */
6129         case 33: /* BT SCO & WiFi site survey */
6130                 RSSI_WiFi_Cmpnstn = 22;
6131                 break;
6132         default:
6133                 break;
6134         }
6135
6136         if (rssi_wifi && RSSI_WiFi_Cmpnstn) {
6137                 RTPRINT(FBT, BT_TRACE,
6138                         ("[BTCoex], 1AntSgnlCmpnstn, case %d, WiFiCmpnstn "
6139                          "=%d(%d => %d)\n", pBtdm8723->curPsTdma,
6140                          RSSI_WiFi_Cmpnstn, *rssi_wifi,
6141                          *rssi_wifi+RSSI_WiFi_Cmpnstn));
6142                 *rssi_wifi += RSSI_WiFi_Cmpnstn;
6143         }
6144
6145         if (rssi_bt && RSSI_BT_Cmpnstn) {
6146                 RTPRINT(FBT, BT_TRACE,
6147                         ("[BTCoex], 1AntSgnlCmpnstn, case %d, BTCmpnstn "
6148                          "=%d(%d => %d)\n", pBtdm8723->curPsTdma,
6149                          RSSI_BT_Cmpnstn, *rssi_bt, *rssi_bt+RSSI_BT_Cmpnstn));
6150                 *rssi_bt += RSSI_BT_Cmpnstn;
6151         }
6152 }
6153
6154 static void BTDM_1AntParaInit(struct rtw_adapter *padapter)
6155 {
6156         struct hal_data_8723a *pHalData;
6157         struct bt_coexist_8723a *pBtCoex;
6158         struct btdm_8723a_1ant *pBtdm8723;
6159
6160         pHalData = GET_HAL_DATA(padapter);
6161         pBtCoex = &pHalData->bt_coexist.halCoex8723;
6162         pBtdm8723 = &pBtCoex->btdm1Ant;
6163
6164         /*  Enable counter statistics */
6165         rtl8723au_write8(padapter, 0x76e, 0x4);
6166         btdm_1AntPtaParaReload(padapter);
6167
6168         pBtdm8723->wifiRssiThresh = 48;
6169
6170         pBtdm8723->bWiFiHalt = false;
6171         pBtdm8723->bRAChanged = false;
6172
6173         if ((pBtCoex->c2hBtInfo != BT_INFO_STATE_DISABLED) &&
6174             (pBtCoex->c2hBtInfo != BT_INFO_STATE_NO_CONNECTION)) {
6175                 BTDM_SetSwRfRxLpfCorner(padapter, BT_RF_RX_LPF_CORNER_SHRINK);
6176                 BTDM_AGCTable(padapter, BT_AGCTABLE_ON);
6177                 BTDM_BBBackOffLevel(padapter, BT_BB_BACKOFF_ON);
6178         }
6179 }
6180
6181 static void BTDM_1AntForHalt(struct rtw_adapter *padapter)
6182 {
6183         RTPRINT(FBT, BT_TRACE, ("\n[BTCoex], 1Ant for halt\n"));
6184
6185         GET_HAL_DATA(padapter)->bt_coexist.halCoex8723.btdm1Ant.bWiFiHalt =
6186                 true;
6187
6188         btdm_1AntWifiParaAdjust(padapter, false);
6189
6190         /*  don't use btdm_1AntSetPSTDMA() here */
6191         /*  it will call rtw_set_ps_mode23a() and request pwrpriv->lock. */
6192         /*  This will lead to deadlock, if this function is called in IPS */
6193         /*  Lucas@20130205 */
6194         btdm_1AntPsTdma(padapter, false, 0);
6195
6196         btdm_SetFwIgnoreWlanAct(padapter, true);
6197 }
6198
6199 static void BTDM_1AntLpsLeave(struct rtw_adapter *padapter)
6200 {
6201         RTPRINT(FBT, BT_TRACE, ("\n[BTCoex], 1Ant for LPS Leave\n"));
6202
6203         /*  Prevent from entering LPS again */
6204         GET_HAL_DATA(padapter)->bt_coexist.halCoex8723.btdm1Ant.bWiFiHalt =
6205                 true;
6206
6207         btdm_1AntSetPSTDMA(padapter, false, 0, false, 8);
6208 /*btdm_1AntPsTdma(padapter, false, 8); */
6209 }
6210
6211 static void BTDM_1AntWifiAssociateNotify(struct rtw_adapter *padapter, u8 type)
6212 {
6213         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6214
6215         RTPRINT(FBT, BT_TRACE,
6216                 ("\n[BTCoex], 1Ant for associate, type =%d\n", type));
6217
6218         if (type) {
6219                 rtl8723a_CheckAntenna_Selection(padapter);
6220                 if (!rtl8723a_BT_enabled(padapter))
6221                         btdm_1AntSetPSTDMA(padapter, false, 0, false, 9);
6222                 else {
6223                         struct bt_coexist_8723a *pBtCoex;
6224                         u8 BtState;
6225
6226                         pBtCoex = &pHalData->bt_coexist.halCoex8723;
6227                         BtState = pBtCoex->c2hBtInfo;
6228
6229                         btdm_1AntTSFSwitch(padapter, true);
6230
6231                         if (BtState == BT_INFO_STATE_NO_CONNECTION ||
6232                             BtState == BT_INFO_STATE_CONNECT_IDLE) {
6233                                 btdm_1AntSetPSTDMA(padapter, false, 0,
6234                                                    true, 28);
6235                         } else if (BtState == BT_INFO_STATE_SCO_ONLY_BUSY ||
6236                                    BtState == BT_INFO_STATE_ACL_SCO_BUSY) {
6237                                 btdm_1AntSetPSTDMA(padapter, false, 0,
6238                                                    false, 8);
6239                                 rtl8723au_write32(padapter, 0x6c0, 0x5a5a5a5a);
6240                                 rtl8723au_write32(padapter, 0x6c4, 0x5a5a5a5a);
6241                         } else if (BtState == BT_INFO_STATE_ACL_ONLY_BUSY ||
6242                                    BtState == BT_INFO_STATE_ACL_INQ_OR_PAG) {
6243                                 if (pBtCoex->c2hBtProfile == BT_INFO_HID)
6244                                         btdm_1AntSetPSTDMA(padapter, false, 0,
6245                                                            true, 35);
6246                                 else
6247                                         btdm_1AntSetPSTDMA(padapter, false, 0,
6248                                                            true, 29);
6249                         }
6250                 }
6251         } else {
6252                 if (!rtl8723a_BT_enabled(padapter)) {
6253                         if (!BTDM_IsWifiConnectionExist(padapter)) {
6254                                 btdm_1AntPsTdma(padapter, false, 0);
6255                                 btdm_1AntTSFSwitch(padapter, false);
6256                         }
6257                 }
6258
6259                 btdm_1AntBtCoexistHandler(padapter);
6260         }
6261 }
6262
6263 static void
6264 BTDM_1AntMediaStatusNotify(struct rtw_adapter *padapter,
6265                            enum rt_media_status mstatus)
6266 {
6267         struct bt_coexist_8723a *pBtCoex;
6268
6269         pBtCoex = &GET_HAL_DATA(padapter)->bt_coexist.halCoex8723;
6270
6271         RTPRINT(FBT, BT_TRACE,
6272                 ("\n\n[BTCoex]******************************\n"));
6273         RTPRINT(FBT, BT_TRACE, ("[BTCoex], MediaStatus, WiFi %s !!\n",
6274                         mstatus == RT_MEDIA_CONNECT?"CONNECT":"DISCONNECT"));
6275         RTPRINT(FBT, BT_TRACE, ("[BTCoex]******************************\n"));
6276
6277         if (RT_MEDIA_CONNECT == mstatus) {
6278                 if (check_fwstate(&padapter->mlmepriv, WIFI_STATION_STATE)) {
6279                         if (pBtCoex->c2hBtInfo == BT_INFO_STATE_SCO_ONLY_BUSY ||
6280                             pBtCoex->c2hBtInfo == BT_INFO_STATE_ACL_SCO_BUSY)
6281                                 btdm_1AntUpdateHalRAMaskForSCO(padapter, true);
6282                 }
6283
6284                 padapter->pwrctrlpriv.DelayLPSLastTimeStamp = jiffies;
6285                 BTDM_1AntForDhcp(padapter);
6286         } else {
6287                 /* DBG_8723A("%s rtl8723a_DeinitAntenna_Selection\n",
6288                    __func__); */
6289                 rtl8723a_DeinitAntenna_Selection(padapter);
6290                 btdm_1AntBtCoexistHandler(padapter);
6291                 pBtCoex->btdm1Ant.bRAChanged = false;
6292         }
6293 }
6294
6295 void BTDM_1AntForDhcp(struct rtw_adapter *padapter)
6296 {
6297         struct hal_data_8723a *pHalData;
6298         u8 BtState;
6299         struct bt_coexist_8723a *pBtCoex;
6300         struct btdm_8723a_1ant *pBtdm8723;
6301
6302         pHalData = GET_HAL_DATA(padapter);
6303         pBtCoex = &pHalData->bt_coexist.halCoex8723;
6304         BtState = pBtCoex->c2hBtInfo;
6305         pBtdm8723 = &pBtCoex->btdm1Ant;
6306
6307         RTPRINT(FBT, BT_TRACE, ("\n[BTCoex], 1Ant for DHCP\n"));
6308         RTPRINT(FBT, BT_TRACE, ("[BTCoex], 1Ant for DHCP, WiFi is %s\n",
6309                                 BTDM_IsWifiBusy(padapter)?"Busy":"IDLE"));
6310         RTPRINT(FBT, BT_TRACE, ("[BTCoex], 1Ant for DHCP, %s\n",
6311                                 BtStateString[BtState]));
6312
6313         BTDM_1AntWifiAssociateNotify(padapter, true);
6314 }
6315
6316 static void BTDM_1AntWifiScanNotify(struct rtw_adapter *padapter, u8 scanType)
6317 {
6318         struct hal_data_8723a *pHalData;
6319         u8 BtState;
6320         struct bt_coexist_8723a *pBtCoex;
6321         struct btdm_8723a_1ant *pBtdm8723;
6322
6323         pHalData = GET_HAL_DATA(padapter);
6324         BtState = pHalData->bt_coexist.halCoex8723.c2hBtInfo;
6325         pBtCoex = &pHalData->bt_coexist.halCoex8723;
6326         pBtdm8723 = &pBtCoex->btdm1Ant;
6327
6328         RTPRINT(FBT, BT_TRACE, ("\n[BTCoex], 1Ant for wifi scan =%d!!\n",
6329                                 scanType));
6330         RTPRINT(FBT, BT_TRACE, ("[BTCoex], 1Ant for wifi scan, WiFi is %s\n",
6331                                 BTDM_IsWifiBusy(padapter)?"Busy":"IDLE"));
6332         RTPRINT(FBT, BT_TRACE, ("[BTCoex], 1Ant for wifi scan, %s\n",
6333                                 BtStateString[BtState]));
6334
6335         if (scanType) {
6336                 rtl8723a_CheckAntenna_Selection(padapter);
6337                 if (!rtl8723a_BT_enabled(padapter)) {
6338                         btdm_1AntSetPSTDMA(padapter, false, 0, false, 9);
6339                 } else if (BTDM_IsWifiConnectionExist(padapter) == false) {
6340                         BTDM_1AntWifiAssociateNotify(padapter, true);
6341                 } else {
6342                         if ((BtState == BT_INFO_STATE_SCO_ONLY_BUSY) ||
6343                             (BtState == BT_INFO_STATE_ACL_SCO_BUSY)) {
6344                                 if (pBtCoex->bC2hBtInquiryPage) {
6345                                         btdm_1AntSetPSTDMA(padapter, false, 0,
6346                                                            true, 32);
6347                                 } else {
6348                                         padapter->pwrctrlpriv.btcoex_rfon =
6349                                                 true;
6350                                         btdm_1AntSetPSTDMA(padapter, true, 0,
6351                                                            true, 33);
6352                                 }
6353                         } else if (true == pBtCoex->bC2hBtInquiryPage) {
6354                                 padapter->pwrctrlpriv.btcoex_rfon = true;
6355                                 btdm_1AntSetPSTDMA(padapter, true, 0, true, 30);
6356                         } else if (BtState == BT_INFO_STATE_ACL_ONLY_BUSY) {
6357                                 padapter->pwrctrlpriv.btcoex_rfon = true;
6358                                 if (pBtCoex->c2hBtProfile == BT_INFO_HID)
6359                                         btdm_1AntSetPSTDMA(padapter, true, 0,
6360                                                            true, 34);
6361                                 else
6362                                         btdm_1AntSetPSTDMA(padapter, true, 0,
6363                                                            true, 4);
6364                         } else {
6365                                 padapter->pwrctrlpriv.btcoex_rfon = true;
6366                                 btdm_1AntSetPSTDMA(padapter, true, 0, true, 5);
6367                         }
6368                 }
6369
6370                 btdm_NotifyFwScan(padapter, 1);
6371         } else {
6372                 /*  WiFi_Finish_Scan */
6373                 btdm_NotifyFwScan(padapter, 0);
6374                 btdm_1AntBtCoexistHandler(padapter);
6375         }
6376 }
6377
6378 static void BTDM_1AntFwC2hBtInfo8723A(struct rtw_adapter *padapter)
6379 {
6380         struct hal_data_8723a *pHalData;
6381         struct bt_30info *pBTInfo;
6382         struct bt_mgnt *pBtMgnt;
6383         struct bt_coexist_8723a *pBtCoex;
6384         u8 u1tmp, btState;
6385
6386         pHalData = GET_HAL_DATA(padapter);
6387         pBTInfo = GET_BT_INFO(padapter);
6388         pBtMgnt = &pBTInfo->BtMgnt;
6389         pBtCoex = &pHalData->bt_coexist.halCoex8723;
6390
6391         u1tmp = pBtCoex->c2hBtInfoOriginal;
6392         /*  sco BUSY bit is not used on voice over PCM platform */
6393         btState = u1tmp & 0xF;
6394         pBtCoex->c2hBtProfile = u1tmp & 0xE0;
6395
6396         /*  default set bt to idle state. */
6397         pBtMgnt->ExtConfig.bBTBusy = false;
6398         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_BT_IDLE;
6399
6400         /*  check BIT2 first ==> check if bt is under inquiry or page scan */
6401         if (btState & BIT(2))
6402                 pBtCoex->bC2hBtInquiryPage = true;
6403         else
6404                 pBtCoex->bC2hBtInquiryPage = false;
6405         btState &= ~BIT(2);
6406
6407         if (!(btState & BIT(0)))
6408                 pBtCoex->c2hBtInfo = BT_INFO_STATE_NO_CONNECTION;
6409         else {
6410                 if (btState == 0x1)
6411                         pBtCoex->c2hBtInfo = BT_INFO_STATE_CONNECT_IDLE;
6412                 else if (btState == 0x9) {
6413                         if (pBtCoex->bC2hBtInquiryPage == true)
6414                                 pBtCoex->c2hBtInfo =
6415                                         BT_INFO_STATE_ACL_INQ_OR_PAG;
6416                         else
6417                                 pBtCoex->c2hBtInfo =
6418                                         BT_INFO_STATE_ACL_ONLY_BUSY;
6419                         pBtMgnt->ExtConfig.bBTBusy = true;
6420                 } else if (btState == 0x3) {
6421                         pBtCoex->c2hBtInfo = BT_INFO_STATE_SCO_ONLY_BUSY;
6422                         pBtMgnt->ExtConfig.bBTBusy = true;
6423                 } else if (btState == 0xb) {
6424                         pBtCoex->c2hBtInfo = BT_INFO_STATE_ACL_SCO_BUSY;
6425                         pBtMgnt->ExtConfig.bBTBusy = true;
6426                 } else
6427                         pBtCoex->c2hBtInfo = BT_INFO_STATE_MAX;
6428                 if (pBtMgnt->ExtConfig.bBTBusy)
6429                         pHalData->bt_coexist.CurrentState &=
6430                                 ~BT_COEX_STATE_BT_IDLE;
6431         }
6432
6433         if (BT_INFO_STATE_NO_CONNECTION == pBtCoex->c2hBtInfo ||
6434             BT_INFO_STATE_CONNECT_IDLE == pBtCoex->c2hBtInfo) {
6435                 if (pBtCoex->bC2hBtInquiryPage)
6436                         pBtCoex->c2hBtInfo = BT_INFO_STATE_INQ_OR_PAG;
6437         }
6438
6439         RTPRINT(FBT, BT_TRACE, ("[BTC2H], %s(%d)\n",
6440                         BtStateString[pBtCoex->c2hBtInfo], pBtCoex->c2hBtInfo));
6441
6442         if (pBtCoex->c2hBtProfile != BT_INFO_HID)
6443                 pBtCoex->c2hBtProfile &= ~BT_INFO_HID;
6444 }
6445
6446 void BTDM_1AntBtCoexist8723A(struct rtw_adapter *padapter)
6447 {
6448         struct mlme_priv *pmlmepriv;
6449         struct hal_data_8723a *pHalData;
6450         unsigned long delta_time;
6451
6452         pmlmepriv = &padapter->mlmepriv;
6453         pHalData = GET_HAL_DATA(padapter);
6454
6455         if (check_fwstate(pmlmepriv, WIFI_SITE_MONITOR)) {
6456                 /*  already done in BTDM_1AntForScan() */
6457                 RTPRINT(FBT, BT_TRACE,
6458                         ("[BTCoex], wifi is under scan progress!!\n"));
6459                 return;
6460         }
6461
6462         if (check_fwstate(pmlmepriv, WIFI_UNDER_LINKING)) {
6463                 RTPRINT(FBT, BT_TRACE,
6464                         ("[BTCoex], wifi is under link progress!!\n"));
6465                 return;
6466         }
6467
6468         /*  under DHCP(Special packet) */
6469         delta_time = jiffies - padapter->pwrctrlpriv.DelayLPSLastTimeStamp;
6470         delta_time = jiffies_to_msecs(delta_time);
6471         if (delta_time < 500) {
6472                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], wifi is under DHCP "
6473                                         "progress(%li ms)!!\n", delta_time));
6474                 return;
6475         }
6476
6477         BTDM_CheckWiFiState(padapter);
6478
6479         btdm_1AntBtCoexistHandler(padapter);
6480 }
6481
6482 /*  ===== End of sync from SD7 driver HAL/BTCoexist/HalBtc87231Ant.c ===== */
6483
6484 /*  ===== Below this line is sync from SD7 driver HAL/BTCoexist/HalBtc87232Ant.c ===== */
6485
6486 /*  local function start with btdm_ */
6487 static u8 btdm_ActionAlgorithm(struct rtw_adapter *padapter)
6488 {
6489         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
6490         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
6491         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6492         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
6493         u8 bScoExist = false, bBtLinkExist = false, bBtHsModeExist = false;
6494         u8 algorithm = BT_2ANT_COEX_ALGO_UNDEFINED;
6495
6496         if (pBtMgnt->ExtConfig.NumberOfHandle)
6497                 bBtLinkExist = true;
6498         if (pBtMgnt->ExtConfig.NumberOfSCO)
6499                 bScoExist = true;
6500         if (BT_HsConnectionEstablished(padapter))
6501                 bBtHsModeExist = true;
6502
6503         /*  here we get BT status first */
6504         /*  1) initialize */
6505         pBtdm8723->btStatus = BT_2ANT_BT_STATUS_IDLE;
6506
6507         if ((bScoExist) || (bBtHsModeExist) ||
6508             (BTHCI_CheckProfileExist(padapter, BT_PROFILE_HID))) {
6509                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], SCO or HID or HS exists, set BT non-idle !!!\n"));
6510                 pBtdm8723->btStatus = BT_2ANT_BT_STATUS_NON_IDLE;
6511         } else {
6512                 /*  A2dp profile */
6513                 if ((pBtMgnt->ExtConfig.NumberOfHandle == 1) &&
6514                     (BTHCI_CheckProfileExist(padapter, BT_PROFILE_A2DP))) {
6515                         if (BTDM_BtTxRxCounterL(padapter) < 100) {
6516                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], A2DP, low priority tx+rx < 100, set BT connected-idle!!!\n"));
6517                                 pBtdm8723->btStatus = BT_2ANT_BT_STATUS_CONNECTED_IDLE;
6518                         } else {
6519                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], A2DP, low priority tx+rx >= 100, set BT non-idle!!!\n"));
6520                                 pBtdm8723->btStatus = BT_2ANT_BT_STATUS_NON_IDLE;
6521                         }
6522                 }
6523                 /*  Pan profile */
6524                 if ((pBtMgnt->ExtConfig.NumberOfHandle == 1) &&
6525                     (BTHCI_CheckProfileExist(padapter, BT_PROFILE_PAN))) {
6526                         if (BTDM_BtTxRxCounterL(padapter) < 600) {
6527                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], PAN, low priority tx+rx < 600, set BT connected-idle!!!\n"));
6528                                 pBtdm8723->btStatus = BT_2ANT_BT_STATUS_CONNECTED_IDLE;
6529                         } else {
6530                                 if (pHalData->bt_coexist.halCoex8723.lowPriorityTx) {
6531                                         if ((pHalData->bt_coexist.halCoex8723.lowPriorityRx /
6532                                             pHalData->bt_coexist.halCoex8723.lowPriorityTx) > 9) {
6533                                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], PAN, low priority rx/tx > 9, set BT connected-idle!!!\n"));
6534                                                 pBtdm8723->btStatus = BT_2ANT_BT_STATUS_CONNECTED_IDLE;
6535                                         }
6536                                 }
6537                         }
6538                         if (BT_2ANT_BT_STATUS_CONNECTED_IDLE != pBtdm8723->btStatus) {
6539                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], PAN, set BT non-idle!!!\n"));
6540                                 pBtdm8723->btStatus = BT_2ANT_BT_STATUS_NON_IDLE;
6541                         }
6542                 }
6543                 /*  Pan+A2dp profile */
6544                 if ((pBtMgnt->ExtConfig.NumberOfHandle == 2) &&
6545                     (BTHCI_CheckProfileExist(padapter, BT_PROFILE_A2DP)) &&
6546                     (BTHCI_CheckProfileExist(padapter, BT_PROFILE_PAN))) {
6547                         if (BTDM_BtTxRxCounterL(padapter) < 600) {
6548                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], PAN+A2DP, low priority tx+rx < 600, set BT connected-idle!!!\n"));
6549                                 pBtdm8723->btStatus = BT_2ANT_BT_STATUS_CONNECTED_IDLE;
6550                         } else {
6551                                 if (pHalData->bt_coexist.halCoex8723.lowPriorityTx) {
6552                                         if ((pHalData->bt_coexist.halCoex8723.lowPriorityRx /
6553                                             pHalData->bt_coexist.halCoex8723.lowPriorityTx) > 9) {
6554                                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], PAN+A2DP, low priority rx/tx > 9, set BT connected-idle!!!\n"));
6555                                                 pBtdm8723->btStatus = BT_2ANT_BT_STATUS_CONNECTED_IDLE;
6556                                         }
6557                                 }
6558                         }
6559                         if (BT_2ANT_BT_STATUS_CONNECTED_IDLE != pBtdm8723->btStatus) {
6560                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], PAN+A2DP, set BT non-idle!!!\n"));
6561                                 pBtdm8723->btStatus = BT_2ANT_BT_STATUS_NON_IDLE;
6562                         }
6563                 }
6564         }
6565         if (BT_2ANT_BT_STATUS_IDLE != pBtdm8723->btStatus)
6566                 pBtMgnt->ExtConfig.bBTBusy = true;
6567         else
6568                 pBtMgnt->ExtConfig.bBTBusy = false;
6569
6570         if (!bBtLinkExist) {
6571                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], No profile exists!!!\n"));
6572                 return algorithm;
6573         }
6574
6575         if (pBtMgnt->ExtConfig.NumberOfHandle == 1) {
6576                 if (bScoExist) {
6577                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], SCO only\n"));
6578                         algorithm = BT_2ANT_COEX_ALGO_SCO;
6579                 } else {
6580                         if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_HID)) {
6581                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID only\n"));
6582                                 algorithm = BT_2ANT_COEX_ALGO_HID;
6583                         } else if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_A2DP)) {
6584                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], A2DP only\n"));
6585                                 algorithm = BT_2ANT_COEX_ALGO_A2DP;
6586                         } else if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_PAN)) {
6587                                 if (bBtHsModeExist) {
6588                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], PAN(HS) only\n"));
6589                                         algorithm = BT_2ANT_COEX_ALGO_PANHS;
6590                                 } else {
6591                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], PAN(EDR) only\n"));
6592                                         algorithm = BT_2ANT_COEX_ALGO_PANEDR;
6593                                 }
6594                         } else {
6595                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], Error!!! NO matched profile for NumberOfHandle =%d \n",
6596                                 pBtMgnt->ExtConfig.NumberOfHandle));
6597                         }
6598                 }
6599         } else if (pBtMgnt->ExtConfig.NumberOfHandle == 2) {
6600                 if (bScoExist) {
6601                         if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_HID)) {
6602                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], SCO + HID\n"));
6603                                 algorithm = BT_2ANT_COEX_ALGO_HID;
6604                         } else if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_A2DP)) {
6605                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], Error!!! SCO + A2DP\n"));
6606                         } else if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_PAN)) {
6607                                 if (bBtHsModeExist) {
6608                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], SCO + PAN(HS)\n"));
6609                                         algorithm = BT_2ANT_COEX_ALGO_SCO;
6610                                 } else {
6611                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], SCO + PAN(EDR)\n"));
6612                                         algorithm = BT_2ANT_COEX_ALGO_PANEDR_HID;
6613                                 }
6614                         } else {
6615                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], Error!!! SCO exists but why NO matched ACL profile for NumberOfHandle =%d\n",
6616                                 pBtMgnt->ExtConfig.NumberOfHandle));
6617                         }
6618                 } else {
6619                         if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_HID) &&
6620                             BTHCI_CheckProfileExist(padapter, BT_PROFILE_A2DP)) {
6621                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + A2DP\n"));
6622                                 algorithm = BT_2ANT_COEX_ALGO_HID_A2DP;
6623                 } else if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_HID) &&
6624                            BTHCI_CheckProfileExist(padapter, BT_PROFILE_PAN)) {
6625                                 if (bBtHsModeExist) {
6626                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + PAN(HS)\n"));
6627                                         algorithm = BT_2ANT_COEX_ALGO_HID_A2DP;
6628                                 } else {
6629                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + PAN(EDR)\n"));
6630                                         algorithm = BT_2ANT_COEX_ALGO_PANEDR_HID;
6631                                 }
6632                         } else if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_PAN) &&
6633                                    BTHCI_CheckProfileExist(padapter, BT_PROFILE_A2DP)) {
6634                                 if (bBtHsModeExist) {
6635                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], A2DP + PAN(HS)\n"));
6636                                         algorithm = BT_2ANT_COEX_ALGO_A2DP;
6637                                 } else {
6638                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], A2DP + PAN(EDR)\n"));
6639                                         algorithm = BT_2ANT_COEX_ALGO_PANEDR_A2DP;
6640                                 }
6641                         } else {
6642                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], Error!!! NO matched profile for NumberOfHandle =%d\n",
6643                                         pBtMgnt->ExtConfig.NumberOfHandle));
6644                         }
6645                 }
6646         } else if (pBtMgnt->ExtConfig.NumberOfHandle == 3) {
6647                 if (bScoExist) {
6648                         if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_HID) &&
6649                             BTHCI_CheckProfileExist(padapter, BT_PROFILE_A2DP)) {
6650                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], Error!!! SCO + HID + A2DP\n"));
6651                         } else if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_HID) &&
6652                                    BTHCI_CheckProfileExist(padapter, BT_PROFILE_PAN)) {
6653                                 if (bBtHsModeExist) {
6654                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], SCO + HID + PAN(HS)\n"));
6655                                         algorithm = BT_2ANT_COEX_ALGO_HID_A2DP;
6656                                 } else {
6657                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], SCO + HID + PAN(EDR)\n"));
6658                                         algorithm = BT_2ANT_COEX_ALGO_PANEDR_HID;
6659                                 }
6660                         } else if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_PAN) &&
6661                                    BTHCI_CheckProfileExist(padapter, BT_PROFILE_A2DP)) {
6662                                 if (bBtHsModeExist) {
6663                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Error!!! SCO + A2DP + PAN(HS)\n"));
6664                                         algorithm = BT_2ANT_COEX_ALGO_SCO;
6665                                 } else {
6666                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Error!!! SCO + A2DP + PAN(EDR)\n"));
6667                                 }
6668                         } else {
6669                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], Error!!! SCO exists but why NO matched profile for NumberOfHandle =%d\n",
6670                                         pBtMgnt->ExtConfig.NumberOfHandle));
6671                         }
6672                 } else {
6673                         if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_HID) &&
6674                             BTHCI_CheckProfileExist(padapter, BT_PROFILE_PAN) &&
6675                             BTHCI_CheckProfileExist(padapter, BT_PROFILE_A2DP)) {
6676                                 if (bBtHsModeExist) {
6677                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + A2DP + PAN(HS)\n"));
6678                                         algorithm = BT_2ANT_COEX_ALGO_HID_A2DP_PANHS;
6679                                 } else {
6680                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + A2DP + PAN(EDR)\n"));
6681                                         algorithm = BT_2ANT_COEX_ALGO_HID_A2DP_PANEDR;
6682                                 }
6683                         } else {
6684                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], Error!!! NO matched profile for NumberOfHandle =%d\n",
6685                                         pBtMgnt->ExtConfig.NumberOfHandle));
6686                         }
6687                 }
6688         } else if (pBtMgnt->ExtConfig.NumberOfHandle >= 3) {
6689                 if (bScoExist) {
6690                         if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_HID) &&
6691                             BTHCI_CheckProfileExist(padapter, BT_PROFILE_PAN) &&
6692                             BTHCI_CheckProfileExist(padapter, BT_PROFILE_A2DP)) {
6693                                 if (bBtHsModeExist)
6694                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Error!!! SCO + HID + A2DP + PAN(HS)\n"));
6695                                 else
6696                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Error!!! SCO + HID + A2DP + PAN(EDR)\n"));
6697                         } else {
6698                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], Error!!! SCO exists but why NO matched profile for NumberOfHandle =%d\n",
6699                                         pBtMgnt->ExtConfig.NumberOfHandle));
6700                         }
6701                 } else {
6702                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Error!!! NO matched profile for NumberOfHandle =%d\n",
6703                                 pBtMgnt->ExtConfig.NumberOfHandle));
6704                 }
6705         }
6706         return algorithm;
6707 }
6708
6709 static u8 btdm_NeedToDecBtPwr(struct rtw_adapter *padapter)
6710 {
6711         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6712         u8 bRet = false;
6713
6714         if (BT_Operation(padapter)) {
6715                 if (pHalData->dmpriv.EntryMinUndecoratedSmoothedPWDB > 47) {
6716                         RTPRINT(FBT, BT_TRACE, ("Need to decrease bt power for HS mode!!\n"));
6717                         bRet = true;
6718                 } else {
6719                         RTPRINT(FBT, BT_TRACE, ("NO Need to decrease bt power for HS mode!!\n"));
6720                 }
6721         } else {
6722                 if (BTDM_IsWifiConnectionExist(padapter)) {
6723                         RTPRINT(FBT, BT_TRACE, ("Need to decrease bt power for Wifi is connected!!\n"));
6724                         bRet = true;
6725                 }
6726         }
6727         return bRet;
6728 }
6729
6730 static void
6731 btdm_SetCoexTable(struct rtw_adapter *padapter, u32 val0x6c0,
6732                   u32 val0x6c8, u8 val0x6cc)
6733 {
6734         RTPRINT(FBT, BT_TRACE, ("set coex table, set 0x6c0 = 0x%x\n", val0x6c0));
6735         rtl8723au_write32(padapter, 0x6c0, val0x6c0);
6736
6737         RTPRINT(FBT, BT_TRACE, ("set coex table, set 0x6c8 = 0x%x\n", val0x6c8));
6738         rtl8723au_write32(padapter, 0x6c8, val0x6c8);
6739
6740         RTPRINT(FBT, BT_TRACE, ("set coex table, set 0x6cc = 0x%x\n", val0x6cc));
6741         rtl8723au_write8(padapter, 0x6cc, val0x6cc);
6742 }
6743
6744 static void
6745 btdm_SetSwFullTimeDacSwing(struct rtw_adapter *padapter, u8 bSwDacSwingOn,
6746                            u32 swDacSwingLvl)
6747 {
6748         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6749
6750         if (bSwDacSwingOn) {
6751                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], SwDacSwing = 0x%x\n", swDacSwingLvl));
6752                 PHY_SetBBReg(padapter, 0x880, 0xff000000, swDacSwingLvl);
6753                 pHalData->bt_coexist.bSWCoexistAllOff = false;
6754         } else {
6755                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], SwDacSwing Off!\n"));
6756                 PHY_SetBBReg(padapter, 0x880, 0xff000000, 0xc0);
6757         }
6758 }
6759
6760 static void
6761 btdm_SetFwDacSwingLevel(struct rtw_adapter *padapter, u8 dacSwingLvl)
6762 {
6763         u8 H2C_Parameter[1] = {0};
6764
6765         H2C_Parameter[0] = dacSwingLvl;
6766
6767         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Set Dac Swing Level = 0x%x\n", dacSwingLvl));
6768         RTPRINT(FBT, BT_TRACE, ("[BTCoex], write 0x29 = 0x%x\n", H2C_Parameter[0]));
6769
6770         FillH2CCmd(padapter, 0x29, 1, H2C_Parameter);
6771 }
6772
6773 static void btdm_2AntDecBtPwr(struct rtw_adapter *padapter, u8 bDecBtPwr)
6774 {
6775         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6776         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
6777
6778         RTPRINT(FBT, BT_TRACE,
6779                 ("[BTCoex], Dec BT power = %s\n",
6780                 ((bDecBtPwr) ? "ON" : "OFF")));
6781         pBtdm8723->bCurDecBtPwr = bDecBtPwr;
6782
6783         if (pBtdm8723->bPreDecBtPwr == pBtdm8723->bCurDecBtPwr)
6784                 return;
6785
6786         BTDM_SetFwDecBtPwr(padapter, pBtdm8723->bCurDecBtPwr);
6787
6788         pBtdm8723->bPreDecBtPwr = pBtdm8723->bCurDecBtPwr;
6789 }
6790
6791 static void
6792 btdm_2AntFwDacSwingLvl(struct rtw_adapter *padapter, u8 fwDacSwingLvl)
6793 {
6794         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6795         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
6796
6797         RTPRINT(FBT, BT_TRACE, ("[BTCoex], set FW Dac Swing level = %d\n",  fwDacSwingLvl));
6798         pBtdm8723->curFwDacSwingLvl = fwDacSwingLvl;
6799
6800         /* RTPRINT(FBT, BT_TRACE, ("[BTCoex], preFwDacSwingLvl =%d, curFwDacSwingLvl =%d\n", */
6801         /*pBtdm8723->preFwDacSwingLvl, pBtdm8723->curFwDacSwingLvl)); */
6802
6803         if (pBtdm8723->preFwDacSwingLvl == pBtdm8723->curFwDacSwingLvl)
6804                 return;
6805
6806         btdm_SetFwDacSwingLevel(padapter, pBtdm8723->curFwDacSwingLvl);
6807
6808         pBtdm8723->preFwDacSwingLvl = pBtdm8723->curFwDacSwingLvl;
6809 }
6810
6811 static void
6812 btdm_2AntRfShrink(struct rtw_adapter *padapter, u8 bRxRfShrinkOn)
6813 {
6814         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6815         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
6816
6817         RTPRINT(FBT, BT_TRACE,
6818                 ("[BTCoex], turn Rx RF Shrink = %s\n",
6819                 ((bRxRfShrinkOn) ? "ON" : "OFF")));
6820         pBtdm8723->bCurRfRxLpfShrink = bRxRfShrinkOn;
6821
6822         /* RTPRINT(FBT, BT_TRACE, ("[BTCoex], bPreRfRxLpfShrink =%d, bCurRfRxLpfShrink =%d\n", */
6823         /*pBtdm8723->bPreRfRxLpfShrink, pBtdm8723->bCurRfRxLpfShrink)); */
6824
6825         if (pBtdm8723->bPreRfRxLpfShrink == pBtdm8723->bCurRfRxLpfShrink)
6826                 return;
6827
6828         BTDM_SetSwRfRxLpfCorner(padapter, (u8)pBtdm8723->bCurRfRxLpfShrink);
6829
6830         pBtdm8723->bPreRfRxLpfShrink = pBtdm8723->bCurRfRxLpfShrink;
6831 }
6832
6833 static void
6834 btdm_2AntLowPenaltyRa(struct rtw_adapter *padapter, u8 bLowPenaltyRa)
6835 {
6836         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6837         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
6838
6839         RTPRINT(FBT, BT_TRACE,
6840                 ("[BTCoex], turn LowPenaltyRA = %s\n",
6841                 ((bLowPenaltyRa) ? "ON" : "OFF")));
6842         pBtdm8723->bCurLowPenaltyRa = bLowPenaltyRa;
6843
6844         /* RTPRINT(FBT, BT_TRACE, ("[BTCoex], bPreLowPenaltyRa =%d, bCurLowPenaltyRa =%d\n", */
6845         /*pBtdm8723->bPreLowPenaltyRa, pBtdm8723->bCurLowPenaltyRa)); */
6846
6847         if (pBtdm8723->bPreLowPenaltyRa == pBtdm8723->bCurLowPenaltyRa)
6848                 return;
6849
6850         BTDM_SetSwPenaltyTxRateAdaptive(padapter, (u8)pBtdm8723->bCurLowPenaltyRa);
6851
6852         pBtdm8723->bPreLowPenaltyRa = pBtdm8723->bCurLowPenaltyRa;
6853 }
6854
6855 static void
6856 btdm_2AntDacSwing(struct rtw_adapter *padapter,
6857                   u8 bDacSwingOn, u32 dacSwingLvl)
6858 {
6859         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6860         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
6861
6862         RTPRINT(FBT, BT_TRACE,
6863                 ("[BTCoex], turn DacSwing =%s, dacSwingLvl = 0x%x\n",
6864                 (bDacSwingOn ? "ON" : "OFF"), dacSwingLvl));
6865         pBtdm8723->bCurDacSwingOn = bDacSwingOn;
6866         pBtdm8723->curDacSwingLvl = dacSwingLvl;
6867
6868         if ((pBtdm8723->bPreDacSwingOn == pBtdm8723->bCurDacSwingOn) &&
6869             (pBtdm8723->preDacSwingLvl == pBtdm8723->curDacSwingLvl))
6870                 return;
6871
6872         mdelay(30);
6873         btdm_SetSwFullTimeDacSwing(padapter, bDacSwingOn, dacSwingLvl);
6874
6875         pBtdm8723->bPreDacSwingOn = pBtdm8723->bCurDacSwingOn;
6876         pBtdm8723->preDacSwingLvl = pBtdm8723->curDacSwingLvl;
6877 }
6878
6879 static void btdm_2AntAdcBackOff(struct rtw_adapter *padapter, u8 bAdcBackOff)
6880 {
6881         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6882         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
6883
6884         RTPRINT(FBT, BT_TRACE,
6885                 ("[BTCoex], turn AdcBackOff = %s\n",
6886                 ((bAdcBackOff) ? "ON" : "OFF")));
6887         pBtdm8723->bCurAdcBackOff = bAdcBackOff;
6888
6889         if (pBtdm8723->bPreAdcBackOff == pBtdm8723->bCurAdcBackOff)
6890                 return;
6891
6892         BTDM_BBBackOffLevel(padapter, (u8)pBtdm8723->bCurAdcBackOff);
6893
6894         pBtdm8723->bPreAdcBackOff = pBtdm8723->bCurAdcBackOff;
6895 }
6896
6897 static void btdm_2AntAgcTable(struct rtw_adapter *padapter, u8 bAgcTableEn)
6898 {
6899         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6900         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
6901
6902         RTPRINT(FBT, BT_TRACE,
6903                 ("[BTCoex], %s Agc Table\n", ((bAgcTableEn) ? "Enable" : "Disable")));
6904         pBtdm8723->bCurAgcTableEn = bAgcTableEn;
6905
6906         /* RTPRINT(FBT, BT_TRACE, ("[BTCoex], bPreAgcTableEn =%d, bCurAgcTableEn =%d\n", */
6907         /*pBtdm8723->bPreAgcTableEn, pBtdm8723->bCurAgcTableEn)); */
6908
6909         if (pBtdm8723->bPreAgcTableEn == pBtdm8723->bCurAgcTableEn)
6910                 return;
6911
6912         BTDM_AGCTable(padapter, (u8)bAgcTableEn);
6913
6914         pBtdm8723->bPreAgcTableEn = pBtdm8723->bCurAgcTableEn;
6915 }
6916
6917 static void
6918 btdm_2AntCoexTable(struct rtw_adapter *padapter,
6919                    u32 val0x6c0, u32 val0x6c8, u8 val0x6cc)
6920 {
6921         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6922         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
6923
6924         RTPRINT(FBT, BT_TRACE, ("[BTCoex], write Coex Table 0x6c0 = 0x%x, 0x6c8 = 0x%x, 0x6cc = 0x%x\n",
6925                 val0x6c0, val0x6c8, val0x6cc));
6926         pBtdm8723->curVal0x6c0 = val0x6c0;
6927         pBtdm8723->curVal0x6c8 = val0x6c8;
6928         pBtdm8723->curVal0x6cc = val0x6cc;
6929
6930         /* RTPRINT(FBT, BT_TRACE, ("[BTCoex], preVal0x6c0 = 0x%x, preVal0x6c8 = 0x%x, preVal0x6cc = 0x%x !!\n", */
6931         /*pBtdm8723->preVal0x6c0, pBtdm8723->preVal0x6c8, pBtdm8723->preVal0x6cc)); */
6932         /* RTPRINT(FBT, BT_TRACE, ("[BTCoex], curVal0x6c0 = 0x%x, curVal0x6c8 = 0x%x, curVal0x6cc = 0x%x !!\n", */
6933         /*pBtdm8723->curVal0x6c0, pBtdm8723->curVal0x6c8, pBtdm8723->curVal0x6cc)); */
6934
6935         if ((pBtdm8723->preVal0x6c0 == pBtdm8723->curVal0x6c0) &&
6936             (pBtdm8723->preVal0x6c8 == pBtdm8723->curVal0x6c8) &&
6937             (pBtdm8723->preVal0x6cc == pBtdm8723->curVal0x6cc))
6938                 return;
6939
6940         btdm_SetCoexTable(padapter, val0x6c0, val0x6c8, val0x6cc);
6941
6942         pBtdm8723->preVal0x6c0 = pBtdm8723->curVal0x6c0;
6943         pBtdm8723->preVal0x6c8 = pBtdm8723->curVal0x6c8;
6944         pBtdm8723->preVal0x6cc = pBtdm8723->curVal0x6cc;
6945 }
6946
6947 static void btdm_2AntIgnoreWlanAct(struct rtw_adapter *padapter, u8 bEnable)
6948 {
6949         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6950         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
6951
6952         RTPRINT(FBT, BT_TRACE,
6953                 ("[BTCoex], turn Ignore WlanAct %s\n", (bEnable ? "ON" : "OFF")));
6954         pBtdm8723->bCurIgnoreWlanAct = bEnable;
6955
6956
6957         if (pBtdm8723->bPreIgnoreWlanAct == pBtdm8723->bCurIgnoreWlanAct)
6958                 return;
6959
6960         btdm_SetFwIgnoreWlanAct(padapter, bEnable);
6961         pBtdm8723->bPreIgnoreWlanAct = pBtdm8723->bCurIgnoreWlanAct;
6962 }
6963
6964 static void
6965 btdm_2AntSetFw3a(struct rtw_adapter *padapter, u8 byte1, u8 byte2,
6966                  u8 byte3, u8 byte4, u8 byte5)
6967 {
6968         u8 H2C_Parameter[5] = {0};
6969
6970         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6971
6972         /*  byte1[1:0] != 0 means enable pstdma */
6973         /*  for 2Ant bt coexist, if byte1 != 0 means enable pstdma */
6974         if (byte1)
6975                 pHalData->bt_coexist.bFWCoexistAllOff = false;
6976         H2C_Parameter[0] = byte1;
6977         H2C_Parameter[1] = byte2;
6978         H2C_Parameter[2] = byte3;
6979         H2C_Parameter[3] = byte4;
6980         H2C_Parameter[4] = byte5;
6981
6982         pHalData->bt_coexist.fw3aVal[0] = byte1;
6983         pHalData->bt_coexist.fw3aVal[1] = byte2;
6984         pHalData->bt_coexist.fw3aVal[2] = byte3;
6985         pHalData->bt_coexist.fw3aVal[3] = byte4;
6986         pHalData->bt_coexist.fw3aVal[4] = byte5;
6987
6988         RTPRINT(FBT, BT_TRACE, ("[BTCoex], FW write 0x3a(5bytes) = 0x%x%08x\n",
6989                 H2C_Parameter[0],
6990                 H2C_Parameter[1]<<24|H2C_Parameter[2]<<16|H2C_Parameter[3]<<8|H2C_Parameter[4]));
6991
6992         FillH2CCmd(padapter, 0x3a, 5, H2C_Parameter);
6993         }
6994
6995 static void btdm_2AntPsTdma(struct rtw_adapter *padapter, u8 bTurnOn, u8 type)
6996 {
6997         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
6998         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
6999         u32                     btTxRxCnt = 0;
7000         u8 bTurnOnByCnt = false;
7001         u8 psTdmaTypeByCnt = 0;
7002
7003         btTxRxCnt = BTDM_BtTxRxCounterH(padapter)+BTDM_BtTxRxCounterL(padapter);
7004         RTPRINT(FBT, BT_TRACE, ("[BTCoex], BT TxRx Counters = %d\n", btTxRxCnt));
7005         if (btTxRxCnt > 3000) {
7006                 bTurnOnByCnt = true;
7007                 psTdmaTypeByCnt = 8;
7008
7009                 RTPRINT(FBT, BT_TRACE,
7010                         ("[BTCoex], For BTTxRxCounters, turn %s PS TDMA, type =%d\n",
7011                         (bTurnOnByCnt ? "ON" : "OFF"), psTdmaTypeByCnt));
7012                 pBtdm8723->bCurPsTdmaOn = bTurnOnByCnt;
7013                 pBtdm8723->curPsTdma = psTdmaTypeByCnt;
7014         } else {
7015                 RTPRINT(FBT, BT_TRACE,
7016                         ("[BTCoex], turn %s PS TDMA, type =%d\n",
7017                         (bTurnOn ? "ON" : "OFF"), type));
7018                 pBtdm8723->bCurPsTdmaOn = bTurnOn;
7019                 pBtdm8723->curPsTdma = type;
7020         }
7021
7022         if ((pBtdm8723->bPrePsTdmaOn == pBtdm8723->bCurPsTdmaOn) &&
7023             (pBtdm8723->prePsTdma == pBtdm8723->curPsTdma))
7024                 return;
7025
7026         if (bTurnOn) {
7027                 switch (type) {
7028                 case 1:
7029                 default:
7030                         btdm_2AntSetFw3a(padapter, 0xe3, 0x1a, 0x1a, 0xa1, 0x98);
7031                         break;
7032                 case 2:
7033                         btdm_2AntSetFw3a(padapter, 0xe3, 0x12, 0x12, 0xa1, 0x98);
7034                         break;
7035                 case 3:
7036                         btdm_2AntSetFw3a(padapter, 0xe3, 0xa, 0xa, 0xa1, 0x98);
7037                         break;
7038                 case 4:
7039                         btdm_2AntSetFw3a(padapter, 0xa3, 0x5, 0x5, 0xa1, 0x80);
7040                         break;
7041                 case 5:
7042                         btdm_2AntSetFw3a(padapter, 0xe3, 0x1a, 0x1a, 0x20, 0x98);
7043                         break;
7044                 case 6:
7045                         btdm_2AntSetFw3a(padapter, 0xe3, 0x12, 0x12, 0x20, 0x98);
7046                         break;
7047                 case 7:
7048                         btdm_2AntSetFw3a(padapter, 0xe3, 0xa, 0xa, 0x20, 0x98);
7049                         break;
7050                 case 8:
7051                         btdm_2AntSetFw3a(padapter, 0xa3, 0x5, 0x5, 0x20, 0x80);
7052                         break;
7053                 case 9:
7054                         btdm_2AntSetFw3a(padapter, 0xe3, 0x1a, 0x1a, 0xa1, 0x98);
7055                         break;
7056                 case 10:
7057                         btdm_2AntSetFw3a(padapter, 0xe3, 0x12, 0x12, 0xa1, 0x98);
7058                         break;
7059                 case 11:
7060                         btdm_2AntSetFw3a(padapter, 0xe3, 0xa, 0xa, 0xa1, 0x98);
7061                         break;
7062                 case 12:
7063                         btdm_2AntSetFw3a(padapter, 0xe3, 0x5, 0x5, 0xa1, 0x98);
7064                         break;
7065                 case 13:
7066                         btdm_2AntSetFw3a(padapter, 0xe3, 0x1a, 0x1a, 0x20, 0x98);
7067                         break;
7068                 case 14:
7069                         btdm_2AntSetFw3a(padapter, 0xe3, 0x12, 0x12, 0x20, 0x98);
7070                         break;
7071                 case 15:
7072                         btdm_2AntSetFw3a(padapter, 0xe3, 0xa, 0xa, 0x20, 0x98);
7073                         break;
7074                 case 16:
7075                         btdm_2AntSetFw3a(padapter, 0xe3, 0x5, 0x5, 0x20, 0x98);
7076                         break;
7077                 case 17:
7078                         btdm_2AntSetFw3a(padapter, 0xa3, 0x2f, 0x2f, 0x20, 0x80);
7079                         break;
7080                 case 18:
7081                         btdm_2AntSetFw3a(padapter, 0xe3, 0x5, 0x5, 0xa1, 0x98);
7082                         break;
7083                 case 19:
7084                         btdm_2AntSetFw3a(padapter, 0xe3, 0x25, 0x25, 0xa1, 0x98);
7085                         break;
7086                 case 20:
7087                         btdm_2AntSetFw3a(padapter, 0xe3, 0x25, 0x25, 0x20, 0x98);
7088                         break;
7089                 }
7090         } else {
7091                 /*  disable PS tdma */
7092                 switch (type) {
7093                 case 0:
7094                         btdm_2AntSetFw3a(padapter, 0x0, 0x0, 0x0, 0x8, 0x0);
7095                         break;
7096                 case 1:
7097                         btdm_2AntSetFw3a(padapter, 0x0, 0x0, 0x0, 0x0, 0x0);
7098                         break;
7099                 default:
7100                         btdm_2AntSetFw3a(padapter, 0x0, 0x0, 0x0, 0x8, 0x0);
7101                         break;
7102                 }
7103         }
7104
7105         /*  update pre state */
7106         pBtdm8723->bPrePsTdmaOn =  pBtdm8723->bCurPsTdmaOn;
7107         pBtdm8723->prePsTdma = pBtdm8723->curPsTdma;
7108 }
7109
7110 static void btdm_2AntBtInquiryPage(struct rtw_adapter *padapter)
7111 {
7112         btdm_2AntCoexTable(padapter, 0x55555555, 0xffff, 0x3);
7113         btdm_2AntIgnoreWlanAct(padapter, false);
7114         btdm_2AntPsTdma(padapter, true, 8);
7115 }
7116
7117 static u8 btdm_HoldForBtInqPage(struct rtw_adapter *padapter)
7118 {
7119         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
7120         u32 curTime = jiffies;
7121
7122         if (pHalData->bt_coexist.halCoex8723.bC2hBtInquiryPage) {
7123                 /*  bt inquiry or page is started. */
7124                 if (pHalData->bt_coexist.halCoex8723.btInqPageStartTime == 0) {
7125                         pHalData->bt_coexist.halCoex8723.btInqPageStartTime = curTime;
7126                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], BT Inquiry/page is started at time : 0x%lx \n",
7127                         pHalData->bt_coexist.halCoex8723.btInqPageStartTime));
7128                 }
7129         }
7130         RTPRINT(FBT, BT_TRACE, ("[BTCoex], BT Inquiry/page started time : 0x%lx, curTime : 0x%x \n",
7131                 pHalData->bt_coexist.halCoex8723.btInqPageStartTime, curTime));
7132
7133         if (pHalData->bt_coexist.halCoex8723.btInqPageStartTime) {
7134                 if (((curTime - pHalData->bt_coexist.halCoex8723.btInqPageStartTime)/1000000) >= 10) {
7135                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], BT Inquiry/page >= 10sec!!!"));
7136                         pHalData->bt_coexist.halCoex8723.btInqPageStartTime = 0;
7137                 }
7138         }
7139
7140         if (pHalData->bt_coexist.halCoex8723.btInqPageStartTime) {
7141                 btdm_2AntCoexTable(padapter, 0x55555555, 0xffff, 0x3);
7142                 btdm_2AntIgnoreWlanAct(padapter, false);
7143                 btdm_2AntPsTdma(padapter, true, 8);
7144                 return true;
7145         } else {
7146                 return false;
7147         }
7148 }
7149
7150 static u8 btdm_Is2Ant8723ACommonAction(struct rtw_adapter *padapter)
7151 {
7152         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
7153         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
7154         u8 bCommon = false;
7155
7156         RTPRINT(FBT, BT_TRACE, ("%s :BTDM_IsWifiConnectionExist =%x check_fwstate =%x pmlmepriv->fw_state = 0x%x\n", __func__, BTDM_IsWifiConnectionExist(padapter), check_fwstate(&padapter->mlmepriv, (_FW_UNDER_SURVEY|_FW_UNDER_LINKING)), padapter->mlmepriv.fw_state));
7157
7158         if ((!BTDM_IsWifiConnectionExist(padapter)) &&
7159             (!check_fwstate(&padapter->mlmepriv, (_FW_UNDER_SURVEY|_FW_UNDER_LINKING))) &&
7160             (BT_2ANT_BT_STATUS_IDLE == pBtdm8723->btStatus)) {
7161                 RTPRINT(FBT, BT_TRACE, ("Wifi idle + Bt idle!!\n"));
7162
7163                 btdm_2AntLowPenaltyRa(padapter, false);
7164                 btdm_2AntRfShrink(padapter, false);
7165                 btdm_2AntCoexTable(padapter, 0x55555555, 0xffff, 0x3);
7166
7167                 btdm_2AntIgnoreWlanAct(padapter, false);
7168                 btdm_2AntPsTdma(padapter, false, 0);
7169                 btdm_2AntFwDacSwingLvl(padapter, 0x20);
7170                 btdm_2AntDecBtPwr(padapter, false);
7171
7172                 btdm_2AntAgcTable(padapter, false);
7173                 btdm_2AntAdcBackOff(padapter, false);
7174                 btdm_2AntDacSwing(padapter, false, 0xc0);
7175
7176                 bCommon = true;
7177         } else if (((BTDM_IsWifiConnectionExist(padapter)) ||
7178                    (check_fwstate(&padapter->mlmepriv, (_FW_UNDER_SURVEY|_FW_UNDER_LINKING)))) &&
7179                    (BT_2ANT_BT_STATUS_IDLE == pBtdm8723->btStatus)) {
7180                 RTPRINT(FBT, BT_TRACE, ("Wifi non-idle + BT idle!!\n"));
7181
7182                 btdm_2AntLowPenaltyRa(padapter, true);
7183                 btdm_2AntRfShrink(padapter, false);
7184                 btdm_2AntCoexTable(padapter, 0x55555555, 0xffff, 0x3);
7185
7186                 btdm_2AntIgnoreWlanAct(padapter, false);
7187                 btdm_2AntPsTdma(padapter, false, 0);
7188                 btdm_2AntFwDacSwingLvl(padapter, 0x20);
7189                 btdm_2AntDecBtPwr(padapter, true);
7190
7191                 btdm_2AntAgcTable(padapter, false);
7192                 btdm_2AntAdcBackOff(padapter, false);
7193                 btdm_2AntDacSwing(padapter, false, 0xc0);
7194
7195                 bCommon = true;
7196         } else if ((!BTDM_IsWifiConnectionExist(padapter)) &&
7197                    (!check_fwstate(&padapter->mlmepriv, (_FW_UNDER_SURVEY|_FW_UNDER_LINKING))) &&
7198                    (BT_2ANT_BT_STATUS_CONNECTED_IDLE == pBtdm8723->btStatus)) {
7199                 RTPRINT(FBT, BT_TRACE, ("Wifi idle + Bt connected idle!!\n"));
7200
7201                 btdm_2AntLowPenaltyRa(padapter, true);
7202                 btdm_2AntRfShrink(padapter, true);
7203                 btdm_2AntCoexTable(padapter, 0x55555555, 0xffff, 0x3);
7204
7205                 btdm_2AntIgnoreWlanAct(padapter, false);
7206                 btdm_2AntPsTdma(padapter, false, 0);
7207                 btdm_2AntFwDacSwingLvl(padapter, 0x20);
7208                 btdm_2AntDecBtPwr(padapter, false);
7209
7210                 btdm_2AntAgcTable(padapter, false);
7211                 btdm_2AntAdcBackOff(padapter, false);
7212                 btdm_2AntDacSwing(padapter, false, 0xc0);
7213
7214                 bCommon = true;
7215         } else if (((BTDM_IsWifiConnectionExist(padapter)) ||
7216                    (check_fwstate(&padapter->mlmepriv, (_FW_UNDER_SURVEY|_FW_UNDER_LINKING)))) &&
7217                    (BT_2ANT_BT_STATUS_CONNECTED_IDLE == pBtdm8723->btStatus)) {
7218                 RTPRINT(FBT, BT_TRACE, ("Wifi non-idle + Bt connected idle!!\n"));
7219
7220                 btdm_2AntLowPenaltyRa(padapter, true);
7221                 btdm_2AntRfShrink(padapter, true);
7222                 btdm_2AntCoexTable(padapter, 0x55555555, 0xffff, 0x3);
7223
7224                 btdm_2AntIgnoreWlanAct(padapter, false);
7225                 btdm_2AntPsTdma(padapter, false, 0);
7226                 btdm_2AntFwDacSwingLvl(padapter, 0x20);
7227                 btdm_2AntDecBtPwr(padapter, true);
7228
7229                 btdm_2AntAgcTable(padapter, false);
7230                 btdm_2AntAdcBackOff(padapter, false);
7231                 btdm_2AntDacSwing(padapter, false, 0xc0);
7232
7233                 bCommon = true;
7234         } else if ((!BTDM_IsWifiConnectionExist(padapter)) &&
7235                    (!check_fwstate(&padapter->mlmepriv, (_FW_UNDER_SURVEY|_FW_UNDER_LINKING))) &&
7236                    (BT_2ANT_BT_STATUS_NON_IDLE == pBtdm8723->btStatus)) {
7237                 RTPRINT(FBT, BT_TRACE, ("Wifi idle + BT non-idle!!\n"));
7238
7239                 btdm_2AntLowPenaltyRa(padapter, true);
7240                 btdm_2AntRfShrink(padapter, true);
7241                 btdm_2AntCoexTable(padapter, 0x55555555, 0xffff, 0x3);
7242
7243                 btdm_2AntIgnoreWlanAct(padapter, false);
7244                 btdm_2AntPsTdma(padapter, false, 0);
7245                 btdm_2AntFwDacSwingLvl(padapter, 0x20);
7246                 btdm_2AntDecBtPwr(padapter, false);
7247
7248                 btdm_2AntAgcTable(padapter, false);
7249                 btdm_2AntAdcBackOff(padapter, false);
7250                 btdm_2AntDacSwing(padapter, false, 0xc0);
7251
7252                 bCommon = true;
7253         } else {
7254                 RTPRINT(FBT, BT_TRACE, ("Wifi non-idle + BT non-idle!!\n"));
7255                 btdm_2AntLowPenaltyRa(padapter, true);
7256                 btdm_2AntRfShrink(padapter, true);
7257                 btdm_2AntCoexTable(padapter, 0x55555555, 0xffff, 0x3);
7258                 btdm_2AntIgnoreWlanAct(padapter, false);
7259                 btdm_2AntFwDacSwingLvl(padapter, 0x20);
7260
7261                 bCommon = false;
7262         }
7263         return bCommon;
7264 }
7265
7266 static void
7267 btdm_2AntTdmaDurationAdjust(struct rtw_adapter *padapter, u8 bScoHid,
7268                             u8 bTxPause, u8 maxInterval)
7269 {
7270         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
7271         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
7272         static s32              up, dn, m, n, WaitCount;
7273         s32                     result;   /* 0: no change, +1: increase WiFi duration, -1: decrease WiFi duration */
7274         u8 retryCount = 0;
7275
7276         RTPRINT(FBT, BT_TRACE, ("[BTCoex], TdmaDurationAdjust()\n"));
7277
7278         if (pBtdm8723->bResetTdmaAdjust) {
7279                 pBtdm8723->bResetTdmaAdjust = false;
7280                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], first run TdmaDurationAdjust()!!\n"));
7281                 if (bScoHid) {
7282                         if (bTxPause) {
7283                                 if (maxInterval == 1) {
7284                                         btdm_2AntPsTdma(padapter, true, 15);
7285                                         pBtdm8723->psTdmaDuAdjType = 15;
7286                                 } else if (maxInterval == 2) {
7287                                         btdm_2AntPsTdma(padapter, true, 15);
7288                                         pBtdm8723->psTdmaDuAdjType = 15;
7289                                 } else if (maxInterval == 3) {
7290                                         btdm_2AntPsTdma(padapter, true, 15);
7291                                         pBtdm8723->psTdmaDuAdjType = 15;
7292                                 } else {
7293                                         btdm_2AntPsTdma(padapter, true, 15);
7294                                         pBtdm8723->psTdmaDuAdjType = 15;
7295                                 }
7296                         } else {
7297                                 if (maxInterval == 1) {
7298                                         btdm_2AntPsTdma(padapter, true, 11);
7299                                         pBtdm8723->psTdmaDuAdjType = 11;
7300                                 } else if (maxInterval == 2) {
7301                                         btdm_2AntPsTdma(padapter, true, 11);
7302                                         pBtdm8723->psTdmaDuAdjType = 11;
7303                                 } else if (maxInterval == 3) {
7304                                         btdm_2AntPsTdma(padapter, true, 11);
7305                                         pBtdm8723->psTdmaDuAdjType = 11;
7306                                 } else {
7307                                         btdm_2AntPsTdma(padapter, true, 11);
7308                                         pBtdm8723->psTdmaDuAdjType = 11;
7309                                 }
7310                         }
7311                 } else {
7312                         if (bTxPause) {
7313                                 if (maxInterval == 1) {
7314                                         btdm_2AntPsTdma(padapter, true, 7);
7315                                         pBtdm8723->psTdmaDuAdjType = 7;
7316                                 } else if (maxInterval == 2) {
7317                                         btdm_2AntPsTdma(padapter, true, 7);
7318                                         pBtdm8723->psTdmaDuAdjType = 7;
7319                                 } else if (maxInterval == 3) {
7320                                         btdm_2AntPsTdma(padapter, true, 7);
7321                                         pBtdm8723->psTdmaDuAdjType = 7;
7322                                 } else {
7323                                         btdm_2AntPsTdma(padapter, true, 7);
7324                                         pBtdm8723->psTdmaDuAdjType = 7;
7325                                 }
7326                         } else {
7327                                 if (maxInterval == 1) {
7328                                         btdm_2AntPsTdma(padapter, true, 3);
7329                                         pBtdm8723->psTdmaDuAdjType = 3;
7330                                 } else if (maxInterval == 2) {
7331                                         btdm_2AntPsTdma(padapter, true, 3);
7332                                         pBtdm8723->psTdmaDuAdjType = 3;
7333                                 } else if (maxInterval == 3) {
7334                                         btdm_2AntPsTdma(padapter, true, 3);
7335                                         pBtdm8723->psTdmaDuAdjType = 3;
7336                                 } else {
7337                                         btdm_2AntPsTdma(padapter, true, 3);
7338                                         pBtdm8723->psTdmaDuAdjType = 3;
7339                                 }
7340                         }
7341                 }
7342                 up = 0;
7343                 dn = 0;
7344                 m = 1;
7345                 n = 3;
7346                 result = 0;
7347                 WaitCount = 0;
7348         } else {
7349                 /* accquire the BT TRx retry count from BT_Info byte2 */
7350                 retryCount = pHalData->bt_coexist.halCoex8723.btRetryCnt;
7351                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], retryCount = %d\n", retryCount));
7352                 result = 0;
7353                 WaitCount++;
7354
7355                 if (retryCount == 0) {  /*  no retry in the last 2-second duration */
7356                         up++;
7357                         dn--;
7358
7359                         if (dn <= 0)
7360                                 dn = 0;
7361
7362                         if (up >= n) {  /*  if ³sÄò n ­Ó2¬í retry count¬°0, «h½Õ¼eWiFi duration */
7363                                 WaitCount = 0;
7364                                 n = 3;
7365                                 up = 0;
7366                                 dn = 0;
7367                                 result = 1;
7368                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], Increase wifi duration!!\n"));
7369                         }
7370                 } else if (retryCount <= 3) {   /*  <= 3 retry in the last 2-second duration */
7371                         up--;
7372                         dn++;
7373
7374                         if (up <= 0)
7375                                 up = 0;
7376
7377                         if (dn == 2) {  /*  if ³sÄò 2 ­Ó2¬í retry count< 3, «h½Õ¯¶WiFi duration */
7378                                 if (WaitCount <= 2)
7379                                         m++; /*  ÁקK¤@ª½¦b¨â­Ólevel¤¤¨Ó¦^ */
7380                                 else
7381                                         m = 1;
7382
7383                                 if (m >= 20) /* m ³Ì¤j­È = 20 ' ³Ì¤j120¬í recheck¬O§_½Õ¾ã WiFi duration. */
7384                                         m = 20;
7385
7386                                 n = 3*m;
7387                                 up = 0;
7388                                 dn = 0;
7389                                 WaitCount = 0;
7390                                 result = -1;
7391                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], Decrease wifi duration for retryCounter<3!!\n"));
7392                         }
7393                 } else {  /* retry count > 3, ¥u­n1¦¸ retry count > 3, «h½Õ¯¶WiFi duration */
7394                         if (WaitCount == 1)
7395                                 m++; /*  ÁקK¤@ª½¦b¨â­Ólevel¤¤¨Ó¦^ */
7396                         else
7397                                 m = 1;
7398
7399                         if (m >= 20) /* m ³Ì¤j­È = 20 ' ³Ì¤j120¬í recheck¬O§_½Õ¾ã WiFi duration. */
7400                                 m = 20;
7401                         n = 3*m;
7402                         up = 0;
7403                         dn = 0;
7404                         WaitCount = 0;
7405                         result = -1;
7406                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Decrease wifi duration for retryCounter>3!!\n"));
7407                 }
7408
7409                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], max Interval = %d\n", maxInterval));
7410                 if (maxInterval == 1) {
7411                         if (bTxPause) {
7412                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], TxPause = 1\n"));
7413                                 if (pBtdm8723->curPsTdma == 1) {
7414                                         btdm_2AntPsTdma(padapter, true, 5);
7415                                         pBtdm8723->psTdmaDuAdjType = 5;
7416                                 } else if (pBtdm8723->curPsTdma == 2) {
7417                                         btdm_2AntPsTdma(padapter, true, 6);
7418                                         pBtdm8723->psTdmaDuAdjType = 6;
7419                                 } else if (pBtdm8723->curPsTdma == 3) {
7420                                         btdm_2AntPsTdma(padapter, true, 7);
7421                                         pBtdm8723->psTdmaDuAdjType = 7;
7422                                 } else if (pBtdm8723->curPsTdma == 4) {
7423                                         btdm_2AntPsTdma(padapter, true, 8);
7424                                         pBtdm8723->psTdmaDuAdjType = 8;
7425                                 }
7426                                 if (pBtdm8723->curPsTdma == 9) {
7427                                         btdm_2AntPsTdma(padapter, true, 13);
7428                                         pBtdm8723->psTdmaDuAdjType = 13;
7429                                 } else if (pBtdm8723->curPsTdma == 10) {
7430                                         btdm_2AntPsTdma(padapter, true, 14);
7431                                         pBtdm8723->psTdmaDuAdjType = 14;
7432                                 } else if (pBtdm8723->curPsTdma == 11) {
7433                                         btdm_2AntPsTdma(padapter, true, 15);
7434                                         pBtdm8723->psTdmaDuAdjType = 15;
7435                                 } else if (pBtdm8723->curPsTdma == 12) {
7436                                         btdm_2AntPsTdma(padapter, true, 16);
7437                                         pBtdm8723->psTdmaDuAdjType = 16;
7438                                 }
7439
7440                                 if (result == -1) {
7441                                         if (pBtdm8723->curPsTdma == 5) {
7442                                                 btdm_2AntPsTdma(padapter, true, 6);
7443                                                 pBtdm8723->psTdmaDuAdjType = 6;
7444                                         } else if (pBtdm8723->curPsTdma == 6) {
7445                                                 btdm_2AntPsTdma(padapter, true, 7);
7446                                                 pBtdm8723->psTdmaDuAdjType = 7;
7447                                         } else if (pBtdm8723->curPsTdma == 7) {
7448                                                 btdm_2AntPsTdma(padapter, true, 8);
7449                                                 pBtdm8723->psTdmaDuAdjType = 8;
7450                                         } else if (pBtdm8723->curPsTdma == 13) {
7451                                                 btdm_2AntPsTdma(padapter, true, 14);
7452                                                 pBtdm8723->psTdmaDuAdjType = 14;
7453                                         } else if (pBtdm8723->curPsTdma == 14) {
7454                                                 btdm_2AntPsTdma(padapter, true, 15);
7455                                                 pBtdm8723->psTdmaDuAdjType = 15;
7456                                         } else if (pBtdm8723->curPsTdma == 15) {
7457                                                 btdm_2AntPsTdma(padapter, true, 16);
7458                                                 pBtdm8723->psTdmaDuAdjType = 16;
7459                                         }
7460                                 } else if (result == 1) {
7461                                         if (pBtdm8723->curPsTdma == 8) {
7462                                                 btdm_2AntPsTdma(padapter, true, 7);
7463                                                 pBtdm8723->psTdmaDuAdjType = 7;
7464                                         } else if (pBtdm8723->curPsTdma == 7) {
7465                                                 btdm_2AntPsTdma(padapter, true, 6);
7466                                                 pBtdm8723->psTdmaDuAdjType = 6;
7467                                         } else if (pBtdm8723->curPsTdma == 6) {
7468                                                 btdm_2AntPsTdma(padapter, true, 5);
7469                                                 pBtdm8723->psTdmaDuAdjType = 5;
7470                                         } else if (pBtdm8723->curPsTdma == 16) {
7471                                                 btdm_2AntPsTdma(padapter, true, 15);
7472                                                 pBtdm8723->psTdmaDuAdjType = 15;
7473                                         } else if (pBtdm8723->curPsTdma == 15) {
7474                                                 btdm_2AntPsTdma(padapter, true, 14);
7475                                                 pBtdm8723->psTdmaDuAdjType = 14;
7476                                         } else if (pBtdm8723->curPsTdma == 14) {
7477                                                 btdm_2AntPsTdma(padapter, true, 13);
7478                                                 pBtdm8723->psTdmaDuAdjType = 13;
7479                                         }
7480                                 }
7481                         } else {
7482                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], TxPause = 0\n"));
7483                                 if (pBtdm8723->curPsTdma == 5) {
7484                                         btdm_2AntPsTdma(padapter, true, 1);
7485                                         pBtdm8723->psTdmaDuAdjType = 1;
7486                                 } else if (pBtdm8723->curPsTdma == 6) {
7487                                         btdm_2AntPsTdma(padapter, true, 2);
7488                                         pBtdm8723->psTdmaDuAdjType = 2;
7489                                 } else if (pBtdm8723->curPsTdma == 7) {
7490                                         btdm_2AntPsTdma(padapter, true, 3);
7491                                         pBtdm8723->psTdmaDuAdjType = 3;
7492                                 } else if (pBtdm8723->curPsTdma == 8) {
7493                                         btdm_2AntPsTdma(padapter, true, 4);
7494                                         pBtdm8723->psTdmaDuAdjType = 4;
7495                                 }
7496                                 if (pBtdm8723->curPsTdma == 13) {
7497                                         btdm_2AntPsTdma(padapter, true, 9);
7498                                         pBtdm8723->psTdmaDuAdjType = 9;
7499                                 } else if (pBtdm8723->curPsTdma == 14) {
7500                                         btdm_2AntPsTdma(padapter, true, 10);
7501                                         pBtdm8723->psTdmaDuAdjType = 10;
7502                                 } else if (pBtdm8723->curPsTdma == 15) {
7503                                         btdm_2AntPsTdma(padapter, true, 11);
7504                                         pBtdm8723->psTdmaDuAdjType = 11;
7505                                 } else if (pBtdm8723->curPsTdma == 16) {
7506                                         btdm_2AntPsTdma(padapter, true, 12);
7507                                         pBtdm8723->psTdmaDuAdjType = 12;
7508                                 }
7509
7510                                 if (result == -1) {
7511                                         if (pBtdm8723->curPsTdma == 1) {
7512                                                 btdm_2AntPsTdma(padapter, true, 2);
7513                                                 pBtdm8723->psTdmaDuAdjType = 2;
7514                                         } else if (pBtdm8723->curPsTdma == 2) {
7515                                                 btdm_2AntPsTdma(padapter, true, 3);
7516                                                 pBtdm8723->psTdmaDuAdjType = 3;
7517                                         } else if (pBtdm8723->curPsTdma == 3) {
7518                                                 btdm_2AntPsTdma(padapter, true, 4);
7519                                                 pBtdm8723->psTdmaDuAdjType = 4;
7520                                         } else if (pBtdm8723->curPsTdma == 9) {
7521                                                 btdm_2AntPsTdma(padapter, true, 10);
7522                                                 pBtdm8723->psTdmaDuAdjType = 10;
7523                                         } else if (pBtdm8723->curPsTdma == 10) {
7524                                                 btdm_2AntPsTdma(padapter, true, 11);
7525                                                 pBtdm8723->psTdmaDuAdjType = 11;
7526                                         } else if (pBtdm8723->curPsTdma == 11) {
7527                                                 btdm_2AntPsTdma(padapter, true, 12);
7528                                                 pBtdm8723->psTdmaDuAdjType = 12;
7529                                         }
7530                                 } else if (result == 1) {
7531                                         if (pBtdm8723->curPsTdma == 4) {
7532                                                 btdm_2AntPsTdma(padapter, true, 3);
7533                                                 pBtdm8723->psTdmaDuAdjType = 3;
7534                                         } else if (pBtdm8723->curPsTdma == 3) {
7535                                                 btdm_2AntPsTdma(padapter, true, 2);
7536                                                 pBtdm8723->psTdmaDuAdjType = 2;
7537                                         } else if (pBtdm8723->curPsTdma == 2) {
7538                                                 btdm_2AntPsTdma(padapter, true, 1);
7539                                                 pBtdm8723->psTdmaDuAdjType = 1;
7540                                         } else if (pBtdm8723->curPsTdma == 12) {
7541                                                 btdm_2AntPsTdma(padapter, true, 11);
7542                                                 pBtdm8723->psTdmaDuAdjType = 11;
7543                                         } else if (pBtdm8723->curPsTdma == 11) {
7544                                                 btdm_2AntPsTdma(padapter, true, 10);
7545                                                 pBtdm8723->psTdmaDuAdjType = 10;
7546                                         } else if (pBtdm8723->curPsTdma == 10) {
7547                                                 btdm_2AntPsTdma(padapter, true, 9);
7548                                                 pBtdm8723->psTdmaDuAdjType = 9;
7549                                         }
7550                                 }
7551                         }
7552                 } else if (maxInterval == 2) {
7553                         if (bTxPause) {
7554                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], TxPause = 1\n"));
7555                                 if (pBtdm8723->curPsTdma == 1) {
7556                                         btdm_2AntPsTdma(padapter, true, 6);
7557                                         pBtdm8723->psTdmaDuAdjType = 6;
7558                                 } else if (pBtdm8723->curPsTdma == 2) {
7559                                         btdm_2AntPsTdma(padapter, true, 6);
7560                                         pBtdm8723->psTdmaDuAdjType = 6;
7561                                 } else if (pBtdm8723->curPsTdma == 3) {
7562                                         btdm_2AntPsTdma(padapter, true, 7);
7563                                         pBtdm8723->psTdmaDuAdjType = 7;
7564                                 } else if (pBtdm8723->curPsTdma == 4) {
7565                                         btdm_2AntPsTdma(padapter, true, 8);
7566                                         pBtdm8723->psTdmaDuAdjType = 8;
7567                                 }
7568                                 if (pBtdm8723->curPsTdma == 9) {
7569                                         btdm_2AntPsTdma(padapter, true, 14);
7570                                         pBtdm8723->psTdmaDuAdjType = 14;
7571                                 } else if (pBtdm8723->curPsTdma == 10) {
7572                                         btdm_2AntPsTdma(padapter, true, 14);
7573                                         pBtdm8723->psTdmaDuAdjType = 14;
7574                                 } else if (pBtdm8723->curPsTdma == 11) {
7575                                         btdm_2AntPsTdma(padapter, true, 15);
7576                                         pBtdm8723->psTdmaDuAdjType = 15;
7577                                 } else if (pBtdm8723->curPsTdma == 12) {
7578                                         btdm_2AntPsTdma(padapter, true, 16);
7579                                         pBtdm8723->psTdmaDuAdjType = 16;
7580                                 }
7581                                 if (result == -1) {
7582                                         if (pBtdm8723->curPsTdma == 5) {
7583                                                 btdm_2AntPsTdma(padapter, true, 6);
7584                                                 pBtdm8723->psTdmaDuAdjType = 6;
7585                                         } else if (pBtdm8723->curPsTdma == 6) {
7586                                                 btdm_2AntPsTdma(padapter, true, 7);
7587                                                 pBtdm8723->psTdmaDuAdjType = 7;
7588                                         } else if (pBtdm8723->curPsTdma == 7) {
7589                                                 btdm_2AntPsTdma(padapter, true, 8);
7590                                                 pBtdm8723->psTdmaDuAdjType = 8;
7591                                         } else if (pBtdm8723->curPsTdma == 13) {
7592                                                 btdm_2AntPsTdma(padapter, true, 14);
7593                                                 pBtdm8723->psTdmaDuAdjType = 14;
7594                                         } else if (pBtdm8723->curPsTdma == 14) {
7595                                                 btdm_2AntPsTdma(padapter, true, 15);
7596                                                 pBtdm8723->psTdmaDuAdjType = 15;
7597                                         } else if (pBtdm8723->curPsTdma == 15) {
7598                                                 btdm_2AntPsTdma(padapter, true, 16);
7599                                                 pBtdm8723->psTdmaDuAdjType = 16;
7600                                         }
7601                                 } else if (result == 1) {
7602                                         if (pBtdm8723->curPsTdma == 8) {
7603                                                 btdm_2AntPsTdma(padapter, true, 7);
7604                                                 pBtdm8723->psTdmaDuAdjType = 7;
7605                                         } else if (pBtdm8723->curPsTdma == 7) {
7606                                                 btdm_2AntPsTdma(padapter, true, 6);
7607                                                 pBtdm8723->psTdmaDuAdjType = 6;
7608                                         } else if (pBtdm8723->curPsTdma == 6) {
7609                                                 btdm_2AntPsTdma(padapter, true, 6);
7610                                                 pBtdm8723->psTdmaDuAdjType = 6;
7611                                         } else if (pBtdm8723->curPsTdma == 16) {
7612                                                 btdm_2AntPsTdma(padapter, true, 15);
7613                                                 pBtdm8723->psTdmaDuAdjType = 15;
7614                                         } else if (pBtdm8723->curPsTdma == 15) {
7615                                                 btdm_2AntPsTdma(padapter, true, 14);
7616                                                 pBtdm8723->psTdmaDuAdjType = 14;
7617                                         } else if (pBtdm8723->curPsTdma == 14) {
7618                                                 btdm_2AntPsTdma(padapter, true, 14);
7619                                                 pBtdm8723->psTdmaDuAdjType = 14;
7620                                         }
7621                                 }
7622                         } else {
7623                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], TxPause = 0\n"));
7624                                 if (pBtdm8723->curPsTdma == 5) {
7625                                         btdm_2AntPsTdma(padapter, true, 2);
7626                                         pBtdm8723->psTdmaDuAdjType = 2;
7627                                 } else if (pBtdm8723->curPsTdma == 6) {
7628                                         btdm_2AntPsTdma(padapter, true, 2);
7629                                         pBtdm8723->psTdmaDuAdjType = 2;
7630                                 } else if (pBtdm8723->curPsTdma == 7) {
7631                                         btdm_2AntPsTdma(padapter, true, 3);
7632                                         pBtdm8723->psTdmaDuAdjType = 3;
7633                                 } else if (pBtdm8723->curPsTdma == 8) {
7634                                         btdm_2AntPsTdma(padapter, true, 4);
7635                                         pBtdm8723->psTdmaDuAdjType = 4;
7636                                 }
7637                                 if (pBtdm8723->curPsTdma == 13) {
7638                                         btdm_2AntPsTdma(padapter, true, 10);
7639                                         pBtdm8723->psTdmaDuAdjType = 10;
7640                                 } else if (pBtdm8723->curPsTdma == 14) {
7641                                         btdm_2AntPsTdma(padapter, true, 10);
7642                                         pBtdm8723->psTdmaDuAdjType = 10;
7643                                 } else if (pBtdm8723->curPsTdma == 15) {
7644                                         btdm_2AntPsTdma(padapter, true, 11);
7645                                         pBtdm8723->psTdmaDuAdjType = 11;
7646                                 } else if (pBtdm8723->curPsTdma == 16) {
7647                                         btdm_2AntPsTdma(padapter, true, 12);
7648                                         pBtdm8723->psTdmaDuAdjType = 12;
7649                                 }
7650                                 if (result == -1) {
7651                                         if (pBtdm8723->curPsTdma == 1) {
7652                                                 btdm_2AntPsTdma(padapter, true, 2);
7653                                                 pBtdm8723->psTdmaDuAdjType = 2;
7654                                         } else if (pBtdm8723->curPsTdma == 2) {
7655                                                 btdm_2AntPsTdma(padapter, true, 3);
7656                                                 pBtdm8723->psTdmaDuAdjType = 3;
7657                                         } else if (pBtdm8723->curPsTdma == 3) {
7658                                                 btdm_2AntPsTdma(padapter, true, 4);
7659                                                 pBtdm8723->psTdmaDuAdjType = 4;
7660                                         } else if (pBtdm8723->curPsTdma == 9) {
7661                                                 btdm_2AntPsTdma(padapter, true, 10);
7662                                                 pBtdm8723->psTdmaDuAdjType = 10;
7663                                         } else if (pBtdm8723->curPsTdma == 10) {
7664                                                 btdm_2AntPsTdma(padapter, true, 11);
7665                                                 pBtdm8723->psTdmaDuAdjType = 11;
7666                                         } else if (pBtdm8723->curPsTdma == 11) {
7667                                                 btdm_2AntPsTdma(padapter, true, 12);
7668                                                 pBtdm8723->psTdmaDuAdjType = 12;
7669                                         }
7670                                 } else if (result == 1) {
7671                                         if (pBtdm8723->curPsTdma == 4) {
7672                                                 btdm_2AntPsTdma(padapter, true, 3);
7673                                                 pBtdm8723->psTdmaDuAdjType = 3;
7674                                         } else if (pBtdm8723->curPsTdma == 3) {
7675                                                 btdm_2AntPsTdma(padapter, true, 2);
7676                                                 pBtdm8723->psTdmaDuAdjType = 2;
7677                                         } else if (pBtdm8723->curPsTdma == 2) {
7678                                                 btdm_2AntPsTdma(padapter, true, 2);
7679                                                 pBtdm8723->psTdmaDuAdjType = 2;
7680                                         } else if (pBtdm8723->curPsTdma == 12) {
7681                                                 btdm_2AntPsTdma(padapter, true, 11);
7682                                                 pBtdm8723->psTdmaDuAdjType = 11;
7683                                         } else if (pBtdm8723->curPsTdma == 11) {
7684                                                 btdm_2AntPsTdma(padapter, true, 10);
7685                                                 pBtdm8723->psTdmaDuAdjType = 10;
7686                                         } else if (pBtdm8723->curPsTdma == 10) {
7687                                                 btdm_2AntPsTdma(padapter, true, 10);
7688                                                 pBtdm8723->psTdmaDuAdjType = 10;
7689                                         }
7690                                 }
7691                         }
7692                 } else if (maxInterval == 3) {
7693                         if (bTxPause) {
7694                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], TxPause = 1\n"));
7695                                 if (pBtdm8723->curPsTdma == 1) {
7696                                         btdm_2AntPsTdma(padapter, true, 7);
7697                                         pBtdm8723->psTdmaDuAdjType = 7;
7698                                 } else if (pBtdm8723->curPsTdma == 2) {
7699                                         btdm_2AntPsTdma(padapter, true, 7);
7700                                         pBtdm8723->psTdmaDuAdjType = 7;
7701                                 } else if (pBtdm8723->curPsTdma == 3) {
7702                                         btdm_2AntPsTdma(padapter, true, 7);
7703                                         pBtdm8723->psTdmaDuAdjType = 7;
7704                                 } else if (pBtdm8723->curPsTdma == 4) {
7705                                         btdm_2AntPsTdma(padapter, true, 8);
7706                                         pBtdm8723->psTdmaDuAdjType = 8;
7707                                 }
7708                                 if (pBtdm8723->curPsTdma == 9) {
7709                                         btdm_2AntPsTdma(padapter, true, 15);
7710                                         pBtdm8723->psTdmaDuAdjType = 15;
7711                                 } else if (pBtdm8723->curPsTdma == 10) {
7712                                         btdm_2AntPsTdma(padapter, true, 15);
7713                                         pBtdm8723->psTdmaDuAdjType = 15;
7714                                 } else if (pBtdm8723->curPsTdma == 11) {
7715                                         btdm_2AntPsTdma(padapter, true, 15);
7716                                         pBtdm8723->psTdmaDuAdjType = 15;
7717                                 } else if (pBtdm8723->curPsTdma == 12) {
7718                                         btdm_2AntPsTdma(padapter, true, 16);
7719                                         pBtdm8723->psTdmaDuAdjType = 16;
7720                                 }
7721                                 if (result == -1) {
7722                                         if (pBtdm8723->curPsTdma == 5) {
7723                                                 btdm_2AntPsTdma(padapter, true, 7);
7724                                                 pBtdm8723->psTdmaDuAdjType = 7;
7725                                         } else if (pBtdm8723->curPsTdma == 6) {
7726                                                 btdm_2AntPsTdma(padapter, true, 7);
7727                                                 pBtdm8723->psTdmaDuAdjType = 7;
7728                                         } else if (pBtdm8723->curPsTdma == 7) {
7729                                                 btdm_2AntPsTdma(padapter, true, 8);
7730                                                 pBtdm8723->psTdmaDuAdjType = 8;
7731                                         } else if (pBtdm8723->curPsTdma == 13) {
7732                                                 btdm_2AntPsTdma(padapter, true, 15);
7733                                                 pBtdm8723->psTdmaDuAdjType = 15;
7734                                         } else if (pBtdm8723->curPsTdma == 14) {
7735                                                 btdm_2AntPsTdma(padapter, true, 15);
7736                                                 pBtdm8723->psTdmaDuAdjType = 15;
7737                                         } else if (pBtdm8723->curPsTdma == 15) {
7738                                                 btdm_2AntPsTdma(padapter, true, 16);
7739                                                 pBtdm8723->psTdmaDuAdjType = 16;
7740                                         }
7741                                 } else if (result == 1) {
7742                                         if (pBtdm8723->curPsTdma == 8) {
7743                                                 btdm_2AntPsTdma(padapter, true, 7);
7744                                                 pBtdm8723->psTdmaDuAdjType = 7;
7745                                         } else if (pBtdm8723->curPsTdma == 7) {
7746                                                 btdm_2AntPsTdma(padapter, true, 7);
7747                                                 pBtdm8723->psTdmaDuAdjType = 7;
7748                                         } else if (pBtdm8723->curPsTdma == 6) {
7749                                                 btdm_2AntPsTdma(padapter, true, 7);
7750                                                 pBtdm8723->psTdmaDuAdjType = 7;
7751                                         } else if (pBtdm8723->curPsTdma == 16) {
7752                                                 btdm_2AntPsTdma(padapter, true, 15);
7753                                                 pBtdm8723->psTdmaDuAdjType = 15;
7754                                         } else if (pBtdm8723->curPsTdma == 15) {
7755                                                 btdm_2AntPsTdma(padapter, true, 15);
7756                                                 pBtdm8723->psTdmaDuAdjType = 15;
7757                                         } else if (pBtdm8723->curPsTdma == 14) {
7758                                                 btdm_2AntPsTdma(padapter, true, 15);
7759                                                 pBtdm8723->psTdmaDuAdjType = 15;
7760                                         }
7761                                 }
7762                         } else {
7763                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], TxPause = 0\n"));
7764                                 if (pBtdm8723->curPsTdma == 5) {
7765                                         btdm_2AntPsTdma(padapter, true, 3);
7766                                         pBtdm8723->psTdmaDuAdjType = 3;
7767                                 } else if (pBtdm8723->curPsTdma == 6) {
7768                                         btdm_2AntPsTdma(padapter, true, 3);
7769                                         pBtdm8723->psTdmaDuAdjType = 3;
7770                                 } else if (pBtdm8723->curPsTdma == 7) {
7771                                         btdm_2AntPsTdma(padapter, true, 3);
7772                                         pBtdm8723->psTdmaDuAdjType = 3;
7773                                 } else if (pBtdm8723->curPsTdma == 8) {
7774                                         btdm_2AntPsTdma(padapter, true, 4);
7775                                         pBtdm8723->psTdmaDuAdjType = 4;
7776                                 }
7777                                 if (pBtdm8723->curPsTdma == 13) {
7778                                         btdm_2AntPsTdma(padapter, true, 11);
7779                                         pBtdm8723->psTdmaDuAdjType = 11;
7780                                 } else if (pBtdm8723->curPsTdma == 14) {
7781                                         btdm_2AntPsTdma(padapter, true, 11);
7782                                         pBtdm8723->psTdmaDuAdjType = 11;
7783                                 } else if (pBtdm8723->curPsTdma == 15) {
7784                                         btdm_2AntPsTdma(padapter, true, 11);
7785                                         pBtdm8723->psTdmaDuAdjType = 11;
7786                                 } else if (pBtdm8723->curPsTdma == 16) {
7787                                         btdm_2AntPsTdma(padapter, true, 12);
7788                                         pBtdm8723->psTdmaDuAdjType = 12;
7789                                 }
7790                                 if (result == -1) {
7791                                         if (pBtdm8723->curPsTdma == 1) {
7792                                                 btdm_2AntPsTdma(padapter, true, 3);
7793                                                 pBtdm8723->psTdmaDuAdjType = 3;
7794                                         } else if (pBtdm8723->curPsTdma == 2) {
7795                                                 btdm_2AntPsTdma(padapter, true, 3);
7796                                                 pBtdm8723->psTdmaDuAdjType = 3;
7797                                         } else if (pBtdm8723->curPsTdma == 3) {
7798                                                 btdm_2AntPsTdma(padapter, true, 4);
7799                                                 pBtdm8723->psTdmaDuAdjType = 4;
7800                                         } else if (pBtdm8723->curPsTdma == 9) {
7801                                                 btdm_2AntPsTdma(padapter, true, 11);
7802                                                 pBtdm8723->psTdmaDuAdjType = 11;
7803                                         } else if (pBtdm8723->curPsTdma == 10) {
7804                                                 btdm_2AntPsTdma(padapter, true, 11);
7805                                                 pBtdm8723->psTdmaDuAdjType = 11;
7806                                         } else if (pBtdm8723->curPsTdma == 11) {
7807                                                 btdm_2AntPsTdma(padapter, true, 12);
7808                                                 pBtdm8723->psTdmaDuAdjType = 12;
7809                                         }
7810                                 } else if (result == 1) {
7811                                         if (pBtdm8723->curPsTdma == 4) {
7812                                                 btdm_2AntPsTdma(padapter, true, 3);
7813                                                 pBtdm8723->psTdmaDuAdjType = 3;
7814                                         } else if (pBtdm8723->curPsTdma == 3) {
7815                                                 btdm_2AntPsTdma(padapter, true, 3);
7816                                                 pBtdm8723->psTdmaDuAdjType = 3;
7817                                         } else if (pBtdm8723->curPsTdma == 2) {
7818                                                 btdm_2AntPsTdma(padapter, true, 3);
7819                                                 pBtdm8723->psTdmaDuAdjType = 3;
7820                                         } else if (pBtdm8723->curPsTdma == 12) {
7821                                                 btdm_2AntPsTdma(padapter, true, 11);
7822                                                 pBtdm8723->psTdmaDuAdjType = 11;
7823                                         } else if (pBtdm8723->curPsTdma == 11) {
7824                                                 btdm_2AntPsTdma(padapter, true, 11);
7825                                                 pBtdm8723->psTdmaDuAdjType = 11;
7826                                         } else if (pBtdm8723->curPsTdma == 10) {
7827                                                 btdm_2AntPsTdma(padapter, true, 11);
7828                                                 pBtdm8723->psTdmaDuAdjType = 11;
7829                                         }
7830                                 }
7831                         }
7832                 }
7833         }
7834         RTPRINT(FBT, BT_TRACE, ("[BTCoex], PsTdma type : recordPsTdma =%d\n", pBtdm8723->psTdmaDuAdjType));
7835         /*  if current PsTdma not match with the recorded one (when scan, dhcp...), */
7836         /*  then we have to adjust it back to the previous record one. */
7837         if (pBtdm8723->curPsTdma != pBtdm8723->psTdmaDuAdjType) {
7838                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], PsTdma type dismatch!!!, curPsTdma =%d, recordPsTdma =%d\n",
7839                         pBtdm8723->curPsTdma, pBtdm8723->psTdmaDuAdjType));
7840
7841                 if (!check_fwstate(&padapter->mlmepriv, _FW_UNDER_SURVEY|_FW_UNDER_LINKING))
7842                         btdm_2AntPsTdma(padapter, true, pBtdm8723->psTdmaDuAdjType);
7843                 else
7844                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], roaming/link/scan is under progress, will adjust next time!!!\n"));
7845         }
7846 }
7847
7848 /*  default Action */
7849 /*  SCO only or SCO+PAN(HS) */
7850 static void btdm_2Ant8723ASCOAction(struct rtw_adapter *padapter)
7851 {
7852         u8 btRssiState, btRssiState1;
7853
7854         if (btdm_NeedToDecBtPwr(padapter))
7855                 btdm_2AntDecBtPwr(padapter, true);
7856         else
7857                 btdm_2AntDecBtPwr(padapter, false);
7858
7859         if (BTDM_IsHT40(padapter)) {
7860                 RTPRINT(FBT, BT_TRACE, ("HT40\n"));
7861                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
7862                 /*  fw mechanism */
7863                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
7864                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
7865                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 high \n"));
7866                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
7867                         btdm_2AntPsTdma(padapter, true, 11);
7868                 } else {
7869                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 low \n"));
7870                         btdm_2AntPsTdma(padapter, true, 15);
7871                 }
7872
7873                 /*  sw mechanism */
7874                 btdm_2AntAgcTable(padapter, false);
7875                 btdm_2AntAdcBackOff(padapter, true);
7876                 btdm_2AntDacSwing(padapter, false, 0xc0);
7877         } else {
7878                 RTPRINT(FBT, BT_TRACE, ("HT20 or Legacy\n"));
7879                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
7880                 btRssiState1 = BTDM_CheckCoexRSSIState1(padapter, 2, 27, 0);
7881
7882                 /*  fw mechanism */
7883                 if ((btRssiState1 == BT_RSSI_STATE_HIGH) ||
7884                     (btRssiState1 == BT_RSSI_STATE_STAY_HIGH)) {
7885                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 high \n"));
7886                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
7887                         btdm_2AntPsTdma(padapter, true, 11);
7888                 } else {
7889                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 low \n"));
7890                         btdm_2AntPsTdma(padapter, true, 15);
7891                 }
7892
7893                 /*  sw mechanism */
7894                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
7895                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
7896                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
7897                         btdm_2AntAgcTable(padapter, true);
7898                         btdm_2AntAdcBackOff(padapter, true);
7899                         btdm_2AntDacSwing(padapter, false, 0xc0);
7900                 } else {
7901                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
7902                         btdm_2AntAgcTable(padapter, false);
7903                         btdm_2AntAdcBackOff(padapter, false);
7904                         btdm_2AntDacSwing(padapter, false, 0xc0);
7905                 }
7906         }
7907 }
7908
7909 static void btdm_2Ant8723AHIDAction(struct rtw_adapter *padapter)
7910 {
7911         u8 btRssiState, btRssiState1;
7912
7913         if (btdm_NeedToDecBtPwr(padapter))
7914                 btdm_2AntDecBtPwr(padapter, true);
7915         else
7916                 btdm_2AntDecBtPwr(padapter, false);
7917
7918         if (BTDM_IsHT40(padapter)) {
7919                 RTPRINT(FBT, BT_TRACE, ("HT40\n"));
7920                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
7921                         /*  fw mechanism */
7922                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
7923                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
7924                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 high \n"));
7925                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
7926                         btdm_2AntPsTdma(padapter, true, 9);
7927                 } else {
7928                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 low \n"));
7929                         btdm_2AntPsTdma(padapter, true, 13);
7930                 }
7931
7932                 /*  sw mechanism */
7933                 btdm_2AntAgcTable(padapter, false);
7934                 btdm_2AntAdcBackOff(padapter, false);
7935                 btdm_2AntDacSwing(padapter, false, 0xc0);
7936         } else {
7937                 RTPRINT(FBT, BT_TRACE, ("HT20 or Legacy\n"));
7938                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
7939                 btRssiState1 = BTDM_CheckCoexRSSIState1(padapter, 2, 27, 0);
7940
7941                 /*  fw mechanism */
7942                 if ((btRssiState1 == BT_RSSI_STATE_HIGH) ||
7943                     (btRssiState1 == BT_RSSI_STATE_STAY_HIGH)) {
7944                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 high \n"));
7945                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
7946                         btdm_2AntPsTdma(padapter, true, 9);
7947                 } else {
7948                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 low \n"));
7949                         btdm_2AntPsTdma(padapter, true, 13);
7950                 }
7951
7952                 /*  sw mechanism */
7953                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
7954                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
7955                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
7956                         btdm_2AntAgcTable(padapter, true);
7957                         btdm_2AntAdcBackOff(padapter, true);
7958                         btdm_2AntDacSwing(padapter, false, 0xc0);
7959                 } else {
7960                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
7961                         btdm_2AntAgcTable(padapter, false);
7962                         btdm_2AntAdcBackOff(padapter, false);
7963                         btdm_2AntDacSwing(padapter, false, 0xc0);
7964                 }
7965         }
7966 }
7967
7968 /* A2DP only / PAN(EDR) only/ A2DP+PAN(HS) */
7969 static void btdm_2Ant8723AA2DPAction(struct rtw_adapter *padapter)
7970 {
7971         u8 btRssiState, btRssiState1;
7972         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
7973         u8 btInfoExt = pHalData->bt_coexist.halCoex8723.btInfoExt;
7974
7975         if (btdm_NeedToDecBtPwr(padapter))
7976                 btdm_2AntDecBtPwr(padapter, true);
7977         else
7978                 btdm_2AntDecBtPwr(padapter, false);
7979
7980         if (BTDM_IsHT40(padapter)) {
7981                 RTPRINT(FBT, BT_TRACE, ("HT40\n"));
7982                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
7983
7984                 /*  fw mechanism */
7985                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
7986                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
7987                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
7988                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
7989
7990                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
7991                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
7992                                 btdm_2AntTdmaDurationAdjust(padapter, false, false, 3);
7993                         } else {
7994                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
7995                                 btdm_2AntTdmaDurationAdjust(padapter, false, false, 1);
7996                         }
7997                 } else {
7998                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
7999                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8000                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8001                                 btdm_2AntTdmaDurationAdjust(padapter, false, true, 3);
8002                         } else {
8003                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8004                         btdm_2AntTdmaDurationAdjust(padapter, false, true, 1);
8005                         }
8006                 }
8007
8008                 /*  sw mechanism */
8009                 btdm_2AntAgcTable(padapter, false);
8010                 btdm_2AntAdcBackOff(padapter, true);
8011                 btdm_2AntDacSwing(padapter, false, 0xc0);
8012         } else {
8013                 RTPRINT(FBT, BT_TRACE, ("HT20 or Legacy\n"));
8014                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
8015                 btRssiState1 = BTDM_CheckCoexRSSIState1(padapter, 2, 27, 0);
8016
8017                 /*  fw mechanism */
8018                 if ((btRssiState1 == BT_RSSI_STATE_HIGH) ||
8019                     (btRssiState1 == BT_RSSI_STATE_STAY_HIGH)) {
8020                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 high \n"));
8021                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
8022                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8023                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8024                                 btdm_2AntTdmaDurationAdjust(padapter, false, false, 3);
8025                         } else {
8026                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8027                                 btdm_2AntTdmaDurationAdjust(padapter, false, false, 1);
8028                         }
8029                 } else {
8030                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 low \n"));
8031                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8032                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8033                                 btdm_2AntTdmaDurationAdjust(padapter, false, true, 3);
8034                         } else {
8035                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8036                                 btdm_2AntTdmaDurationAdjust(padapter, false, true, 1);
8037                         }
8038                 }
8039
8040                 /*  sw mechanism */
8041                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8042                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8043                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
8044                         btdm_2AntAgcTable(padapter, true);
8045                         btdm_2AntAdcBackOff(padapter, true);
8046                         btdm_2AntDacSwing(padapter, false, 0xc0);
8047                 } else {
8048                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
8049                         btdm_2AntAgcTable(padapter, false);
8050                         btdm_2AntAdcBackOff(padapter, false);
8051                         btdm_2AntDacSwing(padapter, false, 0xc0);
8052                 }
8053         }
8054 }
8055
8056 static void btdm_2Ant8723APANEDRAction(struct rtw_adapter *padapter)
8057 {
8058         u8 btRssiState, btRssiState1;
8059
8060         if (btdm_NeedToDecBtPwr(padapter))
8061                 btdm_2AntDecBtPwr(padapter, true);
8062         else
8063                 btdm_2AntDecBtPwr(padapter, false);
8064
8065         if (BTDM_IsHT40(padapter)) {
8066                 RTPRINT(FBT, BT_TRACE, ("HT40\n"));
8067                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
8068
8069                 /*  fw mechanism */
8070                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8071                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8072                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
8073                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
8074                         btdm_2AntPsTdma(padapter, true, 2);
8075                 } else {
8076                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
8077                         btdm_2AntPsTdma(padapter, true, 6);
8078                 }
8079
8080                 /*  sw mechanism */
8081                 btdm_2AntAgcTable(padapter, false);
8082                 btdm_2AntAdcBackOff(padapter, true);
8083                 btdm_2AntDacSwing(padapter, false, 0xc0);
8084         } else {
8085                 RTPRINT(FBT, BT_TRACE, ("HT20 or Legacy\n"));
8086                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
8087                 btRssiState1 = BTDM_CheckCoexRSSIState1(padapter, 2, 27, 0);
8088
8089                 /*  fw mechanism */
8090                 if ((btRssiState1 == BT_RSSI_STATE_HIGH) ||
8091                     (btRssiState1 == BT_RSSI_STATE_STAY_HIGH)) {
8092                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 high \n"));
8093                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
8094                         btdm_2AntPsTdma(padapter, true, 2);
8095                 } else {
8096                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 low \n"));
8097                         btdm_2AntPsTdma(padapter, true, 6);
8098                 }
8099
8100                 /*  sw mechanism */
8101                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8102                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8103                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
8104                         btdm_2AntAgcTable(padapter, true);
8105                         btdm_2AntAdcBackOff(padapter, true);
8106                         btdm_2AntDacSwing(padapter, false, 0xc0);
8107                 } else {
8108                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
8109                         btdm_2AntAgcTable(padapter, false);
8110                         btdm_2AntAdcBackOff(padapter, false);
8111                         btdm_2AntDacSwing(padapter, false, 0xc0);
8112                 }
8113         }
8114 }
8115
8116 /* PAN(HS) only */
8117 static void btdm_2Ant8723APANHSAction(struct rtw_adapter *padapter)
8118 {
8119         u8 btRssiState;
8120
8121         if (BTDM_IsHT40(padapter)) {
8122                 RTPRINT(FBT, BT_TRACE, ("HT40\n"));
8123                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 47, 0);
8124                 /*  fw mechanism */
8125                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8126                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8127                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
8128                         btdm_2AntDecBtPwr(padapter, true);
8129                 } else {
8130                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
8131                         btdm_2AntDecBtPwr(padapter, false);
8132                 }
8133                 btdm_2AntPsTdma(padapter, false, 0);
8134
8135                 /*  sw mechanism */
8136                 btdm_2AntAgcTable(padapter, false);
8137                 btdm_2AntAdcBackOff(padapter, true);
8138                 btdm_2AntDacSwing(padapter, false, 0xc0);
8139         } else {
8140                 RTPRINT(FBT, BT_TRACE, ("HT20 or Legacy\n"));
8141                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 47, 0);
8142
8143                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8144                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8145                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high\n"));
8146                         /*  fw mechanism */
8147                         btdm_2AntDecBtPwr(padapter, true);
8148                         btdm_2AntPsTdma(padapter, false, 0);
8149
8150                         /*  sw mechanism */
8151                         btdm_2AntAgcTable(padapter, true);
8152                         btdm_2AntAdcBackOff(padapter, true);
8153                         btdm_2AntDacSwing(padapter, false, 0xc0);
8154                 } else {
8155                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low\n"));
8156                         /*  fw mechanism */
8157                         btdm_2AntDecBtPwr(padapter, false);
8158                         btdm_2AntPsTdma(padapter, false, 0);
8159
8160                         /*  sw mechanism */
8161                         btdm_2AntAgcTable(padapter, false);
8162                         btdm_2AntAdcBackOff(padapter, false);
8163                         btdm_2AntDacSwing(padapter, false, 0xc0);
8164                 }
8165         }
8166 }
8167
8168 /* PAN(EDR)+A2DP */
8169 static void btdm_2Ant8723APANEDRA2DPAction(struct rtw_adapter *padapter)
8170 {
8171         u8 btRssiState, btRssiState1, btInfoExt;
8172         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
8173
8174         btInfoExt = pHalData->bt_coexist.halCoex8723.btInfoExt;
8175
8176         if (btdm_NeedToDecBtPwr(padapter))
8177                 btdm_2AntDecBtPwr(padapter, true);
8178         else
8179                 btdm_2AntDecBtPwr(padapter, false);
8180
8181         if (BTDM_IsHT40(padapter)) {
8182                 RTPRINT(FBT, BT_TRACE, ("HT40\n"));
8183                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
8184
8185                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8186                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8187                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
8188                         /*  fw mechanism */
8189                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
8190
8191                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8192                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8193                                 btdm_2AntPsTdma(padapter, true, 4);
8194                         } else {
8195                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8196                                 btdm_2AntPsTdma(padapter, true, 2);
8197                         }
8198                 } else {
8199                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
8200                         /*  fw mechanism */
8201                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8202                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8203                                 btdm_2AntPsTdma(padapter, true, 8);
8204                         } else {
8205                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8206                                 btdm_2AntPsTdma(padapter, true, 6);
8207                         }
8208                 }
8209
8210                 /*  sw mechanism */
8211                 btdm_2AntAgcTable(padapter, false);
8212                 btdm_2AntAdcBackOff(padapter, true);
8213                 btdm_2AntDacSwing(padapter, false, 0xc0);
8214         } else {
8215                 RTPRINT(FBT, BT_TRACE, ("HT20 or Legacy\n"));
8216                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
8217                 btRssiState1 = BTDM_CheckCoexRSSIState1(padapter, 2, 27, 0);
8218
8219                 if ((btRssiState1 == BT_RSSI_STATE_HIGH) ||
8220                     (btRssiState1 == BT_RSSI_STATE_STAY_HIGH)) {
8221                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 high \n"));
8222                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
8223                         /*  fw mechanism */
8224                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8225                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8226                                 btdm_2AntPsTdma(padapter, true, 4);
8227                         } else {
8228                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8229                                 btdm_2AntPsTdma(padapter, true, 2);
8230                         }
8231                 } else {
8232                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 low \n"));
8233                         /*  fw mechanism */
8234                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8235                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8236                                 btdm_2AntPsTdma(padapter, true, 8);
8237                         } else {
8238                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8239                                 btdm_2AntPsTdma(padapter, true, 6);
8240                         }
8241                 }
8242
8243                 /*  sw mechanism */
8244                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8245                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8246                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
8247                         btdm_2AntAgcTable(padapter, true);
8248                         btdm_2AntAdcBackOff(padapter, true);
8249                         btdm_2AntDacSwing(padapter, false, 0xc0);
8250                 } else {
8251                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
8252                         btdm_2AntAgcTable(padapter, false);
8253                         btdm_2AntAdcBackOff(padapter, false);
8254                         btdm_2AntDacSwing(padapter, false, 0xc0);
8255                 }
8256         }
8257 }
8258
8259 static void btdm_2Ant8723APANEDRHIDAction(struct rtw_adapter *padapter)
8260 {
8261         u8 btRssiState, btRssiState1;
8262
8263         if (btdm_NeedToDecBtPwr(padapter))
8264                 btdm_2AntDecBtPwr(padapter, true);
8265         else
8266                 btdm_2AntDecBtPwr(padapter, false);
8267
8268         if (BTDM_IsHT40(padapter)) {
8269                 RTPRINT(FBT, BT_TRACE, ("HT40\n"));
8270                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
8271                 /*  fw mechanism */
8272                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8273                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8274                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
8275                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
8276                         btdm_2AntPsTdma(padapter, true, 10);
8277                 } else {
8278                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
8279                         btdm_2AntPsTdma(padapter, true, 14);
8280                 }
8281
8282                 /*  sw mechanism */
8283                 btdm_2AntAgcTable(padapter, false);
8284                 btdm_2AntAdcBackOff(padapter, true);
8285                 btdm_2AntDacSwing(padapter, false, 0xc0);
8286         } else {
8287                 RTPRINT(FBT, BT_TRACE, ("HT20 or Legacy\n"));
8288                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
8289                 btRssiState1 = BTDM_CheckCoexRSSIState1(padapter, 2, 27, 0);
8290
8291                 /*  fw mechanism */
8292                 if ((btRssiState1 == BT_RSSI_STATE_HIGH) ||
8293                     (btRssiState1 == BT_RSSI_STATE_STAY_HIGH)) {
8294                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 high \n"));
8295                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
8296                         btdm_2AntPsTdma(padapter, true, 10);
8297                 } else {
8298                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 low \n"));
8299                         btdm_2AntPsTdma(padapter, true, 14);
8300                 }
8301
8302                 /*  sw mechanism */
8303                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8304                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8305                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
8306                         btdm_2AntAgcTable(padapter, true);
8307                         btdm_2AntAdcBackOff(padapter, true);
8308                         btdm_2AntDacSwing(padapter, false, 0xc0);
8309                 } else {
8310                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
8311                         btdm_2AntAgcTable(padapter, false);
8312                         btdm_2AntAdcBackOff(padapter, false);
8313                         btdm_2AntDacSwing(padapter, false, 0xc0);
8314                 }
8315         }
8316 }
8317
8318 /*  HID+A2DP+PAN(EDR) */
8319 static void btdm_2Ant8723AHIDA2DPPANEDRAction(struct rtw_adapter *padapter)
8320 {
8321         u8 btRssiState, btRssiState1, btInfoExt;
8322         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
8323
8324         btInfoExt = pHalData->bt_coexist.halCoex8723.btInfoExt;
8325
8326         if (btdm_NeedToDecBtPwr(padapter))
8327                 btdm_2AntDecBtPwr(padapter, true);
8328         else
8329                 btdm_2AntDecBtPwr(padapter, false);
8330
8331         if (BTDM_IsHT40(padapter)) {
8332                 RTPRINT(FBT, BT_TRACE, ("HT40\n"));
8333                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
8334                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8335                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8336                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
8337                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
8338
8339                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8340                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8341                                 btdm_2AntPsTdma(padapter, true, 12);
8342                         } else {
8343                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8344                                 btdm_2AntPsTdma(padapter, true, 10);
8345                         }
8346                 } else {
8347                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
8348                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8349                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8350                                 btdm_2AntPsTdma(padapter, true, 16);
8351                         } else {
8352                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8353                                 btdm_2AntPsTdma(padapter, true, 14);
8354                         }
8355                 }
8356
8357                 /*  sw mechanism */
8358                 btdm_2AntAgcTable(padapter, false);
8359                 btdm_2AntAdcBackOff(padapter, true);
8360                 btdm_2AntDacSwing(padapter, false, 0xc0);
8361         } else {
8362                 RTPRINT(FBT, BT_TRACE, ("HT20 or Legacy\n"));
8363                 btRssiState1 = BTDM_CheckCoexRSSIState1(padapter, 2, 37, 0);
8364                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 27, 0);
8365                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8366                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8367                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
8368                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
8369
8370                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8371                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8372                                 btdm_2AntPsTdma(padapter, true, 12);
8373                         } else {
8374                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8375                                 btdm_2AntPsTdma(padapter, true, 10);
8376                         }
8377                 } else {
8378                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
8379                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8380                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8381                                 btdm_2AntPsTdma(padapter, true, 16);
8382                         } else {
8383                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8384                                 btdm_2AntPsTdma(padapter, true, 14);
8385                         }
8386                 }
8387
8388                 /*  sw mechanism */
8389                 if ((btRssiState1 == BT_RSSI_STATE_HIGH) ||
8390                     (btRssiState1 == BT_RSSI_STATE_STAY_HIGH)) {
8391                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 high \n"));
8392                         btdm_2AntAgcTable(padapter, true);
8393                         btdm_2AntAdcBackOff(padapter, true);
8394                         btdm_2AntDacSwing(padapter, false, 0xc0);
8395                 } else {
8396                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 high \n"));
8397                         btdm_2AntAgcTable(padapter, false);
8398                         btdm_2AntAdcBackOff(padapter, false);
8399                         btdm_2AntDacSwing(padapter, false, 0xc0);
8400                 }
8401         }
8402 }
8403
8404 static void btdm_2Ant8723AHIDA2DPAction(struct rtw_adapter *padapter)
8405 {
8406         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
8407         u8 btRssiState, btRssiState1, btInfoExt;
8408
8409         btInfoExt = pHalData->bt_coexist.halCoex8723.btInfoExt;
8410
8411         if (btdm_NeedToDecBtPwr(padapter))
8412                 btdm_2AntDecBtPwr(padapter, true);
8413         else
8414                 btdm_2AntDecBtPwr(padapter, false);
8415
8416         if (BTDM_IsHT40(padapter)) {
8417                 RTPRINT(FBT, BT_TRACE, ("HT40\n"));
8418                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
8419                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8420                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8421                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
8422                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
8423
8424                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8425                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8426                                 btdm_2AntTdmaDurationAdjust(padapter, true, false, 3);
8427                         } else {
8428                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8429                                 btdm_2AntTdmaDurationAdjust(padapter, true, false, 1);
8430                         }
8431                 } else {
8432                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
8433                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8434                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8435                                 btdm_2AntTdmaDurationAdjust(padapter, true, true, 3);
8436                         } else {
8437                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8438                                 btdm_2AntTdmaDurationAdjust(padapter, true, true, 1);
8439                         }
8440                 }
8441                 /*  sw mechanism */
8442                 btdm_2AntAgcTable(padapter, false);
8443                 btdm_2AntAdcBackOff(padapter, true);
8444                 btdm_2AntDacSwing(padapter, false, 0xc0);
8445         } else {
8446                 RTPRINT(FBT, BT_TRACE, ("HT20 or Legacy\n"));
8447                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
8448                 btRssiState1 = BTDM_CheckCoexRSSIState(padapter, 2, 27, 0);
8449
8450                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8451                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8452                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
8453                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
8454
8455                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8456                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8457                                 btdm_2AntTdmaDurationAdjust(padapter, true, false, 3);
8458                         } else {
8459                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8460                                 btdm_2AntTdmaDurationAdjust(padapter, true, false, 1);
8461                         }
8462                 } else {
8463                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
8464                         if (btInfoExt&BIT(0)) { /* a2dp rate, 1:basic /0:edr */
8465                                 RTPRINT(FBT, BT_TRACE, ("a2dp basic rate \n"));
8466                                 btdm_2AntTdmaDurationAdjust(padapter, true, true, 3);
8467                         } else {
8468                                 RTPRINT(FBT, BT_TRACE, ("a2dp edr rate \n"));
8469                                 btdm_2AntTdmaDurationAdjust(padapter, true, true, 1);
8470                         }
8471                 }
8472                 if ((btRssiState1 == BT_RSSI_STATE_HIGH) ||
8473                     (btRssiState1 == BT_RSSI_STATE_STAY_HIGH)) {
8474                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 high \n"));
8475                         /*  sw mechanism */
8476                         btdm_2AntAgcTable(padapter, true);
8477                         btdm_2AntAdcBackOff(padapter, true);
8478                         btdm_2AntDacSwing(padapter, false, 0xc0);
8479                 } else {
8480                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 low \n"));
8481                         /*  sw mechanism */
8482                         btdm_2AntAgcTable(padapter, false);
8483                         btdm_2AntAdcBackOff(padapter, false);
8484                         btdm_2AntDacSwing(padapter, false, 0xc0);
8485                 }
8486         }
8487 }
8488
8489 static void btdm_2Ant8723AA2dp(struct rtw_adapter *padapter)
8490 {
8491         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
8492         u8 btRssiState, btRssiState1, btInfoExt;
8493
8494         btInfoExt = pHalData->bt_coexist.halCoex8723.btInfoExt;
8495
8496         if (btdm_NeedToDecBtPwr(padapter))
8497                 btdm_2AntDecBtPwr(padapter, true);
8498         else
8499                 btdm_2AntDecBtPwr(padapter, false);
8500         /*  coex table */
8501         btdm_2AntCoexTable(padapter, 0x55555555, 0xffff, 0x3);
8502         btdm_2AntIgnoreWlanAct(padapter, false);
8503
8504         if (BTDM_IsHT40(padapter)) {
8505                 RTPRINT(FBT, BT_TRACE, ("HT40\n"));
8506                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 37, 0);
8507                 /*  fw mechanism */
8508                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8509                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8510                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 high \n"));
8511                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
8512                         btdm_2AntTdmaDurationAdjust(padapter, false, false, 1);
8513                 } else {
8514                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 low \n"));
8515                         btdm_2AntTdmaDurationAdjust(padapter, false, true, 1);
8516                 }
8517
8518                 /*  sw mechanism */
8519                 btdm_2AntAgcTable(padapter, false);
8520                 btdm_2AntAdcBackOff(padapter, true);
8521                 btdm_2AntDacSwing(padapter, false, 0xc0);
8522         } else {
8523                 RTPRINT(FBT, BT_TRACE, ("HT20 or Legacy\n"));
8524                 btRssiState = BTDM_CheckCoexRSSIState(padapter, 2, 47, 0);
8525                 btRssiState1 = BTDM_CheckCoexRSSIState1(padapter, 2, 27, 0);
8526
8527                 /*  fw mechanism */
8528                 if ((btRssiState1 == BT_RSSI_STATE_HIGH) ||
8529                     (btRssiState1 == BT_RSSI_STATE_STAY_HIGH)) {
8530                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 high \n"));
8531                         PlatformEFIOWrite1Byte(padapter, 0x883, 0x40);
8532                         btdm_2AntTdmaDurationAdjust(padapter, false, false, 1);
8533                 } else {
8534                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi-1 low \n"));
8535                         btdm_2AntTdmaDurationAdjust(padapter, false, true, 1);
8536                 }
8537
8538                 /*  sw mechanism */
8539                 if ((btRssiState == BT_RSSI_STATE_HIGH) ||
8540                     (btRssiState == BT_RSSI_STATE_STAY_HIGH)) {
8541                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi high \n"));
8542                         btdm_2AntAgcTable(padapter, true);
8543                         btdm_2AntAdcBackOff(padapter, true);
8544                         btdm_2AntDacSwing(padapter, false, 0xc0);
8545                 } else {
8546                         RTPRINT(FBT, BT_TRACE, ("Wifi rssi low \n"));
8547                         btdm_2AntAgcTable(padapter, false);
8548                         btdm_2AntAdcBackOff(padapter, false);
8549                         btdm_2AntDacSwing(padapter, false, 0xc0);
8550                 }
8551         }
8552 }
8553
8554 /*  extern function start with BTDM_ */
8555 static void BTDM_2AntParaInit(struct rtw_adapter *padapter)
8556 {
8557
8558         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
8559         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
8560
8561         RTPRINT(FBT, BT_TRACE, ("[BTCoex], 2Ant Parameter Init!!\n"));
8562
8563         /*  Enable counter statistics */
8564         rtl8723au_write8(padapter, 0x76e, 0x4);
8565         rtl8723au_write8(padapter, 0x778, 0x3);
8566         rtl8723au_write8(padapter, 0x40, 0x20);
8567
8568         /*  force to reset coex mechanism */
8569         pBtdm8723->preVal0x6c0 = 0x0;
8570         btdm_2AntCoexTable(padapter, 0x55555555, 0xffff, 0x3);
8571
8572         pBtdm8723->bPrePsTdmaOn = true;
8573         btdm_2AntPsTdma(padapter, false, 0);
8574
8575         pBtdm8723->preFwDacSwingLvl = 0x10;
8576         btdm_2AntFwDacSwingLvl(padapter, 0x20);
8577
8578         pBtdm8723->bPreDecBtPwr = true;
8579         btdm_2AntDecBtPwr(padapter, false);
8580
8581         pBtdm8723->bPreAgcTableEn = true;
8582         btdm_2AntAgcTable(padapter, false);
8583
8584         pBtdm8723->bPreAdcBackOff = true;
8585         btdm_2AntAdcBackOff(padapter, false);
8586
8587         pBtdm8723->bPreLowPenaltyRa = true;
8588         btdm_2AntLowPenaltyRa(padapter, false);
8589
8590         pBtdm8723->bPreRfRxLpfShrink = true;
8591         btdm_2AntRfShrink(padapter, false);
8592
8593         pBtdm8723->bPreDacSwingOn = true;
8594         btdm_2AntDacSwing(padapter, false, 0xc0);
8595
8596         pBtdm8723->bPreIgnoreWlanAct = true;
8597         btdm_2AntIgnoreWlanAct(padapter, false);
8598 }
8599
8600 static void BTDM_2AntHwCoexAllOff8723A(struct rtw_adapter *padapter)
8601 {
8602         btdm_2AntCoexTable(padapter, 0x55555555, 0xffff, 0x3);
8603 }
8604
8605 static void BTDM_2AntFwCoexAllOff8723A(struct rtw_adapter *padapter)
8606 {
8607         btdm_2AntIgnoreWlanAct(padapter, false);
8608         btdm_2AntPsTdma(padapter, false, 0);
8609         btdm_2AntFwDacSwingLvl(padapter, 0x20);
8610         btdm_2AntDecBtPwr(padapter, false);
8611 }
8612
8613 static void BTDM_2AntSwCoexAllOff8723A(struct rtw_adapter *padapter)
8614 {
8615         btdm_2AntAgcTable(padapter, false);
8616         btdm_2AntAdcBackOff(padapter, false);
8617         btdm_2AntLowPenaltyRa(padapter, false);
8618         btdm_2AntRfShrink(padapter, false);
8619         btdm_2AntDacSwing(padapter, false, 0xc0);
8620 }
8621
8622 static void BTDM_2AntFwC2hBtInfo8723A(struct rtw_adapter *padapter)
8623 {
8624         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
8625         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
8626         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
8627         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
8628         u8 btInfo = 0;
8629         u8 algorithm = BT_2ANT_COEX_ALGO_UNDEFINED;
8630         u8 bBtLinkExist = false, bBtHsModeExist = false;
8631
8632         btInfo = pHalData->bt_coexist.halCoex8723.c2hBtInfoOriginal;
8633         pBtdm8723->btStatus = BT_2ANT_BT_STATUS_IDLE;
8634
8635         /*  check BIT2 first ==> check if bt is under inquiry or page scan */
8636         if (btInfo & BIT(2)) {
8637                 if (!pHalData->bt_coexist.halCoex8723.bC2hBtInquiryPage) {
8638                         pBtMgnt->ExtConfig.bHoldForBtOperation = true;
8639                         pBtMgnt->ExtConfig.bHoldPeriodCnt = 1;
8640                         btdm_2AntBtInquiryPage(padapter);
8641                 } else {
8642                         pBtMgnt->ExtConfig.bHoldPeriodCnt++;
8643                         btdm_HoldForBtInqPage(padapter);
8644                 }
8645                 pHalData->bt_coexist.halCoex8723.bC2hBtInquiryPage = true;
8646
8647         } else {
8648                 pHalData->bt_coexist.halCoex8723.bC2hBtInquiryPage = false;
8649                 pBtMgnt->ExtConfig.bHoldForBtOperation = false;
8650                 pBtMgnt->ExtConfig.bHoldPeriodCnt = 0;
8651
8652         }
8653         RTPRINT(FBT, BT_TRACE,
8654                 ("[BTC2H], pHalData->bt_coexist.halCoex8723.bC2hBtInquiryPage =%x pBtMgnt->ExtConfig.bHoldPeriodCnt =%x pBtMgnt->ExtConfig.bHoldForBtOperation =%x\n",
8655                 pHalData->bt_coexist.halCoex8723.bC2hBtInquiryPage,
8656                 pBtMgnt->ExtConfig.bHoldPeriodCnt,
8657                 pBtMgnt->ExtConfig.bHoldForBtOperation));
8658
8659         RTPRINT(FBT, BT_TRACE,
8660                 ("[BTC2H],   btInfo =%x   pHalData->bt_coexist.halCoex8723.c2hBtInfoOriginal =%x\n",
8661                 btInfo, pHalData->bt_coexist.halCoex8723.c2hBtInfoOriginal));
8662         if (btInfo&BT_INFO_ACL) {
8663                 RTPRINT(FBT, BT_TRACE, ("[BTC2H], BTInfo: bConnect = true   btInfo =%x\n", btInfo));
8664                 bBtLinkExist = true;
8665                 if (((btInfo&(BT_INFO_FTP|BT_INFO_A2DP|BT_INFO_HID|BT_INFO_SCO_BUSY)) != 0) ||
8666                     pHalData->bt_coexist.halCoex8723.btRetryCnt > 0) {
8667                         pBtdm8723->btStatus = BT_2ANT_BT_STATUS_NON_IDLE;
8668                 } else {
8669                         pBtdm8723->btStatus = BT_2ANT_BT_STATUS_CONNECTED_IDLE;
8670                 }
8671
8672                 if (btInfo&BT_INFO_SCO || btInfo&BT_INFO_SCO_BUSY) {
8673                         if (btInfo&BT_INFO_FTP || btInfo&BT_INFO_A2DP || btInfo&BT_INFO_HID) {
8674                                 switch (btInfo&0xe0) {
8675                                 case BT_INFO_HID:
8676                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], SCO + HID\n"));
8677                                         algorithm = BT_2ANT_COEX_ALGO_HID;
8678                                         break;
8679                                 case BT_INFO_A2DP:
8680                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Error!!! SCO + A2DP\n"));
8681                                         break;
8682                                 case BT_INFO_FTP:
8683                                         if (bBtHsModeExist) {
8684                                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], SCO + PAN(HS)\n"));
8685                                                 algorithm = BT_2ANT_COEX_ALGO_SCO;
8686                                         } else {
8687                                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], SCO + PAN(EDR)\n"));
8688                                                 algorithm = BT_2ANT_COEX_ALGO_PANEDR_HID;
8689                                         }
8690                                         break;
8691                                 case (BT_INFO_HID | BT_INFO_A2DP):
8692                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + A2DP\n"));
8693                                         algorithm = BT_2ANT_COEX_ALGO_HID_A2DP;
8694                                         break;
8695                                 case (BT_INFO_HID | BT_INFO_FTP):
8696                                         if (bBtHsModeExist) {
8697                                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + PAN(HS)\n"));
8698                                                 algorithm = BT_2ANT_COEX_ALGO_HID_A2DP;
8699                                         } else {
8700                                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + PAN(EDR)\n"));
8701                                                 algorithm = BT_2ANT_COEX_ALGO_PANEDR_HID;
8702                                         }
8703                                         break;
8704                                 case (BT_INFO_A2DP | BT_INFO_FTP):
8705                                         if (bBtHsModeExist) {
8706                                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], A2DP + PAN(HS)\n"));
8707                                                 algorithm = BT_2ANT_COEX_ALGO_A2DP;
8708                                         } else {
8709                                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], A2DP + PAN(EDR)\n"));
8710                                                 algorithm = BT_2ANT_COEX_ALGO_PANEDR_A2DP;
8711                                         }
8712                                         break;
8713                                 case (BT_INFO_HID | BT_INFO_A2DP | BT_INFO_FTP):
8714                                         if (bBtHsModeExist) {
8715                                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + A2DP + PAN(HS)\n"));
8716                                                 algorithm = BT_2ANT_COEX_ALGO_HID_A2DP;
8717                                         } else {
8718                                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + A2DP + PAN(EDR)\n"));
8719                                                 algorithm = BT_2ANT_COEX_ALGO_HID_A2DP_PANEDR;
8720                                         }
8721                                         break;
8722                                 }
8723                         } else {
8724                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], SCO only\n"));
8725                                 algorithm = BT_2ANT_COEX_ALGO_SCO;
8726                         }
8727                 } else {
8728                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], non SCO\n"));
8729                         switch (btInfo&0xe0) {
8730                         case BT_INFO_HID:
8731                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID\n"));
8732                                 algorithm = BT_2ANT_COEX_ALGO_HID;
8733                                 break;
8734                         case BT_INFO_A2DP:
8735                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex],  A2DP\n"));
8736                                 algorithm = BT_2ANT_COEX_ALGO_A2DP;
8737                                 break;
8738                         case BT_INFO_FTP:
8739                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], PAN(EDR)\n"));
8740                                 algorithm = BT_2ANT_COEX_ALGO_PANEDR_HID;
8741                                 break;
8742                         case (BT_INFO_HID | BT_INFO_A2DP):
8743                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + A2DP\n"));
8744                                 algorithm = BT_2ANT_COEX_ALGO_HID_A2DP;
8745                                 break;
8746                         case (BT_INFO_HID|BT_INFO_FTP):
8747                                 if (bBtHsModeExist) {
8748                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + PAN(HS)\n"));
8749                                         algorithm = BT_2ANT_COEX_ALGO_HID_A2DP;
8750                                 } else {
8751                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + PAN(EDR)\n"));
8752                                         algorithm = BT_2ANT_COEX_ALGO_PANEDR_HID;
8753                                 }
8754                                 break;
8755                         case (BT_INFO_A2DP|BT_INFO_FTP):
8756                                 if (bBtHsModeExist) {
8757                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], A2DP + PAN(HS)\n"));
8758                                         algorithm = BT_2ANT_COEX_ALGO_A2DP;
8759                                 } else {
8760                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], A2DP + PAN(EDR)\n"));
8761                                         algorithm = BT_2ANT_COEX_ALGO_PANEDR_A2DP;
8762                                 }
8763                                 break;
8764                         case (BT_INFO_HID|BT_INFO_A2DP|BT_INFO_FTP):
8765                                 if (bBtHsModeExist) {
8766                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + A2DP + PAN(HS)\n"));
8767                                         algorithm = BT_2ANT_COEX_ALGO_HID_A2DP;
8768                                 } else {
8769                                         RTPRINT(FBT, BT_TRACE, ("[BTCoex], HID + A2DP + PAN(EDR)\n"));
8770                                         algorithm = BT_2ANT_COEX_ALGO_HID_A2DP_PANEDR;
8771                                 }
8772                                 break;
8773                         }
8774
8775                 }
8776         } else {
8777                 RTPRINT(FBT, BT_TRACE, ("[BTC2H], BTInfo: bConnect = false\n"));
8778                 pBtdm8723->btStatus = BT_2ANT_BT_STATUS_IDLE;
8779         }
8780
8781         pBtdm8723->curAlgorithm = algorithm;
8782         RTPRINT(FBT, BT_TRACE, ("[BTCoex], Algorithm = %d \n", pBtdm8723->curAlgorithm));
8783
8784 /* From */
8785         BTDM_CheckWiFiState(padapter);
8786         if (pBtMgnt->ExtConfig.bManualControl) {
8787                 RTPRINT(FBT, BT_TRACE, ("Action Manual control, won't execute bt coexist mechanism!!\n"));
8788                 return;
8789         }
8790 }
8791
8792 void BTDM_2AntBtCoexist8723A(struct rtw_adapter *padapter)
8793 {
8794         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
8795         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
8796         struct bt_dgb *pBtDbg = &pBTInfo->BtDbg;
8797         u8 btInfoOriginal = 0;
8798         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
8799         struct btdm_8723a_2ant *pBtdm8723 = &pHalData->bt_coexist.halCoex8723.btdm2Ant;
8800
8801         if (BTDM_BtProfileSupport(padapter)) {
8802                 if (pBtMgnt->ExtConfig.bHoldForBtOperation) {
8803                         RTPRINT(FBT, BT_TRACE, ("Action for BT Operation adjust!!\n"));
8804                         return;
8805                 }
8806                 if (pBtMgnt->ExtConfig.bHoldPeriodCnt) {
8807                         RTPRINT(FBT, BT_TRACE, ("Hold BT inquiry/page scan setting (cnt = %d)!!\n",
8808                                 pBtMgnt->ExtConfig.bHoldPeriodCnt));
8809                         if (pBtMgnt->ExtConfig.bHoldPeriodCnt >= 11) {
8810                                 pBtMgnt->ExtConfig.bHoldPeriodCnt = 0;
8811                                 /*  next time the coexist parameters should be reset again. */
8812                         } else {
8813                                 pBtMgnt->ExtConfig.bHoldPeriodCnt++;
8814                         }
8815                         return;
8816                 }
8817
8818                 if (pBtDbg->dbgCtrl)
8819                         RTPRINT(FBT, BT_TRACE, ("[Dbg control], "));
8820
8821                 pBtdm8723->curAlgorithm = btdm_ActionAlgorithm(padapter);
8822                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], Algorithm = %d \n", pBtdm8723->curAlgorithm));
8823
8824                 if (btdm_Is2Ant8723ACommonAction(padapter)) {
8825                         RTPRINT(FBT, BT_TRACE, ("Action 2-Ant common.\n"));
8826                         pBtdm8723->bResetTdmaAdjust = true;
8827                 } else {
8828                         if (pBtdm8723->curAlgorithm != pBtdm8723->preAlgorithm) {
8829                                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], preAlgorithm =%d, curAlgorithm =%d\n",
8830                                 pBtdm8723->preAlgorithm, pBtdm8723->curAlgorithm));
8831                                 pBtdm8723->bResetTdmaAdjust = true;
8832                         }
8833                         switch (pBtdm8723->curAlgorithm) {
8834                         case BT_2ANT_COEX_ALGO_SCO:
8835                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = SCO.\n"));
8836                                 btdm_2Ant8723ASCOAction(padapter);
8837                                 break;
8838                         case BT_2ANT_COEX_ALGO_HID:
8839                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = HID.\n"));
8840                                 btdm_2Ant8723AHIDAction(padapter);
8841                                 break;
8842                         case BT_2ANT_COEX_ALGO_A2DP:
8843                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = A2DP.\n"));
8844                                 btdm_2Ant8723AA2DPAction(padapter);
8845                                 break;
8846                         case BT_2ANT_COEX_ALGO_PANEDR:
8847                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = PAN(EDR).\n"));
8848                                 btdm_2Ant8723APANEDRAction(padapter);
8849                                 break;
8850                         case BT_2ANT_COEX_ALGO_PANHS:
8851                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = HS mode.\n"));
8852                                 btdm_2Ant8723APANHSAction(padapter);
8853                                 break;
8854                         case BT_2ANT_COEX_ALGO_PANEDR_A2DP:
8855                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = PAN+A2DP.\n"));
8856                                 btdm_2Ant8723APANEDRA2DPAction(padapter);
8857                                 break;
8858                         case BT_2ANT_COEX_ALGO_PANEDR_HID:
8859                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = PAN(EDR)+HID.\n"));
8860                                 btdm_2Ant8723APANEDRHIDAction(padapter);
8861                                 break;
8862                         case BT_2ANT_COEX_ALGO_HID_A2DP_PANEDR:
8863                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = HID+A2DP+PAN.\n"));
8864                                 btdm_2Ant8723AHIDA2DPPANEDRAction(padapter);
8865                                 break;
8866                         case BT_2ANT_COEX_ALGO_HID_A2DP:
8867                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = HID+A2DP.\n"));
8868                                 btdm_2Ant8723AHIDA2DPAction(padapter);
8869                                 break;
8870                         default:
8871                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = 0.\n"));
8872                                 btdm_2Ant8723AA2DPAction(padapter);
8873                                 break;
8874                         }
8875                         pBtdm8723->preAlgorithm = pBtdm8723->curAlgorithm;
8876                 }
8877         } else {
8878                 RTPRINT(FBT, BT_TRACE, ("[BTCoex] Get bt info by fw!!\n"));
8879                 /* msg shows c2h rsp for bt_info is received or not. */
8880                 if (pHalData->bt_coexist.halCoex8723.bC2hBtInfoReqSent)
8881                         RTPRINT(FBT, BT_TRACE, ("[BTCoex] c2h for btInfo not rcvd yet!!\n"));
8882
8883                 btInfoOriginal = pHalData->bt_coexist.halCoex8723.c2hBtInfoOriginal;
8884
8885                 if (pBtMgnt->ExtConfig.bHoldForBtOperation) {
8886                         RTPRINT(FBT, BT_TRACE, ("Action for BT Operation adjust!!\n"));
8887                         return;
8888                 }
8889                 if (pBtMgnt->ExtConfig.bHoldPeriodCnt) {
8890                         RTPRINT(FBT, BT_TRACE,
8891                                 ("Hold BT inquiry/page scan setting (cnt = %d)!!\n",
8892                                 pBtMgnt->ExtConfig.bHoldPeriodCnt));
8893                         if (pBtMgnt->ExtConfig.bHoldPeriodCnt >= 11) {
8894                                 pBtMgnt->ExtConfig.bHoldPeriodCnt = 0;
8895                                 /*  next time the coexist parameters should be reset again. */
8896                         } else {
8897                                  pBtMgnt->ExtConfig.bHoldPeriodCnt++;
8898                         }
8899                         return;
8900                 }
8901
8902                 if (pBtDbg->dbgCtrl)
8903                         RTPRINT(FBT, BT_TRACE, ("[Dbg control], "));
8904                 if (btdm_Is2Ant8723ACommonAction(padapter)) {
8905                         RTPRINT(FBT, BT_TRACE, ("Action 2-Ant common.\n"));
8906                         pBtdm8723->bResetTdmaAdjust = true;
8907                 } else {
8908                         if (pBtdm8723->curAlgorithm != pBtdm8723->preAlgorithm) {
8909                                 RTPRINT(FBT, BT_TRACE,
8910                                         ("[BTCoex], preAlgorithm =%d, curAlgorithm =%d\n",
8911                                         pBtdm8723->preAlgorithm,
8912                                         pBtdm8723->curAlgorithm));
8913                                 pBtdm8723->bResetTdmaAdjust = true;
8914                         }
8915                         switch (pBtdm8723->curAlgorithm) {
8916                         case BT_2ANT_COEX_ALGO_SCO:
8917                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = SCO.\n"));
8918                                 btdm_2Ant8723ASCOAction(padapter);
8919                                 break;
8920                         case BT_2ANT_COEX_ALGO_HID:
8921                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = HID.\n"));
8922                                 btdm_2Ant8723AHIDAction(padapter);
8923                                 break;
8924                         case BT_2ANT_COEX_ALGO_A2DP:
8925                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = A2DP.\n"));
8926                                 btdm_2Ant8723AA2dp(padapter);
8927                                 break;
8928                         case BT_2ANT_COEX_ALGO_PANEDR:
8929                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = PAN(EDR).\n"));
8930                                 btdm_2Ant8723APANEDRAction(padapter);
8931                                 break;
8932                         case BT_2ANT_COEX_ALGO_PANHS:
8933                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = HS mode.\n"));
8934                                 btdm_2Ant8723APANHSAction(padapter);
8935                                 break;
8936                         case BT_2ANT_COEX_ALGO_PANEDR_A2DP:
8937                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = PAN+A2DP.\n"));
8938                                 btdm_2Ant8723APANEDRA2DPAction(padapter);
8939                                 break;
8940                         case BT_2ANT_COEX_ALGO_PANEDR_HID:
8941                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = PAN(EDR)+HID.\n"));
8942                                 btdm_2Ant8723APANEDRHIDAction(padapter);
8943                                 break;
8944                         case BT_2ANT_COEX_ALGO_HID_A2DP_PANEDR:
8945                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = HID+A2DP+PAN.\n"));
8946                                 btdm_2Ant8723AHIDA2DPPANEDRAction(padapter);
8947                                 break;
8948                         case BT_2ANT_COEX_ALGO_HID_A2DP:
8949                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = HID+A2DP.\n"));
8950                                 btdm_2Ant8723AHIDA2DPAction(padapter);
8951                                 break;
8952                         default:
8953                                 RTPRINT(FBT, BT_TRACE, ("Action 2-Ant, algorithm = 0.\n"));
8954                                 btdm_2Ant8723AA2DPAction(padapter);
8955                                 break;
8956                         }
8957                         pBtdm8723->preAlgorithm = pBtdm8723->curAlgorithm;
8958                 }
8959         }
8960 }
8961
8962 /*  ===== End of sync from SD7 driver HAL/BTCoexist/HalBtc87232Ant.c ===== */
8963
8964 /*  ===== Below this line is sync from SD7 driver HAL/BTCoexist/HalBtc8723.c ===== */
8965
8966 static u8 btCoexDbgBuf[BT_TMP_BUF_SIZE];
8967
8968 static const char *const BtProfileString[] = {
8969         "NONE",
8970         "A2DP",
8971         "PAN",
8972         "HID",
8973         "SCO",
8974 };
8975
8976 static const char *const BtSpecString[] = {
8977         "1.0b",
8978         "1.1",
8979         "1.2",
8980         "2.0+EDR",
8981         "2.1+EDR",
8982         "3.0+HS",
8983         "4.0",
8984 };
8985
8986 static const char *const BtLinkRoleString[] = {
8987         "Master",
8988         "Slave",
8989 };
8990
8991 static u8 btdm_BtWifiAntNum(struct rtw_adapter *padapter)
8992 {
8993         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
8994         struct bt_coexist_8723a *pBtCoex = &pHalData->bt_coexist.halCoex8723;
8995
8996         if (Ant_x2 == pHalData->bt_coexist.BT_Ant_Num) {
8997                 if (Ant_x2 == pBtCoex->TotalAntNum)
8998                         return Ant_x2;
8999                 else
9000                         return Ant_x1;
9001         } else {
9002                 return Ant_x1;
9003         }
9004         return Ant_x2;
9005 }
9006
9007 static void btdm_BtHwCountersMonitor(struct rtw_adapter *padapter)
9008 {
9009         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
9010         u32     regHPTxRx, regLPTxRx, u4Tmp;
9011         u32     regHPTx = 0, regHPRx = 0, regLPTx = 0, regLPRx = 0;
9012
9013         regHPTxRx = REG_HIGH_PRIORITY_TXRX;
9014         regLPTxRx = REG_LOW_PRIORITY_TXRX;
9015
9016         u4Tmp = rtl8723au_read32(padapter, regHPTxRx);
9017         regHPTx = u4Tmp & bMaskLWord;
9018         regHPRx = (u4Tmp & bMaskHWord)>>16;
9019
9020         u4Tmp = rtl8723au_read32(padapter, regLPTxRx);
9021         regLPTx = u4Tmp & bMaskLWord;
9022         regLPRx = (u4Tmp & bMaskHWord)>>16;
9023
9024         pHalData->bt_coexist.halCoex8723.highPriorityTx = regHPTx;
9025         pHalData->bt_coexist.halCoex8723.highPriorityRx = regHPRx;
9026         pHalData->bt_coexist.halCoex8723.lowPriorityTx = regLPTx;
9027         pHalData->bt_coexist.halCoex8723.lowPriorityRx = regLPRx;
9028
9029         RTPRINT(FBT, BT_TRACE, ("High Priority Tx/Rx = %d / %d\n", regHPTx, regHPRx));
9030         RTPRINT(FBT, BT_TRACE, ("Low Priority Tx/Rx = %d / %d\n", regLPTx, regLPRx));
9031
9032         /*  reset counter */
9033         rtl8723au_write8(padapter, 0x76e, 0xc);
9034 }
9035
9036 /*  This function check if 8723 bt is disabled */
9037 static void btdm_BtEnableDisableCheck8723A(struct rtw_adapter *padapter)
9038 {
9039         u8 btAlife = true;
9040         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
9041
9042 #ifdef CHECK_BT_EXIST_FROM_REG
9043         u8 val8;
9044
9045         /*  ox68[28]= 1 => BT enable; otherwise disable */
9046         val8 = rtl8723au_read8(padapter, 0x6B);
9047         if (!(val8 & BIT(4)))
9048                 btAlife = false;
9049
9050         if (btAlife)
9051                 pHalData->bt_coexist.bCurBtDisabled = false;
9052         else
9053                 pHalData->bt_coexist.bCurBtDisabled = true;
9054 #else
9055         if (pHalData->bt_coexist.halCoex8723.highPriorityTx == 0 &&
9056             pHalData->bt_coexist.halCoex8723.highPriorityRx == 0 &&
9057             pHalData->bt_coexist.halCoex8723.lowPriorityTx == 0 &&
9058             pHalData->bt_coexist.halCoex8723.lowPriorityRx == 0)
9059                 btAlife = false;
9060         if (pHalData->bt_coexist.halCoex8723.highPriorityTx == 0xeaea &&
9061             pHalData->bt_coexist.halCoex8723.highPriorityRx == 0xeaea &&
9062             pHalData->bt_coexist.halCoex8723.lowPriorityTx == 0xeaea &&
9063             pHalData->bt_coexist.halCoex8723.lowPriorityRx == 0xeaea)
9064                 btAlife = false;
9065         if (pHalData->bt_coexist.halCoex8723.highPriorityTx == 0xffff &&
9066             pHalData->bt_coexist.halCoex8723.highPriorityRx == 0xffff &&
9067             pHalData->bt_coexist.halCoex8723.lowPriorityTx == 0xffff &&
9068             pHalData->bt_coexist.halCoex8723.lowPriorityRx == 0xffff)
9069                 btAlife = false;
9070         if (btAlife) {
9071                 pHalData->bt_coexist.btActiveZeroCnt = 0;
9072                 pHalData->bt_coexist.bCurBtDisabled = false;
9073                 RTPRINT(FBT, BT_TRACE, ("8723A BT is enabled !!\n"));
9074         } else {
9075                 pHalData->bt_coexist.btActiveZeroCnt++;
9076                 RTPRINT(FBT, BT_TRACE, ("8723A bt all counters = 0, %d times!!\n",
9077                                 pHalData->bt_coexist.btActiveZeroCnt));
9078                 if (pHalData->bt_coexist.btActiveZeroCnt >= 2) {
9079                         pHalData->bt_coexist.bCurBtDisabled = true;
9080                         RTPRINT(FBT, BT_TRACE, ("8723A BT is disabled !!\n"));
9081                 }
9082         }
9083 #endif
9084
9085         if (!pHalData->bt_coexist.bCurBtDisabled) {
9086                 if (BTDM_IsWifiConnectionExist(padapter))
9087                         BTDM_SetFwChnlInfo(padapter, RT_MEDIA_CONNECT);
9088                 else
9089                         BTDM_SetFwChnlInfo(padapter, RT_MEDIA_DISCONNECT);
9090         }
9091
9092         if (pHalData->bt_coexist.bPreBtDisabled !=
9093             pHalData->bt_coexist.bCurBtDisabled) {
9094                 RTPRINT(FBT, BT_TRACE, ("8723A BT is from %s to %s!!\n",
9095                         (pHalData->bt_coexist.bPreBtDisabled ? "disabled":"enabled"),
9096                         (pHalData->bt_coexist.bCurBtDisabled ? "disabled":"enabled")));
9097                 pHalData->bt_coexist.bPreBtDisabled = pHalData->bt_coexist.bCurBtDisabled;
9098         }
9099 }
9100
9101 static void btdm_BTCoexist8723AHandler(struct rtw_adapter *padapter)
9102 {
9103         struct hal_data_8723a *pHalData;
9104
9105         pHalData = GET_HAL_DATA(padapter);
9106
9107         if (btdm_BtWifiAntNum(padapter) == Ant_x2) {
9108                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], 2 Ant mechanism\n"));
9109                 BTDM_2AntBtCoexist8723A(padapter);
9110         } else {
9111                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], 1 Ant mechanism\n"));
9112                 BTDM_1AntBtCoexist8723A(padapter);
9113         }
9114
9115         if (!BTDM_IsSameCoexistState(padapter)) {
9116                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], Coexist State[bitMap] change from 0x%"i64fmt"x to 0x%"i64fmt"x\n",
9117                         pHalData->bt_coexist.PreviousState,
9118                         pHalData->bt_coexist.CurrentState));
9119                 pHalData->bt_coexist.PreviousState = pHalData->bt_coexist.CurrentState;
9120
9121                 RTPRINT(FBT, BT_TRACE, ("["));
9122                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_BT30)
9123                         RTPRINT(FBT, BT_TRACE, ("BT 3.0, "));
9124                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_WIFI_HT20)
9125                         RTPRINT(FBT, BT_TRACE, ("HT20, "));
9126                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_WIFI_HT40)
9127                         RTPRINT(FBT, BT_TRACE, ("HT40, "));
9128                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_WIFI_LEGACY)
9129                         RTPRINT(FBT, BT_TRACE, ("Legacy, "));
9130                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_WIFI_RSSI_LOW)
9131                         RTPRINT(FBT, BT_TRACE, ("Rssi_Low, "));
9132                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_WIFI_RSSI_MEDIUM)
9133                         RTPRINT(FBT, BT_TRACE, ("Rssi_Mid, "));
9134                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_WIFI_RSSI_HIGH)
9135                         RTPRINT(FBT, BT_TRACE, ("Rssi_High, "));
9136                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_WIFI_IDLE)
9137                         RTPRINT(FBT, BT_TRACE, ("Wifi_Idle, "));
9138                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_WIFI_UPLINK)
9139                         RTPRINT(FBT, BT_TRACE, ("Wifi_Uplink, "));
9140                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_WIFI_DOWNLINK)
9141                         RTPRINT(FBT, BT_TRACE, ("Wifi_Downlink, "));
9142                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_BT_IDLE)
9143                         RTPRINT(FBT, BT_TRACE, ("BT_idle, "));
9144                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_PROFILE_HID)
9145                         RTPRINT(FBT, BT_TRACE, ("PRO_HID, "));
9146                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_PROFILE_A2DP)
9147                         RTPRINT(FBT, BT_TRACE, ("PRO_A2DP, "));
9148                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_PROFILE_PAN)
9149                         RTPRINT(FBT, BT_TRACE, ("PRO_PAN, "));
9150                 if (pHalData->bt_coexist.CurrentState & BT_COEX_STATE_PROFILE_SCO)
9151                         RTPRINT(FBT, BT_TRACE, ("PRO_SCO, "));
9152                 RTPRINT(FBT, BT_TRACE, ("]\n"));
9153         }
9154 }
9155
9156 /*  extern function start with BTDM_ */
9157 u32 BTDM_BtTxRxCounterH(struct rtw_adapter *padapter)
9158 {
9159         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
9160         u32     counters = 0;
9161
9162         counters = pHalData->bt_coexist.halCoex8723.highPriorityTx+
9163                 pHalData->bt_coexist.halCoex8723.highPriorityRx;
9164         return counters;
9165 }
9166
9167 u32 BTDM_BtTxRxCounterL(struct rtw_adapter *padapter)
9168 {
9169         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
9170         u32     counters = 0;
9171
9172         counters = pHalData->bt_coexist.halCoex8723.lowPriorityTx+
9173                 pHalData->bt_coexist.halCoex8723.lowPriorityRx ;
9174         return counters;
9175 }
9176
9177 void BTDM_SetFwChnlInfo(struct rtw_adapter *padapter, enum rt_media_status mstatus)
9178 {
9179         struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
9180         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
9181         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
9182         u8 H2C_Parameter[3] = {0};
9183         u8 chnl;
9184
9185         /*  opMode */
9186         if (RT_MEDIA_CONNECT == mstatus)
9187                 H2C_Parameter[0] = 0x1; /*  0: disconnected, 1:connected */
9188
9189         if (check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE)) {
9190                 /*  channel */
9191                 chnl = pmlmeext->cur_channel;
9192                 if (BTDM_IsHT40(padapter)) {
9193                         if (pmlmeext->cur_ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER)
9194                                 chnl -= 2;
9195                         else if (pmlmeext->cur_ch_offset == HAL_PRIME_CHNL_OFFSET_LOWER)
9196                                 chnl += 2;
9197                 }
9198                 H2C_Parameter[1] = chnl;
9199         } else {        /*  check if HS link is exists */
9200                 /*  channel */
9201                 if (BT_Operation(padapter))
9202                         H2C_Parameter[1] = pBtMgnt->BTChannel;
9203                 else
9204                         H2C_Parameter[1] = pmlmeext->cur_channel;
9205         }
9206
9207         if (BTDM_IsHT40(padapter))
9208                 H2C_Parameter[2] = 0x30;
9209         else
9210                 H2C_Parameter[2] = 0x20;
9211
9212         FillH2CCmd(padapter, 0x19, 3, H2C_Parameter);
9213 }
9214
9215 u8 BTDM_IsWifiConnectionExist(struct rtw_adapter *padapter)
9216 {
9217         u8 bRet = false;
9218
9219         if (BTHCI_HsConnectionEstablished(padapter))
9220                 bRet = true;
9221
9222         if (check_fwstate(&padapter->mlmepriv, WIFI_ASOC_STATE) == true)
9223                 bRet = true;
9224
9225         return bRet;
9226 }
9227
9228 void BTDM_SetFw3a(
9229         struct rtw_adapter *padapter,
9230         u8 byte1,
9231         u8 byte2,
9232         u8 byte3,
9233         u8 byte4,
9234         u8 byte5
9235         )
9236 {
9237         u8 H2C_Parameter[5] = {0};
9238
9239         if (rtl8723a_BT_using_antenna_1(padapter)) {
9240                 if ((!check_fwstate(&padapter->mlmepriv, WIFI_STATION_STATE)) &&
9241                     (get_fwstate(&padapter->mlmepriv) != WIFI_NULL_STATE)) {
9242                         /*  for softap mode */
9243                         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
9244                         struct bt_coexist_8723a *pBtCoex = &pHalData->bt_coexist.halCoex8723;
9245                         u8 BtState = pBtCoex->c2hBtInfo;
9246
9247                         if ((BtState != BT_INFO_STATE_NO_CONNECTION) &&
9248                             (BtState != BT_INFO_STATE_CONNECT_IDLE)) {
9249                                 if (byte1 & BIT(4)) {
9250                                         byte1 &= ~BIT(4);
9251                                         byte1 |= BIT(5);
9252                                 }
9253
9254                                 byte5 |= BIT(5);
9255                                 if (byte5 & BIT(6))
9256                                         byte5 &= ~BIT(6);
9257                         }
9258                 }
9259         }
9260
9261         H2C_Parameter[0] = byte1;
9262         H2C_Parameter[1] = byte2;
9263         H2C_Parameter[2] = byte3;
9264         H2C_Parameter[3] = byte4;
9265         H2C_Parameter[4] = byte5;
9266
9267         RTPRINT(FBT, BT_TRACE, ("[BTCoex], FW write 0x3a(5bytes) = 0x%02x%08x\n",
9268                 H2C_Parameter[0],
9269                 H2C_Parameter[1]<<24|H2C_Parameter[2]<<16|H2C_Parameter[3]<<8|H2C_Parameter[4]));
9270
9271         FillH2CCmd(padapter, 0x3a, 5, H2C_Parameter);
9272 }
9273
9274 void BTDM_QueryBtInformation(struct rtw_adapter *padapter)
9275 {
9276         u8 H2C_Parameter[1] = {0};
9277         struct hal_data_8723a *pHalData;
9278         struct bt_coexist_8723a *pBtCoex;
9279
9280         pHalData = GET_HAL_DATA(padapter);
9281         pBtCoex = &pHalData->bt_coexist.halCoex8723;
9282
9283         if (!rtl8723a_BT_enabled(padapter)) {
9284                 pBtCoex->c2hBtInfo = BT_INFO_STATE_DISABLED;
9285                 pBtCoex->bC2hBtInfoReqSent = false;
9286                 return;
9287         }
9288
9289         if (pBtCoex->c2hBtInfo == BT_INFO_STATE_DISABLED)
9290                 pBtCoex->c2hBtInfo = BT_INFO_STATE_NO_CONNECTION;
9291
9292         if (pBtCoex->bC2hBtInfoReqSent == true)
9293                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], didn't recv previous BtInfo report!\n"));
9294         else
9295                 pBtCoex->bC2hBtInfoReqSent = true;
9296
9297         H2C_Parameter[0] |= BIT(0);     /*  trigger */
9298
9299 /*RTPRINT(FBT, BT_TRACE, ("[BTCoex], Query Bt information, write 0x38 = 0x%x\n", */
9300 /*H2C_Parameter[0])); */
9301
9302         FillH2CCmd(padapter, 0x38, 1, H2C_Parameter);
9303 }
9304
9305 void BTDM_SetSwRfRxLpfCorner(struct rtw_adapter *padapter, u8 type)
9306 {
9307         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
9308
9309         if (BT_RF_RX_LPF_CORNER_SHRINK == type) {
9310                 /* Shrink RF Rx LPF corner */
9311                 RTPRINT(FBT, BT_TRACE, ("Shrink RF Rx LPF corner!!\n"));
9312                 PHY_SetRFReg(padapter, PathA, 0x1e, bRFRegOffsetMask, 0xf0ff7);
9313                 pHalData->bt_coexist.bSWCoexistAllOff = false;
9314         } else if (BT_RF_RX_LPF_CORNER_RESUME == type) {
9315                 /* Resume RF Rx LPF corner */
9316                 RTPRINT(FBT, BT_TRACE, ("Resume RF Rx LPF corner!!\n"));
9317                 PHY_SetRFReg(padapter, PathA, 0x1e, bRFRegOffsetMask, pHalData->bt_coexist.BtRfRegOrigin1E);
9318         }
9319 }
9320
9321 void
9322 BTDM_SetSwPenaltyTxRateAdaptive(
9323         struct rtw_adapter *padapter,
9324         u8 raType
9325         )
9326 {
9327         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
9328         u8 tmpU1;
9329
9330         tmpU1 = rtl8723au_read8(padapter, 0x4fd);
9331         tmpU1 |= BIT(0);
9332         if (BT_TX_RATE_ADAPTIVE_LOW_PENALTY == raType) {
9333                 tmpU1 &= ~BIT(2);
9334                 pHalData->bt_coexist.bSWCoexistAllOff = false;
9335         } else if (BT_TX_RATE_ADAPTIVE_NORMAL == raType) {
9336                 tmpU1 |= BIT(2);
9337         }
9338
9339         rtl8723au_write8(padapter, 0x4fd, tmpU1);
9340 }
9341
9342 void BTDM_SetFwDecBtPwr(struct rtw_adapter *padapter, u8 bDecBtPwr)
9343 {
9344         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
9345         u8 H2C_Parameter[1] = {0};
9346
9347         H2C_Parameter[0] = 0;
9348
9349         if (bDecBtPwr) {
9350                 H2C_Parameter[0] |= BIT(1);
9351                 pHalData->bt_coexist.bFWCoexistAllOff = false;
9352         }
9353
9354         RTPRINT(FBT, BT_TRACE, ("[BTCoex], decrease Bt Power : %s, write 0x21 = 0x%x\n",
9355                 (bDecBtPwr ? "Yes!!" : "No!!"), H2C_Parameter[0]));
9356
9357         FillH2CCmd(padapter, 0x21, 1, H2C_Parameter);
9358 }
9359
9360 u8 BTDM_BtProfileSupport(struct rtw_adapter *padapter)
9361 {
9362         u8 bRet = false;
9363         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
9364         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
9365         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
9366
9367         if (pBtMgnt->bSupportProfile &&
9368             !pHalData->bt_coexist.halCoex8723.bForceFwBtInfo)
9369                 bRet = true;
9370
9371         return bRet;
9372 }
9373
9374 static void BTDM_AdjustForBtOperation8723A(struct rtw_adapter *padapter)
9375 {
9376         /* BTDM_2AntAdjustForBtOperation8723(padapter); */
9377 }
9378
9379 static void BTDM_FwC2hBtRssi8723A(struct rtw_adapter *padapter, u8 *tmpBuf)
9380 {
9381         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
9382         u8 percent = 0, u1tmp = 0;
9383
9384         u1tmp = tmpBuf[0];
9385         percent = u1tmp*2+10;
9386
9387         pHalData->bt_coexist.halCoex8723.btRssi = percent;
9388 /*RTPRINT(FBT, BT_TRACE, ("[BTC2H], BT RSSI =%d\n", percent)); */
9389 }
9390
9391 void
9392 rtl8723a_fw_c2h_BT_info(struct rtw_adapter *padapter, u8 *tmpBuf, u8 length)
9393 {
9394         struct hal_data_8723a *pHalData;
9395         struct bt_30info *pBTInfo;
9396         struct bt_mgnt *pBtMgnt;
9397         struct bt_coexist_8723a *pBtCoex;
9398         u8 i;
9399
9400         pHalData = GET_HAL_DATA(padapter);
9401         pBTInfo = GET_BT_INFO(padapter);
9402         pBtMgnt = &pBTInfo->BtMgnt;
9403         pBtCoex = &pHalData->bt_coexist.halCoex8723;
9404
9405         pBtCoex->bC2hBtInfoReqSent = false;
9406
9407         RTPRINT(FBT, BT_TRACE, ("[BTC2H], BT info[%d]=[", length));
9408
9409         pBtCoex->btRetryCnt = 0;
9410         for (i = 0; i < length; i++) {
9411                 switch (i) {
9412                 case 0:
9413                         pBtCoex->c2hBtInfoOriginal = tmpBuf[i];
9414                         break;
9415                 case 1:
9416                         pBtCoex->btRetryCnt = tmpBuf[i];
9417                         break;
9418                 case 2:
9419                         BTDM_FwC2hBtRssi8723A(padapter, &tmpBuf[i]);
9420                         break;
9421                 case 3:
9422                         pBtCoex->btInfoExt = tmpBuf[i]&BIT(0);
9423                         break;
9424                 }
9425
9426                 if (i == length-1)
9427                         RTPRINT(FBT, BT_TRACE, ("0x%02x]\n", tmpBuf[i]));
9428                 else
9429                         RTPRINT(FBT, BT_TRACE, ("0x%02x, ", tmpBuf[i]));
9430         }
9431         RTPRINT(FBT, BT_TRACE, ("[BTC2H], BT RSSI =%d\n", pBtCoex->btRssi));
9432         if (pBtCoex->btInfoExt)
9433                 RTPRINT(FBT, BT_TRACE, ("[BTC2H], pBtCoex->btInfoExt =%x\n", pBtCoex->btInfoExt));
9434
9435         if (btdm_BtWifiAntNum(padapter) == Ant_x1)
9436                 BTDM_1AntFwC2hBtInfo8723A(padapter);
9437         else
9438                 BTDM_2AntFwC2hBtInfo8723A(padapter);
9439
9440         if (pBtMgnt->ExtConfig.bManualControl) {
9441                 RTPRINT(FBT, BT_TRACE, ("%s: Action Manual control!!\n", __func__));
9442                 return;
9443         }
9444
9445         btdm_BTCoexist8723AHandler(padapter);
9446 }
9447
9448 static void BTDM_Display8723ABtCoexInfo(struct rtw_adapter *padapter)
9449 {
9450         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
9451         struct bt_coexist_8723a *pBtCoex = &pHalData->bt_coexist.halCoex8723;
9452         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
9453         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
9454         u8 u1Tmp, u1Tmp1, u1Tmp2, i, btInfoExt, psTdmaCase = 0;
9455         u32 u4Tmp[4];
9456         u8 antNum = Ant_x2;
9457
9458         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n ============[BT Coexist info]============");
9459         DCMD_Printf(btCoexDbgBuf);
9460
9461         if (!rtl8723a_BT_coexist(padapter)) {
9462                 rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n BT not exists !!!");
9463                 DCMD_Printf(btCoexDbgBuf);
9464                 return;
9465         }
9466
9467         antNum = btdm_BtWifiAntNum(padapter);
9468         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/%d ", "Ant mechanism PG/Now run :", \
9469                 ((pHalData->bt_coexist.BT_Ant_Num == Ant_x2) ? 2 : 1), ((antNum == Ant_x2) ? 2 : 1));
9470         DCMD_Printf(btCoexDbgBuf);
9471
9472         if (pBtMgnt->ExtConfig.bManualControl) {
9473                 rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "[Action Manual control]!!");
9474                 DCMD_Printf(btCoexDbgBuf);
9475         } else {
9476                 rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s / %d", "BT stack/ hci ext ver", \
9477                         ((pBtMgnt->bSupportProfile) ? "Yes" : "No"), pBtMgnt->ExtConfig.HCIExtensionVer);
9478                 DCMD_Printf(btCoexDbgBuf);
9479         }
9480
9481         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\n %-35s = / %d", "Dot11 channel / BT channel", \
9482                 pBtMgnt->BTChannel);
9483                 DCMD_Printf(btCoexDbgBuf);
9484
9485         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\n %-35s = %d / %d / %d", "Wifi/BT/HS rssi", \
9486                 BTDM_GetRxSS(padapter),
9487                 pHalData->bt_coexist.halCoex8723.btRssi,
9488                 pHalData->dmpriv.EntryMinUndecoratedSmoothedPWDB);
9489                         DCMD_Printf(btCoexDbgBuf);
9490
9491         if (!pBtMgnt->ExtConfig.bManualControl) {
9492                 rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\n %-35s = %s / %s ", "WIfi status",
9493                         ((BTDM_Legacy(padapter)) ? "Legacy" : (((BTDM_IsHT40(padapter)) ? "HT40" : "HT20"))),
9494                         ((!BTDM_IsWifiBusy(padapter)) ? "idle" : ((BTDM_IsWifiUplink(padapter)) ? "uplink" : "downlink")));
9495                 DCMD_Printf(btCoexDbgBuf);
9496
9497                 if (pBtMgnt->bSupportProfile) {
9498                         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d / %d / %d / %d", "SCO/HID/PAN/A2DP",
9499                                 ((BTHCI_CheckProfileExist(padapter, BT_PROFILE_SCO)) ? 1 : 0),
9500                                 ((BTHCI_CheckProfileExist(padapter, BT_PROFILE_HID)) ? 1 : 0),
9501                                 ((BTHCI_CheckProfileExist(padapter, BT_PROFILE_PAN)) ? 1 : 0),
9502                                 ((BTHCI_CheckProfileExist(padapter, BT_PROFILE_A2DP)) ? 1 : 0));
9503                 DCMD_Printf(btCoexDbgBuf);
9504
9505                         for (i = 0; i < pBtMgnt->ExtConfig.NumberOfHandle; i++) {
9506                                 if (pBtMgnt->ExtConfig.HCIExtensionVer >= 1) {
9507                                         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %s/ %s", "Bt link type/spec/role",
9508                                                 BtProfileString[pBtMgnt->ExtConfig.linkInfo[i].BTProfile],
9509                                                 BtSpecString[pBtMgnt->ExtConfig.linkInfo[i].BTCoreSpec],
9510                                                 BtLinkRoleString[pBtMgnt->ExtConfig.linkInfo[i].linkRole]);
9511                                         DCMD_Printf(btCoexDbgBuf);
9512
9513                                         btInfoExt = pHalData->bt_coexist.halCoex8723.btInfoExt;
9514                                         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s", "A2DP rate", \
9515                                                  (btInfoExt & BIT(0)) ?
9516                                                  "Basic rate" : "EDR rate");
9517                                         DCMD_Printf(btCoexDbgBuf);
9518                                 } else {
9519                                         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %s", "Bt link type/spec", \
9520                                                 BtProfileString[pBtMgnt->ExtConfig.linkInfo[i].BTProfile],
9521                                                 BtSpecString[pBtMgnt->ExtConfig.linkInfo[i].BTCoreSpec]);
9522                                         DCMD_Printf(btCoexDbgBuf);
9523                                 }
9524                         }
9525                 }
9526         }
9527
9528         /*  Sw mechanism */
9529         if (!pBtMgnt->ExtConfig.bManualControl) {
9530                 rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Sw BT Coex mechanism]============");
9531                 DCMD_Printf(btCoexDbgBuf);
9532                 rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d ", "AGC Table", \
9533                         pBtCoex->btdm2Ant.bCurAgcTableEn);
9534                 DCMD_Printf(btCoexDbgBuf);
9535                 rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d ", "ADC Backoff", \
9536                         pBtCoex->btdm2Ant.bCurAdcBackOff);
9537                 DCMD_Printf(btCoexDbgBuf);
9538                 rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d ", "Low penalty RA", \
9539                         pBtCoex->btdm2Ant.bCurLowPenaltyRa);
9540                 DCMD_Printf(btCoexDbgBuf);
9541                 rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d ", "RF Rx LPF Shrink", \
9542                         pBtCoex->btdm2Ant.bCurRfRxLpfShrink);
9543                 DCMD_Printf(btCoexDbgBuf);
9544         }
9545         u4Tmp[0] = PHY_QueryRFReg(padapter, PathA, 0x1e, 0xff0);
9546         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x", "RF-A, 0x1e[11:4]/original val", \
9547                 u4Tmp[0], pHalData->bt_coexist.BtRfRegOrigin1E);
9548         DCMD_Printf(btCoexDbgBuf);
9549
9550         /*  Fw mechanism */
9551         if (!pBtMgnt->ExtConfig.bManualControl) {
9552                 rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Fw BT Coex mechanism]============");
9553                 DCMD_Printf(btCoexDbgBuf);
9554         }
9555         if (!pBtMgnt->ExtConfig.bManualControl) {
9556                 if (btdm_BtWifiAntNum(padapter) == Ant_x1)
9557                         psTdmaCase = pHalData->bt_coexist.halCoex8723.btdm1Ant.curPsTdma;
9558                 else
9559                         psTdmaCase = pHalData->bt_coexist.halCoex8723.btdm2Ant.curPsTdma;
9560                 rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x %02x %02x case-%d", "PS TDMA(0x3a)", \
9561                         pHalData->bt_coexist.fw3aVal[0], pHalData->bt_coexist.fw3aVal[1],
9562                         pHalData->bt_coexist.fw3aVal[2], pHalData->bt_coexist.fw3aVal[3],
9563                         pHalData->bt_coexist.fw3aVal[4], psTdmaCase);
9564                 DCMD_Printf(btCoexDbgBuf);
9565
9566                 rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d ", "Decrease Bt Power", \
9567                         pBtCoex->btdm2Ant.bCurDecBtPwr);
9568                 DCMD_Printf(btCoexDbgBuf);
9569         }
9570         u1Tmp = rtl8723au_read8(padapter, 0x778);
9571         u1Tmp1 = rtl8723au_read8(padapter, 0x783);
9572         u1Tmp2 = rtl8723au_read8(padapter, 0x796);
9573         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x", "0x778/ 0x783/ 0x796", \
9574                 u1Tmp, u1Tmp1, u1Tmp2);
9575         DCMD_Printf(btCoexDbgBuf);
9576
9577         if (!pBtMgnt->ExtConfig.bManualControl) {
9578                 rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x / 0x%x", "Sw DacSwing Ctrl/Val", \
9579                         pBtCoex->btdm2Ant.bCurDacSwingOn, pBtCoex->btdm2Ant.curDacSwingLvl);
9580                 DCMD_Printf(btCoexDbgBuf);
9581         }
9582         u4Tmp[0] =  rtl8723au_read32(padapter, 0x880);
9583         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "0x880", \
9584                 u4Tmp[0]);
9585         DCMD_Printf(btCoexDbgBuf);
9586
9587         /*  Hw mechanism */
9588         if (!pBtMgnt->ExtConfig.bManualControl) {
9589                 rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s", "============[Hw BT Coex mechanism]============");
9590                 DCMD_Printf(btCoexDbgBuf);
9591         }
9592
9593         u1Tmp = rtl8723au_read8(padapter, 0x40);
9594         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "0x40", \
9595                 u1Tmp);
9596         DCMD_Printf(btCoexDbgBuf);
9597
9598         u4Tmp[0] = rtl8723au_read32(padapter, 0x550);
9599         u1Tmp = rtl8723au_read8(padapter, 0x522);
9600         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/0x%x", "0x550(bcn contrl)/0x522", \
9601                 u4Tmp[0], u1Tmp);
9602         DCMD_Printf(btCoexDbgBuf);
9603
9604         u4Tmp[0] = rtl8723au_read32(padapter, 0x484);
9605         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "0x484(rate adaptive)", \
9606                 u4Tmp[0]);
9607         DCMD_Printf(btCoexDbgBuf);
9608
9609         u4Tmp[0] = rtl8723au_read32(padapter, 0x50);
9610         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "0xc50(dig)", \
9611                 u4Tmp[0]);
9612         DCMD_Printf(btCoexDbgBuf);
9613
9614         u4Tmp[0] = rtl8723au_read32(padapter, 0xda0);
9615         u4Tmp[1] = rtl8723au_read32(padapter, 0xda4);
9616         u4Tmp[2] = rtl8723au_read32(padapter, 0xda8);
9617         u4Tmp[3] = rtl8723au_read32(padapter, 0xdac);
9618         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x", "0xda0/0xda4/0xda8/0xdac(FA cnt)", \
9619                 u4Tmp[0], u4Tmp[1], u4Tmp[2], u4Tmp[3]);
9620         DCMD_Printf(btCoexDbgBuf);
9621
9622         u4Tmp[0] = rtl8723au_read32(padapter, 0x6c0);
9623         u4Tmp[1] = rtl8723au_read32(padapter, 0x6c4);
9624         u4Tmp[2] = rtl8723au_read32(padapter, 0x6c8);
9625         u1Tmp = rtl8723au_read8(padapter, 0x6cc);
9626         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x", "0x6c0/0x6c4/0x6c8/0x6cc(coexTable)", \
9627                 u4Tmp[0], u4Tmp[1], u4Tmp[2], u1Tmp);
9628         DCMD_Printf(btCoexDbgBuf);
9629
9630         /* u4Tmp = rtl8723au_read32(padapter, 0x770); */
9631         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d / %d", "0x770(Hi pri Rx[31:16]/Tx[15:0])", \
9632                 pHalData->bt_coexist.halCoex8723.highPriorityRx,
9633                 pHalData->bt_coexist.halCoex8723.highPriorityTx);
9634         DCMD_Printf(btCoexDbgBuf);
9635         /* u4Tmp = rtl8723au_read32(padapter, 0x774); */
9636         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d / %d", "0x774(Lo pri Rx[31:16]/Tx[15:0])", \
9637                 pHalData->bt_coexist.halCoex8723.lowPriorityRx,
9638                 pHalData->bt_coexist.halCoex8723.lowPriorityTx);
9639         DCMD_Printf(btCoexDbgBuf);
9640
9641         /*  Tx mgnt queue hang or not, 0x41b should = 0xf, ex: 0xd ==>hang */
9642         u1Tmp = rtl8723au_read8(padapter, 0x41b);
9643         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "0x41b (hang chk == 0xf)", \
9644                 u1Tmp);
9645         DCMD_Printf(btCoexDbgBuf);
9646         rsprintf(btCoexDbgBuf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x", "lastHMEBoxNum", \
9647                 pHalData->LastHMEBoxNum);
9648         DCMD_Printf(btCoexDbgBuf);
9649 }
9650
9651 static void
9652 BTDM_8723ASignalCompensation(struct rtw_adapter *padapter,
9653                              u8 *rssi_wifi, u8 *rssi_bt)
9654 {
9655         if (btdm_BtWifiAntNum(padapter) == Ant_x1)
9656                 BTDM_1AntSignalCompensation(padapter, rssi_wifi, rssi_bt);
9657 }
9658
9659 static void BTDM_8723AInit(struct rtw_adapter *padapter)
9660 {
9661         if (btdm_BtWifiAntNum(padapter) == Ant_x2)
9662                 BTDM_2AntParaInit(padapter);
9663         else
9664                 BTDM_1AntParaInit(padapter);
9665 }
9666
9667 static void BTDM_HWCoexAllOff8723A(struct rtw_adapter *padapter)
9668 {
9669         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
9670         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
9671
9672         if (pBtMgnt->ExtConfig.bManualControl)
9673                 return;
9674
9675         if (btdm_BtWifiAntNum(padapter) == Ant_x2)
9676                 BTDM_2AntHwCoexAllOff8723A(padapter);
9677 }
9678
9679 static void BTDM_FWCoexAllOff8723A(struct rtw_adapter *padapter)
9680 {
9681         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
9682         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
9683
9684         if (pBtMgnt->ExtConfig.bManualControl)
9685                 return;
9686
9687         if (btdm_BtWifiAntNum(padapter) == Ant_x2)
9688                 BTDM_2AntFwCoexAllOff8723A(padapter);
9689 }
9690
9691 static void BTDM_SWCoexAllOff8723A(struct rtw_adapter *padapter)
9692 {
9693         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
9694         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
9695
9696         if (pBtMgnt->ExtConfig.bManualControl)
9697                 return;
9698
9699         if (btdm_BtWifiAntNum(padapter) == Ant_x2)
9700                 BTDM_2AntSwCoexAllOff8723A(padapter);
9701 }
9702
9703 static void
9704 BTDM_Set8723ABtCoexCurrAntNum(struct rtw_adapter *padapter, u8 antNum)
9705 {
9706         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
9707         struct bt_coexist_8723a *pBtCoex = &pHalData->bt_coexist.halCoex8723;
9708
9709         if (antNum == 1)
9710                 pBtCoex->TotalAntNum = Ant_x1;
9711         else if (antNum == 2)
9712                 pBtCoex->TotalAntNum = Ant_x2;
9713 }
9714
9715 void rtl8723a_BT_lps_leave(struct rtw_adapter *padapter)
9716 {
9717         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
9718         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
9719
9720         if (pBtMgnt->ExtConfig.bManualControl)
9721                 return;
9722
9723         if (btdm_BtWifiAntNum(padapter) == Ant_x1)
9724                 BTDM_1AntLpsLeave(padapter);
9725 }
9726
9727 static void BTDM_ForHalt8723A(struct rtw_adapter *padapter)
9728 {
9729         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
9730         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
9731
9732         if (pBtMgnt->ExtConfig.bManualControl)
9733                 return;
9734
9735         if (btdm_BtWifiAntNum(padapter) == Ant_x1)
9736                 BTDM_1AntForHalt(padapter);
9737 }
9738
9739 static void BTDM_WifiScanNotify8723A(struct rtw_adapter *padapter, u8 scanType)
9740 {
9741         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
9742         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
9743
9744         if (pBtMgnt->ExtConfig.bManualControl)
9745                 return;
9746
9747         if (btdm_BtWifiAntNum(padapter) == Ant_x1)
9748                 BTDM_1AntWifiScanNotify(padapter, scanType);
9749 }
9750
9751 static void
9752 BTDM_WifiAssociateNotify8723A(struct rtw_adapter *padapter, u8 action)
9753 {
9754         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
9755         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
9756
9757         if (pBtMgnt->ExtConfig.bManualControl)
9758                 return;
9759
9760         if (btdm_BtWifiAntNum(padapter) == Ant_x1)
9761                 BTDM_1AntWifiAssociateNotify(padapter, action);
9762 }
9763
9764 static void
9765 BTDM_MediaStatusNotify8723A(struct rtw_adapter *padapter,
9766                             enum rt_media_status mstatus)
9767 {
9768         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
9769         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
9770
9771         RTPRINT(FBT, BT_TRACE, ("[BTCoex], MediaStatusNotify, %s\n",
9772                 mstatus?"connect":"disconnect"));
9773
9774         BTDM_SetFwChnlInfo(padapter, mstatus);
9775
9776         if (pBtMgnt->ExtConfig.bManualControl)
9777                 return;
9778
9779         if (btdm_BtWifiAntNum(padapter) == Ant_x1)
9780                 BTDM_1AntMediaStatusNotify(padapter, mstatus);
9781 }
9782
9783 static void BTDM_ForDhcp8723A(struct rtw_adapter *padapter)
9784 {
9785         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
9786         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
9787
9788         if (pBtMgnt->ExtConfig.bManualControl)
9789                 return;
9790
9791         if (btdm_BtWifiAntNum(padapter) == Ant_x1)
9792                 BTDM_1AntForDhcp(padapter);
9793 }
9794
9795 bool rtl8723a_BT_using_antenna_1(struct rtw_adapter *padapter)
9796 {
9797         if (btdm_BtWifiAntNum(padapter) == Ant_x1)
9798                 return true;
9799         else
9800                 return false;
9801 }
9802
9803 static void BTDM_BTCoexist8723A(struct rtw_adapter *padapter)
9804 {
9805         struct hal_data_8723a *pHalData;
9806         struct bt_30info *pBTInfo;
9807         struct bt_mgnt *pBtMgnt;
9808         struct bt_coexist_8723a *pBtCoex;
9809
9810         pHalData = GET_HAL_DATA(padapter);
9811         pBTInfo = GET_BT_INFO(padapter);
9812         pBtMgnt = &pBTInfo->BtMgnt;
9813         pBtCoex = &pHalData->bt_coexist.halCoex8723;
9814
9815         RTPRINT(FBT, BT_TRACE, ("[BTCoex], beacon RSSI = 0x%x(%d)\n",
9816                 pHalData->dmpriv.EntryMinUndecoratedSmoothedPWDB,
9817                 pHalData->dmpriv.EntryMinUndecoratedSmoothedPWDB));
9818
9819         btdm_BtHwCountersMonitor(padapter);
9820         btdm_BtEnableDisableCheck8723A(padapter);
9821
9822         if (pBtMgnt->ExtConfig.bManualControl) {
9823                 RTPRINT(FBT, BT_TRACE, ("%s: Action Manual control!!\n", __func__));
9824                 return;
9825         }
9826
9827         if (pBtCoex->bC2hBtInfoReqSent) {
9828                 if (!rtl8723a_BT_enabled(padapter)) {
9829                         pBtCoex->c2hBtInfo = BT_INFO_STATE_DISABLED;
9830                 } else {
9831                         if (pBtCoex->c2hBtInfo == BT_INFO_STATE_DISABLED)
9832                                 pBtCoex->c2hBtInfo = BT_INFO_STATE_NO_CONNECTION;
9833                 }
9834
9835                 btdm_BTCoexist8723AHandler(padapter);
9836         } else if (!rtl8723a_BT_enabled(padapter)) {
9837                 pBtCoex->c2hBtInfo = BT_INFO_STATE_DISABLED;
9838                 btdm_BTCoexist8723AHandler(padapter);
9839         }
9840
9841         BTDM_QueryBtInformation(padapter);
9842 }
9843
9844 /*  ===== End of sync from SD7 driver HAL/BTCoexist/HalBtc8723.c ===== */
9845
9846 /*  ===== Below this line is sync from SD7 driver HAL/BTCoexist/HalBtcCsr1Ant.c ===== */
9847
9848 /*  local function start with btdm_ */
9849 /*  extern function start with BTDM_ */
9850
9851 static void BTDM_SetAntenna(struct rtw_adapter *padapter, u8 who)
9852 {
9853 }
9854
9855 void
9856 BTDM_SingleAnt(
9857         struct rtw_adapter *padapter,
9858         u8 bSingleAntOn,
9859         u8 bInterruptOn,
9860         u8 bMultiNAVOn
9861         )
9862 {
9863         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
9864         u8 H2C_Parameter[3] = {0};
9865
9866         if (pHalData->bt_coexist.BT_Ant_Num != Ant_x1)
9867                 return;
9868
9869         H2C_Parameter[2] = 0;
9870         H2C_Parameter[1] = 0;
9871         H2C_Parameter[0] = 0;
9872
9873         if (bInterruptOn) {
9874                 H2C_Parameter[2] |= 0x02;       /* BIT1 */
9875                 pHalData->bt_coexist.bFWCoexistAllOff = false;
9876         }
9877         pHalData->bt_coexist.bInterruptOn = bInterruptOn;
9878
9879         if (bSingleAntOn) {
9880                 H2C_Parameter[2] |= 0x10;       /* BIT4 */
9881                 pHalData->bt_coexist.bFWCoexistAllOff = false;
9882         }
9883         pHalData->bt_coexist.bSingleAntOn = bSingleAntOn;
9884
9885         if (bMultiNAVOn) {
9886                 H2C_Parameter[2] |= 0x20;       /* BIT5 */
9887                 pHalData->bt_coexist.bFWCoexistAllOff = false;
9888         }
9889         pHalData->bt_coexist.bMultiNAVOn = bMultiNAVOn;
9890
9891         RTPRINT(FBT, BT_TRACE, ("[DM][BT], SingleAntenna =[%s:%s:%s], write 0xe = 0x%x\n",
9892                 bSingleAntOn?"ON":"OFF", bInterruptOn?"ON":"OFF", bMultiNAVOn?"ON":"OFF",
9893                 H2C_Parameter[0]<<16|H2C_Parameter[1]<<8|H2C_Parameter[2]));
9894 }
9895
9896 void BTDM_CheckBTIdleChange1Ant(struct rtw_adapter *padapter)
9897 {
9898         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
9899         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
9900         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
9901 /*PMGNT_INFO            pMgntInfo = &padapter->MgntInfo; */
9902         u8      stateChange = false;
9903         u32                     BT_Polling, Ratio_Act, Ratio_STA;
9904         u32                             BT_Active, BT_State;
9905         u32                             regBTActive = 0, regBTState = 0, regBTPolling = 0;
9906
9907         if (!rtl8723a_BT_coexist(padapter))
9908                 return;
9909         if (pBtMgnt->ExtConfig.bManualControl)
9910                 return;
9911         if (pHalData->bt_coexist.BT_CoexistType != BT_CSR_BC8)
9912                 return;
9913         if (pHalData->bt_coexist.BT_Ant_Num != Ant_x1)
9914                 return;
9915
9916         /*  The following we only consider CSR BC8 and fw version should be >= 62 */
9917         RTPRINT(FBT, BT_TRACE, ("[DM][BT], FirmwareVersion = 0x%x(%d)\n",
9918         pHalData->FirmwareVersion, pHalData->FirmwareVersion));
9919         regBTActive = REG_BT_ACTIVE;
9920         regBTState = REG_BT_STATE;
9921         if (pHalData->FirmwareVersion >= FW_VER_BT_REG1)
9922                 regBTPolling = REG_BT_POLLING1;
9923         else
9924                 regBTPolling = REG_BT_POLLING;
9925
9926         BT_Active = rtl8723au_read32(padapter, regBTActive);
9927         RTPRINT(FBT, BT_TRACE, ("[DM][BT], BT_Active(0x%x) =%x\n", regBTActive, BT_Active));
9928         BT_Active = BT_Active & 0x00ffffff;
9929
9930         BT_State = rtl8723au_read32(padapter, regBTState);
9931         RTPRINT(FBT, BT_TRACE, ("[DM][BT], BT_State(0x%x) =%x\n", regBTState, BT_State));
9932         BT_State = BT_State & 0x00ffffff;
9933
9934         BT_Polling = rtl8723au_read32(padapter, regBTPolling);
9935         RTPRINT(FBT, BT_TRACE, ("[DM][BT], BT_Polling(0x%x) =%x\n", regBTPolling, BT_Polling));
9936
9937         if (BT_Active == 0xffffffff && BT_State == 0xffffffff && BT_Polling == 0xffffffff)
9938                 return;
9939         if (BT_Polling == 0)
9940                 return;
9941
9942         Ratio_Act = BT_Active*1000/BT_Polling;
9943         Ratio_STA = BT_State*1000/BT_Polling;
9944
9945         pHalData->bt_coexist.Ratio_Tx = Ratio_Act;
9946         pHalData->bt_coexist.Ratio_PRI = Ratio_STA;
9947
9948         RTPRINT(FBT, BT_TRACE, ("[DM][BT], Ratio_Act =%d\n", Ratio_Act));
9949         RTPRINT(FBT, BT_TRACE, ("[DM][BT], Ratio_STA =%d\n", Ratio_STA));
9950
9951         if (Ratio_STA < 60 && Ratio_Act < 500) {        /*  BT PAN idle */
9952                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_BT_PAN_IDLE;
9953                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_BT_PAN_DOWNLINK;
9954                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_BT_PAN_UPLINK;
9955         } else {
9956                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_BT_PAN_IDLE;
9957
9958                 if (Ratio_STA) {
9959                         /*  Check if BT PAN (under BT 2.1) is uplink or downlink */
9960                         if ((Ratio_Act/Ratio_STA) < 2) {
9961                                 /*  BT PAN Uplink */
9962                                 pHalData->bt_coexist.BT21TrafficStatistics.bTxBusyTraffic = true;
9963                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_BT_PAN_UPLINK;
9964                                 pHalData->bt_coexist.BT21TrafficStatistics.bRxBusyTraffic = false;
9965                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_BT_PAN_DOWNLINK;
9966                         } else {
9967                                 /*  BT PAN downlink */
9968                                 pHalData->bt_coexist.BT21TrafficStatistics.bTxBusyTraffic = false;
9969                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_BT_PAN_UPLINK;
9970                                 pHalData->bt_coexist.BT21TrafficStatistics.bRxBusyTraffic = true;
9971                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_BT_PAN_DOWNLINK;
9972                         }
9973                 } else {
9974                         /*  BT PAN downlink */
9975                         pHalData->bt_coexist.BT21TrafficStatistics.bTxBusyTraffic = false;
9976                         pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_BT_PAN_UPLINK;
9977                         pHalData->bt_coexist.BT21TrafficStatistics.bRxBusyTraffic = true;
9978                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_BT_PAN_DOWNLINK;
9979                 }
9980         }
9981
9982         /*  Check BT is idle or not */
9983         if (pBtMgnt->ExtConfig.NumberOfHandle == 0 &&
9984             pBtMgnt->ExtConfig.NumberOfSCO == 0) {
9985                 pBtMgnt->ExtConfig.bBTBusy = false;
9986                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_BT_IDLE;
9987         } else {
9988                 if (Ratio_STA < 60) {
9989                         pBtMgnt->ExtConfig.bBTBusy = false;
9990                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_BT_IDLE;
9991                 } else {
9992                         pBtMgnt->ExtConfig.bBTBusy = true;
9993                         pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_BT_IDLE;
9994                 }
9995         }
9996
9997         if (pBtMgnt->ExtConfig.NumberOfHandle == 0 &&
9998             pBtMgnt->ExtConfig.NumberOfSCO == 0) {
9999                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_BT_RSSI_LOW;
10000                 pBtMgnt->ExtConfig.MIN_BT_RSSI = 0;
10001                 BTDM_SetAntenna(padapter, BTDM_ANT_BT_IDLE);
10002         } else {
10003                 if (pBtMgnt->ExtConfig.MIN_BT_RSSI <= -5) {
10004                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_BT_RSSI_LOW;
10005                         RTPRINT(FBT, BT_TRACE, ("[DM][BT], core stack notify bt rssi Low\n"));
10006                 } else {
10007                         pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_BT_RSSI_LOW;
10008                         RTPRINT(FBT, BT_TRACE, ("[DM][BT], core stack notify bt rssi Normal\n"));
10009                 }
10010         }
10011
10012         if (pHalData->bt_coexist.bBTBusyTraffic != pBtMgnt->ExtConfig.bBTBusy) {
10013                 /*  BT idle or BT non-idle */
10014                 pHalData->bt_coexist.bBTBusyTraffic = pBtMgnt->ExtConfig.bBTBusy;
10015                 stateChange = true;
10016         }
10017
10018         if (stateChange) {
10019                 if (!pBtMgnt->ExtConfig.bBTBusy)
10020                         RTPRINT(FBT, BT_TRACE, ("[DM][BT], BT is idle or disable\n"));
10021                 else
10022                         RTPRINT(FBT, BT_TRACE, ("[DM][BT], BT is non-idle\n"));
10023         }
10024         if (!pBtMgnt->ExtConfig.bBTBusy) {
10025                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], BT is idle or disable\n"));
10026                 if (check_fwstate(&padapter->mlmepriv, WIFI_UNDER_LINKING|WIFI_SITE_MONITOR) == true)
10027                         BTDM_SetAntenna(padapter, BTDM_ANT_WIFI);
10028         }
10029 }
10030
10031 /*  ===== End of sync from SD7 driver HAL/BTCoexist/HalBtcCsr1Ant.c ===== */
10032
10033 /*  ===== Below this line is sync from SD7 driver HAL/BTCoexist/HalBtcCsr2Ant.c ===== */
10034
10035 /*  local function start with btdm_ */
10036
10037 /*  Note: */
10038 /*  In the following, FW should be done before SW mechanism. */
10039 /*  BTDM_Balance(), BTDM_DiminishWiFi(), BT_NAV() should be done */
10040 /*  before BTDM_AGCTable(), BTDM_BBBackOffLevel(), btdm_DacSwing(). */
10041
10042 /*  extern function start with BTDM_ */
10043
10044 void
10045 BTDM_DiminishWiFi(
10046         struct rtw_adapter *padapter,
10047         u8 bDACOn,
10048         u8 bInterruptOn,
10049         u8 DACSwingLevel,
10050         u8 bNAVOn
10051         )
10052 {
10053         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10054         u8 H2C_Parameter[3] = {0};
10055
10056         if (pHalData->bt_coexist.BT_Ant_Num != Ant_x2)
10057                 return;
10058
10059         if ((pHalData->bt_coexist.CurrentState & BT_COEX_STATE_BT_RSSI_LOW) &&
10060             (DACSwingLevel == 0x20)) {
10061                 RTPRINT(FBT, BT_TRACE, ("[BT]DiminishWiFi 0x20 original, but set 0x18 for Low RSSI!\n"));
10062                 DACSwingLevel = 0x18;
10063         }
10064
10065         H2C_Parameter[2] = 0;
10066         H2C_Parameter[1] = DACSwingLevel;
10067         H2C_Parameter[0] = 0;
10068         if (bDACOn) {
10069                 H2C_Parameter[2] |= 0x01;       /* BIT0 */
10070                 if (bInterruptOn)
10071                         H2C_Parameter[2] |= 0x02;       /* BIT1 */
10072                 pHalData->bt_coexist.bFWCoexistAllOff = false;
10073         }
10074         if (bNAVOn) {
10075                 H2C_Parameter[2] |= 0x08;       /* BIT3 */
10076                 pHalData->bt_coexist.bFWCoexistAllOff = false;
10077         }
10078
10079         RTPRINT(FBT, BT_TRACE, ("[DM][BT], bDACOn = %s, bInterruptOn = %s, write 0xe = 0x%x\n",
10080                 bDACOn?"ON":"OFF", bInterruptOn?"ON":"OFF",
10081                 H2C_Parameter[0]<<16|H2C_Parameter[1]<<8|H2C_Parameter[2]));
10082         RTPRINT(FBT, BT_TRACE, ("[DM][BT], bNAVOn = %s\n",
10083                 bNAVOn?"ON":"OFF"));
10084 }
10085
10086 /*  ===== End of sync from SD7 driver HAL/BTCoexist/HalBtcCsr2Ant.c ===== */
10087
10088 /*  ===== Below this line is sync from SD7 driver HAL/BTCoexist/HalBtCoexist.c ===== */
10089
10090 /*  local function */
10091 static void btdm_ResetFWCoexState(struct rtw_adapter *padapter)
10092 {
10093         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10094
10095         pHalData->bt_coexist.CurrentState = 0;
10096         pHalData->bt_coexist.PreviousState = 0;
10097 }
10098
10099 static void btdm_InitBtCoexistDM(struct rtw_adapter *padapter)
10100 {
10101         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10102
10103         /*  20100415 Joseph: Restore RF register 0x1E and 0x1F value for further usage. */
10104         pHalData->bt_coexist.BtRfRegOrigin1E = PHY_QueryRFReg(padapter, PathA, RF_RCK1, bRFRegOffsetMask);
10105         pHalData->bt_coexist.BtRfRegOrigin1F = PHY_QueryRFReg(padapter, PathA, RF_RCK2, 0xf0);
10106
10107         pHalData->bt_coexist.CurrentState = 0;
10108         pHalData->bt_coexist.PreviousState = 0;
10109
10110         BTDM_8723AInit(padapter);
10111         pHalData->bt_coexist.bInitlized = true;
10112 }
10113
10114 /*  */
10115 /*  extern function */
10116 /*  */
10117 void BTDM_CheckAntSelMode(struct rtw_adapter *padapter)
10118 {
10119 }
10120
10121 void BTDM_FwC2hBtRssi(struct rtw_adapter *padapter, u8 *tmpBuf)
10122 {
10123         BTDM_FwC2hBtRssi8723A(padapter, tmpBuf);
10124 }
10125
10126 void BTDM_DisplayBtCoexInfo(struct rtw_adapter *padapter)
10127 {
10128         BTDM_Display8723ABtCoexInfo(padapter);
10129 }
10130
10131 void BTDM_RejectAPAggregatedPacket(struct rtw_adapter *padapter, u8 bReject)
10132 {
10133 }
10134
10135 u8 BTDM_IsHT40(struct rtw_adapter *padapter)
10136 {
10137         u8 isht40 = true;
10138         enum ht_channel_width bw;
10139
10140         bw = padapter->mlmeextpriv.cur_bwmode;
10141
10142         if (bw == HT_CHANNEL_WIDTH_20)
10143                 isht40 = false;
10144         else if (bw == HT_CHANNEL_WIDTH_40)
10145                 isht40 = true;
10146
10147         return isht40;
10148 }
10149
10150 u8 BTDM_Legacy(struct rtw_adapter *padapter)
10151 {
10152         struct mlme_ext_priv *pmlmeext;
10153         u8 isLegacy = false;
10154
10155         pmlmeext = &padapter->mlmeextpriv;
10156         if ((pmlmeext->cur_wireless_mode == WIRELESS_11B) ||
10157                 (pmlmeext->cur_wireless_mode == WIRELESS_11G) ||
10158                 (pmlmeext->cur_wireless_mode == WIRELESS_11BG))
10159                 isLegacy = true;
10160
10161         return isLegacy;
10162 }
10163
10164 void BTDM_CheckWiFiState(struct rtw_adapter *padapter)
10165 {
10166         struct hal_data_8723a *pHalData;
10167         struct mlme_priv *pmlmepriv;
10168         struct bt_30info *pBTInfo;
10169         struct bt_mgnt *pBtMgnt;
10170
10171         pHalData = GET_HAL_DATA(padapter);
10172         pmlmepriv = &padapter->mlmepriv;
10173         pBTInfo = GET_BT_INFO(padapter);
10174         pBtMgnt = &pBTInfo->BtMgnt;
10175
10176         if (pmlmepriv->LinkDetectInfo.bBusyTraffic) {
10177                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_IDLE;
10178
10179                 if (pmlmepriv->LinkDetectInfo.bTxBusyTraffic)
10180                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_UPLINK;
10181                 else
10182                         pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_UPLINK;
10183
10184                 if (pmlmepriv->LinkDetectInfo.bRxBusyTraffic)
10185                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_DOWNLINK;
10186                 else
10187                         pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_DOWNLINK;
10188         } else {
10189                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_IDLE;
10190                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_UPLINK;
10191                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_DOWNLINK;
10192         }
10193
10194         if (BTDM_Legacy(padapter)) {
10195                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_LEGACY;
10196                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_HT20;
10197                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_HT40;
10198         } else {
10199                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_LEGACY;
10200                 if (BTDM_IsHT40(padapter)) {
10201                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_HT40;
10202                         pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_HT20;
10203                 } else {
10204                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_HT20;
10205                         pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_HT40;
10206                 }
10207         }
10208
10209         if (pBtMgnt->BtOperationOn)
10210                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_BT30;
10211         else
10212                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_BT30;
10213 }
10214
10215 s32 BTDM_GetRxSS(struct rtw_adapter *padapter)
10216 {
10217 /*PMGNT_INFO            pMgntInfo = &padapter->MgntInfo; */
10218         struct mlme_priv *pmlmepriv;
10219         struct hal_data_8723a *pHalData;
10220         s32                     UndecoratedSmoothedPWDB = 0;
10221
10222         pmlmepriv = &padapter->mlmepriv;
10223         pHalData = GET_HAL_DATA(padapter);
10224
10225         if (check_fwstate(pmlmepriv, _FW_LINKED)) {
10226                 UndecoratedSmoothedPWDB = GET_UNDECORATED_AVERAGE_RSSI(padapter);
10227         } else { /*  associated entry pwdb */
10228                 UndecoratedSmoothedPWDB = pHalData->dmpriv.EntryMinUndecoratedSmoothedPWDB;
10229                 /* pHalData->BT_EntryMinUndecoratedSmoothedPWDB */
10230         }
10231         RTPRINT(FBT, BT_TRACE, ("BTDM_GetRxSS() = %d\n", UndecoratedSmoothedPWDB));
10232         return UndecoratedSmoothedPWDB;
10233 }
10234
10235 static s32 BTDM_GetRxBeaconSS(struct rtw_adapter *padapter)
10236 {
10237 /*PMGNT_INFO            pMgntInfo = &padapter->MgntInfo; */
10238         struct mlme_priv *pmlmepriv;
10239         struct hal_data_8723a *pHalData;
10240         s32                     pwdbBeacon = 0;
10241
10242         pmlmepriv = &padapter->mlmepriv;
10243         pHalData = GET_HAL_DATA(padapter);
10244
10245         if (check_fwstate(pmlmepriv, _FW_LINKED)) {
10246                 /* pwdbBeacon = pHalData->dmpriv.UndecoratedSmoothedBeacon; */
10247                 pwdbBeacon = pHalData->dmpriv.EntryMinUndecoratedSmoothedPWDB;
10248         }
10249         RTPRINT(FBT, BT_TRACE, ("BTDM_GetRxBeaconSS() = %d\n", pwdbBeacon));
10250         return pwdbBeacon;
10251 }
10252
10253 /*  Get beacon rssi state */
10254 u8 BTDM_CheckCoexBcnRssiState(struct rtw_adapter *padapter, u8 levelNum,
10255                               u8 RssiThresh, u8 RssiThresh1)
10256 {
10257         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10258         s32 pwdbBeacon = 0;
10259         u8 bcnRssiState = 0;
10260
10261         pwdbBeacon = BTDM_GetRxBeaconSS(padapter);
10262
10263         if (levelNum == 2) {
10264                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_BEACON_MEDIUM;
10265
10266                 if ((pHalData->bt_coexist.preRssiStateBeacon == BT_RSSI_STATE_LOW) ||
10267                     (pHalData->bt_coexist.preRssiStateBeacon == BT_RSSI_STATE_STAY_LOW)) {
10268                         if (pwdbBeacon >= (RssiThresh+BT_FW_COEX_THRESH_TOL)) {
10269                                 bcnRssiState = BT_RSSI_STATE_HIGH;
10270                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_BEACON_HIGH;
10271                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_BEACON_LOW;
10272                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_BEACON state switch to High\n"));
10273                         } else {
10274                                 bcnRssiState = BT_RSSI_STATE_STAY_LOW;
10275                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_BEACON state stay at Low\n"));
10276                         }
10277                 } else {
10278                         if (pwdbBeacon < RssiThresh) {
10279                                 bcnRssiState = BT_RSSI_STATE_LOW;
10280                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_BEACON_LOW;
10281                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_BEACON_HIGH;
10282                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_BEACON state switch to Low\n"));
10283                         } else {
10284                                 bcnRssiState = BT_RSSI_STATE_STAY_HIGH;
10285                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_BEACON state stay at High\n"));
10286                         }
10287                 }
10288         } else if (levelNum == 3) {
10289                 if (RssiThresh > RssiThresh1) {
10290                         RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_BEACON thresh error!!\n"));
10291                         return pHalData->bt_coexist.preRssiStateBeacon;
10292                 }
10293
10294                 if ((pHalData->bt_coexist.preRssiStateBeacon == BT_RSSI_STATE_LOW) ||
10295                     (pHalData->bt_coexist.preRssiStateBeacon == BT_RSSI_STATE_STAY_LOW)) {
10296                         if (pwdbBeacon >= (RssiThresh+BT_FW_COEX_THRESH_TOL)) {
10297                                 bcnRssiState = BT_RSSI_STATE_MEDIUM;
10298                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_BEACON_MEDIUM;
10299                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_BEACON_LOW;
10300                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_BEACON_HIGH;
10301                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_BEACON state switch to Medium\n"));
10302                         } else {
10303                                 bcnRssiState = BT_RSSI_STATE_STAY_LOW;
10304                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_BEACON state stay at Low\n"));
10305                         }
10306                 } else if ((pHalData->bt_coexist.preRssiStateBeacon == BT_RSSI_STATE_MEDIUM) ||
10307                            (pHalData->bt_coexist.preRssiStateBeacon == BT_RSSI_STATE_STAY_MEDIUM)) {
10308                         if (pwdbBeacon >= (RssiThresh1+BT_FW_COEX_THRESH_TOL)) {
10309                                 bcnRssiState = BT_RSSI_STATE_HIGH;
10310                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_BEACON_HIGH;
10311                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_BEACON_LOW;
10312                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_BEACON_MEDIUM;
10313                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_BEACON state switch to High\n"));
10314                         } else if (pwdbBeacon < RssiThresh) {
10315                                 bcnRssiState = BT_RSSI_STATE_LOW;
10316                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_BEACON_LOW;
10317                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_BEACON_HIGH;
10318                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_BEACON_MEDIUM;
10319                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_BEACON state switch to Low\n"));
10320                         } else {
10321                                 bcnRssiState = BT_RSSI_STATE_STAY_MEDIUM;
10322                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_BEACON state stay at Medium\n"));
10323                         }
10324                 } else {
10325                         if (pwdbBeacon < RssiThresh1) {
10326                                 bcnRssiState = BT_RSSI_STATE_MEDIUM;
10327                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_BEACON_MEDIUM;
10328                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_BEACON_HIGH;
10329                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_BEACON_LOW;
10330                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_BEACON state switch to Medium\n"));
10331                         } else {
10332                                 bcnRssiState = BT_RSSI_STATE_STAY_HIGH;
10333                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_BEACON state stay at High\n"));
10334                         }
10335                 }
10336         }
10337
10338         pHalData->bt_coexist.preRssiStateBeacon = bcnRssiState;
10339
10340         return bcnRssiState;
10341 }
10342
10343 u8 BTDM_CheckCoexRSSIState1(struct rtw_adapter *padapter, u8 levelNum,
10344                             u8 RssiThresh, u8 RssiThresh1)
10345 {
10346         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10347         s32 UndecoratedSmoothedPWDB = 0;
10348         u8 btRssiState = 0;
10349
10350         UndecoratedSmoothedPWDB = BTDM_GetRxSS(padapter);
10351
10352         if (levelNum == 2) {
10353                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_1_MEDIUM;
10354
10355                 if ((pHalData->bt_coexist.preRssiState1 == BT_RSSI_STATE_LOW) ||
10356                     (pHalData->bt_coexist.preRssiState1 == BT_RSSI_STATE_STAY_LOW)) {
10357                         if (UndecoratedSmoothedPWDB >= (RssiThresh+BT_FW_COEX_THRESH_TOL)) {
10358                                 btRssiState = BT_RSSI_STATE_HIGH;
10359                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_1_HIGH;
10360                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_1_LOW;
10361                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_1 state switch to High\n"));
10362                         } else {
10363                                 btRssiState = BT_RSSI_STATE_STAY_LOW;
10364                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_1 state stay at Low\n"));
10365                         }
10366                 } else {
10367                         if (UndecoratedSmoothedPWDB < RssiThresh) {
10368                                 btRssiState = BT_RSSI_STATE_LOW;
10369                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_1_LOW;
10370                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_1_HIGH;
10371                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_1 state switch to Low\n"));
10372                         } else {
10373                                 btRssiState = BT_RSSI_STATE_STAY_HIGH;
10374                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_1 state stay at High\n"));
10375                         }
10376                 }
10377         } else if (levelNum == 3) {
10378                 if (RssiThresh > RssiThresh1) {
10379                         RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_1 thresh error!!\n"));
10380                         return pHalData->bt_coexist.preRssiState1;
10381                 }
10382
10383                 if ((pHalData->bt_coexist.preRssiState1 == BT_RSSI_STATE_LOW) ||
10384                     (pHalData->bt_coexist.preRssiState1 == BT_RSSI_STATE_STAY_LOW)) {
10385                         if (UndecoratedSmoothedPWDB >= (RssiThresh+BT_FW_COEX_THRESH_TOL)) {
10386                                 btRssiState = BT_RSSI_STATE_MEDIUM;
10387                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_1_MEDIUM;
10388                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_1_LOW;
10389                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_1_HIGH;
10390                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_1 state switch to Medium\n"));
10391                         } else {
10392                                 btRssiState = BT_RSSI_STATE_STAY_LOW;
10393                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_1 state stay at Low\n"));
10394                         }
10395                 } else if ((pHalData->bt_coexist.preRssiState1 == BT_RSSI_STATE_MEDIUM) ||
10396                            (pHalData->bt_coexist.preRssiState1 == BT_RSSI_STATE_STAY_MEDIUM)) {
10397                         if (UndecoratedSmoothedPWDB >= (RssiThresh1+BT_FW_COEX_THRESH_TOL)) {
10398                                 btRssiState = BT_RSSI_STATE_HIGH;
10399                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_1_HIGH;
10400                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_1_LOW;
10401                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_1_MEDIUM;
10402                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_1 state switch to High\n"));
10403                         } else if (UndecoratedSmoothedPWDB < RssiThresh) {
10404                                 btRssiState = BT_RSSI_STATE_LOW;
10405                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_1_LOW;
10406                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_1_HIGH;
10407                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_1_MEDIUM;
10408                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_1 state switch to Low\n"));
10409                         } else {
10410                                 btRssiState = BT_RSSI_STATE_STAY_MEDIUM;
10411                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_1 state stay at Medium\n"));
10412                         }
10413                 } else {
10414                         if (UndecoratedSmoothedPWDB < RssiThresh1) {
10415                                 btRssiState = BT_RSSI_STATE_MEDIUM;
10416                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_1_MEDIUM;
10417                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_1_HIGH;
10418                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_1_LOW;
10419                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_1 state switch to Medium\n"));
10420                         } else {
10421                                 btRssiState = BT_RSSI_STATE_STAY_HIGH;
10422                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI_1 state stay at High\n"));
10423                         }
10424                 }
10425         }
10426
10427         pHalData->bt_coexist.preRssiState1 = btRssiState;
10428
10429         return btRssiState;
10430 }
10431
10432 u8 BTDM_CheckCoexRSSIState(struct rtw_adapter *padapter, u8 levelNum,
10433                            u8 RssiThresh, u8 RssiThresh1)
10434 {
10435         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10436         s32 UndecoratedSmoothedPWDB = 0;
10437         u8 btRssiState = 0;
10438
10439         UndecoratedSmoothedPWDB = BTDM_GetRxSS(padapter);
10440
10441         if (levelNum == 2) {
10442                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_MEDIUM;
10443
10444                 if ((pHalData->bt_coexist.preRssiState == BT_RSSI_STATE_LOW) ||
10445                     (pHalData->bt_coexist.preRssiState == BT_RSSI_STATE_STAY_LOW)) {
10446                         if (UndecoratedSmoothedPWDB >= (RssiThresh+BT_FW_COEX_THRESH_TOL)) {
10447                                 btRssiState = BT_RSSI_STATE_HIGH;
10448                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_HIGH;
10449                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_LOW;
10450                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI state switch to High\n"));
10451                         } else {
10452                                 btRssiState = BT_RSSI_STATE_STAY_LOW;
10453                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI state stay at Low\n"));
10454                         }
10455                 } else {
10456                         if (UndecoratedSmoothedPWDB < RssiThresh) {
10457                                 btRssiState = BT_RSSI_STATE_LOW;
10458                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_LOW;
10459                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_HIGH;
10460                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI state switch to Low\n"));
10461                         } else {
10462                                 btRssiState = BT_RSSI_STATE_STAY_HIGH;
10463                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI state stay at High\n"));
10464                         }
10465                 }
10466         } else if (levelNum == 3) {
10467                 if (RssiThresh > RssiThresh1) {
10468                         RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI thresh error!!\n"));
10469                         return pHalData->bt_coexist.preRssiState;
10470                 }
10471
10472                 if ((pHalData->bt_coexist.preRssiState == BT_RSSI_STATE_LOW) ||
10473                     (pHalData->bt_coexist.preRssiState == BT_RSSI_STATE_STAY_LOW)) {
10474                         if (UndecoratedSmoothedPWDB >= (RssiThresh+BT_FW_COEX_THRESH_TOL)) {
10475                                 btRssiState = BT_RSSI_STATE_MEDIUM;
10476                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_MEDIUM;
10477                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_LOW;
10478                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_HIGH;
10479                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI state switch to Medium\n"));
10480                         } else {
10481                                 btRssiState = BT_RSSI_STATE_STAY_LOW;
10482                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI state stay at Low\n"));
10483                         }
10484                 } else if ((pHalData->bt_coexist.preRssiState == BT_RSSI_STATE_MEDIUM) ||
10485                            (pHalData->bt_coexist.preRssiState == BT_RSSI_STATE_STAY_MEDIUM)) {
10486                         if (UndecoratedSmoothedPWDB >= (RssiThresh1+BT_FW_COEX_THRESH_TOL)) {
10487                                 btRssiState = BT_RSSI_STATE_HIGH;
10488                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_HIGH;
10489                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_LOW;
10490                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_MEDIUM;
10491                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI state switch to High\n"));
10492                         } else if (UndecoratedSmoothedPWDB < RssiThresh) {
10493                                 btRssiState = BT_RSSI_STATE_LOW;
10494                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_LOW;
10495                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_HIGH;
10496                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_MEDIUM;
10497                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI state switch to Low\n"));
10498                         } else {
10499                                 btRssiState = BT_RSSI_STATE_STAY_MEDIUM;
10500                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI state stay at Medium\n"));
10501                         }
10502                 } else {
10503                         if (UndecoratedSmoothedPWDB < RssiThresh1) {
10504                                 btRssiState = BT_RSSI_STATE_MEDIUM;
10505                                 pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_WIFI_RSSI_MEDIUM;
10506                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_HIGH;
10507                                 pHalData->bt_coexist.CurrentState &= ~BT_COEX_STATE_WIFI_RSSI_LOW;
10508                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI state switch to Medium\n"));
10509                         } else {
10510                                 btRssiState = BT_RSSI_STATE_STAY_HIGH;
10511                                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], RSSI state stay at High\n"));
10512                         }
10513                 }
10514         }
10515
10516         pHalData->bt_coexist.preRssiState = btRssiState;
10517
10518         return btRssiState;
10519 }
10520
10521 bool rtl8723a_BT_disable_EDCA_turbo(struct rtw_adapter *padapter)
10522 {
10523         struct bt_mgnt *pBtMgnt;
10524         struct hal_data_8723a *pHalData;
10525         u8 bBtChangeEDCA = false;
10526         u32 EDCA_BT_BE = 0x5ea42b, cur_EDCA_reg;
10527         bool bRet = false;
10528
10529         pHalData = GET_HAL_DATA(padapter);
10530         pBtMgnt = &pHalData->BtInfo.BtMgnt;
10531
10532         if (!rtl8723a_BT_coexist(padapter)) {
10533                 bRet = false;
10534                 pHalData->bt_coexist.lastBtEdca = 0;
10535                 return bRet;
10536         }
10537         if (!((pBtMgnt->bSupportProfile) ||
10538             (pHalData->bt_coexist.BT_CoexistType == BT_CSR_BC8))) {
10539                 bRet = false;
10540                 pHalData->bt_coexist.lastBtEdca = 0;
10541                 return bRet;
10542         }
10543
10544         if (rtl8723a_BT_using_antenna_1(padapter)) {
10545                 bRet = false;
10546                 pHalData->bt_coexist.lastBtEdca = 0;
10547                 return bRet;
10548         }
10549
10550         if (pHalData->bt_coexist.exec_cnt < 3)
10551                 pHalData->bt_coexist.exec_cnt++;
10552         else
10553                 pHalData->bt_coexist.bEDCAInitialized = true;
10554
10555         /*  When BT is non idle */
10556         if (!(pHalData->bt_coexist.CurrentState & BT_COEX_STATE_BT_IDLE)) {
10557                 RTPRINT(FBT, BT_TRACE, ("BT state non idle, set bt EDCA\n"));
10558
10559                 /* aggr_num = 0x0909; */
10560                 if (pHalData->odmpriv.DM_EDCA_Table.bCurrentTurboEDCA) {
10561                         bBtChangeEDCA = true;
10562                         pHalData->odmpriv.DM_EDCA_Table.bCurrentTurboEDCA = false;
10563                         pHalData->dmpriv.prv_traffic_idx = 3;
10564                 }
10565                 cur_EDCA_reg = rtl8723au_read32(padapter, REG_EDCA_BE_PARAM);
10566
10567                 if (cur_EDCA_reg != EDCA_BT_BE)
10568                         bBtChangeEDCA = true;
10569                 if (bBtChangeEDCA || !pHalData->bt_coexist.bEDCAInitialized) {
10570                         rtl8723au_write32(padapter, REG_EDCA_BE_PARAM,
10571                                           EDCA_BT_BE);
10572                         pHalData->bt_coexist.lastBtEdca = EDCA_BT_BE;
10573                 }
10574                 bRet = true;
10575         } else {
10576                 RTPRINT(FBT, BT_TRACE, ("BT state idle, set original EDCA\n"));
10577                 pHalData->bt_coexist.lastBtEdca = 0;
10578                 bRet = false;
10579         }
10580         return bRet;
10581 }
10582
10583 void
10584 BTDM_Balance(
10585         struct rtw_adapter *padapter,
10586         u8 bBalanceOn,
10587         u8 ms0,
10588         u8 ms1
10589         )
10590 {
10591         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10592         u8 H2C_Parameter[3] = {0};
10593
10594         if (bBalanceOn) {
10595                 H2C_Parameter[2] = 1;
10596                 H2C_Parameter[1] = ms1;
10597                 H2C_Parameter[0] = ms0;
10598                 pHalData->bt_coexist.bFWCoexistAllOff = false;
10599         } else {
10600                 H2C_Parameter[2] = 0;
10601                 H2C_Parameter[1] = 0;
10602                 H2C_Parameter[0] = 0;
10603         }
10604         pHalData->bt_coexist.bBalanceOn = bBalanceOn;
10605
10606         RTPRINT(FBT, BT_TRACE, ("[DM][BT], Balance =[%s:%dms:%dms], write 0xc = 0x%x\n",
10607                 bBalanceOn?"ON":"OFF", ms0, ms1,
10608                 H2C_Parameter[0]<<16|H2C_Parameter[1]<<8|H2C_Parameter[2]));
10609
10610         FillH2CCmd(padapter, 0xc, 3, H2C_Parameter);
10611 }
10612
10613 void BTDM_AGCTable(struct rtw_adapter *padapter, u8 type)
10614 {
10615         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10616         if (type == BT_AGCTABLE_OFF) {
10617                 RTPRINT(FBT, BT_TRACE, ("[BT]AGCTable Off!\n"));
10618                 rtl8723au_write32(padapter, 0xc78, 0x641c0001);
10619                 rtl8723au_write32(padapter, 0xc78, 0x631d0001);
10620                 rtl8723au_write32(padapter, 0xc78, 0x621e0001);
10621                 rtl8723au_write32(padapter, 0xc78, 0x611f0001);
10622                 rtl8723au_write32(padapter, 0xc78, 0x60200001);
10623
10624                 PHY_SetRFReg(padapter, PathA, RF_RX_AGC_HP, bRFRegOffsetMask, 0x32000);
10625                 PHY_SetRFReg(padapter, PathA, RF_RX_AGC_HP, bRFRegOffsetMask, 0x71000);
10626                 PHY_SetRFReg(padapter, PathA, RF_RX_AGC_HP, bRFRegOffsetMask, 0xb0000);
10627                 PHY_SetRFReg(padapter, PathA, RF_RX_AGC_HP, bRFRegOffsetMask, 0xfc000);
10628                 PHY_SetRFReg(padapter, PathA, RF_RX_G1, bRFRegOffsetMask, 0x30355);
10629
10630                 pHalData->bt_coexist.b8723aAgcTableOn = false;
10631         } else if (type == BT_AGCTABLE_ON) {
10632                 RTPRINT(FBT, BT_TRACE, ("[BT]AGCTable On!\n"));
10633                 rtl8723au_write32(padapter, 0xc78, 0x4e1c0001);
10634                 rtl8723au_write32(padapter, 0xc78, 0x4d1d0001);
10635                 rtl8723au_write32(padapter, 0xc78, 0x4c1e0001);
10636                 rtl8723au_write32(padapter, 0xc78, 0x4b1f0001);
10637                 rtl8723au_write32(padapter, 0xc78, 0x4a200001);
10638
10639                 PHY_SetRFReg(padapter, PathA, RF_RX_AGC_HP, bRFRegOffsetMask, 0xdc000);
10640                 PHY_SetRFReg(padapter, PathA, RF_RX_AGC_HP, bRFRegOffsetMask, 0x90000);
10641                 PHY_SetRFReg(padapter, PathA, RF_RX_AGC_HP, bRFRegOffsetMask, 0x51000);
10642                 PHY_SetRFReg(padapter, PathA, RF_RX_AGC_HP, bRFRegOffsetMask, 0x12000);
10643                 PHY_SetRFReg(padapter, PathA, RF_RX_G1, bRFRegOffsetMask, 0x00355);
10644
10645                 pHalData->bt_coexist.b8723aAgcTableOn = true;
10646
10647                 pHalData->bt_coexist.bSWCoexistAllOff = false;
10648         }
10649 }
10650
10651 void BTDM_BBBackOffLevel(struct rtw_adapter *padapter, u8 type)
10652 {
10653         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10654
10655         if (type == BT_BB_BACKOFF_OFF) {
10656                 RTPRINT(FBT, BT_TRACE, ("[BT]BBBackOffLevel Off!\n"));
10657                 rtl8723au_write32(padapter, 0xc04, 0x3a05611);
10658         } else if (type == BT_BB_BACKOFF_ON) {
10659                 RTPRINT(FBT, BT_TRACE, ("[BT]BBBackOffLevel On!\n"));
10660                 rtl8723au_write32(padapter, 0xc04, 0x3a07611);
10661                 pHalData->bt_coexist.bSWCoexistAllOff = false;
10662         }
10663 }
10664
10665 void BTDM_FWCoexAllOff(struct rtw_adapter *padapter)
10666 {
10667         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);;
10668
10669         RTPRINT(FBT, BT_TRACE, ("BTDM_FWCoexAllOff()\n"));
10670         if (pHalData->bt_coexist.bFWCoexistAllOff)
10671                 return;
10672         RTPRINT(FBT, BT_TRACE, ("BTDM_FWCoexAllOff(), real Do\n"));
10673
10674         BTDM_FWCoexAllOff8723A(padapter);
10675
10676         pHalData->bt_coexist.bFWCoexistAllOff = true;
10677 }
10678
10679 void BTDM_SWCoexAllOff(struct rtw_adapter *padapter)
10680 {
10681         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);;
10682
10683         RTPRINT(FBT, BT_TRACE, ("BTDM_SWCoexAllOff()\n"));
10684         if (pHalData->bt_coexist.bSWCoexistAllOff)
10685                 return;
10686         RTPRINT(FBT, BT_TRACE, ("BTDM_SWCoexAllOff(), real Do\n"));
10687         BTDM_SWCoexAllOff8723A(padapter);
10688
10689         pHalData->bt_coexist.bSWCoexistAllOff = true;
10690 }
10691
10692 void BTDM_HWCoexAllOff(struct rtw_adapter *padapter)
10693 {
10694         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);;
10695
10696         RTPRINT(FBT, BT_TRACE, ("BTDM_HWCoexAllOff()\n"));
10697         if (pHalData->bt_coexist.bHWCoexistAllOff)
10698                 return;
10699         RTPRINT(FBT, BT_TRACE, ("BTDM_HWCoexAllOff(), real Do\n"));
10700
10701         BTDM_HWCoexAllOff8723A(padapter);
10702
10703         pHalData->bt_coexist.bHWCoexistAllOff = true;
10704 }
10705
10706 void BTDM_CoexAllOff(struct rtw_adapter *padapter)
10707 {
10708         BTDM_FWCoexAllOff(padapter);
10709         BTDM_SWCoexAllOff(padapter);
10710         BTDM_HWCoexAllOff(padapter);
10711 }
10712
10713 void rtl8723a_BT_disable_coexist(struct rtw_adapter *padapter)
10714 {
10715         struct pwrctrl_priv *ppwrctrl = &padapter->pwrctrlpriv;
10716
10717         if (!rtl8723a_BT_coexist(padapter))
10718                 return;
10719
10720         /*  8723 1Ant doesn't need to turn off bt coexist mechanism. */
10721         if (rtl8723a_BT_using_antenna_1(padapter))
10722                 return;
10723
10724         /*  Before enter IPS, turn off FW BT Co-exist mechanism */
10725         if (ppwrctrl->reg_rfoff == rf_on) {
10726                 RTPRINT(FBT, BT_TRACE, ("[BT][DM], Before enter IPS, turn off all Coexist DM\n"));
10727                 btdm_ResetFWCoexState(padapter);
10728                 BTDM_CoexAllOff(padapter);
10729                 BTDM_SetAntenna(padapter, BTDM_ANT_BT);
10730         }
10731 }
10732
10733 void BTDM_SignalCompensation(struct rtw_adapter *padapter, u8 *rssi_wifi, u8 *rssi_bt)
10734 {
10735         BTDM_8723ASignalCompensation(padapter, rssi_wifi, rssi_bt);
10736 }
10737
10738 void rtl8723a_BT_do_coexist(struct rtw_adapter *padapter)
10739 {
10740         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10741
10742         if (!rtl8723a_BT_coexist(padapter)) {
10743                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], BT not exists!!\n"));
10744                 return;
10745         }
10746
10747         if (!pHalData->bt_coexist.bInitlized) {
10748                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], btdm_InitBtCoexistDM()\n"));
10749                 btdm_InitBtCoexistDM(padapter);
10750         }
10751
10752         RTPRINT(FBT, BT_TRACE, ("\n\n[DM][BT], BTDM start!!\n"));
10753
10754         BTDM_PWDBMonitor(padapter);
10755
10756         RTPRINT(FBT, BT_TRACE, ("[DM][BT], HW type is 8723\n"));
10757         BTDM_BTCoexist8723A(padapter);
10758         RTPRINT(FBT, BT_TRACE, ("[DM][BT], BTDM end!!\n\n"));
10759 }
10760
10761 void BTDM_UpdateCoexState(struct rtw_adapter *padapter)
10762 {
10763         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10764
10765         if (!BTDM_IsSameCoexistState(padapter)) {
10766                 RTPRINT(FBT, BT_TRACE, ("[BTCoex], Coexist State[bitMap] change from 0x%"i64fmt"x to 0x%"i64fmt"x,  changeBits = 0x%"i64fmt"x\n",
10767                         pHalData->bt_coexist.PreviousState,
10768                         pHalData->bt_coexist.CurrentState,
10769                         (pHalData->bt_coexist.PreviousState^pHalData->bt_coexist.CurrentState)));
10770                 pHalData->bt_coexist.PreviousState = pHalData->bt_coexist.CurrentState;
10771         }
10772 }
10773
10774 u8 BTDM_IsSameCoexistState(struct rtw_adapter *padapter)
10775 {
10776         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10777
10778         if (pHalData->bt_coexist.PreviousState == pHalData->bt_coexist.CurrentState) {
10779                 return true;
10780         } else {
10781                 RTPRINT(FBT, BT_TRACE, ("[DM][BT], Coexist state changed!!\n"));
10782                 return false;
10783         }
10784 }
10785
10786 void BTDM_PWDBMonitor(struct rtw_adapter *padapter)
10787 {
10788         struct bt_30info *pBTInfo = GET_BT_INFO(GetDefaultAdapter(padapter));
10789         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
10790         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10791         u8 H2C_Parameter[3] = {0};
10792         s32 tmpBTEntryMaxPWDB = 0, tmpBTEntryMinPWDB = 0xff;
10793         u8 i;
10794
10795         if (pBtMgnt->BtOperationOn) {
10796                 for (i = 0; i < MAX_BT_ASOC_ENTRY_NUM; i++) {
10797                         if (pBTInfo->BtAsocEntry[i].bUsed) {
10798                                 if (pBTInfo->BtAsocEntry[i].UndecoratedSmoothedPWDB < tmpBTEntryMinPWDB)
10799                                         tmpBTEntryMinPWDB = pBTInfo->BtAsocEntry[i].UndecoratedSmoothedPWDB;
10800                                 if (pBTInfo->BtAsocEntry[i].UndecoratedSmoothedPWDB > tmpBTEntryMaxPWDB)
10801                                         tmpBTEntryMaxPWDB = pBTInfo->BtAsocEntry[i].UndecoratedSmoothedPWDB;
10802                                 /*  Report every BT connection (HS mode) RSSI to FW */
10803                                 H2C_Parameter[2] = (u8)(pBTInfo->BtAsocEntry[i].UndecoratedSmoothedPWDB & 0xFF);
10804                                 H2C_Parameter[0] = (MAX_FW_SUPPORT_MACID_NUM-1-i);
10805                                 RTPRINT(FDM, DM_BT30, ("RSSI report for BT[%d], H2C_Par = 0x%x\n", i, H2C_Parameter[0]));
10806                                 FillH2CCmd(padapter, RSSI_SETTING_EID, 3, H2C_Parameter);
10807                                 RTPRINT_ADDR(FDM, (DM_PWDB|DM_BT30), ("BT_Entry Mac :"),
10808                                              pBTInfo->BtAsocEntry[i].BTRemoteMACAddr)
10809                                 RTPRINT(FDM, (DM_PWDB|DM_BT30),
10810                                         ("BT rx pwdb[%d] = 0x%x(%d)\n", i,
10811                                         pBTInfo->BtAsocEntry[i].UndecoratedSmoothedPWDB,
10812                                         pBTInfo->BtAsocEntry[i].UndecoratedSmoothedPWDB));
10813                         }
10814                 }
10815                 if (tmpBTEntryMaxPWDB != 0) {   /*  If associated entry is found */
10816                         pHalData->dmpriv.BT_EntryMaxUndecoratedSmoothedPWDB = tmpBTEntryMaxPWDB;
10817                         RTPRINT(FDM, (DM_PWDB|DM_BT30), ("BT_EntryMaxPWDB = 0x%x(%d)\n",
10818                                 tmpBTEntryMaxPWDB, tmpBTEntryMaxPWDB));
10819                 } else {
10820                         pHalData->dmpriv.BT_EntryMaxUndecoratedSmoothedPWDB = 0;
10821                 }
10822                 if (tmpBTEntryMinPWDB != 0xff) { /*  If associated entry is found */
10823                         pHalData->dmpriv.BT_EntryMinUndecoratedSmoothedPWDB = tmpBTEntryMinPWDB;
10824                         RTPRINT(FDM, (DM_PWDB|DM_BT30), ("BT_EntryMinPWDB = 0x%x(%d)\n",
10825                                 tmpBTEntryMinPWDB, tmpBTEntryMinPWDB));
10826                 } else {
10827                         pHalData->dmpriv.BT_EntryMinUndecoratedSmoothedPWDB = 0;
10828                 }
10829         }
10830 }
10831
10832 u8 BTDM_IsBTBusy(struct rtw_adapter *padapter)
10833 {
10834         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
10835         struct bt_mgnt *pBtMgnt = &pBTInfo->BtMgnt;
10836
10837         if (pBtMgnt->ExtConfig.bBTBusy)
10838                 return true;
10839         else
10840                 return false;
10841 }
10842
10843 u8 BTDM_IsWifiBusy(struct rtw_adapter *padapter)
10844 {
10845 /*PMGNT_INFO            pMgntInfo = &GetDefaultAdapter(padapter)->MgntInfo; */
10846         struct mlme_priv *pmlmepriv = &GetDefaultAdapter(padapter)->mlmepriv;
10847         struct bt_30info *pBTInfo = GET_BT_INFO(padapter);
10848         struct bt_traffic *pBtTraffic = &pBTInfo->BtTraffic;
10849
10850         if (pmlmepriv->LinkDetectInfo.bBusyTraffic ||
10851                 pBtTraffic->Bt30TrafficStatistics.bTxBusyTraffic ||
10852                 pBtTraffic->Bt30TrafficStatistics.bRxBusyTraffic)
10853                 return true;
10854         else
10855                 return false;
10856 }
10857
10858 u8 BTDM_IsCoexistStateChanged(struct rtw_adapter *padapter)
10859 {
10860         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10861
10862         if (pHalData->bt_coexist.PreviousState == pHalData->bt_coexist.CurrentState)
10863                 return false;
10864         else
10865                 return true;
10866 }
10867
10868 u8 BTDM_IsWifiUplink(struct rtw_adapter *padapter)
10869 {
10870 /*PMGNT_INFO            pMgntInfo = &GetDefaultAdapter(padapter)->MgntInfo; */
10871         struct mlme_priv *pmlmepriv;
10872         struct bt_30info *pBTInfo;
10873         struct bt_traffic *pBtTraffic;
10874
10875         pmlmepriv = &padapter->mlmepriv;
10876         pBTInfo = GET_BT_INFO(padapter);
10877         pBtTraffic = &pBTInfo->BtTraffic;
10878
10879         if ((pmlmepriv->LinkDetectInfo.bTxBusyTraffic) ||
10880                 (pBtTraffic->Bt30TrafficStatistics.bTxBusyTraffic))
10881                 return true;
10882         else
10883                 return false;
10884 }
10885
10886 u8 BTDM_IsWifiDownlink(struct rtw_adapter *padapter)
10887 {
10888 /*PMGNT_INFO            pMgntInfo = &GetDefaultAdapter(padapter)->MgntInfo; */
10889         struct mlme_priv *pmlmepriv;
10890         struct bt_30info *pBTInfo;
10891         struct bt_traffic *pBtTraffic;
10892
10893         pmlmepriv = &padapter->mlmepriv;
10894         pBTInfo = GET_BT_INFO(padapter);
10895         pBtTraffic = &pBTInfo->BtTraffic;
10896
10897         if ((pmlmepriv->LinkDetectInfo.bRxBusyTraffic) ||
10898                 (pBtTraffic->Bt30TrafficStatistics.bRxBusyTraffic))
10899                 return true;
10900         else
10901                 return false;
10902 }
10903
10904 u8 BTDM_IsBTHSMode(struct rtw_adapter *padapter)
10905 {
10906 /*PMGNT_INFO            pMgntInfo = &GetDefaultAdapter(padapter)->MgntInfo; */
10907         struct hal_data_8723a *pHalData;
10908         struct bt_mgnt *pBtMgnt;
10909
10910         pHalData = GET_HAL_DATA(padapter);
10911         pBtMgnt = &pHalData->BtInfo.BtMgnt;
10912
10913         if (pBtMgnt->BtOperationOn)
10914                 return true;
10915         else
10916                 return false;
10917 }
10918
10919 u8 BTDM_IsBTUplink(struct rtw_adapter *padapter)
10920 {
10921         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10922
10923         if (pHalData->bt_coexist.BT21TrafficStatistics.bTxBusyTraffic)
10924                 return true;
10925         else
10926                 return false;
10927 }
10928
10929 u8 BTDM_IsBTDownlink(struct rtw_adapter *padapter)
10930 {
10931         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10932
10933         if (pHalData->bt_coexist.BT21TrafficStatistics.bRxBusyTraffic)
10934                 return true;
10935         else
10936                 return false;
10937 }
10938
10939 void BTDM_AdjustForBtOperation(struct rtw_adapter *padapter)
10940 {
10941         RTPRINT(FBT, BT_TRACE, ("[BT][DM], BTDM_AdjustForBtOperation()\n"));
10942         BTDM_AdjustForBtOperation8723A(padapter);
10943 }
10944
10945 void BTDM_SetBtCoexCurrAntNum(struct rtw_adapter *padapter, u8 antNum)
10946 {
10947         BTDM_Set8723ABtCoexCurrAntNum(padapter, antNum);
10948 }
10949
10950 void BTDM_ForHalt(struct rtw_adapter *padapter)
10951 {
10952         if (!rtl8723a_BT_coexist(padapter))
10953                 return;
10954
10955         BTDM_ForHalt8723A(padapter);
10956         GET_HAL_DATA(padapter)->bt_coexist.bInitlized = false;
10957 }
10958
10959 void BTDM_WifiScanNotify(struct rtw_adapter *padapter, u8 scanType)
10960 {
10961         if (!rtl8723a_BT_coexist(padapter))
10962                 return;
10963
10964         BTDM_WifiScanNotify8723A(padapter, scanType);
10965 }
10966
10967 void BTDM_WifiAssociateNotify(struct rtw_adapter *padapter, u8 action)
10968 {
10969         if (!rtl8723a_BT_coexist(padapter))
10970                 return;
10971
10972         BTDM_WifiAssociateNotify8723A(padapter, action);
10973 }
10974
10975 void rtl8723a_BT_mediastatus_notify(struct rtw_adapter *padapter,
10976                                     enum rt_media_status mstatus)
10977 {
10978         if (!rtl8723a_BT_coexist(padapter))
10979                 return;
10980
10981         BTDM_MediaStatusNotify8723A(padapter, mstatus);
10982 }
10983
10984 void rtl8723a_BT_specialpacket_notify(struct rtw_adapter *padapter)
10985 {
10986         if (!rtl8723a_BT_coexist(padapter))
10987                 return;
10988
10989         BTDM_ForDhcp8723A(padapter);
10990 }
10991
10992 void BTDM_ResetActionProfileState(struct rtw_adapter *padapter)
10993 {
10994         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
10995
10996         pHalData->bt_coexist.CurrentState &= ~\
10997                 (BT_COEX_STATE_PROFILE_HID|BT_COEX_STATE_PROFILE_A2DP|
10998                 BT_COEX_STATE_PROFILE_PAN|BT_COEX_STATE_PROFILE_SCO);
10999 }
11000
11001 u8 BTDM_IsActionSCO(struct rtw_adapter *padapter)
11002 {
11003         struct hal_data_8723a *pHalData;
11004         struct bt_30info *pBTInfo;
11005         struct bt_mgnt *pBtMgnt;
11006         struct bt_dgb *pBtDbg;
11007         u8 bRet;
11008
11009         pHalData = GET_HAL_DATA(padapter);
11010         pBTInfo = GET_BT_INFO(padapter);
11011         pBtMgnt = &pBTInfo->BtMgnt;
11012         pBtDbg = &pBTInfo->BtDbg;
11013         bRet = false;
11014
11015         if (pBtDbg->dbgCtrl) {
11016                 if (pBtDbg->dbgProfile == BT_DBG_PROFILE_SCO) {
11017                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_PROFILE_SCO;
11018                         bRet = true;
11019                 }
11020         } else {
11021                 if (pBtMgnt->ExtConfig.NumberOfSCO > 0) {
11022                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_PROFILE_SCO;
11023                         bRet = true;
11024                 }
11025         }
11026         return bRet;
11027 }
11028
11029 u8 BTDM_IsActionHID(struct rtw_adapter *padapter)
11030 {
11031         struct bt_30info *pBTInfo;
11032         struct hal_data_8723a *pHalData;
11033         struct bt_mgnt *pBtMgnt;
11034         struct bt_dgb *pBtDbg;
11035         u8 bRet;
11036
11037         pHalData = GET_HAL_DATA(padapter);
11038         pBTInfo = GET_BT_INFO(padapter);
11039         pBtMgnt = &pBTInfo->BtMgnt;
11040         pBtDbg = &pBTInfo->BtDbg;
11041         bRet = false;
11042
11043         if (pBtDbg->dbgCtrl) {
11044                 if (pBtDbg->dbgProfile == BT_DBG_PROFILE_HID) {
11045                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_PROFILE_HID;
11046                         bRet = true;
11047                 }
11048         } else {
11049                 if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_HID) &&
11050                     pBtMgnt->ExtConfig.NumberOfHandle == 1) {
11051                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_PROFILE_HID;
11052                         bRet = true;
11053                 }
11054         }
11055         return bRet;
11056 }
11057
11058 u8 BTDM_IsActionA2DP(struct rtw_adapter *padapter)
11059 {
11060         struct hal_data_8723a *pHalData;
11061         struct bt_30info *pBTInfo;
11062         struct bt_mgnt *pBtMgnt;
11063         struct bt_dgb *pBtDbg;
11064         u8 bRet;
11065
11066         pHalData = GET_HAL_DATA(padapter);
11067         pBTInfo = GET_BT_INFO(padapter);
11068         pBtMgnt = &pBTInfo->BtMgnt;
11069         pBtDbg = &pBTInfo->BtDbg;
11070         bRet = false;
11071
11072         if (pBtDbg->dbgCtrl) {
11073                 if (pBtDbg->dbgProfile == BT_DBG_PROFILE_A2DP) {
11074                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_PROFILE_A2DP;
11075                         bRet = true;
11076                 }
11077         } else {
11078                 if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_A2DP) &&
11079                     pBtMgnt->ExtConfig.NumberOfHandle == 1) {
11080                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_PROFILE_A2DP;
11081                         bRet = true;
11082                 }
11083         }
11084         return bRet;
11085 }
11086
11087 u8 BTDM_IsActionPAN(struct rtw_adapter *padapter)
11088 {
11089         struct hal_data_8723a *pHalData;
11090         struct bt_30info *pBTInfo;
11091         struct bt_mgnt *pBtMgnt;
11092         struct bt_dgb *pBtDbg;
11093         u8 bRet;
11094
11095         pHalData = GET_HAL_DATA(padapter);
11096         pBTInfo = GET_BT_INFO(padapter);
11097         pBtMgnt = &pBTInfo->BtMgnt;
11098         pBtDbg = &pBTInfo->BtDbg;
11099         bRet = false;
11100
11101         if (pBtDbg->dbgCtrl) {
11102                 if (pBtDbg->dbgProfile == BT_DBG_PROFILE_PAN) {
11103                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_PROFILE_PAN;
11104                         bRet = true;
11105                 }
11106         } else {
11107                 if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_PAN) &&
11108                     pBtMgnt->ExtConfig.NumberOfHandle == 1) {
11109                         pHalData->bt_coexist.CurrentState |= BT_COEX_STATE_PROFILE_PAN;
11110                         bRet = true;
11111                 }
11112         }
11113         return bRet;
11114 }
11115
11116 u8 BTDM_IsActionHIDA2DP(struct rtw_adapter *padapter)
11117 {
11118         struct hal_data_8723a *pHalData;
11119         struct bt_30info *pBTInfo;
11120         struct bt_mgnt *pBtMgnt;
11121         struct bt_dgb *pBtDbg;
11122         u8 bRet;
11123
11124         pHalData = GET_HAL_DATA(padapter);
11125         pBTInfo = GET_BT_INFO(padapter);
11126         pBtMgnt = &pBTInfo->BtMgnt;
11127         pBtDbg = &pBTInfo->BtDbg;
11128         bRet = false;
11129
11130         if (pBtDbg->dbgCtrl) {
11131                 if (pBtDbg->dbgProfile == BT_DBG_PROFILE_HID_A2DP) {
11132                         pHalData->bt_coexist.CurrentState |= (BT_COEX_STATE_PROFILE_HID|BT_COEX_STATE_PROFILE_A2DP);
11133                         bRet = true;
11134                 }
11135         } else {
11136                 if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_HID) &&
11137                     BTHCI_CheckProfileExist(padapter, BT_PROFILE_A2DP)) {
11138                         pHalData->bt_coexist.CurrentState |= (BT_COEX_STATE_PROFILE_HID|BT_COEX_STATE_PROFILE_A2DP);
11139                         bRet = true;
11140                 }
11141         }
11142         return bRet;
11143 }
11144
11145 u8 BTDM_IsActionHIDPAN(struct rtw_adapter *padapter)
11146 {
11147         struct hal_data_8723a *pHalData;
11148         struct bt_30info *pBTInfo;
11149         struct bt_dgb *pBtDbg;
11150         u8 bRet;
11151
11152         pHalData = GET_HAL_DATA(padapter);
11153         pBTInfo = GET_BT_INFO(padapter);
11154         pBtDbg = &pBTInfo->BtDbg;
11155         bRet = false;
11156
11157         if (pBtDbg->dbgCtrl) {
11158                 if (pBtDbg->dbgProfile == BT_DBG_PROFILE_HID_PAN) {
11159                         pHalData->bt_coexist.CurrentState |= (BT_COEX_STATE_PROFILE_HID|BT_COEX_STATE_PROFILE_PAN);
11160                         bRet = true;
11161                 }
11162         } else {
11163                 if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_HID) &&
11164                     BTHCI_CheckProfileExist(padapter, BT_PROFILE_PAN)) {
11165                         pHalData->bt_coexist.CurrentState |= (BT_COEX_STATE_PROFILE_HID|BT_COEX_STATE_PROFILE_PAN);
11166                         bRet = true;
11167                 }
11168         }
11169         return bRet;
11170 }
11171
11172 u8 BTDM_IsActionPANA2DP(struct rtw_adapter *padapter)
11173 {
11174         struct hal_data_8723a *pHalData;
11175         struct bt_30info *pBTInfo;
11176         struct bt_dgb *pBtDbg;
11177         u8 bRet;
11178
11179         pHalData = GET_HAL_DATA(padapter);
11180         pBTInfo = GET_BT_INFO(padapter);
11181         pBtDbg = &pBTInfo->BtDbg;
11182         bRet = false;
11183
11184         if (pBtDbg->dbgCtrl) {
11185                 if (pBtDbg->dbgProfile == BT_DBG_PROFILE_PAN_A2DP) {
11186                         pHalData->bt_coexist.CurrentState |= (BT_COEX_STATE_PROFILE_PAN|BT_COEX_STATE_PROFILE_A2DP);
11187                         bRet = true;
11188                 }
11189         } else {
11190                 if (BTHCI_CheckProfileExist(padapter, BT_PROFILE_PAN) && BTHCI_CheckProfileExist(padapter, BT_PROFILE_A2DP)) {
11191                         pHalData->bt_coexist.CurrentState |= (BT_COEX_STATE_PROFILE_PAN|BT_COEX_STATE_PROFILE_A2DP);
11192                         bRet = true;
11193                 }
11194         }
11195         return bRet;
11196 }
11197
11198 bool rtl8723a_BT_enabled(struct rtw_adapter *padapter)
11199 {
11200         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
11201
11202         if (pHalData->bt_coexist.bCurBtDisabled)
11203                 return false;
11204         else
11205                 return true;
11206 }
11207
11208 /*  ===== End of sync from SD7 driver HAL/BTCoexist/HalBtCoexist.c ===== */
11209
11210 /*  ===== Below this line is sync from SD7 driver HAL/HalBT.c ===== */
11211
11212 /*  */
11213 /*local function */
11214 /*  */
11215
11216 static void halbt_InitHwConfig8723A(struct rtw_adapter *padapter)
11217 {
11218 }
11219
11220 /*  */
11221 /*extern function */
11222 /*  */
11223 u8 HALBT_GetPGAntNum(struct rtw_adapter *padapter)
11224 {
11225         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
11226
11227         return pHalData->bt_coexist.BT_Ant_Num;
11228 }
11229
11230 void HALBT_SetKey(struct rtw_adapter *padapter, u8 EntryNum)
11231 {
11232         struct bt_30info *pBTinfo;
11233         struct bt_asoc_entry *pBtAssocEntry;
11234         u16                             usConfig = 0;
11235
11236         pBTinfo = GET_BT_INFO(padapter);
11237         pBtAssocEntry = &pBTinfo->BtAsocEntry[EntryNum];
11238
11239         pBtAssocEntry->HwCAMIndex = BT_HWCAM_STAR + EntryNum;
11240
11241         usConfig = CAM_VALID | (CAM_AES << 2);
11242         rtl8723a_cam_write(padapter, pBtAssocEntry->HwCAMIndex, usConfig,
11243                            pBtAssocEntry->BTRemoteMACAddr,
11244                            pBtAssocEntry->PTK + TKIP_ENC_KEY_POS);
11245 }
11246
11247 void HALBT_RemoveKey(struct rtw_adapter *padapter, u8 EntryNum)
11248 {
11249         struct bt_30info *pBTinfo;
11250         struct bt_asoc_entry *pBtAssocEntry;
11251
11252         pBTinfo = GET_BT_INFO(padapter);
11253         pBtAssocEntry = &pBTinfo->BtAsocEntry[EntryNum];
11254
11255         if (pBTinfo->BtAsocEntry[EntryNum].HwCAMIndex != 0) {
11256                 /*  ToDo : add New HALBT_RemoveKey function !! */
11257                 if (pBtAssocEntry->HwCAMIndex >= BT_HWCAM_STAR &&
11258                     pBtAssocEntry->HwCAMIndex < HALF_CAM_ENTRY)
11259                         rtl8723a_cam_empty_entry(padapter,
11260                                                  pBtAssocEntry->HwCAMIndex);
11261                 pBTinfo->BtAsocEntry[EntryNum].HwCAMIndex = 0;
11262         }
11263 }
11264
11265 void rtl8723a_BT_init_hal_vars(struct rtw_adapter *padapter)
11266 {
11267         struct hal_data_8723a *pHalData;
11268
11269         pHalData = GET_HAL_DATA(padapter);
11270
11271         pHalData->bt_coexist.BluetoothCoexist = pHalData->EEPROMBluetoothCoexist;
11272         pHalData->bt_coexist.BT_Ant_Num = pHalData->EEPROMBluetoothAntNum;
11273         pHalData->bt_coexist.BT_CoexistType = pHalData->EEPROMBluetoothType;
11274         pHalData->bt_coexist.BT_Ant_isolation = pHalData->EEPROMBluetoothAntIsolation;
11275         pHalData->bt_coexist.bt_radiosharedtype = pHalData->EEPROMBluetoothRadioShared;
11276
11277         RT_TRACE(_module_hal_init_c_, _drv_info_,
11278                  ("BT Coexistance = 0x%x\n", rtl8723a_BT_coexist(padapter)));
11279
11280         if (rtl8723a_BT_coexist(padapter)) {
11281                 if (pHalData->bt_coexist.BT_Ant_Num == Ant_x2) {
11282                         BTDM_SetBtCoexCurrAntNum(padapter, 2);
11283                         RT_TRACE(_module_hal_init_c_, _drv_info_, ("BlueTooth BT_Ant_Num = Antx2\n"));
11284                 } else if (pHalData->bt_coexist.BT_Ant_Num == Ant_x1) {
11285                         BTDM_SetBtCoexCurrAntNum(padapter, 1);
11286                         RT_TRACE(_module_hal_init_c_, _drv_info_, ("BlueTooth BT_Ant_Num = Antx1\n"));
11287                 }
11288                 pHalData->bt_coexist.bBTBusyTraffic = false;
11289                 pHalData->bt_coexist.bBTTrafficModeSet = false;
11290                 pHalData->bt_coexist.bBTNonTrafficModeSet = false;
11291                 pHalData->bt_coexist.CurrentState = 0;
11292                 pHalData->bt_coexist.PreviousState = 0;
11293
11294                 RT_TRACE(_module_hal_init_c_, _drv_info_,
11295                          ("bt_radiosharedType = 0x%x\n",
11296                          pHalData->bt_coexist.bt_radiosharedtype));
11297         }
11298 }
11299
11300 bool rtl8723a_BT_coexist(struct rtw_adapter *padapter)
11301 {
11302         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
11303
11304         if (pHalData->bt_coexist.BluetoothCoexist)
11305                 return true;
11306         else
11307                 return false;
11308 }
11309
11310 u8 HALBT_BTChipType(struct rtw_adapter *padapter)
11311 {
11312         struct hal_data_8723a *pHalData = GET_HAL_DATA(padapter);
11313
11314         return pHalData->bt_coexist.BT_CoexistType;
11315 }
11316
11317 void rtl8723a_BT_init_hwconfig(struct rtw_adapter *padapter)
11318 {
11319         halbt_InitHwConfig8723A(padapter);
11320         rtl8723a_BT_do_coexist(padapter);
11321 }
11322
11323 void HALBT_SetRtsCtsNoLenLimit(struct rtw_adapter *padapter)
11324 {
11325 }
11326
11327 /*  ===== End of sync from SD7 driver HAL/HalBT.c ===== */
11328
11329 void rtl8723a_dual_antenna_detection(struct rtw_adapter *padapter)
11330 {
11331         struct hal_data_8723a *pHalData;
11332         struct dm_odm_t *pDM_Odm;
11333         struct sw_ant_sw *pDM_SWAT_Table;
11334         u8 i;
11335
11336         pHalData = GET_HAL_DATA(padapter);
11337         pDM_Odm = &pHalData->odmpriv;
11338         pDM_SWAT_Table = &pDM_Odm->DM_SWAT_Table;
11339
11340         /*  */
11341         /*  <Roger_Notes> RTL8723A Single and Dual antenna dynamic detection
11342             mechanism when RF power state is on. */
11343         /*  We should take power tracking, IQK, LCK, RCK RF read/write
11344             operation into consideration. */
11345         /*  2011.12.15. */
11346         /*  */
11347         if (!pHalData->bAntennaDetected) {
11348                 u8 btAntNum = BT_GetPGAntNum(padapter);
11349
11350                 /*  Set default antenna B status */
11351                 if (btAntNum == Ant_x2)
11352                         pDM_SWAT_Table->ANTB_ON = true;
11353                 else if (btAntNum == Ant_x1)
11354                         pDM_SWAT_Table->ANTB_ON = false;
11355                 else
11356                         pDM_SWAT_Table->ANTB_ON = true;
11357
11358                 if (pHalData->CustomerID != RT_CID_TOSHIBA) {
11359                         for (i = 0; i < MAX_ANTENNA_DETECTION_CNT; i++) {
11360                                 if (ODM_SingleDualAntennaDetection
11361                                     (&pHalData->odmpriv, ANTTESTALL) == true)
11362                                         break;
11363                         }
11364
11365                         /*  Set default antenna number for BT coexistence */
11366                         if (btAntNum == Ant_x2)
11367                                 BT_SetBtCoexCurrAntNum(padapter,
11368                                                        pDM_SWAT_Table->
11369                                                        ANTB_ON ? 2 : 1);
11370                 }
11371                 pHalData->bAntennaDetected = true;
11372         }
11373 }