Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/ralf/upstream-linus
[cascardo/linux.git] / drivers / staging / vt6655 / datarate.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: datarate.c
20  *
21  * Purpose: Handles the auto fallback & data rates functions
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: July 17, 2002
26  *
27  * Functions:
28  *      RATEvParseMaxRate - Parsing the highest basic & support rate in rate field of frame
29  *      RATEvTxRateFallBack - Rate fallback Algorithm Implementaion
30  *      RATEuSetIE- Set rate IE field.
31  *
32  * Revision History:
33  *
34  */
35
36 #include "ttype.h"
37 #include "tmacro.h"
38 #include "mac.h"
39 #include "80211mgr.h"
40 #include "bssdb.h"
41 #include "datarate.h"
42 #include "card.h"
43 #include "baseband.h"
44 #include "srom.h"
45
46 /*---------------------  Static Definitions -------------------------*/
47
48 /*---------------------  Static Classes  ----------------------------*/
49
50 extern unsigned short TxRate_iwconfig; /* 2008-5-8 <add> by chester */
51 /*---------------------  Static Variables  --------------------------*/
52 static int msglevel = MSG_LEVEL_INFO;
53 static const unsigned char acbyIERate[MAX_RATE] = {
54 0x02, 0x04, 0x0B, 0x16, 0x0C, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C
55 };
56
57 #define AUTORATE_TXOK_CNT       0x0400
58 #define AUTORATE_TXFAIL_CNT     0x0064
59 #define AUTORATE_TIMEOUT        10
60
61 /*---------------------  Static Functions  --------------------------*/
62
63 void s_vResetCounter(
64         PKnownNodeDB psNodeDBTable
65 );
66
67 void
68 s_vResetCounter(
69         PKnownNodeDB psNodeDBTable
70 )
71 {
72         unsigned char ii;
73
74         /* clear statistic counter for auto_rate */
75         for (ii = 0; ii <= MAX_RATE; ii++) {
76                 psNodeDBTable->uTxOk[ii] = 0;
77                 psNodeDBTable->uTxFail[ii] = 0;
78         }
79 }
80
81 /*---------------------  Export Variables  --------------------------*/
82
83 /*---------------------  Export Functions  --------------------------*/
84
85 /*+
86  *
87  * Description:
88  *      Get RateIdx from the value in SuppRates IE or ExtSuppRates IE
89  *
90  * Parameters:
91  *  In:
92  *      unsigned char - Rate value in SuppRates IE or ExtSuppRates IE
93  *  Out:
94  *      none
95  *
96  * Return Value: RateIdx
97  *
98  -*/
99 unsigned char
100 DATARATEbyGetRateIdx(
101         unsigned char byRate
102 )
103 {
104         unsigned char ii;
105
106         /* Erase basicRate flag. */
107         byRate = byRate & 0x7F;/* 0111 1111 */
108
109         for (ii = 0; ii < MAX_RATE; ii++) {
110                 if (acbyIERate[ii] == byRate)
111                         return ii;
112         }
113         return 0;
114 }
115
116 /*+
117  *
118  * Routine Description:
119  *      Rate fallback Algorithm Implementation
120  *
121  * Parameters:
122  *  In:
123  *      pDevice         - Pointer to the adapter
124  *      psNodeDBTable   - Pointer to Node Data Base
125  *  Out:
126  *      none
127  *
128  * Return Value: none
129  *
130  -*/
131 #define AUTORATE_TXCNT_THRESHOLD        20
132 #define AUTORATE_INC_THRESHOLD          30
133
134 /*+
135  *
136  * Description:
137  *      Get RateIdx from the value in SuppRates IE or ExtSuppRates IE
138  *
139  * Parameters:
140  *  In:
141  *      unsigned char - Rate value in SuppRates IE or ExtSuppRates IE
142  *  Out:
143  *      none
144  *
145  * Return Value: RateIdx
146  *
147  -*/
148 unsigned short
149 wGetRateIdx(
150         unsigned char byRate
151 )
152 {
153         unsigned short ii;
154
155         /* Erase basicRate flag. */
156         byRate = byRate & 0x7F;/* 0111 1111 */
157
158         for (ii = 0; ii < MAX_RATE; ii++) {
159                 if (acbyIERate[ii] == byRate)
160                         return ii;
161         }
162
163         return 0;
164 }
165
166 /*+
167  *
168  * Description:
169  *      Parsing the highest basic & support rate in rate field of frame.
170  *
171  * Parameters:
172  *  In:
173  *      pDevice         - Pointer to the adapter
174  *      pItemRates      - Pointer to Rate field defined in 802.11 spec.
175  *      pItemExtRates      - Pointer to Extended Rate field defined in 802.11 spec.
176  *  Out:
177  *      pwMaxBasicRate  - Maximum Basic Rate
178  *      pwMaxSuppRate   - Maximum Supported Rate
179  *      pbyTopCCKRate   - Maximum Basic Rate in CCK mode
180  *      pbyTopOFDMRate  - Maximum Basic Rate in OFDM mode
181  *
182  * Return Value: none
183  *
184  -*/
185 void
186 RATEvParseMaxRate(
187         void *pDeviceHandler,
188         PWLAN_IE_SUPP_RATES pItemRates,
189         PWLAN_IE_SUPP_RATES pItemExtRates,
190         bool bUpdateBasicRate,
191         unsigned short *pwMaxBasicRate,
192         unsigned short *pwMaxSuppRate,
193         unsigned short *pwSuppRate,
194         unsigned char *pbyTopCCKRate,
195         unsigned char *pbyTopOFDMRate
196 )
197 {
198         PSDevice  pDevice = (PSDevice) pDeviceHandler;
199         unsigned int ii;
200         unsigned char byHighSuppRate = 0;
201         unsigned char byRate = 0;
202         unsigned short wOldBasicRate = pDevice->wBasicRate;
203         unsigned int uRateLen;
204
205         if (pItemRates == NULL)
206                 return;
207
208         *pwSuppRate = 0;
209         uRateLen = pItemRates->len;
210
211         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ParseMaxRate Len: %d\n", uRateLen);
212         if (pDevice->eCurrentPHYType != PHY_TYPE_11B) {
213                 if (uRateLen > WLAN_RATES_MAXLEN)
214                         uRateLen = WLAN_RATES_MAXLEN;
215         } else {
216                 if (uRateLen > WLAN_RATES_MAXLEN_11B)
217                         uRateLen = WLAN_RATES_MAXLEN_11B;
218         }
219
220         for (ii = 0; ii < uRateLen; ii++) {
221                 byRate = (unsigned char)(pItemRates->abyRates[ii]);
222                 if (WLAN_MGMT_IS_BASICRATE(byRate) && bUpdateBasicRate)  {
223                         /* Add to basic rate set, update pDevice->byTopCCKBasicRate and pDevice->byTopOFDMBasicRate */
224                         CARDbAddBasicRate((void *)pDevice, wGetRateIdx(byRate));
225                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ParseMaxRate AddBasicRate: %d\n", wGetRateIdx(byRate));
226                 }
227                 byRate = (unsigned char)(pItemRates->abyRates[ii]&0x7F);
228                 if (byHighSuppRate == 0)
229                         byHighSuppRate = byRate;
230                 if (byRate > byHighSuppRate)
231                         byHighSuppRate = byRate;
232                 *pwSuppRate |= (1<<wGetRateIdx(byRate));
233         }
234         if ((pItemExtRates != NULL) && (pItemExtRates->byElementID == WLAN_EID_EXTSUPP_RATES) &&
235             (pDevice->eCurrentPHYType != PHY_TYPE_11B)) {
236                 unsigned int uExtRateLen = pItemExtRates->len;
237
238                 if (uExtRateLen > WLAN_RATES_MAXLEN)
239                         uExtRateLen = WLAN_RATES_MAXLEN;
240
241                 for (ii = 0; ii < uExtRateLen; ii++) {
242                         byRate = (unsigned char)(pItemExtRates->abyRates[ii]);
243                         /* select highest basic rate */
244                         if (WLAN_MGMT_IS_BASICRATE(pItemExtRates->abyRates[ii])) {
245                                 /* Add to basic rate set, update pDevice->byTopCCKBasicRate and pDevice->byTopOFDMBasicRate */
246                                 CARDbAddBasicRate((void *)pDevice, wGetRateIdx(byRate));
247                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ParseMaxRate AddBasicRate: %d\n", wGetRateIdx(byRate));
248                         }
249                         byRate = (unsigned char)(pItemExtRates->abyRates[ii]&0x7F);
250                         if (byHighSuppRate == 0)
251                                 byHighSuppRate = byRate;
252                         if (byRate > byHighSuppRate)
253                                 byHighSuppRate = byRate;
254                         *pwSuppRate |= (1<<wGetRateIdx(byRate));
255                 }
256         }
257
258         if ((pDevice->byPacketType == PK_TYPE_11GB) && CARDbIsOFDMinBasicRate((void *)pDevice))
259                 pDevice->byPacketType = PK_TYPE_11GA;
260
261         *pbyTopCCKRate = pDevice->byTopCCKBasicRate;
262         *pbyTopOFDMRate = pDevice->byTopOFDMBasicRate;
263         *pwMaxSuppRate = wGetRateIdx(byHighSuppRate);
264         if ((pDevice->byPacketType == PK_TYPE_11B) || (pDevice->byPacketType == PK_TYPE_11GB))
265                 *pwMaxBasicRate = pDevice->byTopCCKBasicRate;
266         else
267                 *pwMaxBasicRate = pDevice->byTopOFDMBasicRate;
268         if (wOldBasicRate != pDevice->wBasicRate)
269                 CARDvSetRSPINF((void *)pDevice, pDevice->eCurrentPHYType);
270
271         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Exit ParseMaxRate\n");
272 }
273
274 /*+
275  *
276  * Routine Description:
277  *      Rate fallback Algorithm Implementaion
278  *
279  * Parameters:
280  *  In:
281  *      pDevice         - Pointer to the adapter
282  *      psNodeDBTable   - Pointer to Node Data Base
283  *  Out:
284  *      none
285  *
286  * Return Value: none
287  *
288  -*/
289 #define AUTORATE_TXCNT_THRESHOLD        20
290 #define AUTORATE_INC_THRESHOLD          30
291
292 void
293 RATEvTxRateFallBack(
294         void *pDeviceHandler,
295         PKnownNodeDB psNodeDBTable
296 )
297 {
298         PSDevice        pDevice = (PSDevice) pDeviceHandler;
299         unsigned short wIdxDownRate = 0;
300         unsigned int ii;
301         bool bAutoRate[MAX_RATE]    = {true, true, true, true, false, false, true, true, true, true, true, true};
302         unsigned long dwThroughputTbl[MAX_RATE] = {10, 20, 55, 110, 60, 90, 120, 180, 240, 360, 480, 540};
303         unsigned long dwThroughput = 0;
304         unsigned short wIdxUpRate = 0;
305         unsigned long dwTxDiff = 0;
306
307         if (pDevice->pMgmt->eScanState != WMAC_NO_SCANNING)
308                 /* Don't do Fallback when scanning Channel */
309                 return;
310
311         psNodeDBTable->uTimeCount++;
312
313         if (psNodeDBTable->uTxFail[MAX_RATE] > psNodeDBTable->uTxOk[MAX_RATE])
314                 dwTxDiff = psNodeDBTable->uTxFail[MAX_RATE] - psNodeDBTable->uTxOk[MAX_RATE];
315
316         if ((psNodeDBTable->uTxOk[MAX_RATE] < AUTORATE_TXOK_CNT) &&
317             (dwTxDiff < AUTORATE_TXFAIL_CNT) &&
318             (psNodeDBTable->uTimeCount < AUTORATE_TIMEOUT)) {
319                 return;
320         }
321
322         if (psNodeDBTable->uTimeCount >= AUTORATE_TIMEOUT)
323                 psNodeDBTable->uTimeCount = 0;
324
325         for (ii = 0; ii < MAX_RATE; ii++) {
326                 if (psNodeDBTable->wSuppRate & (0x0001<<ii)) {
327                         if (bAutoRate[ii])
328                                 wIdxUpRate = (unsigned short) ii;
329
330                 } else {
331                         bAutoRate[ii] = false;
332                 }
333         }
334
335         for (ii = 0; ii <= psNodeDBTable->wTxDataRate; ii++) {
336                 if ((psNodeDBTable->uTxOk[ii] != 0) ||
337                     (psNodeDBTable->uTxFail[ii] != 0)) {
338                         dwThroughputTbl[ii] *= psNodeDBTable->uTxOk[ii];
339                         if (ii < RATE_11M)
340                                 psNodeDBTable->uTxFail[ii] *= 4;
341
342                         dwThroughputTbl[ii] /= (psNodeDBTable->uTxOk[ii] + psNodeDBTable->uTxFail[ii]);
343                 }
344         }
345         dwThroughput = dwThroughputTbl[psNodeDBTable->wTxDataRate];
346
347         wIdxDownRate = psNodeDBTable->wTxDataRate;
348         for (ii = psNodeDBTable->wTxDataRate; ii > 0;) {
349                 ii--;
350                 if ((dwThroughputTbl[ii] > dwThroughput) && bAutoRate[ii]) {
351                         dwThroughput = dwThroughputTbl[ii];
352                         wIdxDownRate = (unsigned short) ii;
353                 }
354         }
355         psNodeDBTable->wTxDataRate = wIdxDownRate;
356         if (psNodeDBTable->uTxOk[MAX_RATE]) {
357                 if (psNodeDBTable->uTxOk[MAX_RATE] >
358                     (psNodeDBTable->uTxFail[MAX_RATE] * 4)) {
359                         psNodeDBTable->wTxDataRate = wIdxUpRate;
360                 }
361         } else {
362                 /* adhoc, if uTxOk =0 & uTxFail = 0 */
363                 if (psNodeDBTable->uTxFail[MAX_RATE] == 0)
364                         psNodeDBTable->wTxDataRate = wIdxUpRate;
365         }
366
367         /* 2008-5-8 <add> by chester */
368         TxRate_iwconfig = psNodeDBTable->wTxDataRate;
369         s_vResetCounter(psNodeDBTable);
370 }
371
372 /*+
373  *
374  * Description:
375  *    This routine is used to assemble available Rate IE.
376  *
377  * Parameters:
378  *  In:
379  *    pDevice
380  *  Out:
381  *
382  * Return Value: None
383  *
384  -*/
385 unsigned char
386 RATEuSetIE(
387         PWLAN_IE_SUPP_RATES pSrcRates,
388         PWLAN_IE_SUPP_RATES pDstRates,
389         unsigned int uRateLen
390 )
391 {
392         unsigned int ii, uu, uRateCnt = 0;
393
394         if ((pSrcRates == NULL) || (pDstRates == NULL))
395                 return 0;
396
397         if (pSrcRates->len == 0)
398                 return 0;
399
400         for (ii = 0; ii < uRateLen; ii++) {
401                 for (uu = 0; uu < pSrcRates->len; uu++) {
402                         if ((pSrcRates->abyRates[uu] & 0x7F) == acbyIERate[ii]) {
403                                 pDstRates->abyRates[uRateCnt++] = pSrcRates->abyRates[uu];
404                                 break;
405                         }
406                 }
407         }
408         return (unsigned char)uRateCnt;
409 }