staging: rtl8192e: Convert typedefs that can be replaced with #define
[pandora-kernel.git] / drivers / staging / rtl8192e / rtl_cam.c
1 /******************************************************************************
2  * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
3  *
4  * Based on the r8180 driver, which is:
5  * Copyright 2004-2005 Andrea Merello <andreamrl@tiscali.it>, et al.
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of version 2 of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18  *
19  * The full GNU General Public License is included in this distribution in the
20  * file called LICENSE.
21  *
22  * Contact Information:
23  * wlanfae <wlanfae@realtek.com>
24 ******************************************************************************/
25 #include "rtl_core.h"
26 #include "r8192E_phy.h"
27 #include "r8192E_phyreg.h"
28 #include "r8190P_rtl8256.h" /* RTL8225 Radio frontend */
29 #include "r8192E_cmdpkt.h"
30
31 extern int hwwep;
32 void CamResetAllEntry(struct net_device *dev)
33 {
34         u32 ulcommand = 0;
35
36         ulcommand |= BIT31|BIT30;
37         write_nic_dword(dev, RWCAM, ulcommand);
38 }
39
40 void write_cam(struct net_device *dev, u8 addr, u32 data)
41 {
42         write_nic_dword(dev, WCAMI, data);
43         write_nic_dword(dev, RWCAM, BIT31|BIT16|(addr&0xff) );
44 }
45
46 u32 read_cam(struct net_device *dev, u8 addr)
47 {
48         write_nic_dword(dev, RWCAM, 0x80000000|(addr&0xff) );
49         return read_nic_dword(dev, 0xa8);
50 }
51
52 void EnableHWSecurityConfig8192(struct net_device *dev)
53 {
54         u8 SECR_value = 0x0;
55         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
56         struct rtllib_device* ieee = priv->rtllib;
57         SECR_value = SCR_TxEncEnable | SCR_RxDecEnable;
58         if (((KEY_TYPE_WEP40 == ieee->pairwise_key_type) || (KEY_TYPE_WEP104 == ieee->pairwise_key_type)) && (priv->rtllib->auth_mode != 2))
59         {
60                 SECR_value |= SCR_RxUseDK;
61                 SECR_value |= SCR_TxUseDK;
62         }
63         else if ((ieee->iw_mode == IW_MODE_ADHOC) && (ieee->pairwise_key_type & (KEY_TYPE_CCMP | KEY_TYPE_TKIP)))
64         {
65                 SECR_value |= SCR_RxUseDK;
66                 SECR_value |= SCR_TxUseDK;
67         }
68
69
70         ieee->hwsec_active = 1;
71         if ((ieee->pHTInfo->IOTAction&HT_IOT_ACT_PURE_N_MODE) || !hwwep)
72         {
73                 ieee->hwsec_active = 0;
74                 SECR_value &= ~SCR_RxDecEnable;
75         }
76
77         RT_TRACE(COMP_SEC,"%s:, hwsec:%d, pairwise_key:%d, SECR_value:%x\n", __func__, \
78                         ieee->hwsec_active, ieee->pairwise_key_type, SECR_value);
79         {
80                 write_nic_byte(dev, SECR,  SECR_value);
81         }
82 }
83
84 void set_swcam(struct net_device *dev,
85                 u8 EntryNo,
86                 u8 KeyIndex,
87                 u16 KeyType,
88                 u8 *MacAddr,
89                 u8 DefaultKey,
90                 u32 *KeyContent,
91                 u8 is_mesh)
92 {
93         struct r8192_priv *priv = rtllib_priv(dev);
94         struct rtllib_device *ieee = priv->rtllib;
95         RT_TRACE(COMP_DBG, "===========>%s():EntryNo is %d,KeyIndex is "
96                  "%d,KeyType is %d,is_mesh is %d\n", __func__, EntryNo,
97                  KeyIndex, KeyType, is_mesh);
98         if (is_mesh){
99         } else {
100                 ieee->swcamtable[EntryNo].bused=true;
101                 ieee->swcamtable[EntryNo].key_index=KeyIndex;
102                 ieee->swcamtable[EntryNo].key_type=KeyType;
103                 memcpy(ieee->swcamtable[EntryNo].macaddr,MacAddr,6);
104                 ieee->swcamtable[EntryNo].useDK=DefaultKey;
105                 memcpy(ieee->swcamtable[EntryNo].key_buf,(u8*)KeyContent,16);
106         }
107 }
108
109 void setKey(struct net_device *dev,
110                 u8 EntryNo,
111                 u8 KeyIndex,
112                 u16 KeyType,
113                 u8 *MacAddr,
114                 u8 DefaultKey,
115                 u32 *KeyContent )
116 {
117         u32 TargetCommand = 0;
118         u32 TargetContent = 0;
119         u16 usConfig = 0;
120         u8 i;
121         struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
122         enum rt_rf_power_state rtState;
123         rtState = priv->rtllib->eRFPowerState;
124         if (priv->rtllib->PowerSaveControl.bInactivePs){
125                 if (rtState == eRfOff){
126                         if (priv->rtllib->RfOffReason > RF_CHANGE_BY_IPS)
127                         {
128                                 RT_TRACE(COMP_ERR, "%s(): RF is OFF.\n",__func__);
129                                 return ;
130                         }
131                         else{
132                                 down(&priv->rtllib->ips_sem);
133                                 IPSLeave(dev);
134                                 up(&priv->rtllib->ips_sem);                     }
135                 }
136         }
137         priv->rtllib->is_set_key = true;
138         if (EntryNo >= TOTAL_CAM_ENTRY)
139                 RT_TRACE(COMP_ERR, "cam entry exceeds in setKey()\n");
140
141         RT_TRACE(COMP_SEC, "====>to setKey(), dev:%p, EntryNo:%d, KeyIndex:%d,"
142                  "KeyType:%d, MacAddr"MAC_FMT"\n", dev,EntryNo, KeyIndex,
143                  KeyType, MAC_ARG(MacAddr));
144
145         if (DefaultKey)
146                 usConfig |= BIT15 | (KeyType<<2);
147         else
148                 usConfig |= BIT15 | (KeyType<<2) | KeyIndex;
149
150
151         for (i = 0; i < CAM_CONTENT_COUNT; i++) {
152                 TargetCommand  = i+CAM_CONTENT_COUNT*EntryNo;
153                 TargetCommand |= BIT31|BIT16;
154
155                 if (i == 0) {
156                         TargetContent = (u32)(*(MacAddr+0)) << 16|
157                                 (u32)(*(MacAddr+1)) << 24|
158                                 (u32)usConfig;
159
160                         write_nic_dword(dev, WCAMI, TargetContent);
161                         write_nic_dword(dev, RWCAM, TargetCommand);
162                 } else if (i == 1) {
163                         TargetContent = (u32)(*(MacAddr+2))      |
164                                 (u32)(*(MacAddr+3)) <<  8|
165                                 (u32)(*(MacAddr+4)) << 16|
166                                 (u32)(*(MacAddr+5)) << 24;
167                         write_nic_dword(dev, WCAMI, TargetContent);
168                         write_nic_dword(dev, RWCAM, TargetCommand);
169                 } else {
170                         if (KeyContent != NULL) {
171                                 write_nic_dword(dev, WCAMI, (u32)(*(KeyContent+i-2)) );
172                                 write_nic_dword(dev, RWCAM, TargetCommand);
173                                 udelay(100);
174                         }
175                 }
176         }
177         RT_TRACE(COMP_SEC,"=========>after set key, usconfig:%x\n", usConfig);
178 }
179
180 void CAM_read_entry(struct net_device *dev, u32 iIndex)
181 {
182         u32 target_command=0;
183         u32 target_content=0;
184         u8 entry_i=0;
185         u32 ulStatus;
186         s32 i=100;
187         for (entry_i=0;entry_i<CAM_CONTENT_COUNT;entry_i++)
188         {
189                 target_command= entry_i+CAM_CONTENT_COUNT*iIndex;
190                 target_command= target_command | BIT31;
191
192                 while((i--)>=0)
193                 {
194                         ulStatus = read_nic_dword(dev, RWCAM);
195                         if (ulStatus & BIT31){
196                                 continue;
197                         }
198                         else{
199                                 break;
200                         }
201                 }
202                 write_nic_dword(dev, RWCAM, target_command);
203                 RT_TRACE(COMP_SEC,"CAM_read_entry(): WRITE A0: %x \n",target_command);
204                 target_content = read_nic_dword(dev, RCAMO);
205                 RT_TRACE(COMP_SEC, "CAM_read_entry(): WRITE A8: %x \n",target_content);
206         }
207         printk("\n");
208 }
209
210 void CamRestoreAllEntry(        struct net_device *dev)
211 {
212         u8 EntryId = 0;
213         struct r8192_priv *priv = rtllib_priv(dev);
214         u8*     MacAddr = priv->rtllib->current_network.bssid;
215
216         static u8       CAM_CONST_ADDR[4][6] = {
217                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
218                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
219                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
220                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x03}};
221         static u8       CAM_CONST_BROAD[] =
222         {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
223
224         RT_TRACE(COMP_SEC, "CamRestoreAllEntry: \n");
225
226
227         if ((priv->rtllib->pairwise_key_type == KEY_TYPE_WEP40)||
228                         (priv->rtllib->pairwise_key_type == KEY_TYPE_WEP104))
229         {
230
231                 for (EntryId=0; EntryId<4; EntryId++)
232                 {
233                         {
234                                 MacAddr = CAM_CONST_ADDR[EntryId];
235                                 if (priv->rtllib->swcamtable[EntryId].bused )
236                                 {
237                                         setKey(dev,
238                                                         EntryId ,
239                                                         EntryId,
240                                                         priv->rtllib->pairwise_key_type,
241                                                         MacAddr,
242                                                         0,
243                                                         (u32*)(&priv->rtllib->swcamtable[EntryId].key_buf[0])
244                                               );
245                                 }
246                         }
247                 }
248
249         }
250         else if (priv->rtllib->pairwise_key_type == KEY_TYPE_TKIP)
251         {
252
253                 {
254                         if (priv->rtllib->iw_mode == IW_MODE_ADHOC)
255                         {
256                                 setKey(dev,
257                                                 4,
258                                                 0,
259                                                 priv->rtllib->pairwise_key_type,
260                                                 (u8*)dev->dev_addr,
261                                                 0,
262                                                 (u32*)(&priv->rtllib->swcamtable[4].key_buf[0])
263                                       );
264                         }
265                         else
266                         {
267                                 setKey(dev,
268                                                 4,
269                                                 0,
270                                                 priv->rtllib->pairwise_key_type,
271                                                 MacAddr,
272                                                 0,
273                                                 (u32*)(&priv->rtllib->swcamtable[4].key_buf[0])
274                                       );
275                         }
276
277                 }
278         }
279         else if (priv->rtllib->pairwise_key_type == KEY_TYPE_CCMP)
280         {
281
282                 {
283                         if (priv->rtllib->iw_mode == IW_MODE_ADHOC)
284                         {
285                                 setKey(dev,
286                                                 4,
287                                                 0,
288                                                 priv->rtllib->pairwise_key_type,
289                                                 (u8*)dev->dev_addr,
290                                                 0,
291                                                 (u32*)(&priv->rtllib->swcamtable[4].key_buf[0])
292                                       );
293                         }
294                         else
295                         {
296                                 setKey(dev,
297                                                 4,
298                                                 0,
299                                                 priv->rtllib->pairwise_key_type,
300                                                 MacAddr,
301                                                 0,
302                                                 (u32*)(&priv->rtllib->swcamtable[4].key_buf[0])
303                                       );
304                         }
305                 }
306         }
307
308
309
310         if (priv->rtllib->group_key_type == KEY_TYPE_TKIP)
311         {
312                 MacAddr = CAM_CONST_BROAD;
313                 for (EntryId=1 ; EntryId<4 ; EntryId++)
314                 {
315                         if (priv->rtllib->swcamtable[EntryId].bused )
316                         {
317                                 setKey(dev,
318                                                 EntryId,
319                                                 EntryId,
320                                                 priv->rtllib->group_key_type,
321                                                 MacAddr,
322                                                 0,
323                                                 (u32*)(&priv->rtllib->swcamtable[EntryId].key_buf[0])
324                                       );
325                         }
326                 }
327                 if (priv->rtllib->iw_mode == IW_MODE_ADHOC)
328                 {
329                         if (priv->rtllib->swcamtable[0].bused ){
330                                 setKey(dev,
331                                                 0,
332                                                 0,
333                                                 priv->rtllib->group_key_type,
334                                                 CAM_CONST_ADDR[0],
335                                                 0,
336                                                 (u32*)(&priv->rtllib->swcamtable[0].key_buf[0])
337                                       );
338                         }
339                         else
340                         {
341                                 RT_TRACE(COMP_ERR,"===>%s():ERR!! ADHOC TKIP ,but 0 entry is have no data\n",__func__);
342                                 return;
343                         }
344                 }
345         } else if (priv->rtllib->group_key_type == KEY_TYPE_CCMP) {
346                 MacAddr = CAM_CONST_BROAD;
347                 for (EntryId=1; EntryId<4 ; EntryId++)
348                 {
349                         if (priv->rtllib->swcamtable[EntryId].bused )
350                         {
351                                 setKey(dev,
352                                        EntryId ,
353                                        EntryId,
354                                        priv->rtllib->group_key_type,
355                                        MacAddr,
356                                        0,
357                                        (u32*)(&priv->rtllib->swcamtable[EntryId].key_buf[0]));
358                         }
359                 }
360
361                 if (priv->rtllib->iw_mode == IW_MODE_ADHOC) {
362                         if (priv->rtllib->swcamtable[0].bused) {
363                                 setKey(dev,
364                                         0 ,
365                                         0,
366                                         priv->rtllib->group_key_type,
367                                         CAM_CONST_ADDR[0],
368                                         0,
369                                         (u32*)(&priv->rtllib->swcamtable[0].key_buf[0]));
370                         } else {
371                                 RT_TRACE(COMP_ERR,"===>%s():ERR!! ADHOC CCMP ,but 0 entry is have no data\n",
372                                                 __func__);
373                                 return;
374                         }
375                 }
376         }
377 }