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