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