Linux-libre 2.6.32.42-gnu1
[librecmc/linux-libre.git] / drivers / staging / rtl8192e / ieee80211 / dot11d.c
1 #ifdef ENABLE_DOT11D
2 //-----------------------------------------------------------------------------
3 //      File:
4 //              Dot11d.c
5 //
6 //      Description:
7 //              Implement 802.11d.
8 //
9 //-----------------------------------------------------------------------------
10
11 #include "dot11d.h"
12
13 void
14 Dot11d_Init(struct ieee80211_device *ieee)
15 {
16         PRT_DOT11D_INFO pDot11dInfo = GET_DOT11D_INFO(ieee);
17
18         pDot11dInfo->bEnabled = 0;
19
20         pDot11dInfo->State = DOT11D_STATE_NONE;
21         pDot11dInfo->CountryIeLen = 0;
22         memset(pDot11dInfo->channel_map, 0, MAX_CHANNEL_NUMBER+1);
23         memset(pDot11dInfo->MaxTxPwrDbmList, 0xFF, MAX_CHANNEL_NUMBER+1);
24         RESET_CIE_WATCHDOG(ieee);
25
26         printk("Dot11d_Init()\n");
27 }
28
29 //
30 //      Description:
31 //              Reset to the state as we are just entering a regulatory domain.
32 //
33 void
34 Dot11d_Reset(struct ieee80211_device *ieee)
35 {
36         u32 i;
37         PRT_DOT11D_INFO pDot11dInfo = GET_DOT11D_INFO(ieee);
38 #if 0
39         if(!pDot11dInfo->bEnabled)
40                 return;
41 #endif
42         // Clear old channel map
43         memset(pDot11dInfo->channel_map, 0, MAX_CHANNEL_NUMBER+1);
44         memset(pDot11dInfo->MaxTxPwrDbmList, 0xFF, MAX_CHANNEL_NUMBER+1);
45         // Set new channel map
46         for (i=1; i<=11; i++) {
47                 (pDot11dInfo->channel_map)[i] = 1;
48         }
49         for (i=12; i<=14; i++) {
50                 (pDot11dInfo->channel_map)[i] = 2;
51         }
52
53         pDot11dInfo->State = DOT11D_STATE_NONE;
54         pDot11dInfo->CountryIeLen = 0;
55         RESET_CIE_WATCHDOG(ieee);
56
57         //printk("Dot11d_Reset()\n");
58 }
59
60 //
61 //      Description:
62 //              Update country IE from Beacon or Probe Resopnse
63 //              and configure PHY for operation in the regulatory domain.
64 //
65 //      TODO:
66 //              Configure Tx power.
67 //
68 //      Assumption:
69 //              1. IS_DOT11D_ENABLE() is TRUE.
70 //              2. Input IE is an valid one.
71 //
72 void
73 Dot11d_UpdateCountryIe(
74         struct ieee80211_device *dev,
75         u8 *            pTaddr,
76         u16     CoutryIeLen,
77         u8 * pCoutryIe
78         )
79 {
80         PRT_DOT11D_INFO pDot11dInfo = GET_DOT11D_INFO(dev);
81         u8 i, j, NumTriples, MaxChnlNum;
82         PCHNL_TXPOWER_TRIPLE pTriple;
83
84         memset(pDot11dInfo->channel_map, 0, MAX_CHANNEL_NUMBER+1);
85         memset(pDot11dInfo->MaxTxPwrDbmList, 0xFF, MAX_CHANNEL_NUMBER+1);
86         MaxChnlNum = 0;
87         NumTriples = (CoutryIeLen - 3) / 3; // skip 3-byte country string.
88         pTriple = (PCHNL_TXPOWER_TRIPLE)(pCoutryIe + 3);
89         for(i = 0; i < NumTriples; i++)
90         {
91                 if(MaxChnlNum >= pTriple->FirstChnl)
92                 { // It is not in a monotonically increasing order, so stop processing.
93                         printk("Dot11d_UpdateCountryIe(): Invalid country IE, skip it........1\n");
94                         return;
95                 }
96                 if(MAX_CHANNEL_NUMBER < (pTriple->FirstChnl + pTriple->NumChnls))
97                 { // It is not a valid set of channel id, so stop processing.
98                         printk("Dot11d_UpdateCountryIe(): Invalid country IE, skip it........2\n");
99                         return;
100                 }
101
102                 for(j = 0 ; j < pTriple->NumChnls; j++)
103                 {
104                         pDot11dInfo->channel_map[pTriple->FirstChnl + j] = 1;
105                         pDot11dInfo->MaxTxPwrDbmList[pTriple->FirstChnl + j] = pTriple->MaxTxPowerInDbm;
106                         MaxChnlNum = pTriple->FirstChnl + j;
107                 }
108
109                 pTriple = (PCHNL_TXPOWER_TRIPLE)((u8*)pTriple + 3);
110         }
111 #if 1
112         //printk("Dot11d_UpdateCountryIe(): Channel List:\n");
113         printk("Channel List:");
114         for(i=1; i<= MAX_CHANNEL_NUMBER; i++)
115                 if(pDot11dInfo->channel_map[i] > 0)
116                         printk(" %d", i);
117         printk("\n");
118 #endif
119
120         UPDATE_CIE_SRC(dev, pTaddr);
121
122         pDot11dInfo->CountryIeLen = CoutryIeLen;
123         memcpy(pDot11dInfo->CountryIeBuf, pCoutryIe,CoutryIeLen);
124         pDot11dInfo->State = DOT11D_STATE_LEARNED;
125 }
126
127
128 u8
129 DOT11D_GetMaxTxPwrInDbm(
130         struct ieee80211_device *dev,
131         u8 Channel
132         )
133 {
134         PRT_DOT11D_INFO pDot11dInfo = GET_DOT11D_INFO(dev);
135         u8 MaxTxPwrInDbm = 255;
136
137         if(MAX_CHANNEL_NUMBER < Channel)
138         {
139                 printk("DOT11D_GetMaxTxPwrInDbm(): Invalid Channel\n");
140                 return MaxTxPwrInDbm;
141         }
142         if(pDot11dInfo->channel_map[Channel])
143         {
144                 MaxTxPwrInDbm = pDot11dInfo->MaxTxPwrDbmList[Channel];
145         }
146
147         return MaxTxPwrInDbm;
148 }
149
150
151 void
152 DOT11D_ScanComplete(
153         struct ieee80211_device * dev
154         )
155 {
156         PRT_DOT11D_INFO pDot11dInfo = GET_DOT11D_INFO(dev);
157
158         switch(pDot11dInfo->State)
159         {
160         case DOT11D_STATE_LEARNED:
161                 pDot11dInfo->State = DOT11D_STATE_DONE;
162                 break;
163
164         case DOT11D_STATE_DONE:
165                 if( GET_CIE_WATCHDOG(dev) == 0 )
166                 { // Reset country IE if previous one is gone.
167                         Dot11d_Reset(dev);
168                 }
169                 break;
170         case DOT11D_STATE_NONE:
171                 break;
172         }
173 }
174
175 int IsLegalChannel(
176         struct ieee80211_device * dev,
177         u8 channel
178 )
179 {
180         PRT_DOT11D_INFO pDot11dInfo = GET_DOT11D_INFO(dev);
181
182         if(MAX_CHANNEL_NUMBER < channel)
183         {
184                 printk("IsLegalChannel(): Invalid Channel\n");
185                 return 0;
186         }
187         if(pDot11dInfo->channel_map[channel] > 0)
188                 return 1;
189         return 0;
190 }
191
192 int ToLegalChannel(
193         struct ieee80211_device * dev,
194         u8 channel
195 )
196 {
197         PRT_DOT11D_INFO pDot11dInfo = GET_DOT11D_INFO(dev);
198         u8 default_chn = 0;
199         u32 i = 0;
200
201         for (i=1; i<= MAX_CHANNEL_NUMBER; i++)
202         {
203                 if(pDot11dInfo->channel_map[i] > 0)
204                 {
205                         default_chn = i;
206                         break;
207                 }
208         }
209
210         if(MAX_CHANNEL_NUMBER < channel)
211         {
212                 printk("IsLegalChannel(): Invalid Channel\n");
213                 return default_chn;
214         }
215
216         if(pDot11dInfo->channel_map[channel] > 0)
217                 return channel;
218
219         return default_chn;
220 }
221 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
222 //EXPORT_SYMBOL(Dot11d_Init);
223 //EXPORT_SYMBOL(Dot11d_Reset);
224 //EXPORT_SYMBOL(Dot11d_UpdateCountryIe);
225 //EXPORT_SYMBOL(DOT11D_GetMaxTxPwrInDbm);
226 //EXPORT_SYMBOL(DOT11D_ScanComplete);
227 //EXPORT_SYMBOL(IsLegalChannel);
228 //EXPORT_SYMBOL(ToLegalChannel);
229 #else
230 EXPORT_SYMBOL_NOVERS(Dot11d_Init);
231 EXPORT_SYMBOL_NOVERS(Dot11d_Reset);
232 EXPORT_SYMBOL_NOVERS(Dot11d_UpdateCountryIe);
233 EXPORT_SYMBOL_NOVERS(DOT11D_GetMaxTxPwrInDbm);
234 EXPORT_SYMBOL_NOVERS(DOT11D_ScanComplete);
235 EXPORT_SYMBOL_NOVERS(IsLegalChannel);
236 EXPORT_SYMBOL_NOVERS(ToLegalChannel);
237 #endif
238
239 #endif