c1cf2bf4680d36f49682774eb9d0f99a12dd510b
[firefly-linux-kernel-4.4.55.git] / drivers / staging / rt2860 / 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
39
40 #include "../rt_config.h"
41
42
43 char* GetPhyMode(
44         int Mode)
45 {
46         switch(Mode)
47         {
48                 case MODE_CCK:
49                         return "CCK";
50
51                 case MODE_OFDM:
52                         return "OFDM";
53                 case MODE_HTMIX:
54                         return "HTMIX";
55
56                 case MODE_HTGREENFIELD:
57                         return "GREEN";
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                 case BW_40:
75                         return "40M";
76                 default:
77                         return "N/A";
78         }
79 }
80
81
82 /*
83     ==========================================================================
84     Description:
85         Set Country Region to pAd->CommonCfg.CountryRegion.
86         This command will not work, if the field of CountryRegion in eeprom is programmed.
87
88     Return:
89         TRUE if all parameters are OK, FALSE otherwise
90     ==========================================================================
91 */
92 INT RT_CfgSetCountryRegion(
93         IN PRTMP_ADAPTER        pAd,
94         IN PSTRING                      arg,
95         IN INT                          band)
96 {
97         LONG region, regionMax;
98         UCHAR *pCountryRegion;
99
100         region = simple_strtol(arg, 0, 10);
101
102         if (band == BAND_24G)
103         {
104                 pCountryRegion = &pAd->CommonCfg.CountryRegion;
105                 regionMax = REGION_MAXIMUM_BG_BAND;
106         }
107         else
108         {
109                 pCountryRegion = &pAd->CommonCfg.CountryRegionForABand;
110                 regionMax = REGION_MAXIMUM_A_BAND;
111         }
112
113         // TODO: Is it neccesay for following check???
114         // Country can be set only when EEPROM not programmed
115         if (*pCountryRegion & 0x80)
116         {
117                 DBGPRINT(RT_DEBUG_ERROR, ("CfgSetCountryRegion():CountryRegion in eeprom was programmed\n"));
118                 return FALSE;
119         }
120
121         if((region >= 0) && (region <= REGION_MAXIMUM_BG_BAND))
122         {
123                 *pCountryRegion= (UCHAR) region;
124         }
125         else if ((region == REGION_31_BG_BAND) && (band == BAND_24G))
126         {
127                 *pCountryRegion = (UCHAR) region;
128         }
129         else
130         {
131                 DBGPRINT(RT_DEBUG_ERROR, ("CfgSetCountryRegion():region(%ld) out of range!\n", region));
132                 return FALSE;
133         }
134
135         return TRUE;
136
137 }
138
139
140 /*
141     ==========================================================================
142     Description:
143         Set Wireless Mode
144     Return:
145         TRUE if all parameters are OK, FALSE otherwise
146     ==========================================================================
147 */
148 INT RT_CfgSetWirelessMode(
149         IN      PRTMP_ADAPTER   pAd,
150         IN      PSTRING                 arg)
151 {
152         INT             MaxPhyMode = PHY_11G;
153         LONG    WirelessMode;
154
155         MaxPhyMode = PHY_11N_5G;
156
157         WirelessMode = simple_strtol(arg, 0, 10);
158         if (WirelessMode <= MaxPhyMode)
159         {
160                 pAd->CommonCfg.PhyMode = WirelessMode;
161                 return TRUE;
162         }
163
164         return FALSE;
165
166 }
167
168
169 INT RT_CfgSetShortSlot(
170         IN      PRTMP_ADAPTER   pAd,
171         IN      PSTRING                 arg)
172 {
173         LONG ShortSlot;
174
175         ShortSlot = simple_strtol(arg, 0, 10);
176
177         if (ShortSlot == 1)
178                 pAd->CommonCfg.bUseShortSlotTime = TRUE;
179         else if (ShortSlot == 0)
180                 pAd->CommonCfg.bUseShortSlotTime = FALSE;
181         else
182                 return FALSE;  //Invalid argument
183
184         return TRUE;
185 }
186
187
188 /*
189     ==========================================================================
190     Description:
191         Set WEP KEY base on KeyIdx
192     Return:
193         TRUE if all parameters are OK, FALSE otherwise
194     ==========================================================================
195 */
196 INT     RT_CfgSetWepKey(
197         IN      PRTMP_ADAPTER   pAd,
198         IN      PSTRING                 keyString,
199         IN      CIPHER_KEY              *pSharedKey,
200         IN      INT                             keyIdx)
201 {
202         INT                             KeyLen;
203         INT                             i;
204         UCHAR                   CipherAlg = CIPHER_NONE;
205         BOOLEAN                 bKeyIsHex = FALSE;
206
207         // TODO: Shall we do memset for the original key info??
208         memset(pSharedKey, 0, sizeof(CIPHER_KEY));
209         KeyLen = strlen(keyString);
210         switch (KeyLen)
211         {
212                 case 5: //wep 40 Ascii type
213                 case 13: //wep 104 Ascii type
214                         bKeyIsHex = FALSE;
215                         pSharedKey->KeyLen = KeyLen;
216                         NdisMoveMemory(pSharedKey->Key, keyString, KeyLen);
217                         break;
218
219                 case 10: //wep 40 Hex type
220                 case 26: //wep 104 Hex type
221                         for(i=0; i < KeyLen; i++)
222                         {
223                                 if( !isxdigit(*(keyString+i)) )
224                                         return FALSE;  //Not Hex value;
225                         }
226                         bKeyIsHex = TRUE;
227                         pSharedKey->KeyLen = KeyLen/2 ;
228                         AtoH(keyString, pSharedKey->Key, pSharedKey->KeyLen);
229                         break;
230
231                 default: //Invalid argument
232                         DBGPRINT(RT_DEBUG_TRACE, ("RT_CfgSetWepKey(keyIdx=%d):Invalid argument (arg=%s)\n", keyIdx, keyString));
233                         return FALSE;
234         }
235
236         pSharedKey->CipherAlg = ((KeyLen % 5) ? CIPHER_WEP128 : CIPHER_WEP64);
237         DBGPRINT(RT_DEBUG_TRACE, ("RT_CfgSetWepKey:(KeyIdx=%d,type=%s, Alg=%s)\n",
238                                                 keyIdx, (bKeyIsHex == FALSE ? "Ascii" : "Hex"), CipherName[CipherAlg]));
239
240         return TRUE;
241 }
242
243
244 /*
245     ==========================================================================
246     Description:
247         Set WPA PSK key
248
249     Arguments:
250         pAdapter        Pointer to our adapter
251         keyString       WPA pre-shared key string
252         pHashStr        String used for password hash function
253         hashStrLen      Lenght of the hash string
254         pPMKBuf         Output buffer of WPAPSK key
255
256     Return:
257         TRUE if all parameters are OK, FALSE otherwise
258     ==========================================================================
259 */
260 INT RT_CfgSetWPAPSKKey(
261         IN RTMP_ADAPTER *pAd,
262         IN PSTRING              keyString,
263         IN UCHAR                *pHashStr,
264         IN INT                  hashStrLen,
265         OUT PUCHAR              pPMKBuf)
266 {
267         int keyLen;
268         UCHAR keyMaterial[40];
269
270         keyLen = strlen(keyString);
271         if ((keyLen < 8) || (keyLen > 64))
272         {
273                 DBGPRINT(RT_DEBUG_TRACE, ("WPAPSK Key length(%d) error, required 8 ~ 64 characters!(keyStr=%s)\n",
274                                                                         keyLen, keyString));
275                 return FALSE;
276         }
277
278         memset(pPMKBuf, 0, 32);
279         if (keyLen == 64)
280         {
281             AtoH(keyString, pPMKBuf, 32);
282         }
283         else
284         {
285             PasswordHash(keyString, pHashStr, hashStrLen, keyMaterial);
286             NdisMoveMemory(pPMKBuf, keyMaterial, 32);
287         }
288
289         return TRUE;
290 }