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