]> Pileus Git - ~andy/linux/blob - drivers/staging/vt6656/datarate.c
Merge branch 'for-linus' of git://git.linaro.org/people/rmk/linux-arm
[~andy/linux] / drivers / staging / vt6656 / 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 #include "rf.h"
46
47 /*---------------------  Static Definitions -------------------------*/
48
49
50
51
52 /*---------------------  Static Classes  ----------------------------*/
53
54
55
56 /*---------------------  Static Variables  --------------------------*/
57
58 /* static int msglevel = MSG_LEVEL_DEBUG; */
59 static int          msglevel                =MSG_LEVEL_INFO;
60 const BYTE acbyIERate[MAX_RATE] =
61 {0x02, 0x04, 0x0B, 0x16, 0x0C, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C};
62
63 #define AUTORATE_TXOK_CNT       0x0400
64 #define AUTORATE_TXFAIL_CNT     0x0064
65 #define AUTORATE_TIMEOUT        10
66
67 /*---------------------  Static Functions  --------------------------*/
68
69 void s_vResetCounter(PKnownNodeDB psNodeDBTable);
70
71 void s_vResetCounter(PKnownNodeDB psNodeDBTable)
72 {
73     BYTE            ii;
74
75     /* clear statistics counter for auto_rate */
76     for (ii = 0; ii <= MAX_RATE; ii++) {
77         psNodeDBTable->uTxOk[ii] = 0;
78         psNodeDBTable->uTxFail[ii] = 0;
79     }
80 }
81
82 /*---------------------  Export Variables  --------------------------*/
83
84
85 /*---------------------  Export Functions  --------------------------*/
86
87
88 /*+
89  *
90  * Description:
91  *      Get RateIdx from the value in SuppRates IE or ExtSuppRates IE
92  *
93  * Parameters:
94  *  In:
95  *      BYTE    - Rate value in SuppRates IE or ExtSuppRates IE
96  *  Out:
97  *      none
98  *
99  * Return Value: RateIdx
100  *
101 -*/
102 BYTE
103 DATARATEbyGetRateIdx (
104      BYTE byRate
105     )
106 {
107     BYTE    ii;
108
109     /* erase BasicRate flag */
110     byRate = byRate & 0x7F;
111
112     for (ii = 0; ii < MAX_RATE; ii ++) {
113         if (acbyIERate[ii] == byRate)
114             return ii;
115     }
116     return 0;
117 }
118
119
120
121 /*+
122  *
123  * Routine Description:
124  *      Rate fallback Algorithm Implementaion
125  *
126  * Parameters:
127  *  In:
128  *      pDevice         - Pointer to the adapter
129  *      psNodeDBTable   - Pointer to Node Data Base
130  *  Out:
131  *      none
132  *
133  * Return Value: none
134  *
135 -*/
136 #define AUTORATE_TXCNT_THRESHOLD        20
137 #define AUTORATE_INC_THRESHOLD          30
138
139
140
141
142 /*+
143  *
144  * Description:
145  *      Get RateIdx from the value in SuppRates IE or ExtSuppRates IE
146  *
147  * Parameters:
148  *  In:
149  *      BYTE    - Rate value in SuppRates IE or ExtSuppRates IE
150  *  Out:
151  *      none
152  *
153  * Return Value: RateIdx
154  *
155 -*/
156 WORD
157 RATEwGetRateIdx(
158      BYTE byRate
159     )
160 {
161     WORD    ii;
162
163     /* erase BasicRate flag */
164     byRate = byRate & 0x7F;
165
166     for (ii = 0; ii < MAX_RATE; ii ++) {
167         if (acbyIERate[ii] == byRate)
168             return ii;
169     }
170     return 0;
171 }
172
173 /*+
174  *
175  * Description:
176  *      Parsing the highest basic & support rate in rate field of frame.
177  *
178  * Parameters:
179  *  In:
180  *      pDevice         - Pointer to the adapter
181  *      pItemRates      - Pointer to Rate field defined in 802.11 spec.
182  *      pItemExtRates      - Pointer to Extended Rate field defined in 802.11 spec.
183  *  Out:
184  *      pwMaxBasicRate  - Maximum Basic Rate
185  *      pwMaxSuppRate   - Maximum Supported Rate
186  *      pbyTopCCKRate   - Maximum Basic Rate in CCK mode
187  *      pbyTopOFDMRate  - Maximum Basic Rate in OFDM mode
188  *
189  * Return Value: none
190  *
191 -*/
192
193 void RATEvParseMaxRate(struct vnt_private *pDevice,
194         PWLAN_IE_SUPP_RATES pItemRates, PWLAN_IE_SUPP_RATES pItemExtRates,
195         int bUpdateBasicRate, u16 *pwMaxBasicRate, u16 *pwMaxSuppRate,
196         u16 *pwSuppRate, u8 *pbyTopCCKRate, u8 *pbyTopOFDMRate)
197 {
198         int  ii;
199         u8 byHighSuppRate = 0, byRate = 0;
200         u16 wOldBasicRate = pDevice->wBasicRate;
201         u32 uRateLen;
202
203         if (pItemRates == NULL)
204                 return;
205
206     *pwSuppRate = 0;
207     uRateLen = pItemRates->len;
208
209     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ParseMaxRate Len: %d\n", uRateLen);
210     if (pDevice->byBBType != BB_TYPE_11B) {
211         if (uRateLen > WLAN_RATES_MAXLEN)
212             uRateLen = WLAN_RATES_MAXLEN;
213     } else {
214         if (uRateLen > WLAN_RATES_MAXLEN_11B)
215             uRateLen = WLAN_RATES_MAXLEN_11B;
216     }
217
218     for (ii = 0; ii < uRateLen; ii++) {
219         byRate = (BYTE)(pItemRates->abyRates[ii]);
220         if (WLAN_MGMT_IS_BASICRATE(byRate) &&
221             (bUpdateBasicRate == true))  {
222           /*
223            * add to basic rate set, update pDevice->byTopCCKBasicRate and
224            * pDevice->byTopOFDMBasicRate
225            */
226                 CARDbAddBasicRate((void *)pDevice, RATEwGetRateIdx(byRate));
227             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ParseMaxRate AddBasicRate: %d\n", RATEwGetRateIdx(byRate));
228         }
229         byRate = (BYTE)(pItemRates->abyRates[ii]&0x7F);
230         if (byHighSuppRate == 0)
231             byHighSuppRate = byRate;
232         if (byRate > byHighSuppRate)
233             byHighSuppRate = byRate;
234         *pwSuppRate |= (1<<RATEwGetRateIdx(byRate));
235     }
236     if ((pItemExtRates != NULL) && (pItemExtRates->byElementID == WLAN_EID_EXTSUPP_RATES) &&
237         (pDevice->byBBType != BB_TYPE_11B)) {
238
239         unsigned int uExtRateLen = pItemExtRates->len;
240
241         if (uExtRateLen > WLAN_RATES_MAXLEN)
242             uExtRateLen = WLAN_RATES_MAXLEN;
243
244         for (ii = 0; ii < uExtRateLen ; ii++) {
245             byRate = (BYTE)(pItemExtRates->abyRates[ii]);
246             /* select highest basic rate */
247             if (WLAN_MGMT_IS_BASICRATE(pItemExtRates->abyRates[ii])) {
248               /*
249                * add to basic rate set, update pDevice->byTopCCKBasicRate and
250                * pDevice->byTopOFDMBasicRate
251                */
252                     CARDbAddBasicRate((void *)pDevice, RATEwGetRateIdx(byRate));
253                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ParseMaxRate AddBasicRate: %d\n", RATEwGetRateIdx(byRate));
254             }
255             byRate = (BYTE)(pItemExtRates->abyRates[ii]&0x7F);
256             if (byHighSuppRate == 0)
257                 byHighSuppRate = byRate;
258             if (byRate > byHighSuppRate)
259                 byHighSuppRate = byRate;
260             *pwSuppRate |= (1<<RATEwGetRateIdx(byRate));
261
262             /* DBG_PRN_GRP09(("ParseMaxRate : HighSuppRate: %d, %X\n",
263                RATEwGetRateIdx(byRate), byRate)); */
264         }
265     }
266
267     if ((pDevice->byPacketType == PK_TYPE_11GB)
268         && CARDbIsOFDMinBasicRate((void *)pDevice)) {
269         pDevice->byPacketType = PK_TYPE_11GA;
270     }
271
272     *pbyTopCCKRate = pDevice->byTopCCKBasicRate;
273     *pbyTopOFDMRate = pDevice->byTopOFDMBasicRate;
274     *pwMaxSuppRate = RATEwGetRateIdx(byHighSuppRate);
275     if ((pDevice->byPacketType==PK_TYPE_11B) || (pDevice->byPacketType==PK_TYPE_11GB))
276        *pwMaxBasicRate = pDevice->byTopCCKBasicRate;
277     else
278        *pwMaxBasicRate = pDevice->byTopOFDMBasicRate;
279     if (wOldBasicRate != pDevice->wBasicRate)
280         CARDvSetRSPINF((void *)pDevice, pDevice->byBBType);
281
282      DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Exit ParseMaxRate\n");
283 }
284
285
286 /*+
287  *
288  * Routine Description:
289  *      Rate fallback Algorithm Implementaion
290  *
291  * Parameters:
292  *  In:
293  *      pDevice         - Pointer to the adapter
294  *      psNodeDBTable   - Pointer to Node Data Base
295  *  Out:
296  *      none
297  *
298  * Return Value: none
299  *
300 -*/
301 #define AUTORATE_TXCNT_THRESHOLD        20
302 #define AUTORATE_INC_THRESHOLD          30
303
304 void RATEvTxRateFallBack(struct vnt_private *pDevice,
305         PKnownNodeDB psNodeDBTable)
306 {
307         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
308         u16 wIdxDownRate = 0;
309         int ii;
310         int bAutoRate[MAX_RATE] = {true, true, true, true, false, false, true,
311                                          true, true, true, true, true};
312         u32 dwThroughputTbl[MAX_RATE] = {10, 20, 55, 110, 60, 90, 120, 180,
313                 240, 360, 480, 540};
314         u32 dwThroughput = 0;
315         u16 wIdxUpRate = 0;
316         u32 dwTxDiff = 0;
317
318         if (pMgmt->eScanState != WMAC_NO_SCANNING)
319                 return; /* Don't do Fallback when scanning Channel */
320
321         psNodeDBTable->uTimeCount++;
322
323     if (psNodeDBTable->uTxFail[MAX_RATE] > psNodeDBTable->uTxOk[MAX_RATE])
324         dwTxDiff = psNodeDBTable->uTxFail[MAX_RATE] - psNodeDBTable->uTxOk[MAX_RATE];
325
326     if ((psNodeDBTable->uTxOk[MAX_RATE] < AUTORATE_TXOK_CNT) &&
327         (dwTxDiff < AUTORATE_TXFAIL_CNT) &&
328         (psNodeDBTable->uTimeCount < AUTORATE_TIMEOUT)) {
329         return;
330     }
331
332     if (psNodeDBTable->uTimeCount >= AUTORATE_TIMEOUT) {
333         psNodeDBTable->uTimeCount = 0;
334     }
335
336     for (ii = 0; ii < MAX_RATE; ii++) {
337         if (psNodeDBTable->wSuppRate & (0x0001<<ii)) {
338             if (bAutoRate[ii] == true) {
339                 wIdxUpRate = (WORD) ii;
340             }
341         } else {
342             bAutoRate[ii] = false;
343         }
344     }
345
346     for (ii = 0; ii <= psNodeDBTable->wTxDataRate; ii++) {
347         if ( (psNodeDBTable->uTxOk[ii] != 0) ||
348              (psNodeDBTable->uTxFail[ii] != 0) ) {
349             dwThroughputTbl[ii] *= psNodeDBTable->uTxOk[ii];
350             if (ii < RATE_11M) {
351                 psNodeDBTable->uTxFail[ii] *= 4;
352             }
353             dwThroughputTbl[ii] /= (psNodeDBTable->uTxOk[ii] + psNodeDBTable->uTxFail[ii]);
354         }
355         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Rate %d,Ok: %d, Fail:%d, Throughput:%d\n",
356                        ii, (int)psNodeDBTable->uTxOk[ii], (int)psNodeDBTable->uTxFail[ii], (int)dwThroughputTbl[ii]);
357     }
358     dwThroughput = dwThroughputTbl[psNodeDBTable->wTxDataRate];
359
360     wIdxDownRate = psNodeDBTable->wTxDataRate;
361     for (ii = psNodeDBTable->wTxDataRate; ii > 0;) {
362         ii--;
363         if ( (dwThroughputTbl[ii] > dwThroughput) &&
364              (bAutoRate[ii]==true) ) {
365             dwThroughput = dwThroughputTbl[ii];
366             wIdxDownRate = (WORD) ii;
367         }
368     }
369     psNodeDBTable->wTxDataRate = wIdxDownRate;
370     if (psNodeDBTable->uTxOk[MAX_RATE]) {
371         if (psNodeDBTable->uTxOk[MAX_RATE] >
372            (psNodeDBTable->uTxFail[MAX_RATE] * 4) ) {
373             psNodeDBTable->wTxDataRate = wIdxUpRate;
374         }
375     } else { /* adhoc, if uTxOk(total) == 0 & uTxFail(total) == 0 */
376         if (psNodeDBTable->uTxFail[MAX_RATE] == 0)
377             psNodeDBTable->wTxDataRate = wIdxUpRate;
378     }
379
380     if (pDevice->byBBType == BB_TYPE_11A) {
381         if (psNodeDBTable->wTxDataRate <= RATE_11M)
382             psNodeDBTable->wTxDataRate = RATE_6M;
383     }
384     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"uTxOk[MAX_RATE] %d, uTxFail[MAX_RATE]:%d\n",(int)psNodeDBTable->uTxOk[MAX_RATE], (int)psNodeDBTable->uTxFail[MAX_RATE]);
385     s_vResetCounter(psNodeDBTable);
386     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Rate: %d, U:%d, D:%d\n", (int)psNodeDBTable->wTxDataRate, (int)wIdxUpRate, (int)wIdxDownRate);
387     return;
388 }
389
390 /*+
391  *
392  * Description:
393  *    This routine is used to assemble available Rate IE.
394  *
395  * Parameters:
396  *  In:
397  *    pDevice
398  *  Out:
399  *
400  * Return Value: None
401  *
402 -*/
403 BYTE
404 RATEuSetIE (
405      PWLAN_IE_SUPP_RATES pSrcRates,
406      PWLAN_IE_SUPP_RATES pDstRates,
407      unsigned int                uRateLen
408     )
409 {
410     unsigned int ii, uu, uRateCnt = 0;
411
412     if ((pSrcRates == NULL) || (pDstRates == NULL))
413         return 0;
414
415     if (pSrcRates->len == 0)
416         return 0;
417
418     for (ii = 0; ii < uRateLen; ii++) {
419         for (uu = 0; uu < pSrcRates->len; uu++) {
420             if ((pSrcRates->abyRates[uu] & 0x7F) == acbyIERate[ii]) {
421                 pDstRates->abyRates[uRateCnt ++] = pSrcRates->abyRates[uu];
422                 break;
423             }
424         }
425     }
426     return (BYTE)uRateCnt;
427 }
428