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