Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / staging / rtl8192u / ieee80211 / rtl819x_HTProc.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * As this function is mainly ported from Windows driver, so leave the name
5  * little changed. If any confusion caused, tell me. Created by WB. 2008.05.08
6  */
7 #include "ieee80211.h"
8
9 u8 MCS_FILTER_ALL[16] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
10
11 u8 MCS_FILTER_1SS[16] = {0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
12
13 u16 MCS_DATA_RATE[2][2][77] = {
14         {       {13, 26, 39, 52, 78, 104, 117, 130, 26, 52, 78, 104, 156, 208, 234, 260,
15                  39, 78, 117, 234, 312, 351, 390, 52, 104, 156, 208, 312, 416, 468, 520,
16                  0, 78, 104, 130, 117, 156, 195, 104, 130, 130, 156, 182, 182, 208, 156, 195,
17                  195, 234, 273, 273, 312, 130, 156, 181, 156, 181, 208, 234, 208, 234, 260, 260,
18                  286, 195, 234, 273, 234, 273, 312, 351, 312, 351, 390, 390, 429},                      // Long GI, 20MHz
19                 {14, 29, 43, 58, 87, 116, 130, 144, 29, 58, 87, 116, 173, 231, 260, 289,
20                  43, 87, 130, 173, 260, 347, 390, 433, 58, 116, 173, 231, 347, 462, 520, 578,
21                  0, 87, 116, 144, 130, 173, 217, 116, 144, 144, 173, 202, 202, 231, 173, 217,
22                  217, 260, 303, 303, 347, 144, 173, 202, 173, 202, 231, 260, 231, 260, 289, 289,
23                  318, 217, 260, 303, 260, 303, 347, 390, 347, 390, 433, 433, 477}       },              // Short GI, 20MHz
24         {       {27, 54, 81, 108, 162, 216, 243, 270, 54, 108, 162, 216, 324, 432, 486, 540,
25                  81, 162, 243, 324, 486, 648, 729, 810, 108, 216, 324, 432, 648, 864, 972, 1080,
26                  12, 162, 216, 270, 243, 324, 405, 216, 270, 270, 324, 378, 378, 432, 324, 405,
27                  405, 486, 567, 567, 648, 270, 324, 378, 324, 378, 432, 486, 432, 486, 540, 540,
28                  594, 405, 486, 567, 486, 567, 648, 729, 648, 729, 810, 810, 891},      // Long GI, 40MHz
29                 {30, 60, 90, 120, 180, 240, 270, 300, 60, 120, 180, 240, 360, 480, 540, 600,
30                  90, 180, 270, 360, 540, 720, 810, 900, 120, 240, 360, 480, 720, 960, 1080, 1200,
31                  13, 180, 240, 300, 270, 360, 450, 240, 300, 300, 360, 420, 420, 480, 360, 450,
32                  450, 540, 630, 630, 720, 300, 360, 420, 360, 420, 480, 540, 480, 540, 600, 600,
33                  660, 450, 540, 630, 540, 630, 720, 810, 720, 810, 900, 900, 990}       }       // Short GI, 40MHz
34 };
35
36 static u8 UNKNOWN_BORADCOM[3] = {0x00, 0x14, 0xbf};
37 static u8 LINKSYSWRT330_LINKSYSWRT300_BROADCOM[3] = {0x00, 0x1a, 0x70};
38 static u8 LINKSYSWRT350_LINKSYSWRT150_BROADCOM[3] = {0x00, 0x1d, 0x7e};
39 static u8 NETGEAR834Bv2_BROADCOM[3] = {0x00, 0x1b, 0x2f};
40 static u8 BELKINF5D8233V1_RALINK[3] = {0x00, 0x17, 0x3f};       //cosa 03202008
41 static u8 BELKINF5D82334V3_RALINK[3] = {0x00, 0x1c, 0xdf};
42 static u8 PCI_RALINK[3] = {0x00, 0x90, 0xcc};
43 static u8 EDIMAX_RALINK[3] = {0x00, 0x0e, 0x2e};
44 static u8 AIRLINK_RALINK[3] = {0x00, 0x18, 0x02};
45 //static u8 DLINK_ATHEROS[3] = {0x00, 0x1c, 0xf0};
46 static u8 CISCO_BROADCOM[3] = {0x00, 0x17, 0x94};
47 /*
48  * 2008/04/01 MH For Cisco G mode RX TP We need to change FW duration. Should we
49  * put the code in other place??
50  * static u8 WIFI_CISCO_G_AP[3] = {0x00, 0x40, 0x96};
51  */
52 /*
53  *function:  This function update default settings in pHTInfo structure
54  *   input:  PRT_HIGH_THROUGHPUT        pHTInfo
55  *  output:  none
56  *  return:  none
57  *  notice:  These value need be modified if any changes.
58  */
59 void HTUpdateDefaultSetting(struct ieee80211_device *ieee)
60 {
61         PRT_HIGH_THROUGHPUT     pHTInfo = ieee->pHTInfo;
62         //const typeof( ((struct ieee80211_device *)0)->pHTInfo ) *__mptr = &pHTInfo;
63
64         //printk("pHTinfo:%p, &pHTinfo:%p, mptr:%p,  offsetof:%x\n", pHTInfo, &pHTInfo, __mptr, offsetof(struct ieee80211_device, pHTInfo));
65         //printk("===>ieee:%p,\n", ieee);
66         // ShortGI support
67         pHTInfo->bRegShortGI20MHz = 1;
68         pHTInfo->bRegShortGI40MHz = 1;
69
70         // 40MHz channel support
71         pHTInfo->bRegBW40MHz = 1;
72
73         // CCK rate support in 40MHz channel
74         if (pHTInfo->bRegBW40MHz)
75                 pHTInfo->bRegSuppCCK = 1;
76         else
77                 pHTInfo->bRegSuppCCK = true;
78
79         // AMSDU related
80         pHTInfo->nAMSDU_MaxSize = 7935UL;
81         pHTInfo->bAMSDU_Support = 0;
82
83         // AMPDU related
84         pHTInfo->bAMPDUEnable = 1;
85         pHTInfo->AMPDU_Factor = 2; //// 0: 2n13(8K), 1:2n14(16K), 2:2n15(32K), 3:2n16(64k)
86         pHTInfo->MPDU_Density = 0;// 0: No restriction, 1: 1/8usec, 2: 1/4usec, 3: 1/2usec, 4: 1usec, 5: 2usec, 6: 4usec, 7:8usec
87
88         // MIMO Power Save
89         pHTInfo->SelfMimoPs = 3;// 0: Static Mimo Ps, 1: Dynamic Mimo Ps, 3: No Limitation, 2: Reserved(Set to 3 automatically.)
90         if (pHTInfo->SelfMimoPs == 2)
91                 pHTInfo->SelfMimoPs = 3;
92         // 8190 only. Assign rate operation mode to firmware
93         ieee->bTxDisableRateFallBack = 0;
94         ieee->bTxUseDriverAssingedRate = 0;
95
96 #ifdef  TO_DO_LIST
97         // 8190 only. Assign duration operation mode to firmware
98         pMgntInfo->bTxEnableFwCalcDur = (BOOLEAN)pNdisCommon->bRegTxEnableFwCalcDur;
99 #endif
100         /*
101          * 8190 only, Realtek proprietary aggregation mode
102          * Set MPDUDensity=2,   1: Set MPDUDensity=2(32k)  for Realtek AP and set MPDUDensity=0(8k) for others
103          */
104         pHTInfo->bRegRT2RTAggregation = 1;//0: Set MPDUDensity=2,   1: Set MPDUDensity=2(32k)  for Realtek AP and set MPDUDensity=0(8k) for others
105
106         // For Rx Reorder Control
107         pHTInfo->bRegRxReorderEnable = 1;
108         pHTInfo->RxReorderWinSize = 64;
109         pHTInfo->RxReorderPendingTime = 30;
110
111 #ifdef USB_TX_DRIVER_AGGREGATION_ENABLE
112         pHTInfo->UsbTxAggrNum = 4;
113 #endif
114 #ifdef USB_RX_AGGREGATION_SUPPORT
115         pHTInfo->UsbRxFwAggrEn = 1;
116         pHTInfo->UsbRxFwAggrPageNum = 24;
117         pHTInfo->UsbRxFwAggrPacketNum = 8;
118         pHTInfo->UsbRxFwAggrTimeout = 16; ////usb rx FW aggregation timeout threshold.It's in units of 64us
119 #endif
120 }
121
122 /*
123  *function:  This function print out each field on HT capability
124  *           IE mainly from (Beacon/ProbeRsp/AssocReq)
125  *   input:  u8*        CapIE       //Capability IE to be printed out
126  *           u8*        TitleString //mainly print out caller function
127  *  output:  none
128  *  return:  none
129  *  notice:  Driver should not print out this message by default.
130  */
131 void HTDebugHTCapability(u8 *CapIE, u8 *TitleString)
132 {
133         static u8                 EWC11NHTCap[] = {0x00, 0x90, 0x4c, 0x33};     // For 11n EWC definition, 2007.07.17, by Emily
134         struct ht_capability_ele *pCapELE;
135
136         if (!memcmp(CapIE, EWC11NHTCap, sizeof(EWC11NHTCap))) {
137                 //EWC IE
138                 IEEE80211_DEBUG(IEEE80211_DL_HT, "EWC IE in %s()\n", __func__);
139                 pCapELE = (struct ht_capability_ele *)(&CapIE[4]);
140         } else {
141                 pCapELE = (struct ht_capability_ele *)(&CapIE[0]);
142         }
143         IEEE80211_DEBUG(IEEE80211_DL_HT, "<Log HT Capability>. Called by %s\n", TitleString);
144
145         IEEE80211_DEBUG(IEEE80211_DL_HT,  "\tSupported Channel Width = %s\n", (pCapELE->ChlWidth) ? "20MHz" : "20/40MHz");
146         IEEE80211_DEBUG(IEEE80211_DL_HT,  "\tSupport Short GI for 20M = %s\n", (pCapELE->ShortGI20Mhz) ? "YES" : "NO");
147         IEEE80211_DEBUG(IEEE80211_DL_HT,  "\tSupport Short GI for 40M = %s\n", (pCapELE->ShortGI40Mhz) ? "YES" : "NO");
148         IEEE80211_DEBUG(IEEE80211_DL_HT,  "\tSupport TX STBC = %s\n", (pCapELE->TxSTBC) ? "YES" : "NO");
149         IEEE80211_DEBUG(IEEE80211_DL_HT,  "\tMax AMSDU Size = %s\n", (pCapELE->MaxAMSDUSize) ? "3839" : "7935");
150         IEEE80211_DEBUG(IEEE80211_DL_HT,  "\tSupport CCK in 20/40 mode = %s\n", (pCapELE->DssCCk) ? "YES" : "NO");
151         IEEE80211_DEBUG(IEEE80211_DL_HT,  "\tMax AMPDU Factor = %d\n", pCapELE->MaxRxAMPDUFactor);
152         IEEE80211_DEBUG(IEEE80211_DL_HT,  "\tMPDU Density = %d\n", pCapELE->MPDUDensity);
153         IEEE80211_DEBUG(IEEE80211_DL_HT,  "\tMCS Rate Set = [%x][%x][%x][%x][%x]\n", pCapELE->MCS[0],\
154                                 pCapELE->MCS[1], pCapELE->MCS[2], pCapELE->MCS[3], pCapELE->MCS[4]);
155 }
156
157 /*
158  *function:  This function print out each field on HT Information
159  *           IE mainly from (Beacon/ProbeRsp)
160  *   input:  u8*        InfoIE       //Capability IE to be printed out
161  *           u8*        TitleString //mainly print out caller function
162  *  output:  none
163  *  return:  none
164  *  notice:  Driver should not print out this message by default.
165  */
166 void HTDebugHTInfo(u8 *InfoIE, u8 *TitleString)
167 {
168         static u8       EWC11NHTInfo[] = {0x00, 0x90, 0x4c, 0x34};      // For 11n EWC definition, 2007.07.17, by Emily
169         PHT_INFORMATION_ELE             pHTInfoEle;
170
171         if (!memcmp(InfoIE, EWC11NHTInfo, sizeof(EWC11NHTInfo))) {
172                 // Not EWC IE
173                 IEEE80211_DEBUG(IEEE80211_DL_HT, "EWC IE in %s()\n", __func__);
174                 pHTInfoEle = (PHT_INFORMATION_ELE)(&InfoIE[4]);
175         } else {
176                 pHTInfoEle = (PHT_INFORMATION_ELE)(&InfoIE[0]);
177         }
178
179         IEEE80211_DEBUG(IEEE80211_DL_HT, "<Log HT Information Element>. Called by %s\n", TitleString);
180
181         IEEE80211_DEBUG(IEEE80211_DL_HT, "\tPrimary channel = %d\n", pHTInfoEle->ControlChl);
182         IEEE80211_DEBUG(IEEE80211_DL_HT, "\tSecondary channel =");
183         switch (pHTInfoEle->ExtChlOffset) {
184         case 0:
185                 IEEE80211_DEBUG(IEEE80211_DL_HT, "Not Present\n");
186                 break;
187         case 1:
188                 IEEE80211_DEBUG(IEEE80211_DL_HT, "Upper channel\n");
189                 break;
190         case 2:
191                 IEEE80211_DEBUG(IEEE80211_DL_HT, "Reserved. Eooro!!!\n");
192                 break;
193         case 3:
194                 IEEE80211_DEBUG(IEEE80211_DL_HT, "Lower Channel\n");
195                 break;
196         }
197         IEEE80211_DEBUG(IEEE80211_DL_HT, "\tRecommended channel width = %s\n", (pHTInfoEle->RecommemdedTxWidth) ? "20Mhz" : "40Mhz");
198
199         IEEE80211_DEBUG(IEEE80211_DL_HT, "\tOperation mode for protection = ");
200         switch (pHTInfoEle->OptMode) {
201         case 0:
202                 IEEE80211_DEBUG(IEEE80211_DL_HT, "No Protection\n");
203                 break;
204         case 1:
205                 IEEE80211_DEBUG(IEEE80211_DL_HT, "HT non-member protection mode\n");
206                 break;
207         case 2:
208                 IEEE80211_DEBUG(IEEE80211_DL_HT, "Suggest to open protection\n");
209                 break;
210         case 3:
211                 IEEE80211_DEBUG(IEEE80211_DL_HT, "HT mixed mode\n");
212                 break;
213         }
214
215         IEEE80211_DEBUG(IEEE80211_DL_HT, "\tBasic MCS Rate Set = [%x][%x][%x][%x][%x]\n", pHTInfoEle->BasicMSC[0],\
216                                 pHTInfoEle->BasicMSC[1], pHTInfoEle->BasicMSC[2], pHTInfoEle->BasicMSC[3], pHTInfoEle->BasicMSC[4]);
217 }
218
219 static u16 HTMcsToDataRate(struct ieee80211_device *ieee, u8 nMcsRate)
220 {
221         PRT_HIGH_THROUGHPUT     pHTInfo = ieee->pHTInfo;
222
223         u8      is40MHz = (pHTInfo->bCurBW40MHz) ? 1 : 0;
224         u8      isShortGI = (pHTInfo->bCurBW40MHz) ?
225                                                 ((pHTInfo->bCurShortGI40MHz) ? 1 : 0) :
226                                                 ((pHTInfo->bCurShortGI20MHz) ? 1 : 0);
227         return MCS_DATA_RATE[is40MHz][isShortGI][(nMcsRate & 0x7f)];
228 }
229
230 /*
231  *function:  This function returns current datarate.
232  *   input:  struct ieee80211_device*   ieee
233  *           u8                         nDataRate
234  *  output:  none
235  *  return:  tx rate
236  *  notice:  quite unsure about how to use this function //wb
237  */
238 u16  TxCountToDataRate(struct ieee80211_device *ieee, u8 nDataRate)
239 {
240         //PRT_HIGH_THROUGHPUT   pHTInfo = ieee->pHTInfo;
241         u16             CCKOFDMRate[12] = {0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c};
242         u8      is40MHz = 0;
243         u8      isShortGI = 0;
244
245         if (nDataRate < 12) {
246                 return CCKOFDMRate[nDataRate];
247         } else {
248                 if (nDataRate >= 0x10 && nDataRate <= 0x1f) { //if(nDataRate > 11 && nDataRate < 28 )
249                         is40MHz = 0;
250                         isShortGI = 0;
251
252                       // nDataRate = nDataRate - 12;
253                 } else if (nDataRate >= 0x20  && nDataRate <= 0x2f) { //(27, 44)
254                         is40MHz = 1;
255                         isShortGI = 0;
256
257                         //nDataRate = nDataRate - 28;
258                 } else if (nDataRate >= 0x30  && nDataRate <= 0x3f) { //(43, 60)
259                         is40MHz = 0;
260                         isShortGI = 1;
261
262                         //nDataRate = nDataRate - 44;
263                 } else if (nDataRate >= 0x40  && nDataRate <= 0x4f) { //(59, 76)
264                         is40MHz = 1;
265                         isShortGI = 1;
266
267                         //nDataRate = nDataRate - 60;
268                 }
269                 return MCS_DATA_RATE[is40MHz][isShortGI][nDataRate & 0xf];
270         }
271 }
272
273 bool IsHTHalfNmodeAPs(struct ieee80211_device *ieee)
274 {
275         bool                    retValue = false;
276         struct ieee80211_network *net = &ieee->current_network;
277
278         if ((memcmp(net->bssid, BELKINF5D8233V1_RALINK, 3) == 0) ||
279             (memcmp(net->bssid, BELKINF5D82334V3_RALINK, 3) == 0) ||
280             (memcmp(net->bssid, PCI_RALINK, 3) == 0) ||
281             (memcmp(net->bssid, EDIMAX_RALINK, 3) == 0) ||
282             (memcmp(net->bssid, AIRLINK_RALINK, 3) == 0) ||
283             (net->ralink_cap_exist))
284                 retValue = true;
285         else if ((memcmp(net->bssid, UNKNOWN_BORADCOM, 3) == 0) ||
286                  (memcmp(net->bssid, LINKSYSWRT330_LINKSYSWRT300_BROADCOM, 3) == 0) ||
287                  (memcmp(net->bssid, LINKSYSWRT350_LINKSYSWRT150_BROADCOM, 3) == 0) ||
288                  (memcmp(net->bssid, NETGEAR834Bv2_BROADCOM, 3) == 0) ||
289                  (net->broadcom_cap_exist))
290                 retValue = true;
291         else if (net->bssht.bdRT2RTAggregation)
292                 retValue = true;
293         else
294                 retValue = false;
295
296         return retValue;
297 }
298
299 /*
300  *function:  This function returns peer IOT.
301  *   input:  struct ieee80211_device*   ieee
302  *  output:  none
303  *  return:
304  *  notice:
305  */
306 static void HTIOTPeerDetermine(struct ieee80211_device *ieee)
307 {
308         PRT_HIGH_THROUGHPUT     pHTInfo = ieee->pHTInfo;
309         struct ieee80211_network *net = &ieee->current_network;
310
311         if (net->bssht.bdRT2RTAggregation)
312                 pHTInfo->IOTPeer = HT_IOT_PEER_REALTEK;
313         else if (net->broadcom_cap_exist)
314                 pHTInfo->IOTPeer = HT_IOT_PEER_BROADCOM;
315         else if ((memcmp(net->bssid, UNKNOWN_BORADCOM, 3) == 0) ||
316                  (memcmp(net->bssid, LINKSYSWRT330_LINKSYSWRT300_BROADCOM, 3) == 0) ||
317                  (memcmp(net->bssid, LINKSYSWRT350_LINKSYSWRT150_BROADCOM, 3) == 0) ||
318                  (memcmp(net->bssid, NETGEAR834Bv2_BROADCOM, 3) == 0))
319                 pHTInfo->IOTPeer = HT_IOT_PEER_BROADCOM;
320         else if ((memcmp(net->bssid, BELKINF5D8233V1_RALINK, 3) == 0) ||
321                  (memcmp(net->bssid, BELKINF5D82334V3_RALINK, 3) == 0) ||
322                  (memcmp(net->bssid, PCI_RALINK, 3) == 0) ||
323                  (memcmp(net->bssid, EDIMAX_RALINK, 3) == 0) ||
324                  (memcmp(net->bssid, AIRLINK_RALINK, 3) == 0) ||
325                  net->ralink_cap_exist)
326                 pHTInfo->IOTPeer = HT_IOT_PEER_RALINK;
327         else if (net->atheros_cap_exist)
328                 pHTInfo->IOTPeer = HT_IOT_PEER_ATHEROS;
329         else if (memcmp(net->bssid, CISCO_BROADCOM, 3) == 0)
330                 pHTInfo->IOTPeer = HT_IOT_PEER_CISCO;
331         else
332                 pHTInfo->IOTPeer = HT_IOT_PEER_UNKNOWN;
333
334         IEEE80211_DEBUG(IEEE80211_DL_IOT, "Joseph debug!! IOTPEER: %x\n", pHTInfo->IOTPeer);
335 }
336
337 /*
338  *function:  Check whether driver should declare received rate up to MCS13
339  *           only since some chipset is not good at receiving MCS14~15 frame
340  *           from some AP.
341  *   input:  struct ieee80211_device*   ieee
342  *           u8 *                       PeerMacAddr
343  *  output:  none
344  *  return:  return 1 if driver should declare MCS13 only(otherwise return 0)
345  */
346 static u8 HTIOTActIsDisableMCS14(struct ieee80211_device *ieee, u8 *PeerMacAddr)
347 {
348         return 0;
349 }
350
351 /*
352  * Function:    HTIOTActIsDisableMCS15
353  *
354  * Overview:    Check whether driver should declare capability of receiving
355  *              MCS15
356  *
357  * Input:
358  *                      PADAPTER                Adapter,
359  *
360  * Output:              None
361  * Return:      true if driver should disable MCS15
362  * 2008.04.15   Emily
363  */
364 static bool HTIOTActIsDisableMCS15(struct ieee80211_device *ieee)
365 {
366         bool retValue = false;
367
368 #ifdef TODO
369         // Apply for 819u only
370 #if (HAL_CODE_BASE == RTL8192)
371
372 #if (DEV_BUS_TYPE == USB_INTERFACE)
373         // Alway disable MCS15 by Jerry Chang's request.by Emily, 2008.04.15
374         retValue = true;
375 #elif (DEV_BUS_TYPE == PCI_INTERFACE)
376         // Enable MCS15 if the peer is Cisco AP. by Emily, 2008.05.12
377 //      if(pBssDesc->bCiscoCapExist)
378 //              retValue = false;
379 //      else
380                 retValue = false;
381 #endif
382 #endif
383 #endif
384         // Jerry Chang suggest that 8190 1x2 does not need to disable MCS15
385
386         return retValue;
387 }
388
389 /*
390  * Function:    HTIOTActIsDisableMCSTwoSpatialStream
391  *
392  * Overview:    Check whether driver should declare capability of receiving
393  *              All 2 ss packets
394  *
395  * Input:
396  *                      PADAPTER                Adapter,
397  *
398  * Output:              None
399  * Return:      true if driver should disable all two spatial stream packet
400  * 2008.04.21   Emily
401  */
402 static bool HTIOTActIsDisableMCSTwoSpatialStream(struct ieee80211_device *ieee,
403                                                  u8 *PeerMacAddr)
404 {
405 #ifdef TODO
406         // Apply for 819u only
407 #endif
408         return false;
409 }
410
411 /*
412  *function:  Check whether driver should disable EDCA turbo mode
413  *   input:  struct ieee80211_device*   ieee
414  *           u8*                        PeerMacAddr
415  *  output:  none
416  *  return:  return 1 if driver should disable EDCA turbo mode
417  *           (otherwise return 0)
418  */
419 static u8 HTIOTActIsDisableEDCATurbo(struct ieee80211_device *ieee,
420                                      u8 *PeerMacAddr)
421 {       /* default enable EDCA Turbo mode. */
422         return false;
423 }
424
425 /*
426  *function:  Check whether we need to use OFDM to sned MGNT frame for
427  *           broadcom AP
428  *   input:  struct ieee80211_network *network   //current network we live
429  *  output:  none
430  *  return:  return 1 if true
431  */
432 static u8 HTIOTActIsMgntUseCCK6M(struct ieee80211_network *network)
433 {
434         u8      retValue = 0;
435
436         // 2008/01/25 MH Judeg if we need to use OFDM to sned MGNT frame for broadcom AP.
437         // 2008/01/28 MH We must prevent that we select null bssid to link.
438
439         if (network->broadcom_cap_exist)
440                 retValue = 1;
441
442         return retValue;
443 }
444
445 static u8 HTIOTActIsCCDFsync(u8 *PeerMacAddr)
446 {
447         u8      retValue = 0;
448
449         if ((memcmp(PeerMacAddr, UNKNOWN_BORADCOM, 3) == 0) ||
450             (memcmp(PeerMacAddr, LINKSYSWRT330_LINKSYSWRT300_BROADCOM, 3) == 0) ||
451             (memcmp(PeerMacAddr, LINKSYSWRT350_LINKSYSWRT150_BROADCOM, 3) == 0))
452                 retValue = 1;
453
454         return retValue;
455 }
456
457 void HTResetIOTSetting(PRT_HIGH_THROUGHPUT pHTInfo)
458 {
459         pHTInfo->IOTAction = 0;
460         pHTInfo->IOTPeer = HT_IOT_PEER_UNKNOWN;
461 }
462
463 /*
464  *function:  Construct Capablility Element in Beacon... if HTEnable is turned on
465  *   input:  struct ieee80211_device*   ieee
466  *           u8*                     posHTCap //pointer to store Capability Ele
467  *           u8*                     len //store length of CE
468  *           u8                      IsEncrypt //whether encrypt, needed further
469  *  output:  none
470  *  return:  none
471  *  notice:  posHTCap can't be null and should be initialized before.
472  */
473 void HTConstructCapabilityElement(struct ieee80211_device *ieee, u8 *posHTCap, u8 *len, u8 IsEncrypt)
474 {
475         PRT_HIGH_THROUGHPUT     pHT = ieee->pHTInfo;
476         struct ht_capability_ele   *pCapELE = NULL;
477         //u8 bIsDeclareMCS13;
478
479         if (!posHTCap || !pHT) {
480                 IEEE80211_DEBUG(IEEE80211_DL_ERR,
481                                 "posHTCap or pHTInfo can't be null in %s\n",
482                                 __func__);
483                 return;
484         }
485         memset(posHTCap, 0, *len);
486         if (pHT->ePeerHTSpecVer == HT_SPEC_VER_EWC) {
487                 u8      EWC11NHTCap[] = {0x00, 0x90, 0x4c, 0x33};       // For 11n EWC definition, 2007.07.17, by Emily
488
489                 memcpy(posHTCap, EWC11NHTCap, sizeof(EWC11NHTCap));
490                 pCapELE = (struct ht_capability_ele *)&posHTCap[4];
491         } else {
492                 pCapELE = (struct ht_capability_ele *)posHTCap;
493         }
494
495         //HT capability info
496         pCapELE->AdvCoding              = 0; // This feature is not supported now!!
497         if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
498                 pCapELE->ChlWidth = 0;
499         else
500                 pCapELE->ChlWidth = (pHT->bRegBW40MHz ? 1 : 0);
501
502 //      pCapELE->ChlWidth               = (pHT->bRegBW40MHz?1:0);
503         pCapELE->MimoPwrSave            = pHT->SelfMimoPs;
504         pCapELE->GreenField             = 0; // This feature is not supported now!!
505         pCapELE->ShortGI20Mhz           = 1; // We can receive Short GI!!
506         pCapELE->ShortGI40Mhz           = 1; // We can receive Short GI!!
507         //DbgPrint("TX HT cap/info ele BW=%d SG20=%d SG40=%d\n\r",
508         //pCapELE->ChlWidth, pCapELE->ShortGI20Mhz, pCapELE->ShortGI40Mhz);
509         pCapELE->TxSTBC                 = 1;
510         pCapELE->RxSTBC                 = 0;
511         pCapELE->DelayBA                = 0;    // Do not support now!!
512         pCapELE->MaxAMSDUSize           = (MAX_RECEIVE_BUFFER_SIZE >= 7935) ? 1 : 0;
513         pCapELE->DssCCk                 = ((pHT->bRegBW40MHz) ? (pHT->bRegSuppCCK ? 1 : 0) : 0);
514         pCapELE->PSMP                   = 0; // Do not support now!!
515         pCapELE->LSigTxopProtect        = 0; // Do not support now!!
516
517         /*
518          * MAC HT parameters info
519          * TODO: Nedd to take care of this part
520          */
521         IEEE80211_DEBUG(IEEE80211_DL_HT, "TX HT cap/info ele BW=%d MaxAMSDUSize:%d DssCCk:%d\n", pCapELE->ChlWidth, pCapELE->MaxAMSDUSize, pCapELE->DssCCk);
522
523         if (IsEncrypt) {
524                 pCapELE->MPDUDensity    = 7; // 8us
525                 pCapELE->MaxRxAMPDUFactor = 2; // 2 is for 32 K and 3 is 64K
526         } else {
527                 pCapELE->MaxRxAMPDUFactor = 3; // 2 is for 32 K and 3 is 64K
528                 pCapELE->MPDUDensity    = 0; // no density
529         }
530
531         //Supported MCS set
532         memcpy(pCapELE->MCS, ieee->Regdot11HTOperationalRateSet, 16);
533         if (pHT->IOTAction & HT_IOT_ACT_DISABLE_MCS15)
534                 pCapELE->MCS[1] &= 0x7f;
535
536         if (pHT->IOTAction & HT_IOT_ACT_DISABLE_MCS14)
537                 pCapELE->MCS[1] &= 0xbf;
538
539         if (pHT->IOTAction & HT_IOT_ACT_DISABLE_ALL_2SS)
540                 pCapELE->MCS[1] &= 0x00;
541
542         /*
543          * 2008.06.12
544          * For RTL819X, if pairwisekey = wep/tkip, ap is ralink, we support only MCS0~7.
545          */
546         if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev)) {
547                 int i;
548
549                 for (i = 1; i < 16; i++)
550                         pCapELE->MCS[i] = 0;
551         }
552
553         //Extended HT Capability Info
554         memset(&pCapELE->ExtHTCapInfo, 0, 2);
555
556         //TXBF Capabilities
557         memset(pCapELE->TxBFCap, 0, 4);
558
559         //Antenna Selection Capabilities
560         pCapELE->ASCap = 0;
561 //add 2 to give space for element ID and len when construct frames
562         if (pHT->ePeerHTSpecVer == HT_SPEC_VER_EWC)
563                 *len = 30 + 2;
564         else
565                 *len = 26 + 2;
566
567 //      IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA | IEEE80211_DL_HT, posHTCap, *len -2);
568
569         /*
570          * Print each field in detail. Driver should not print out this message
571          * by default
572          */
573 //      HTDebugHTCapability(posHTCap, (u8*)"HTConstructCapability()");
574 }
575
576 /*
577  *function:  Construct Information Element in Beacon... if HTEnable is turned on
578  *   input:  struct ieee80211_device*   ieee
579  *           u8*                     posHTCap //pointer to store Information Ele
580  *           u8*                     len   //store len of
581  *           u8                      IsEncrypt //whether encrypt, needed further
582  *  output:  none
583  *  return:  none
584  *  notice:  posHTCap can't be null and be initialized before.
585  *           Only AP and IBSS sta should do this
586  */
587 void HTConstructInfoElement(struct ieee80211_device *ieee, u8 *posHTInfo, u8 *len, u8 IsEncrypt)
588 {
589         PRT_HIGH_THROUGHPUT     pHT = ieee->pHTInfo;
590         PHT_INFORMATION_ELE             pHTInfoEle = (PHT_INFORMATION_ELE)posHTInfo;
591
592         if (!posHTInfo || !pHTInfoEle) {
593                 IEEE80211_DEBUG(IEEE80211_DL_ERR,
594                                 "posHTInfo or pHTInfoEle can't be null in %s\n",
595                                 __func__);
596                 return;
597         }
598
599         memset(posHTInfo, 0, *len);
600         if ((ieee->iw_mode == IW_MODE_ADHOC) || (ieee->iw_mode == IW_MODE_MASTER)) { //ap mode is not currently supported
601                 pHTInfoEle->ControlChl                  = ieee->current_network.channel;
602                 pHTInfoEle->ExtChlOffset                = ((!pHT->bRegBW40MHz) ? HT_EXTCHNL_OFFSET_NO_EXT :
603                                                                                         (ieee->current_network.channel <= 6) ?
604                                                                                                 HT_EXTCHNL_OFFSET_UPPER : HT_EXTCHNL_OFFSET_LOWER);
605                 pHTInfoEle->RecommemdedTxWidth  = pHT->bRegBW40MHz;
606                 pHTInfoEle->RIFS                                        = 0;
607                 pHTInfoEle->PSMPAccessOnly              = 0;
608                 pHTInfoEle->SrvIntGranularity           = 0;
609                 pHTInfoEle->OptMode                             = pHT->CurrentOpMode;
610                 pHTInfoEle->NonGFDevPresent             = 0;
611                 pHTInfoEle->DualBeacon                  = 0;
612                 pHTInfoEle->SecondaryBeacon             = 0;
613                 pHTInfoEle->LSigTxopProtectFull         = 0;
614                 pHTInfoEle->PcoActive                           = 0;
615                 pHTInfoEle->PcoPhase                            = 0;
616
617                 memset(pHTInfoEle->BasicMSC, 0, 16);
618
619                 *len = 22 + 2; //same above
620         } else {
621                 //STA should not generate High Throughput Information Element
622                 *len = 0;
623         }
624         //IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA | IEEE80211_DL_HT, posHTInfo, *len - 2);
625         //HTDebugHTInfo(posHTInfo, "HTConstructInforElement");
626 }
627
628 /*
629  * According to experiment, Realtek AP to STA (based on rtl8190) may achieve
630  * best performance if both STA and AP set limitation of aggregation size to
631  * 32K, that is, set AMPDU density to 2 (Ref: IEEE 11n specification).
632  * However, if Realtek STA associates to other AP, STA should set limitation of
633  * aggregation size to 8K, otherwise, performance of traffic stream from STA to
634  * AP will be much less than the traffic stream from AP to STA if both of the
635  * stream runs concurrently at the same time.
636  *
637  *  Frame Format
638  *  Element ID          Length          OUI             Type1           Reserved
639  *  1 byte              1 byte          3 bytes         1 byte          1 byte
640  *
641  *  OUI         = 0x00, 0xe0, 0x4c,
642  *  Type        = 0x02
643  *  Reserved    = 0x00
644  *
645  *  2007.8.21 by Emily
646  */
647 /*
648  *function:  Construct  Information Element in Beacon... in RT2RT condition
649  *   input:  struct ieee80211_device*   ieee
650  *           u8*                  posRT2RTAgg //pointer to store Information Ele
651  *           u8*                  len   //store len
652  *  output:  none
653  *  return:  none
654  *  notice:
655  */
656 void HTConstructRT2RTAggElement(struct ieee80211_device *ieee, u8 *posRT2RTAgg, u8 *len)
657 {
658         if (!posRT2RTAgg) {
659                 IEEE80211_DEBUG(IEEE80211_DL_ERR,
660                                 "posRT2RTAgg can't be null in %s\n",
661                                 __func__);
662                 return;
663         }
664         memset(posRT2RTAgg, 0, *len);
665         *posRT2RTAgg++ = 0x00;
666         *posRT2RTAgg++ = 0xe0;
667         *posRT2RTAgg++ = 0x4c;
668         *posRT2RTAgg++ = 0x02;
669         *posRT2RTAgg++ = 0x01;
670         *posRT2RTAgg = 0x10;//*posRT2RTAgg = 0x02;
671
672         if (ieee->bSupportRemoteWakeUp)
673                 *posRT2RTAgg |= 0x08;//RT_HT_CAP_USE_WOW;
674
675         *len = 6 + 2;
676         return;
677 #ifdef TODO
678 #if (HAL_CODE_BASE == RTL8192 && DEV_BUS_TYPE == USB_INTERFACE)
679         /*
680         //Emily. If it is required to Ask Realtek AP to send AMPDU during AES mode, enable this
681            section of code.
682         if(IS_UNDER_11N_AES_MODE(Adapter))
683         {
684                 posRT2RTAgg->octet[5] |= RT_HT_CAP_USE_AMPDU;
685         }else
686         {
687                 posRT2RTAgg->octet[5] &= 0xfb;
688         }
689         */
690 #else
691         // Do Nothing
692 #endif
693
694         posRT2RTAgg->Length = 6;
695 #endif
696 }
697
698 /*
699  *function:  Pick the right Rate Adaptive table to use
700  *   input:  struct ieee80211_device*   ieee
701  *           u8*                      pOperateMCS //A pointer to MCS rate bitmap
702  *  return:  always we return true
703  *  notice:
704  */
705 static u8 HT_PickMCSRate(struct ieee80211_device *ieee, u8 *pOperateMCS)
706 {
707         if (!pOperateMCS) {
708                 IEEE80211_DEBUG(IEEE80211_DL_ERR,
709                                 "pOperateMCS can't be null in %s\n",
710                                 __func__);
711                 return false;
712         }
713
714         switch (ieee->mode) {
715         case IEEE_A:
716         case IEEE_B:
717         case IEEE_G:
718                 //legacy rate routine handled at selectedrate
719
720                 //no MCS rate
721                 memset(pOperateMCS, 0, 16);
722                 break;
723
724         case IEEE_N_24G:        //assume CCK rate ok
725         case IEEE_N_5G:
726                 // Legacy part we only use 6, 5.5,2,1 for N_24G and 6 for N_5G.
727                 // Legacy part shall be handled at SelectRateSet().
728
729                 //HT part
730                 // TODO: may be different if we have different number of antenna
731                 pOperateMCS[0] &= RATE_ADPT_1SS_MASK;   //support MCS 0~7
732                 pOperateMCS[1] &= RATE_ADPT_2SS_MASK;
733                 pOperateMCS[3] &= RATE_ADPT_MCS32_MASK;
734                 break;
735
736         //should never reach here
737         default:
738                 break;
739         }
740
741         return true;
742 }
743
744 /*
745  *      Description:
746  *              This function will get the highest speed rate in input MCS set.
747  *
748  *      /param  Adapter                 Pionter to Adapter entity
749  *                      pMCSRateSet             Pointer to MCS rate bitmap
750  *                      pMCSFilter              Pointer to MCS rate filter
751  *
752  *      /return Highest MCS rate included in pMCSRateSet and filtered by pMCSFilter.
753  *
754  */
755 /*
756  *function:  This function will get the highest speed rate in input MCS set.
757  *   input:  struct ieee80211_device*   ieee
758  *           u8*                        pMCSRateSet //Pointer to MCS rate bitmap
759  *           u8*                        pMCSFilter //Pointer to MCS rate filter
760  *  return:  Highest MCS rate included in pMCSRateSet and filtered by pMCSFilter
761  *  notice:
762  */
763 u8 HTGetHighestMCSRate(struct ieee80211_device *ieee, u8 *pMCSRateSet, u8 *pMCSFilter)
764 {
765         u8              i, j;
766         u8              bitMap;
767         u8              mcsRate = 0;
768         u8              availableMcsRate[16];
769
770         if (!pMCSRateSet || !pMCSFilter) {
771                 IEEE80211_DEBUG(IEEE80211_DL_ERR,
772                                 "pMCSRateSet or pMCSFilter can't be null in %s\n",
773                                 __func__);
774                 return false;
775         }
776         for (i = 0; i < 16; i++)
777                 availableMcsRate[i] = pMCSRateSet[i] & pMCSFilter[i];
778
779         for (i = 0; i < 16; i++) {
780                 if (availableMcsRate[i] != 0)
781                         break;
782         }
783         if (i == 16)
784                 return false;
785
786         for (i = 0; i < 16; i++) {
787                 if (availableMcsRate[i] != 0) {
788                         bitMap = availableMcsRate[i];
789                         for (j = 0; j < 8; j++) {
790                                 if ((bitMap % 2) != 0) {
791                                         if (HTMcsToDataRate(ieee, (8 * i + j)) > HTMcsToDataRate(ieee, mcsRate))
792                                                 mcsRate = (8 * i + j);
793                                 }
794                                 bitMap >>= 1;
795                         }
796                 }
797         }
798         return (mcsRate | 0x80);
799 }
800
801 /*
802  * 1.Filter our operation rate set with AP's rate set
803  * 2.shall reference channel bandwidth, STBC, Antenna number
804  * 3.generate rate adative table for firmware
805  * David 20060906
806  *
807  * \pHTSupportedCap: the connected STA's supported rate Capability element
808  */
809 static u8 HTFilterMCSRate(struct ieee80211_device *ieee, u8 *pSupportMCS,
810                           u8 *pOperateMCS)
811 {
812         u8 i = 0;
813
814         // filter out operational rate set not supported by AP, the length of it is 16
815         for (i = 0; i <= 15; i++)
816                 pOperateMCS[i] = ieee->Regdot11HTOperationalRateSet[i] & pSupportMCS[i];
817
818         // TODO: adjust our operational rate set  according to our channel bandwidth, STBC and Antenna number
819         /*
820          * TODO: fill suggested rate adaptive rate index and give firmware info
821          * using Tx command packet we also shall suggested the first start rate
822          * set according to our signal strength
823          */
824         HT_PickMCSRate(ieee, pOperateMCS);
825
826         // For RTL819X, if pairwisekey = wep/tkip, we support only MCS0~7.
827         if (ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))
828                 pOperateMCS[1] = 0;
829
830         /*
831          * For RTL819X, we support only MCS0~15.
832          * And also, we do not know how to use MCS32 now.
833          */
834         for (i = 2; i <= 15; i++)
835                 pOperateMCS[i] = 0;
836
837         return true;
838 }
839
840 void HTOnAssocRsp(struct ieee80211_device *ieee)
841 {
842         PRT_HIGH_THROUGHPUT     pHTInfo = ieee->pHTInfo;
843         struct ht_capability_ele       *pPeerHTCap = NULL;
844         PHT_INFORMATION_ELE             pPeerHTInfo = NULL;
845         u16     nMaxAMSDUSize = 0;
846         u8      *pMcsFilter = NULL;
847
848         static u8                               EWC11NHTCap[] = {0x00, 0x90, 0x4c, 0x33};               // For 11n EWC definition, 2007.07.17, by Emily
849         static u8                               EWC11NHTInfo[] = {0x00, 0x90, 0x4c, 0x34};      // For 11n EWC definition, 2007.07.17, by Emily
850
851         if (!pHTInfo->bCurrentHTSupport) {
852                 IEEE80211_DEBUG(IEEE80211_DL_ERR,
853                                 "<=== %s: HT_DISABLE\n",
854                                 __func__);
855                 return;
856         }
857         IEEE80211_DEBUG(IEEE80211_DL_HT, "===> HTOnAssocRsp_wq(): HT_ENABLE\n");
858 //      IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA, pHTInfo->PeerHTCapBuf, sizeof(struct ht_capability_ele));
859 //      IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA, pHTInfo->PeerHTInfoBuf, sizeof(HT_INFORMATION_ELE));
860
861 //      HTDebugHTCapability(pHTInfo->PeerHTCapBuf,"HTOnAssocRsp_wq");
862 //      HTDebugHTInfo(pHTInfo->PeerHTInfoBuf,"HTOnAssocRsp_wq");
863         //
864         if (!memcmp(pHTInfo->PeerHTCapBuf, EWC11NHTCap, sizeof(EWC11NHTCap)))
865                 pPeerHTCap = (struct ht_capability_ele *)(&pHTInfo->PeerHTCapBuf[4]);
866         else
867                 pPeerHTCap = (struct ht_capability_ele *)(pHTInfo->PeerHTCapBuf);
868
869         if (!memcmp(pHTInfo->PeerHTInfoBuf, EWC11NHTInfo, sizeof(EWC11NHTInfo)))
870                 pPeerHTInfo = (PHT_INFORMATION_ELE)(&pHTInfo->PeerHTInfoBuf[4]);
871         else
872                 pPeerHTInfo = (PHT_INFORMATION_ELE)(pHTInfo->PeerHTInfoBuf);
873
874         ////////////////////////////////////////////////////////
875         // Configurations:
876         ////////////////////////////////////////////////////////
877         IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA | IEEE80211_DL_HT, pPeerHTCap, sizeof(struct ht_capability_ele));
878 //      IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA|IEEE80211_DL_HT, pPeerHTInfo, sizeof(HT_INFORMATION_ELE));
879         // Config Supported Channel Width setting
880         //
881         HTSetConnectBwMode(ieee, (enum ht_channel_width)(pPeerHTCap->ChlWidth), (enum ht_extension_chan_offset)(pPeerHTInfo->ExtChlOffset));
882
883         pHTInfo->bCurTxBW40MHz = (pPeerHTInfo->RecommemdedTxWidth == 1);
884
885         /*
886          * Update short GI/ long GI setting
887          *
888          * TODO:
889          */
890         pHTInfo->bCurShortGI20MHz = pHTInfo->bRegShortGI20MHz &&
891                                     (pPeerHTCap->ShortGI20Mhz == 1);
892         pHTInfo->bCurShortGI40MHz = pHTInfo->bRegShortGI40MHz &&
893                                    (pPeerHTCap->ShortGI40Mhz == 1);
894
895         /*
896          * Config TX STBC setting
897          *
898          * TODO:
899          */
900
901         /*
902          * Config DSSS/CCK  mode in 40MHz mode
903          *
904          * TODO:
905          */
906         pHTInfo->bCurSuppCCK = pHTInfo->bRegSuppCCK &&
907                                (pPeerHTCap->DssCCk == 1);
908
909         /*
910          * Config and configure A-MSDU setting
911          */
912         pHTInfo->bCurrent_AMSDU_Support = pHTInfo->bAMSDU_Support;
913
914         nMaxAMSDUSize = (pPeerHTCap->MaxAMSDUSize == 0) ? 3839 : 7935;
915
916         if (pHTInfo->nAMSDU_MaxSize > nMaxAMSDUSize)
917                 pHTInfo->nCurrent_AMSDU_MaxSize = nMaxAMSDUSize;
918         else
919                 pHTInfo->nCurrent_AMSDU_MaxSize = pHTInfo->nAMSDU_MaxSize;
920         /*
921          * Config A-MPDU setting
922          */
923         pHTInfo->bCurrentAMPDUEnable = pHTInfo->bAMPDUEnable;
924
925         /*
926          * <1> Decide AMPDU Factor
927          * By Emily
928          */
929         if (!pHTInfo->bRegRT2RTAggregation) {
930                 // Decide AMPDU Factor according to protocol handshake
931                 if (pHTInfo->AMPDU_Factor > pPeerHTCap->MaxRxAMPDUFactor)
932                         pHTInfo->CurrentAMPDUFactor = pPeerHTCap->MaxRxAMPDUFactor;
933                 else
934                         pHTInfo->CurrentAMPDUFactor = pHTInfo->AMPDU_Factor;
935         } else {
936                 /*
937                  * Set MPDU density to 2 to Realtek AP, and set it to 0 for others
938                  * Replace MPDU factor declared in original association response frame format. 2007.08.20 by Emily
939                  */
940                 if (ieee->current_network.bssht.bdRT2RTAggregation) {
941                         if (ieee->pairwise_key_type != KEY_TYPE_NA)
942                                 // Realtek may set 32k in security mode and 64k for others
943                                 pHTInfo->CurrentAMPDUFactor = pPeerHTCap->MaxRxAMPDUFactor;
944                         else
945                                 pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_64K;
946                 } else {
947                         if (pPeerHTCap->MaxRxAMPDUFactor < HT_AGG_SIZE_32K)
948                                 pHTInfo->CurrentAMPDUFactor = pPeerHTCap->MaxRxAMPDUFactor;
949                         else
950                                 pHTInfo->CurrentAMPDUFactor = HT_AGG_SIZE_32K;
951                 }
952         }
953
954         /*
955          * <2> Set AMPDU Minimum MPDU Start Spacing
956          * 802.11n 3.0 section 9.7d.3
957          */
958         if (pHTInfo->MPDU_Density > pPeerHTCap->MPDUDensity)
959                 pHTInfo->CurrentMPDUDensity = pHTInfo->MPDU_Density;
960         else
961                 pHTInfo->CurrentMPDUDensity = pPeerHTCap->MPDUDensity;
962         if (ieee->pairwise_key_type != KEY_TYPE_NA)
963                 pHTInfo->CurrentMPDUDensity     = 7; // 8us
964         // Force TX AMSDU
965
966         // Lanhsin: mark for tmp to avoid deauth by ap from  s3
967         //if(memcmp(pMgntInfo->Bssid, NETGEAR834Bv2_BROADCOM, 3)==0)
968         if (0) {
969                 pHTInfo->bCurrentAMPDUEnable = false;
970                 pHTInfo->ForcedAMSDUMode = HT_AGG_FORCE_ENABLE;
971                 pHTInfo->ForcedAMSDUMaxSize = 7935;
972
973                 pHTInfo->IOTAction |=  HT_IOT_ACT_TX_USE_AMSDU_8K;
974         }
975
976         // Rx Reorder Setting
977         pHTInfo->bCurRxReorderEnable = pHTInfo->bRegRxReorderEnable;
978
979         /*
980          * Filter out unsupported HT rate for this AP
981          * Update RATR table
982          * This is only for 8190 ,8192 or later product which using firmware to
983          * handle rate adaptive mechanism.
984          */
985
986         /*
987          * Handle Ralink AP bad MCS rate set condition. Joseph.
988          * This fix the bug of Ralink AP. This may be removed in the future.
989          */
990         if (pPeerHTCap->MCS[0] == 0)
991                 pPeerHTCap->MCS[0] = 0xff;
992
993         HTFilterMCSRate(ieee, pPeerHTCap->MCS, ieee->dot11HTOperationalRateSet);
994
995         /*
996          * Config MIMO Power Save setting
997          */
998         pHTInfo->PeerMimoPs = pPeerHTCap->MimoPwrSave;
999         if (pHTInfo->PeerMimoPs == MIMO_PS_STATIC)
1000                 pMcsFilter = MCS_FILTER_1SS;
1001         else
1002                 pMcsFilter = MCS_FILTER_ALL;
1003         //WB add for MCS8 bug
1004 //      pMcsFilter = MCS_FILTER_1SS;
1005         ieee->HTHighestOperaRate = HTGetHighestMCSRate(ieee, ieee->dot11HTOperationalRateSet, pMcsFilter);
1006         ieee->HTCurrentOperaRate = ieee->HTHighestOperaRate;
1007
1008         /*
1009          * Config current operation mode.
1010          */
1011         pHTInfo->CurrentOpMode = pPeerHTInfo->OptMode;
1012 }
1013
1014 /*
1015  *function:  initialize HT info(struct PRT_HIGH_THROUGHPUT)
1016  *   input:  struct ieee80211_device*   ieee
1017  *  output:  none
1018  *  return:  none
1019  *  notice: This function is called when
1020  *                                  *  (1) MPInitialization Phase
1021  *                                  *  (2) Receiving of Deauthentication from AP
1022  */
1023 // TODO: Should this funciton be called when receiving of Disassociation?
1024 void HTInitializeHTInfo(struct ieee80211_device *ieee)
1025 {
1026         PRT_HIGH_THROUGHPUT pHTInfo = ieee->pHTInfo;
1027
1028         /*
1029          * These parameters will be reset when receiving deauthentication packet
1030          */
1031         IEEE80211_DEBUG(IEEE80211_DL_HT, "===========>%s()\n", __func__);
1032         pHTInfo->bCurrentHTSupport = false;
1033
1034         // 40MHz channel support
1035         pHTInfo->bCurBW40MHz = false;
1036         pHTInfo->bCurTxBW40MHz = false;
1037
1038         // Short GI support
1039         pHTInfo->bCurShortGI20MHz = false;
1040         pHTInfo->bCurShortGI40MHz = false;
1041         pHTInfo->bForcedShortGI = false;
1042
1043         /*
1044          * CCK rate support
1045          * This flag is set to true to support CCK rate by default.
1046          * It will be affected by "pHTInfo->bRegSuppCCK" and AP capabilities
1047          * only when associate to 11N BSS.
1048          */
1049         pHTInfo->bCurSuppCCK = true;
1050
1051         // AMSDU related
1052         pHTInfo->bCurrent_AMSDU_Support = false;
1053         pHTInfo->nCurrent_AMSDU_MaxSize = pHTInfo->nAMSDU_MaxSize;
1054
1055         // AMPUD related
1056         pHTInfo->CurrentMPDUDensity = pHTInfo->MPDU_Density;
1057         pHTInfo->CurrentAMPDUFactor = pHTInfo->AMPDU_Factor;
1058
1059         // Initialize all of the parameters related to 11n
1060         memset(&pHTInfo->SelfHTCap, 0, sizeof(pHTInfo->SelfHTCap));
1061         memset(&pHTInfo->SelfHTInfo, 0, sizeof(pHTInfo->SelfHTInfo));
1062         memset(&pHTInfo->PeerHTCapBuf, 0, sizeof(pHTInfo->PeerHTCapBuf));
1063         memset(&pHTInfo->PeerHTInfoBuf, 0, sizeof(pHTInfo->PeerHTInfoBuf));
1064
1065         pHTInfo->bSwBwInProgress = false;
1066
1067         // Set default IEEE spec for Draft N
1068         pHTInfo->ePeerHTSpecVer = HT_SPEC_VER_IEEE;
1069
1070         // Realtek proprietary aggregation mode
1071         pHTInfo->bCurrentRT2RTAggregation = false;
1072         pHTInfo->bCurrentRT2RTLongSlotTime = false;
1073         pHTInfo->IOTPeer = 0;
1074         pHTInfo->IOTAction = 0;
1075
1076         //MCS rate initialized here
1077         {
1078                 u8 *RegHTSuppRateSets = &ieee->RegHTSuppRateSet[0];
1079
1080                 RegHTSuppRateSets[0] = 0xFF;    //support MCS 0~7
1081                 RegHTSuppRateSets[1] = 0xFF;    //support MCS 8~15
1082                 RegHTSuppRateSets[4] = 0x01;    //support MCS 32
1083         }
1084 }
1085
1086 /*
1087  *function:  initialize Bss HT structure(struct PBSS_HT)
1088  *   input:  PBSS_HT pBssHT //to be initialized
1089  *  output:  none
1090  *  return:  none
1091  *  notice: This function is called when initialize network structure
1092  */
1093 void HTInitializeBssDesc(PBSS_HT pBssHT)
1094 {
1095         pBssHT->bdSupportHT = false;
1096         memset(pBssHT->bdHTCapBuf, 0, sizeof(pBssHT->bdHTCapBuf));
1097         pBssHT->bdHTCapLen = 0;
1098         memset(pBssHT->bdHTInfoBuf, 0, sizeof(pBssHT->bdHTInfoBuf));
1099         pBssHT->bdHTInfoLen = 0;
1100
1101         pBssHT->bdHTSpecVer = HT_SPEC_VER_IEEE;
1102
1103         pBssHT->bdRT2RTAggregation = false;
1104         pBssHT->bdRT2RTLongSlotTime = false;
1105 }
1106
1107 /*
1108  *function:  initialize Bss HT structure(struct PBSS_HT)
1109  *   input:  struct ieee80211_device    *ieee
1110  *           struct ieee80211_network   *pNetwork //usually current network
1111  *                                                  we are live in
1112  *  output:  none
1113  *  return:  none
1114  *  notice: This function should ONLY be called before association
1115  */
1116 void HTResetSelfAndSavePeerSetting(struct ieee80211_device *ieee,       struct ieee80211_network *pNetwork)
1117 {
1118         PRT_HIGH_THROUGHPUT             pHTInfo = ieee->pHTInfo;
1119 //      u16                                             nMaxAMSDUSize;
1120 //      struct ht_capability_ele       *pPeerHTCap = (struct ht_capability_ele *)pNetwork->bssht.bdHTCapBuf;
1121 //      PHT_INFORMATION_ELE             pPeerHTInfo = (PHT_INFORMATION_ELE)pNetwork->bssht.bdHTInfoBuf;
1122 //      u8*     pMcsFilter;
1123         u8      bIOTAction = 0;
1124
1125         //
1126         //  Save Peer Setting before Association
1127         //
1128         IEEE80211_DEBUG(IEEE80211_DL_HT, "==============>%s()\n", __func__);
1129         /*unmark bEnableHT flag here is the same reason why unmarked in function ieee80211_softmac_new_net. WB 2008.09.10*/
1130 //      if( pHTInfo->bEnableHT &&  pNetwork->bssht.bdSupportHT)
1131         if (pNetwork->bssht.bdSupportHT) {
1132                 pHTInfo->bCurrentHTSupport = true;
1133                 pHTInfo->ePeerHTSpecVer = pNetwork->bssht.bdHTSpecVer;
1134
1135                 // Save HTCap and HTInfo information Element
1136                 if (pNetwork->bssht.bdHTCapLen > 0 &&   pNetwork->bssht.bdHTCapLen <= sizeof(pHTInfo->PeerHTCapBuf))
1137                         memcpy(pHTInfo->PeerHTCapBuf, pNetwork->bssht.bdHTCapBuf, pNetwork->bssht.bdHTCapLen);
1138
1139                 if (pNetwork->bssht.bdHTInfoLen > 0 && pNetwork->bssht.bdHTInfoLen <= sizeof(pHTInfo->PeerHTInfoBuf))
1140                         memcpy(pHTInfo->PeerHTInfoBuf, pNetwork->bssht.bdHTInfoBuf, pNetwork->bssht.bdHTInfoLen);
1141
1142                 // Check whether RT to RT aggregation mode is enabled
1143                 if (pHTInfo->bRegRT2RTAggregation) {
1144                         pHTInfo->bCurrentRT2RTAggregation = pNetwork->bssht.bdRT2RTAggregation;
1145                         pHTInfo->bCurrentRT2RTLongSlotTime = pNetwork->bssht.bdRT2RTLongSlotTime;
1146                 } else {
1147                         pHTInfo->bCurrentRT2RTAggregation = false;
1148                         pHTInfo->bCurrentRT2RTLongSlotTime = false;
1149                 }
1150
1151                 // Determine the IOT Peer Vendor.
1152                 HTIOTPeerDetermine(ieee);
1153
1154                 /*
1155                  * Decide IOT Action
1156                  * Must be called after the parameter of pHTInfo->bCurrentRT2RTAggregation is decided
1157                  */
1158                 pHTInfo->IOTAction = 0;
1159                 bIOTAction = HTIOTActIsDisableMCS14(ieee, pNetwork->bssid);
1160                 if (bIOTAction)
1161                         pHTInfo->IOTAction |= HT_IOT_ACT_DISABLE_MCS14;
1162
1163                 bIOTAction = HTIOTActIsDisableMCS15(ieee);
1164                 if (bIOTAction)
1165                         pHTInfo->IOTAction |= HT_IOT_ACT_DISABLE_MCS15;
1166
1167                 bIOTAction = HTIOTActIsDisableMCSTwoSpatialStream(ieee, pNetwork->bssid);
1168                 if (bIOTAction)
1169                         pHTInfo->IOTAction |= HT_IOT_ACT_DISABLE_ALL_2SS;
1170
1171                 bIOTAction = HTIOTActIsDisableEDCATurbo(ieee, pNetwork->bssid);
1172                 if (bIOTAction)
1173                         pHTInfo->IOTAction |= HT_IOT_ACT_DISABLE_EDCA_TURBO;
1174
1175                 bIOTAction = HTIOTActIsMgntUseCCK6M(pNetwork);
1176                 if (bIOTAction)
1177                         pHTInfo->IOTAction |= HT_IOT_ACT_MGNT_USE_CCK_6M;
1178
1179                 bIOTAction = HTIOTActIsCCDFsync(pNetwork->bssid);
1180                 if (bIOTAction)
1181                         pHTInfo->IOTAction |= HT_IOT_ACT_CDD_FSYNC;
1182         } else {
1183                 pHTInfo->bCurrentHTSupport = false;
1184                 pHTInfo->bCurrentRT2RTAggregation = false;
1185                 pHTInfo->bCurrentRT2RTLongSlotTime = false;
1186
1187                 pHTInfo->IOTAction = 0;
1188         }
1189 }
1190
1191 void HTUpdateSelfAndPeerSetting(struct ieee80211_device *ieee,  struct ieee80211_network *pNetwork)
1192 {
1193         PRT_HIGH_THROUGHPUT             pHTInfo = ieee->pHTInfo;
1194 //      struct ht_capability_ele       *pPeerHTCap = (struct ht_capability_ele *)pNetwork->bssht.bdHTCapBuf;
1195         PHT_INFORMATION_ELE             pPeerHTInfo = (PHT_INFORMATION_ELE)pNetwork->bssht.bdHTInfoBuf;
1196
1197         if (pHTInfo->bCurrentHTSupport) {
1198                 /*
1199                  * Config current operation mode.
1200                  */
1201                 if (pNetwork->bssht.bdHTInfoLen != 0)
1202                         pHTInfo->CurrentOpMode = pPeerHTInfo->OptMode;
1203
1204                 /*
1205                  * <TODO: Config according to OBSS non-HT STA present!!>
1206                  */
1207         }
1208 }
1209 EXPORT_SYMBOL(HTUpdateSelfAndPeerSetting);
1210
1211 /*
1212  *function:  check whether HT control field exists
1213  *   input:  struct ieee80211_device    *ieee
1214  *           u8*                        pFrame //coming skb->data
1215  *  output:  none
1216  *  return:  return true if HT control field exists(false otherwise)
1217  *  notice:
1218  */
1219 u8 HTCCheck(struct ieee80211_device *ieee, u8 *pFrame)
1220 {
1221         if (ieee->pHTInfo->bCurrentHTSupport) {
1222                 if ((IsQoSDataFrame(pFrame) && Frame_Order(pFrame)) == 1) {
1223                         IEEE80211_DEBUG(IEEE80211_DL_HT, "HT CONTROL FILED EXIST!!\n");
1224                         return true;
1225                 }
1226         }
1227         return false;
1228 }
1229
1230 static void HTSetConnectBwModeCallback(struct ieee80211_device *ieee)
1231 {
1232         PRT_HIGH_THROUGHPUT pHTInfo = ieee->pHTInfo;
1233
1234         IEEE80211_DEBUG(IEEE80211_DL_HT, "======>%s()\n", __func__);
1235
1236         if (pHTInfo->bCurBW40MHz) {
1237                 if (pHTInfo->CurSTAExtChnlOffset == HT_EXTCHNL_OFFSET_UPPER)
1238                         ieee->set_chan(ieee->dev, ieee->current_network.channel + 2);
1239                 else if (pHTInfo->CurSTAExtChnlOffset == HT_EXTCHNL_OFFSET_LOWER)
1240                         ieee->set_chan(ieee->dev, ieee->current_network.channel - 2);
1241                 else
1242                         ieee->set_chan(ieee->dev, ieee->current_network.channel);
1243
1244                 ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20_40, pHTInfo->CurSTAExtChnlOffset);
1245         } else {
1246                 ieee->set_chan(ieee->dev, ieee->current_network.channel);
1247                 ieee->SetBWModeHandler(ieee->dev, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
1248         }
1249
1250         pHTInfo->bSwBwInProgress = false;
1251 }
1252
1253 /*
1254  * This function set bandwidth mode in protocol layer.
1255  */
1256 void HTSetConnectBwMode(struct ieee80211_device *ieee, enum ht_channel_width Bandwidth, enum ht_extension_chan_offset Offset)
1257 {
1258         PRT_HIGH_THROUGHPUT pHTInfo = ieee->pHTInfo;
1259 //      u32 flags = 0;
1260
1261         if (!pHTInfo->bRegBW40MHz)
1262                 return;
1263
1264         // To reduce dummy operation
1265 //      if((pHTInfo->bCurBW40MHz==false && Bandwidth==HT_CHANNEL_WIDTH_20) ||
1266 //         (pHTInfo->bCurBW40MHz==true && Bandwidth==HT_CHANNEL_WIDTH_20_40 && Offset==pHTInfo->CurSTAExtChnlOffset))
1267 //              return;
1268
1269 //      spin_lock_irqsave(&(ieee->bw_spinlock), flags);
1270         if (pHTInfo->bSwBwInProgress) {
1271 //              spin_unlock_irqrestore(&(ieee->bw_spinlock), flags);
1272                 return;
1273         }
1274         //if in half N mode, set to 20M bandwidth please 09.08.2008 WB.
1275         if (Bandwidth == HT_CHANNEL_WIDTH_20_40 && (!ieee->GetHalfNmodeSupportByAPsHandler(ieee->dev))) {
1276                         // Handle Illegal extension channel offset!!
1277                 if (ieee->current_network.channel < 2 && Offset == HT_EXTCHNL_OFFSET_LOWER)
1278                         Offset = HT_EXTCHNL_OFFSET_NO_EXT;
1279                 if (Offset == HT_EXTCHNL_OFFSET_UPPER || Offset == HT_EXTCHNL_OFFSET_LOWER) {
1280                         pHTInfo->bCurBW40MHz = true;
1281                         pHTInfo->CurSTAExtChnlOffset = Offset;
1282                 } else {
1283                         pHTInfo->bCurBW40MHz = false;
1284                         pHTInfo->CurSTAExtChnlOffset = HT_EXTCHNL_OFFSET_NO_EXT;
1285                 }
1286         } else {
1287                 pHTInfo->bCurBW40MHz = false;
1288                 pHTInfo->CurSTAExtChnlOffset = HT_EXTCHNL_OFFSET_NO_EXT;
1289         }
1290
1291         pHTInfo->bSwBwInProgress = true;
1292
1293         /*
1294          * TODO: 2007.7.13 by Emily Wait 2000ms  in order to guarantee that
1295          * switching bandwidth is executed after scan is finished. It is a
1296          * temporal solution because software should ganrantee the last
1297          * operation of switching bandwidth is executed properlly.
1298          */
1299         HTSetConnectBwModeCallback(ieee);
1300
1301 //      spin_unlock_irqrestore(&(ieee->bw_spinlock), flags);
1302 }