Merge branch 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6/btrfs-unstable.git] / drivers / staging / vt6656 / datarate.c
blob17fbc35ebcbfe4022fbfe40afbac8faf2c89eb54
1 /*
2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3 * All rights reserved.
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.
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.
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.
19 * File: datarate.c
21 * Purpose: Handles the auto fallback & data rates functions
23 * Author: Lyndon Chen
25 * Date: July 17, 2002
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.
32 * Revision History:
36 #include "tmacro.h"
37 #include "mac.h"
38 #include "80211mgr.h"
39 #include "bssdb.h"
40 #include "datarate.h"
41 #include "card.h"
42 #include "baseband.h"
43 #include "srom.h"
44 #include "rf.h"
46 /* static int msglevel = MSG_LEVEL_DEBUG; */
47 static int msglevel =MSG_LEVEL_INFO;
48 const u8 acbyIERate[MAX_RATE] =
49 {0x02, 0x04, 0x0B, 0x16, 0x0C, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C};
51 #define AUTORATE_TXOK_CNT 0x0400
52 #define AUTORATE_TXFAIL_CNT 0x0064
53 #define AUTORATE_TIMEOUT 10
55 void s_vResetCounter(PKnownNodeDB psNodeDBTable);
57 void s_vResetCounter(PKnownNodeDB psNodeDBTable)
59 u8 ii;
61 /* clear statistics counter for auto_rate */
62 for (ii = 0; ii <= MAX_RATE; ii++) {
63 psNodeDBTable->uTxOk[ii] = 0;
64 psNodeDBTable->uTxFail[ii] = 0;
68 /*+
70 * Routine Description:
71 * Rate fallback Algorithm Implementaion
73 * Parameters:
74 * In:
75 * pDevice - Pointer to the adapter
76 * psNodeDBTable - Pointer to Node Data Base
77 * Out:
78 * none
80 * Return Value: none
82 -*/
83 #define AUTORATE_TXCNT_THRESHOLD 20
84 #define AUTORATE_INC_THRESHOLD 30
86 /*+
88 * Description:
89 * Get RateIdx from the value in SuppRates IE or ExtSuppRates IE
91 * Parameters:
92 * In:
93 * u8 - Rate value in SuppRates IE or ExtSuppRates IE
94 * Out:
95 * none
97 * Return Value: RateIdx
99 -*/
101 RATEwGetRateIdx(
102 u8 byRate
105 u16 ii;
107 /* erase BasicRate flag */
108 byRate = byRate & 0x7F;
110 for (ii = 0; ii < MAX_RATE; ii ++) {
111 if (acbyIERate[ii] == byRate)
112 return ii;
114 return 0;
119 * Description:
120 * Parsing the highest basic & support rate in rate field of frame.
122 * Parameters:
123 * In:
124 * pDevice - Pointer to the adapter
125 * pItemRates - Pointer to Rate field defined in 802.11 spec.
126 * pItemExtRates - Pointer to Extended Rate field defined in 802.11 spec.
127 * Out:
128 * pwMaxBasicRate - Maximum Basic Rate
129 * pwMaxSuppRate - Maximum Supported Rate
130 * pbyTopCCKRate - Maximum Basic Rate in CCK mode
131 * pbyTopOFDMRate - Maximum Basic Rate in OFDM mode
133 * Return Value: none
137 void RATEvParseMaxRate(struct vnt_private *pDevice,
138 PWLAN_IE_SUPP_RATES pItemRates, PWLAN_IE_SUPP_RATES pItemExtRates,
139 int bUpdateBasicRate, u16 *pwMaxBasicRate, u16 *pwMaxSuppRate,
140 u16 *pwSuppRate, u8 *pbyTopCCKRate, u8 *pbyTopOFDMRate)
142 int ii;
143 u8 byHighSuppRate = 0, byRate = 0;
144 u16 wOldBasicRate = pDevice->wBasicRate;
145 u32 uRateLen;
147 if (pItemRates == NULL)
148 return;
150 *pwSuppRate = 0;
151 uRateLen = pItemRates->len;
153 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ParseMaxRate Len: %d\n", uRateLen);
154 if (pDevice->byBBType != BB_TYPE_11B) {
155 if (uRateLen > WLAN_RATES_MAXLEN)
156 uRateLen = WLAN_RATES_MAXLEN;
157 } else {
158 if (uRateLen > WLAN_RATES_MAXLEN_11B)
159 uRateLen = WLAN_RATES_MAXLEN_11B;
162 for (ii = 0; ii < uRateLen; ii++) {
163 byRate = (u8)(pItemRates->abyRates[ii]);
164 if (WLAN_MGMT_IS_BASICRATE(byRate) &&
165 (bUpdateBasicRate == true)) {
167 * add to basic rate set, update pDevice->byTopCCKBasicRate and
168 * pDevice->byTopOFDMBasicRate
170 CARDbAddBasicRate((void *)pDevice, RATEwGetRateIdx(byRate));
171 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ParseMaxRate AddBasicRate: %d\n", RATEwGetRateIdx(byRate));
173 byRate = (u8)(pItemRates->abyRates[ii]&0x7F);
174 if (byHighSuppRate == 0)
175 byHighSuppRate = byRate;
176 if (byRate > byHighSuppRate)
177 byHighSuppRate = byRate;
178 *pwSuppRate |= (1<<RATEwGetRateIdx(byRate));
180 if ((pItemExtRates != NULL) && (pItemExtRates->byElementID == WLAN_EID_EXTSUPP_RATES) &&
181 (pDevice->byBBType != BB_TYPE_11B)) {
183 unsigned int uExtRateLen = pItemExtRates->len;
185 if (uExtRateLen > WLAN_RATES_MAXLEN)
186 uExtRateLen = WLAN_RATES_MAXLEN;
188 for (ii = 0; ii < uExtRateLen ; ii++) {
189 byRate = (u8)(pItemExtRates->abyRates[ii]);
190 /* select highest basic rate */
191 if (WLAN_MGMT_IS_BASICRATE(pItemExtRates->abyRates[ii])) {
193 * add to basic rate set, update pDevice->byTopCCKBasicRate and
194 * pDevice->byTopOFDMBasicRate
196 CARDbAddBasicRate((void *)pDevice, RATEwGetRateIdx(byRate));
197 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ParseMaxRate AddBasicRate: %d\n", RATEwGetRateIdx(byRate));
199 byRate = (u8)(pItemExtRates->abyRates[ii]&0x7F);
200 if (byHighSuppRate == 0)
201 byHighSuppRate = byRate;
202 if (byRate > byHighSuppRate)
203 byHighSuppRate = byRate;
204 *pwSuppRate |= (1<<RATEwGetRateIdx(byRate));
206 /* DBG_PRN_GRP09(("ParseMaxRate : HighSuppRate: %d, %X\n",
207 RATEwGetRateIdx(byRate), byRate)); */
211 if ((pDevice->byPacketType == PK_TYPE_11GB)
212 && CARDbIsOFDMinBasicRate((void *)pDevice)) {
213 pDevice->byPacketType = PK_TYPE_11GA;
216 *pbyTopCCKRate = pDevice->byTopCCKBasicRate;
217 *pbyTopOFDMRate = pDevice->byTopOFDMBasicRate;
218 *pwMaxSuppRate = RATEwGetRateIdx(byHighSuppRate);
219 if ((pDevice->byPacketType==PK_TYPE_11B) || (pDevice->byPacketType==PK_TYPE_11GB))
220 *pwMaxBasicRate = pDevice->byTopCCKBasicRate;
221 else
222 *pwMaxBasicRate = pDevice->byTopOFDMBasicRate;
223 if (wOldBasicRate != pDevice->wBasicRate)
224 CARDvSetRSPINF((void *)pDevice, pDevice->byBBType);
226 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Exit ParseMaxRate\n");
231 * Routine Description:
232 * Rate fallback Algorithm Implementaion
234 * Parameters:
235 * In:
236 * pDevice - Pointer to the adapter
237 * psNodeDBTable - Pointer to Node Data Base
238 * Out:
239 * none
241 * Return Value: none
244 #define AUTORATE_TXCNT_THRESHOLD 20
245 #define AUTORATE_INC_THRESHOLD 30
247 void RATEvTxRateFallBack(struct vnt_private *pDevice,
248 PKnownNodeDB psNodeDBTable)
250 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
251 u16 wIdxDownRate = 0;
252 int ii;
253 int bAutoRate[MAX_RATE] = {true, true, true, true, false, false, true,
254 true, true, true, true, true};
255 u32 dwThroughputTbl[MAX_RATE] = {10, 20, 55, 110, 60, 90, 120, 180,
256 240, 360, 480, 540};
257 u32 dwThroughput = 0;
258 u16 wIdxUpRate = 0;
259 u32 dwTxDiff = 0;
261 if (pMgmt->eScanState != WMAC_NO_SCANNING)
262 return; /* Don't do Fallback when scanning Channel */
264 psNodeDBTable->uTimeCount++;
266 if (psNodeDBTable->uTxFail[MAX_RATE] > psNodeDBTable->uTxOk[MAX_RATE])
267 dwTxDiff = psNodeDBTable->uTxFail[MAX_RATE] - psNodeDBTable->uTxOk[MAX_RATE];
269 if ((psNodeDBTable->uTxOk[MAX_RATE] < AUTORATE_TXOK_CNT) &&
270 (dwTxDiff < AUTORATE_TXFAIL_CNT) &&
271 (psNodeDBTable->uTimeCount < AUTORATE_TIMEOUT)) {
272 return;
275 if (psNodeDBTable->uTimeCount >= AUTORATE_TIMEOUT) {
276 psNodeDBTable->uTimeCount = 0;
279 for (ii = 0; ii < MAX_RATE; ii++) {
280 if (psNodeDBTable->wSuppRate & (0x0001<<ii)) {
281 if (bAutoRate[ii] == true) {
282 wIdxUpRate = (u16) ii;
284 } else {
285 bAutoRate[ii] = false;
289 for (ii = 0; ii <= psNodeDBTable->wTxDataRate; ii++) {
290 if ( (psNodeDBTable->uTxOk[ii] != 0) ||
291 (psNodeDBTable->uTxFail[ii] != 0) ) {
292 dwThroughputTbl[ii] *= psNodeDBTable->uTxOk[ii];
293 if (ii < RATE_11M) {
294 psNodeDBTable->uTxFail[ii] *= 4;
296 dwThroughputTbl[ii] /= (psNodeDBTable->uTxOk[ii] + psNodeDBTable->uTxFail[ii]);
298 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Rate %d,Ok: %d, Fail:%d, Throughput:%d\n",
299 ii, (int)psNodeDBTable->uTxOk[ii], (int)psNodeDBTable->uTxFail[ii], (int)dwThroughputTbl[ii]);
301 dwThroughput = dwThroughputTbl[psNodeDBTable->wTxDataRate];
303 wIdxDownRate = psNodeDBTable->wTxDataRate;
304 for (ii = psNodeDBTable->wTxDataRate; ii > 0;) {
305 ii--;
306 if ( (dwThroughputTbl[ii] > dwThroughput) &&
307 (bAutoRate[ii]==true) ) {
308 dwThroughput = dwThroughputTbl[ii];
309 wIdxDownRate = (u16) ii;
312 psNodeDBTable->wTxDataRate = wIdxDownRate;
313 if (psNodeDBTable->uTxOk[MAX_RATE]) {
314 if (psNodeDBTable->uTxOk[MAX_RATE] >
315 (psNodeDBTable->uTxFail[MAX_RATE] * 4) ) {
316 psNodeDBTable->wTxDataRate = wIdxUpRate;
318 } else { /* adhoc, if uTxOk(total) == 0 & uTxFail(total) == 0 */
319 if (psNodeDBTable->uTxFail[MAX_RATE] == 0)
320 psNodeDBTable->wTxDataRate = wIdxUpRate;
323 if (pDevice->byBBType == BB_TYPE_11A) {
324 if (psNodeDBTable->wTxDataRate <= RATE_11M)
325 psNodeDBTable->wTxDataRate = RATE_6M;
327 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]);
328 s_vResetCounter(psNodeDBTable);
329 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Rate: %d, U:%d, D:%d\n", (int)psNodeDBTable->wTxDataRate, (int)wIdxUpRate, (int)wIdxDownRate);
330 return;
335 * Description:
336 * This routine is used to assemble available Rate IE.
338 * Parameters:
339 * In:
340 * pDevice
341 * Out:
343 * Return Value: None
347 RATEuSetIE (
348 PWLAN_IE_SUPP_RATES pSrcRates,
349 PWLAN_IE_SUPP_RATES pDstRates,
350 unsigned int uRateLen
353 unsigned int ii, uu, uRateCnt = 0;
355 if ((pSrcRates == NULL) || (pDstRates == NULL))
356 return 0;
358 if (pSrcRates->len == 0)
359 return 0;
361 for (ii = 0; ii < uRateLen; ii++) {
362 for (uu = 0; uu < pSrcRates->len; uu++) {
363 if ((pSrcRates->abyRates[uu] & 0x7F) == acbyIERate[ii]) {
364 pDstRates->abyRates[uRateCnt ++] = pSrcRates->abyRates[uu];
365 break;
369 return (u8)uRateCnt;