1 /******************************************************************************
2 * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
4 * Based on the r8180 driver, which is:
5 * Copyright 2004-2005 Andrea Merello <andrea.merello@gmail.com>, 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.
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
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
19 * The full GNU General Public License is included in this distribution in the
20 * file called LICENSE.
22 * Contact Information:
23 * wlanfae <wlanfae@realtek.com>
24 ******************************************************************************/
26 #include "r8192E_phy.h"
27 #include "r8192E_phyreg.h"
28 #include "r8190P_rtl8256.h" /* RTL8225 Radio frontend */
29 #include "r8192E_cmdpkt.h"
31 void CamResetAllEntry(struct net_device *dev)
35 ulcommand |= BIT31|BIT30;
36 write_nic_dword(dev, RWCAM, ulcommand);
39 void write_cam(struct net_device *dev, u8 addr, u32 data)
41 write_nic_dword(dev, WCAMI, data);
42 write_nic_dword(dev, RWCAM, BIT31|BIT16|(addr&0xff));
45 u32 read_cam(struct net_device *dev, u8 addr)
47 write_nic_dword(dev, RWCAM, 0x80000000|(addr&0xff));
48 return read_nic_dword(dev, 0xa8);
51 void EnableHWSecurityConfig8192(struct net_device *dev)
54 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
55 struct rtllib_device *ieee = priv->rtllib;
57 SECR_value = SCR_TxEncEnable | SCR_RxDecEnable;
58 if (((KEY_TYPE_WEP40 == ieee->pairwise_key_type) ||
59 (KEY_TYPE_WEP104 == ieee->pairwise_key_type)) &&
60 (priv->rtllib->auth_mode != 2)) {
61 SECR_value |= SCR_RxUseDK;
62 SECR_value |= SCR_TxUseDK;
63 } else if ((ieee->iw_mode == IW_MODE_ADHOC) &&
64 (ieee->pairwise_key_type & (KEY_TYPE_CCMP |
66 SECR_value |= SCR_RxUseDK;
67 SECR_value |= SCR_TxUseDK;
71 ieee->hwsec_active = 1;
72 if ((ieee->pHTInfo->IOTAction&HT_IOT_ACT_PURE_N_MODE) || !hwwep) {
73 ieee->hwsec_active = 0;
74 SECR_value &= ~SCR_RxDecEnable;
77 RT_TRACE(COMP_SEC, "%s:, hwsec:%d, pairwise_key:%d, SECR_value:%x\n",
78 __func__, ieee->hwsec_active, ieee->pairwise_key_type,
80 write_nic_byte(dev, SECR, SECR_value);
83 void set_swcam(struct net_device *dev, u8 EntryNo, u8 KeyIndex, u16 KeyType,
84 u8 *MacAddr, u8 DefaultKey, u32 *KeyContent, u8 is_mesh)
86 struct r8192_priv *priv = rtllib_priv(dev);
87 struct rtllib_device *ieee = priv->rtllib;
90 "===========>%s():EntryNo is %d,KeyIndex is %d,KeyType is %d,is_mesh is %d\n",
91 __func__, EntryNo, KeyIndex, KeyType, is_mesh);
93 ieee->swcamtable[EntryNo].bused = true;
94 ieee->swcamtable[EntryNo].key_index = KeyIndex;
95 ieee->swcamtable[EntryNo].key_type = KeyType;
96 memcpy(ieee->swcamtable[EntryNo].macaddr, MacAddr, 6);
97 ieee->swcamtable[EntryNo].useDK = DefaultKey;
98 memcpy(ieee->swcamtable[EntryNo].key_buf, (u8 *)KeyContent, 16);
102 void setKey(struct net_device *dev, u8 EntryNo, u8 KeyIndex, u16 KeyType,
103 u8 *MacAddr, u8 DefaultKey, u32 *KeyContent)
105 u32 TargetCommand = 0;
106 u32 TargetContent = 0;
109 struct r8192_priv *priv = (struct r8192_priv *)rtllib_priv(dev);
110 enum rt_rf_power_state rtState;
112 rtState = priv->rtllib->eRFPowerState;
113 if (priv->rtllib->PowerSaveControl.bInactivePs) {
114 if (rtState == eRfOff) {
115 if (priv->rtllib->RfOffReason > RF_CHANGE_BY_IPS) {
116 RT_TRACE(COMP_ERR, "%s(): RF is OFF.\n",
120 down(&priv->rtllib->ips_sem);
122 up(&priv->rtllib->ips_sem);
125 priv->rtllib->is_set_key = true;
126 if (EntryNo >= TOTAL_CAM_ENTRY)
127 RT_TRACE(COMP_ERR, "cam entry exceeds in setKey()\n");
130 "====>to setKey(), dev:%p, EntryNo:%d, KeyIndex:%d,KeyType:%d, MacAddr %pM\n",
131 dev, EntryNo, KeyIndex, KeyType, MacAddr);
134 usConfig |= BIT15 | (KeyType<<2);
136 usConfig |= BIT15 | (KeyType<<2) | KeyIndex;
139 for (i = 0; i < CAM_CONTENT_COUNT; i++) {
140 TargetCommand = i + CAM_CONTENT_COUNT * EntryNo;
141 TargetCommand |= BIT31|BIT16;
144 TargetContent = (u32)(*(MacAddr+0)) << 16 |
145 (u32)(*(MacAddr+1)) << 24 |
148 write_nic_dword(dev, WCAMI, TargetContent);
149 write_nic_dword(dev, RWCAM, TargetCommand);
151 TargetContent = (u32)(*(MacAddr+2)) |
152 (u32)(*(MacAddr+3)) << 8 |
153 (u32)(*(MacAddr+4)) << 16 |
154 (u32)(*(MacAddr+5)) << 24;
155 write_nic_dword(dev, WCAMI, TargetContent);
156 write_nic_dword(dev, RWCAM, TargetCommand);
158 if (KeyContent != NULL) {
159 write_nic_dword(dev, WCAMI,
160 (u32)(*(KeyContent+i-2)));
161 write_nic_dword(dev, RWCAM, TargetCommand);
166 RT_TRACE(COMP_SEC, "=========>after set key, usconfig:%x\n", usConfig);
169 void CamRestoreAllEntry(struct net_device *dev)
172 struct r8192_priv *priv = rtllib_priv(dev);
173 u8 *MacAddr = priv->rtllib->current_network.bssid;
175 static u8 CAM_CONST_ADDR[4][6] = {
176 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
177 {0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
178 {0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
179 {0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
181 static u8 CAM_CONST_BROAD[] = {
182 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
185 RT_TRACE(COMP_SEC, "CamRestoreAllEntry:\n");
188 if ((priv->rtllib->pairwise_key_type == KEY_TYPE_WEP40) ||
189 (priv->rtllib->pairwise_key_type == KEY_TYPE_WEP104)) {
191 for (EntryId = 0; EntryId < 4; EntryId++) {
192 MacAddr = CAM_CONST_ADDR[EntryId];
193 if (priv->rtllib->swcamtable[EntryId].bused) {
194 setKey(dev, EntryId, EntryId,
195 priv->rtllib->pairwise_key_type, MacAddr,
196 0, (u32 *)(&priv->rtllib->swcamtable
197 [EntryId].key_buf[0]));
201 } else if (priv->rtllib->pairwise_key_type == KEY_TYPE_TKIP) {
202 if (priv->rtllib->iw_mode == IW_MODE_ADHOC) {
203 setKey(dev, 4, 0, priv->rtllib->pairwise_key_type,
204 (u8 *)dev->dev_addr, 0,
205 (u32 *)(&priv->rtllib->swcamtable[4].key_buf[0]));
207 setKey(dev, 4, 0, priv->rtllib->pairwise_key_type,
209 (u32 *)(&priv->rtllib->swcamtable[4].key_buf[0]));
212 } else if (priv->rtllib->pairwise_key_type == KEY_TYPE_CCMP) {
213 if (priv->rtllib->iw_mode == IW_MODE_ADHOC) {
215 priv->rtllib->pairwise_key_type,
216 (u8 *)dev->dev_addr, 0,
217 (u32 *)(&priv->rtllib->swcamtable[4].
221 priv->rtllib->pairwise_key_type, MacAddr,
222 0, (u32 *)(&priv->rtllib->swcamtable[4].
227 if (priv->rtllib->group_key_type == KEY_TYPE_TKIP) {
228 MacAddr = CAM_CONST_BROAD;
229 for (EntryId = 1; EntryId < 4; EntryId++) {
230 if (priv->rtllib->swcamtable[EntryId].bused) {
231 setKey(dev, EntryId, EntryId,
232 priv->rtllib->group_key_type,
234 (u32 *)(&priv->rtllib->swcamtable[EntryId].key_buf[0])
238 if (priv->rtllib->iw_mode == IW_MODE_ADHOC) {
239 if (priv->rtllib->swcamtable[0].bused) {
241 priv->rtllib->group_key_type,
242 CAM_CONST_ADDR[0], 0,
243 (u32 *)(&priv->rtllib->swcamtable[0].key_buf[0])
247 "===>%s():ERR!! ADHOC TKIP ,but 0 entry is have no data\n",
252 } else if (priv->rtllib->group_key_type == KEY_TYPE_CCMP) {
253 MacAddr = CAM_CONST_BROAD;
254 for (EntryId = 1; EntryId < 4; EntryId++) {
255 if (priv->rtllib->swcamtable[EntryId].bused) {
256 setKey(dev, EntryId, EntryId,
257 priv->rtllib->group_key_type,
259 (u32 *)(&priv->rtllib->swcamtable[EntryId].key_buf[0]));
263 if (priv->rtllib->iw_mode == IW_MODE_ADHOC) {
264 if (priv->rtllib->swcamtable[0].bused) {
266 priv->rtllib->group_key_type,
267 CAM_CONST_ADDR[0], 0,
268 (u32 *)(&priv->rtllib->swcamtable[0].key_buf[0]));
271 "===>%s():ERR!! ADHOC CCMP ,but 0 entry is have no data\n",