]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/rt3090/common/cmm_cfg.c
Staging: rt2860: add RT3090 chipset support
[net-next-2.6.git] / drivers / staging / rt3090 / common / cmm_cfg.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify  *
11  * it under the terms of the GNU General Public License as published by  *
12  * the Free Software Foundation; either version 2 of the License, or     *
13  * (at your option) any later version.                                   *
14  *                                                                       *
15  * This program is distributed in the hope that it will be useful,       *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  * GNU General Public License for more details.                          *
19  *                                                                       *
20  * You should have received a copy of the GNU General Public License     *
21  * along with this program; if not, write to the                         *
22  * Free Software Foundation, Inc.,                                       *
23  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  *                                                                       *
25  *************************************************************************
26
27     Module Name:
28         cmm_cfg.c
29
30     Abstract:
31     Ralink WiFi Driver configuration related subroutines
32
33     Revision History:
34     Who          When          What
35     ---------    ----------    ----------------------------------------------
36 */
37
38 #include "../rt_config.h"
39
40
41 char* GetPhyMode(
42         int Mode)
43 {
44         switch(Mode)
45         {
46                 case MODE_CCK:
47                         return "CCK";
48
49                 case MODE_OFDM:
50                         return "OFDM";
51 #ifdef DOT11_N_SUPPORT
52                 case MODE_HTMIX:
53                         return "HTMIX";
54
55                 case MODE_HTGREENFIELD:
56                         return "GREEN";
57 #endif // DOT11_N_SUPPORT //
58                 default:
59                         return "N/A";
60         }
61 }
62
63
64 char* GetBW(
65         int BW)
66 {
67         switch(BW)
68         {
69                 case BW_10:
70                         return "10M";
71
72                 case BW_20:
73                         return "20M";
74 #ifdef DOT11_N_SUPPORT
75                 case BW_40:
76                         return "40M";
77 #endif // DOT11_N_SUPPORT //
78                 default:
79                         return "N/A";
80         }
81 }
82
83
84 /*
85     ==========================================================================
86     Description:
87         Set Country Region to pAd->CommonCfg.CountryRegion.
88         This command will not work, if the field of CountryRegion in eeprom is programmed.
89
90     Return:
91         TRUE if all parameters are OK, FALSE otherwise
92     ==========================================================================
93 */
94 INT RT_CfgSetCountryRegion(
95         IN PRTMP_ADAPTER        pAd,
96         IN PSTRING                      arg,
97         IN INT                          band)
98 {
99         LONG region, regionMax;
100         UCHAR *pCountryRegion;
101
102         region = simple_strtol(arg, 0, 10);
103
104         if (band == BAND_24G)
105         {
106                 pCountryRegion = &pAd->CommonCfg.CountryRegion;
107                 regionMax = REGION_MAXIMUM_BG_BAND;
108         }
109         else
110         {
111                 pCountryRegion = &pAd->CommonCfg.CountryRegionForABand;
112                 regionMax = REGION_MAXIMUM_A_BAND;
113         }
114
115         // TODO: Is it neccesay for following check???
116         // Country can be set only when EEPROM not programmed
117         if (*pCountryRegion & 0x80)
118         {
119                 DBGPRINT(RT_DEBUG_ERROR, ("CfgSetCountryRegion():CountryRegion in eeprom was programmed\n"));
120                 return FALSE;
121         }
122
123         if((region >= 0) && (region <= REGION_MAXIMUM_BG_BAND))
124         {
125                 *pCountryRegion= (UCHAR) region;
126         }
127         else if ((region == REGION_31_BG_BAND) && (band == BAND_24G))
128         {
129                 *pCountryRegion = (UCHAR) region;
130         }
131         else
132         {
133                 DBGPRINT(RT_DEBUG_ERROR, ("CfgSetCountryRegion():region(%ld) out of range!\n", region));
134                 return FALSE;
135         }
136
137         return TRUE;
138
139 }
140
141
142 /*
143     ==========================================================================
144     Description:
145         Set Wireless Mode
146     Return:
147         TRUE if all parameters are OK, FALSE otherwise
148     ==========================================================================
149 */
150 INT RT_CfgSetWirelessMode(
151         IN      PRTMP_ADAPTER   pAd,
152         IN      PSTRING                 arg)
153 {
154         INT             MaxPhyMode = PHY_11G;
155         LONG    WirelessMode;
156
157 #ifdef DOT11_N_SUPPORT
158         MaxPhyMode = PHY_11N_5G;
159 #endif // DOT11_N_SUPPORT //
160
161         WirelessMode = simple_strtol(arg, 0, 10);
162         if (WirelessMode <= MaxPhyMode)
163         {
164                 pAd->CommonCfg.PhyMode = WirelessMode;
165                 pAd->CommonCfg.DesiredPhyMode = WirelessMode;
166                 return TRUE;
167         }
168
169         return FALSE;
170
171 }
172
173
174 INT RT_CfgSetShortSlot(
175         IN      PRTMP_ADAPTER   pAd,
176         IN      PSTRING                 arg)
177 {
178         LONG ShortSlot;
179
180         ShortSlot = simple_strtol(arg, 0, 10);
181
182         if (ShortSlot == 1)
183                 pAd->CommonCfg.bUseShortSlotTime = TRUE;
184         else if (ShortSlot == 0)
185                 pAd->CommonCfg.bUseShortSlotTime = FALSE;
186         else
187                 return FALSE;  //Invalid argument
188
189         return TRUE;
190 }
191
192
193 /*
194     ==========================================================================
195     Description:
196         Set WEP KEY base on KeyIdx
197     Return:
198         TRUE if all parameters are OK, FALSE otherwise
199     ==========================================================================
200 */
201 INT     RT_CfgSetWepKey(
202         IN      PRTMP_ADAPTER   pAd,
203         IN      PSTRING                 keyString,
204         IN      CIPHER_KEY              *pSharedKey,
205         IN      INT                             keyIdx)
206 {
207         INT                             KeyLen;
208         INT                             i;
209         UCHAR                   CipherAlg = CIPHER_NONE;
210         BOOLEAN                 bKeyIsHex = FALSE;
211
212         // TODO: Shall we do memset for the original key info??
213         memset(pSharedKey, 0, sizeof(CIPHER_KEY));
214         KeyLen = strlen(keyString);
215         switch (KeyLen)
216         {
217                 case 5: //wep 40 Ascii type
218                 case 13: //wep 104 Ascii type
219                         bKeyIsHex = FALSE;
220                         pSharedKey->KeyLen = KeyLen;
221                         NdisMoveMemory(pSharedKey->Key, keyString, KeyLen);
222                         break;
223
224                 case 10: //wep 40 Hex type
225                 case 26: //wep 104 Hex type
226                         for(i=0; i < KeyLen; i++)
227                         {
228                                 if( !isxdigit(*(keyString+i)) )
229                                         return FALSE;  //Not Hex value;
230                         }
231                         bKeyIsHex = TRUE;
232                         pSharedKey->KeyLen = KeyLen/2 ;
233                         AtoH(keyString, pSharedKey->Key, pSharedKey->KeyLen);
234                         break;
235
236                 default: //Invalid argument
237                         DBGPRINT(RT_DEBUG_TRACE, ("RT_CfgSetWepKey(keyIdx=%d):Invalid argument (arg=%s)\n", keyIdx, keyString));
238                         return FALSE;
239         }
240
241         pSharedKey->CipherAlg = ((KeyLen % 5) ? CIPHER_WEP128 : CIPHER_WEP64);
242         DBGPRINT(RT_DEBUG_TRACE, ("RT_CfgSetWepKey:(KeyIdx=%d,type=%s, Alg=%s)\n",
243                                                 keyIdx, (bKeyIsHex == FALSE ? "Ascii" : "Hex"), CipherName[CipherAlg]));
244
245         return TRUE;
246 }
247
248
249 /*
250     ==========================================================================
251     Description:
252         Set WPA PSK key
253
254     Arguments:
255         pAdapter        Pointer to our adapter
256         keyString       WPA pre-shared key string
257         pHashStr        String used for password hash function
258         hashStrLen      Lenght of the hash string
259         pPMKBuf         Output buffer of WPAPSK key
260
261     Return:
262         TRUE if all parameters are OK, FALSE otherwise
263     ==========================================================================
264 */
265 INT RT_CfgSetWPAPSKKey(
266         IN RTMP_ADAPTER *pAd,
267         IN PSTRING              keyString,
268         IN UCHAR                *pHashStr,
269         IN INT                  hashStrLen,
270         OUT PUCHAR              pPMKBuf)
271 {
272         int keyLen;
273         UCHAR keyMaterial[40];
274
275         keyLen = strlen(keyString);
276         if ((keyLen < 8) || (keyLen > 64))
277         {
278                 DBGPRINT(RT_DEBUG_TRACE, ("WPAPSK Key length(%d) error, required 8 ~ 64 characters!(keyStr=%s)\n",
279                                                                         keyLen, keyString));
280                 return FALSE;
281         }
282
283         memset(pPMKBuf, 0, 32);
284         if (keyLen == 64)
285         {
286             AtoH(keyString, pPMKBuf, 32);
287         }
288         else
289         {
290             PasswordHash(keyString, pHashStr, hashStrLen, keyMaterial);
291             NdisMoveMemory(pPMKBuf, keyMaterial, 32);
292         }
293
294         return TRUE;
295 }