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