blob: 6e2103885959e1e1467aa069c2772f0cd2b824f4 [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/**
2 * This file contains the handling of command
3 * responses as well as events generated by firmware.
4 */
5#include <linux/delay.h>
Alan Cox767e3662009-10-12 05:27:48 +00006#include <linux/sched.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02007#include <linux/if_arp.h>
8#include <linux/netdevice.h>
Cliff Cai2c5b9e512009-05-27 14:03:09 -04009#include <asm/unaligned.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020010#include <net/iw_handler.h>
11
12#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020013#include "decl.h"
Holger Schurigbca61f82009-10-16 17:33:23 +020014#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020015#include "defs.h"
16#include "dev.h"
Holger Schurig697900a2008-04-02 16:27:10 +020017#include "assoc.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020018#include "wext.h"
19
20/**
21 * @brief This function handles disconnect event. it
22 * reports disconnect to upper layer, clean tx/rx packets,
23 * reset link state etc.
24 *
Holger Schurig69f90322007-11-23 15:43:44 +010025 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020026 * @return n/a
27 */
Holger Schurig69f90322007-11-23 15:43:44 +010028void lbs_mac_event_disconnected(struct lbs_private *priv)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020029{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020030 union iwreq_data wrqu;
31
David Woodhouseaa21c002007-12-08 20:04:36 +000032 if (priv->connect_status != LBS_CONNECTED)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020033 return;
34
Holger Schurig91843462007-11-28 14:05:02 +010035 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020036
37 memset(wrqu.ap_addr.sa_data, 0x00, ETH_ALEN);
38 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
39
40 /*
41 * Cisco AP sends EAP failure and de-auth in less than 0.5 ms.
42 * It causes problem in the Supplicant
43 */
44
45 msleep_interruptible(1000);
Holger Schurig634b8f42007-05-25 13:05:16 -040046 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020047
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020048 /* report disconnect to upper layer */
Holger Schurig634b8f42007-05-25 13:05:16 -040049 netif_stop_queue(priv->dev);
50 netif_carrier_off(priv->dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020051
David Woodhousea27b9f92007-12-12 00:41:51 -050052 /* Free Tx and Rx packets */
53 kfree_skb(priv->currenttxskb);
54 priv->currenttxskb = NULL;
55 priv->tx_pending_len = 0;
56
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020057 /* reset SNR/NF/RSSI values */
David Woodhouseaa21c002007-12-08 20:04:36 +000058 memset(priv->SNR, 0x00, sizeof(priv->SNR));
59 memset(priv->NF, 0x00, sizeof(priv->NF));
60 memset(priv->RSSI, 0x00, sizeof(priv->RSSI));
61 memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
62 memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
63 priv->nextSNRNF = 0;
64 priv->numSNRNF = 0;
65 priv->connect_status = LBS_DISCONNECTED;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020066
Dan Williamse76850d2007-05-25 17:09:41 -040067 /* Clear out associated SSID and BSSID since connection is
68 * no longer valid.
69 */
David Woodhouseaa21c002007-12-08 20:04:36 +000070 memset(&priv->curbssparams.bssid, 0, ETH_ALEN);
Holger Schurig243e84e2009-10-22 15:30:47 +020071 memset(&priv->curbssparams.ssid, 0, IEEE80211_MAX_SSID_LEN);
David Woodhouseaa21c002007-12-08 20:04:36 +000072 priv->curbssparams.ssid_len = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020073
David Woodhouseaa21c002007-12-08 20:04:36 +000074 if (priv->psstate != PS_STATE_FULL_POWER) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020075 /* make firmware to exit PS mode */
Holger Schuriga6c87002007-08-02 11:54:10 -040076 lbs_deb_cmd("disconnected, so exit PS mode\n");
Holger Schurig10078322007-11-15 18:05:47 -050077 lbs_ps_wakeup(priv, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020078 }
Holger Schurig52507c22008-01-28 17:25:53 +010079 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020080}
81
82/**
83 * @brief This function handles MIC failure event.
84 *
Holger Schurig69f90322007-11-23 15:43:44 +010085 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020086 * @para event the event id
87 * @return n/a
88 */
Holger Schurig69f90322007-11-23 15:43:44 +010089static void handle_mic_failureevent(struct lbs_private *priv, u32 event)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020090{
91 char buf[50];
92
Holger Schuriga6c87002007-08-02 11:54:10 -040093 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020094 memset(buf, 0, sizeof(buf));
95
96 sprintf(buf, "%s", "MLME-MICHAELMICFAILURE.indication ");
97
98 if (event == MACREG_INT_CODE_MIC_ERR_UNICAST) {
99 strcat(buf, "unicast ");
100 } else {
101 strcat(buf, "multicast ");
102 }
103
Holger Schurig10078322007-11-15 18:05:47 -0500104 lbs_send_iwevcustom_event(priv, buf);
Holger Schuriga6c87002007-08-02 11:54:10 -0400105 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200106}
107
Holger Schurig69f90322007-11-23 15:43:44 +0100108static int lbs_ret_reg_access(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200109 u16 type, struct cmd_ds_command *resp)
110{
Holger Schurig9012b282007-05-25 11:27:16 -0400111 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200112
Holger Schurig9012b282007-05-25 11:27:16 -0400113 lbs_deb_enter(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200114
115 switch (type) {
Holger Schurig6b63cd02007-08-02 11:53:36 -0400116 case CMD_RET(CMD_MAC_REG_ACCESS):
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200117 {
David Woodhouse981f1872007-05-25 23:36:54 -0400118 struct cmd_ds_mac_reg_access *reg = &resp->params.macreg;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200119
David Woodhouseaa21c002007-12-08 20:04:36 +0000120 priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
121 priv->offsetvalue.value = le32_to_cpu(reg->value);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200122 break;
123 }
124
Holger Schurig6b63cd02007-08-02 11:53:36 -0400125 case CMD_RET(CMD_BBP_REG_ACCESS):
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200126 {
David Woodhouse981f1872007-05-25 23:36:54 -0400127 struct cmd_ds_bbp_reg_access *reg = &resp->params.bbpreg;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200128
David Woodhouseaa21c002007-12-08 20:04:36 +0000129 priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
130 priv->offsetvalue.value = reg->value;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200131 break;
132 }
133
Holger Schurig6b63cd02007-08-02 11:53:36 -0400134 case CMD_RET(CMD_RF_REG_ACCESS):
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200135 {
David Woodhouse981f1872007-05-25 23:36:54 -0400136 struct cmd_ds_rf_reg_access *reg = &resp->params.rfreg;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200137
David Woodhouseaa21c002007-12-08 20:04:36 +0000138 priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
139 priv->offsetvalue.value = reg->value;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200140 break;
141 }
142
143 default:
Holger Schurig9012b282007-05-25 11:27:16 -0400144 ret = -1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200145 }
146
Holger Schurig8b17d722007-10-08 11:09:30 +0200147 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Holger Schurig9012b282007-05-25 11:27:16 -0400148 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200149}
150
Holger Schurig69f90322007-11-23 15:43:44 +0100151static int lbs_ret_802_11_rssi(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200152 struct cmd_ds_command *resp)
153{
154 struct cmd_ds_802_11_rssi_rsp *rssirsp = &resp->params.rssirsp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200155
Holger Schuriga6c87002007-08-02 11:54:10 -0400156 lbs_deb_enter(LBS_DEB_CMD);
157
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200158 /* store the non average value */
Cliff Cai2c5b9e512009-05-27 14:03:09 -0400159 priv->SNR[TYPE_BEACON][TYPE_NOAVG] = get_unaligned_le16(&rssirsp->SNR);
160 priv->NF[TYPE_BEACON][TYPE_NOAVG] = get_unaligned_le16(&rssirsp->noisefloor);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200161
Cliff Cai2c5b9e512009-05-27 14:03:09 -0400162 priv->SNR[TYPE_BEACON][TYPE_AVG] = get_unaligned_le16(&rssirsp->avgSNR);
163 priv->NF[TYPE_BEACON][TYPE_AVG] = get_unaligned_le16(&rssirsp->avgnoisefloor);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200164
David Woodhouseaa21c002007-12-08 20:04:36 +0000165 priv->RSSI[TYPE_BEACON][TYPE_NOAVG] =
166 CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_NOAVG],
167 priv->NF[TYPE_BEACON][TYPE_NOAVG]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200168
David Woodhouseaa21c002007-12-08 20:04:36 +0000169 priv->RSSI[TYPE_BEACON][TYPE_AVG] =
170 CAL_RSSI(priv->SNR[TYPE_BEACON][TYPE_AVG] / AVG_SCALE,
171 priv->NF[TYPE_BEACON][TYPE_AVG] / AVG_SCALE);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200172
Holger Schuriga6c87002007-08-02 11:54:10 -0400173 lbs_deb_cmd("RSSI: beacon %d, avg %d\n",
David Woodhouseaa21c002007-12-08 20:04:36 +0000174 priv->RSSI[TYPE_BEACON][TYPE_NOAVG],
175 priv->RSSI[TYPE_BEACON][TYPE_AVG]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200176
Holger Schuriga6c87002007-08-02 11:54:10 -0400177 lbs_deb_leave(LBS_DEB_CMD);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200178 return 0;
179}
180
Brajesh Dave96287ac2007-11-20 17:44:28 -0500181static int lbs_ret_802_11_bcn_ctrl(struct lbs_private * priv,
182 struct cmd_ds_command *resp)
183{
184 struct cmd_ds_802_11_beacon_control *bcn_ctrl =
185 &resp->params.bcn_ctrl;
Brajesh Dave96287ac2007-11-20 17:44:28 -0500186
187 lbs_deb_enter(LBS_DEB_CMD);
188
189 if (bcn_ctrl->action == CMD_ACT_GET) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000190 priv->beacon_enable = (u8) le16_to_cpu(bcn_ctrl->beacon_enable);
191 priv->beacon_period = le16_to_cpu(bcn_ctrl->beacon_period);
Brajesh Dave96287ac2007-11-20 17:44:28 -0500192 }
193
194 lbs_deb_enter(LBS_DEB_CMD);
195 return 0;
196}
197
David Woodhouse1309b552007-12-10 13:36:10 -0500198static inline int handle_cmd_response(struct lbs_private *priv,
Dan Williamsddac4522007-12-11 13:49:39 -0500199 struct cmd_header *cmd_response)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200200{
Dan Williamsddac4522007-12-11 13:49:39 -0500201 struct cmd_ds_command *resp = (struct cmd_ds_command *) cmd_response;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200202 int ret = 0;
203 unsigned long flags;
David Woodhouse1309b552007-12-10 13:36:10 -0500204 uint16_t respcmd = le16_to_cpu(resp->command);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200205
Holger Schuriga6c87002007-08-02 11:54:10 -0400206 lbs_deb_enter(LBS_DEB_HOST);
207
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200208 switch (respcmd) {
Holger Schurig6b63cd02007-08-02 11:53:36 -0400209 case CMD_RET(CMD_MAC_REG_ACCESS):
210 case CMD_RET(CMD_BBP_REG_ACCESS):
211 case CMD_RET(CMD_RF_REG_ACCESS):
Holger Schurig10078322007-11-15 18:05:47 -0500212 ret = lbs_ret_reg_access(priv, respcmd, resp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200213 break;
214
Holger Schurig6b63cd02007-08-02 11:53:36 -0400215 case CMD_RET(CMD_802_11_SET_AFC):
216 case CMD_RET(CMD_802_11_GET_AFC):
David Woodhouseaa21c002007-12-08 20:04:36 +0000217 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouse75567672007-12-15 02:38:17 -0500218 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.afc,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200219 sizeof(struct cmd_ds_802_11_afc));
David Woodhouseaa21c002007-12-08 20:04:36 +0000220 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200221
222 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200223
Holger Schurig6b63cd02007-08-02 11:53:36 -0400224 case CMD_RET(CMD_802_11_BEACON_STOP):
Dan Williams18c96c342007-06-18 12:01:12 -0400225 break;
226
Holger Schurig6b63cd02007-08-02 11:53:36 -0400227 case CMD_RET(CMD_802_11_RSSI):
Holger Schurig10078322007-11-15 18:05:47 -0500228 ret = lbs_ret_802_11_rssi(priv, resp);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200229 break;
230
Holger Schurig6b63cd02007-08-02 11:53:36 -0400231 case CMD_RET(CMD_802_11_TPC_CFG):
David Woodhouseaa21c002007-12-08 20:04:36 +0000232 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouse75567672007-12-15 02:38:17 -0500233 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.tpccfg,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200234 sizeof(struct cmd_ds_802_11_tpc_cfg));
David Woodhouseaa21c002007-12-08 20:04:36 +0000235 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200236 break;
Holger Schurig6b63cd02007-08-02 11:53:36 -0400237 case CMD_RET(CMD_802_11_LED_GPIO_CTRL):
David Woodhouseaa21c002007-12-08 20:04:36 +0000238 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouse75567672007-12-15 02:38:17 -0500239 memmove((void *)priv->cur_cmd->callback_arg, &resp->params.ledgpio,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200240 sizeof(struct cmd_ds_802_11_led_ctrl));
David Woodhouseaa21c002007-12-08 20:04:36 +0000241 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200242 break;
Holger Schurig3a188642007-11-26 10:07:14 +0100243
Holger Schurig6b63cd02007-08-02 11:53:36 -0400244 case CMD_RET(CMD_GET_TSF):
David Woodhouseaa21c002007-12-08 20:04:36 +0000245 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouse75567672007-12-15 02:38:17 -0500246 memcpy((void *)priv->cur_cmd->callback_arg,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200247 &resp->params.gettsf.tsfvalue, sizeof(u64));
David Woodhouseaa21c002007-12-08 20:04:36 +0000248 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200249 break;
Holger Schurig6b63cd02007-08-02 11:53:36 -0400250 case CMD_RET(CMD_BT_ACCESS):
David Woodhouseaa21c002007-12-08 20:04:36 +0000251 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouse75567672007-12-15 02:38:17 -0500252 if (priv->cur_cmd->callback_arg)
253 memcpy((void *)priv->cur_cmd->callback_arg,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200254 &resp->params.bt.addr1, 2 * ETH_ALEN);
David Woodhouseaa21c002007-12-08 20:04:36 +0000255 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200256 break;
Holger Schurig6b63cd02007-08-02 11:53:36 -0400257 case CMD_RET(CMD_FWT_ACCESS):
David Woodhouseaa21c002007-12-08 20:04:36 +0000258 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouse75567672007-12-15 02:38:17 -0500259 if (priv->cur_cmd->callback_arg)
260 memcpy((void *)priv->cur_cmd->callback_arg, &resp->params.fwt,
David Woodhouse981f1872007-05-25 23:36:54 -0400261 sizeof(resp->params.fwt));
David Woodhouseaa21c002007-12-08 20:04:36 +0000262 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200263 break;
Brajesh Dave96287ac2007-11-20 17:44:28 -0500264 case CMD_RET(CMD_802_11_BEACON_CTRL):
265 ret = lbs_ret_802_11_bcn_ctrl(priv, resp);
266 break;
267
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200268 default:
David Woodhousee37fc6e2008-05-20 11:47:16 +0100269 lbs_pr_err("CMD_RESP: unknown cmd response 0x%04x\n",
270 le16_to_cpu(resp->command));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200271 break;
272 }
Holger Schuriga6c87002007-08-02 11:54:10 -0400273 lbs_deb_leave(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200274 return ret;
275}
276
Holger Schurig7919b892008-04-01 14:50:43 +0200277int lbs_process_command_response(struct lbs_private *priv, u8 *data, u32 len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200278{
David Woodhousee1258172007-12-11 23:42:49 -0500279 uint16_t respcmd, curcmd;
Dan Williamsddac4522007-12-11 13:49:39 -0500280 struct cmd_header *resp;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200281 int ret = 0;
David Woodhousee1258172007-12-11 23:42:49 -0500282 unsigned long flags;
283 uint16_t result;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200284
Holger Schuriga6c87002007-08-02 11:54:10 -0400285 lbs_deb_enter(LBS_DEB_HOST);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200286
David Woodhouseaa21c002007-12-08 20:04:36 +0000287 mutex_lock(&priv->lock);
288 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200289
David Woodhouseaa21c002007-12-08 20:04:36 +0000290 if (!priv->cur_cmd) {
Holger Schuriga6c87002007-08-02 11:54:10 -0400291 lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200292 ret = -1;
David Woodhouseaa21c002007-12-08 20:04:36 +0000293 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200294 goto done;
295 }
David Woodhousee1258172007-12-11 23:42:49 -0500296
Holger Schurig7919b892008-04-01 14:50:43 +0200297 resp = (void *)data;
Sebastian Siewior8a96df82008-03-04 18:22:27 +0100298 curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200299 respcmd = le16_to_cpu(resp->command);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200300 result = le16_to_cpu(resp->result);
301
Holger Schurige5225b32008-03-26 10:04:44 +0100302 lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d\n",
Holger Schurig7919b892008-04-01 14:50:43 +0200303 respcmd, le16_to_cpu(resp->seqnum), len);
304 lbs_deb_hex(LBS_DEB_CMD, "CMD_RESP", (void *) resp, len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200305
David Woodhouse6305f492008-03-03 12:20:12 +0100306 if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
David Woodhousee1258172007-12-11 23:42:49 -0500307 lbs_pr_info("Received CMD_RESP with invalid sequence %d (expected %d)\n",
David Woodhouse6305f492008-03-03 12:20:12 +0100308 le16_to_cpu(resp->seqnum), le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
David Woodhouseaa21c002007-12-08 20:04:36 +0000309 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200310 ret = -1;
311 goto done;
312 }
David Woodhousee1258172007-12-11 23:42:49 -0500313 if (respcmd != CMD_RET(curcmd) &&
Sebastian Siewior5f0547c2008-03-06 10:30:21 +0100314 respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
David Woodhousee1258172007-12-11 23:42:49 -0500315 lbs_pr_info("Invalid CMD_RESP %x to command %x!\n", respcmd, curcmd);
316 spin_unlock_irqrestore(&priv->driver_lock, flags);
317 ret = -1;
318 goto done;
319 }
320
David Woodhouse85388232007-12-17 15:41:30 -0500321 if (resp->result == cpu_to_le16(0x0004)) {
322 /* 0x0004 means -EAGAIN. Drop the response, let it time out
323 and be resubmitted */
324 lbs_pr_info("Firmware returns DEFER to command %x. Will let it time out...\n",
325 le16_to_cpu(resp->command));
326 spin_unlock_irqrestore(&priv->driver_lock, flags);
327 ret = -1;
328 goto done;
329 }
330
David Woodhousee1258172007-12-11 23:42:49 -0500331 /* Now we got response from FW, cancel the command timer */
332 del_timer(&priv->command_timer);
David Woodhouse2a345092007-12-15 19:33:43 -0500333 priv->cmd_timed_out = 0;
334 if (priv->nr_retries) {
335 lbs_pr_info("Received result %x to command %x after %d retries\n",
336 result, curcmd, priv->nr_retries);
337 priv->nr_retries = 0;
338 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200339
340 /* Store the response code to cur_cmd_retcode. */
David Woodhouseaa21c002007-12-08 20:04:36 +0000341 priv->cur_cmd_retcode = result;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200342
Holger Schurig6b63cd02007-08-02 11:53:36 -0400343 if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
David Woodhouse38bfab12007-12-16 23:26:54 -0500344 struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
David Woodhouse981f1872007-05-25 23:36:54 -0400345 u16 action = le16_to_cpu(psmode->action);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200346
Holger Schuriga6c87002007-08-02 11:54:10 -0400347 lbs_deb_host(
348 "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400349 result, action);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200350
351 if (result) {
Holger Schuriga6c87002007-08-02 11:54:10 -0400352 lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
David Woodhouse981f1872007-05-25 23:36:54 -0400353 result);
354 /*
355 * We should not re-try enter-ps command in
356 * ad-hoc mode. It takes place in
Holger Schurig10078322007-11-15 18:05:47 -0500357 * lbs_execute_next_command().
David Woodhouse981f1872007-05-25 23:36:54 -0400358 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000359 if (priv->mode == IW_MODE_ADHOC &&
Dan Williams0aef64d2007-08-02 11:31:18 -0400360 action == CMD_SUBCMD_ENTER_PS)
David Woodhouseaa21c002007-12-08 20:04:36 +0000361 priv->psmode = LBS802_11POWERMODECAM;
Dan Williams0aef64d2007-08-02 11:31:18 -0400362 } else if (action == CMD_SUBCMD_ENTER_PS) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000363 priv->needtowakeup = 0;
364 priv->psstate = PS_STATE_AWAKE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200365
Holger Schuriga6c87002007-08-02 11:54:10 -0400366 lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000367 if (priv->connect_status != LBS_CONNECTED) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200368 /*
369 * When Deauth Event received before Enter_PS command
370 * response, We need to wake up the firmware.
371 */
Holger Schuriga6c87002007-08-02 11:54:10 -0400372 lbs_deb_host(
Holger Schurig10078322007-11-15 18:05:47 -0500373 "disconnected, invoking lbs_ps_wakeup\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200374
David Woodhouseaa21c002007-12-08 20:04:36 +0000375 spin_unlock_irqrestore(&priv->driver_lock, flags);
376 mutex_unlock(&priv->lock);
Holger Schurig10078322007-11-15 18:05:47 -0500377 lbs_ps_wakeup(priv, 0);
David Woodhouseaa21c002007-12-08 20:04:36 +0000378 mutex_lock(&priv->lock);
379 spin_lock_irqsave(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200380 }
Dan Williams0aef64d2007-08-02 11:31:18 -0400381 } else if (action == CMD_SUBCMD_EXIT_PS) {
David Woodhouseaa21c002007-12-08 20:04:36 +0000382 priv->needtowakeup = 0;
383 priv->psstate = PS_STATE_FULL_POWER;
Holger Schuriga6c87002007-08-02 11:54:10 -0400384 lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200385 } else {
Holger Schuriga6c87002007-08-02 11:54:10 -0400386 lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200387 }
388
David Woodhouse183aeac2007-12-15 01:52:54 -0500389 lbs_complete_command(priv, priv->cur_cmd, result);
David Woodhouseaa21c002007-12-08 20:04:36 +0000390 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200391
392 ret = 0;
393 goto done;
394 }
395
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200396 /* If the command is not successful, cleanup and return failure */
397 if ((result != 0 || !(respcmd & 0x8000))) {
Holger Schuriga6c87002007-08-02 11:54:10 -0400398 lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
399 result, respcmd);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200400 /*
401 * Handling errors here
402 */
403 switch (respcmd) {
Holger Schurig6b63cd02007-08-02 11:53:36 -0400404 case CMD_RET(CMD_GET_HW_SPEC):
405 case CMD_RET(CMD_802_11_RESET):
Holger Schuriga6c87002007-08-02 11:54:10 -0400406 lbs_deb_host("CMD_RESP: reset failed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200407 break;
408
409 }
David Woodhouse183aeac2007-12-15 01:52:54 -0500410 lbs_complete_command(priv, priv->cur_cmd, result);
David Woodhouseaa21c002007-12-08 20:04:36 +0000411 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200412
413 ret = -1;
414 goto done;
415 }
416
David Woodhouseaa21c002007-12-08 20:04:36 +0000417 spin_unlock_irqrestore(&priv->driver_lock, flags);
David Woodhouse17230472007-12-07 15:13:05 +0000418
Dan Williams7ad994d2007-12-11 12:33:30 -0500419 if (priv->cur_cmd && priv->cur_cmd->callback) {
420 ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
Dan Williamsddac4522007-12-11 13:49:39 -0500421 resp);
Dan Williams7ad994d2007-12-11 12:33:30 -0500422 } else
Holger Schurige98a88d2008-03-19 14:25:58 +0100423 ret = handle_cmd_response(priv, resp);
David Woodhouse17230472007-12-07 15:13:05 +0000424
David Woodhouseaa21c002007-12-08 20:04:36 +0000425 spin_lock_irqsave(&priv->driver_lock, flags);
David Woodhouse17230472007-12-07 15:13:05 +0000426
David Woodhouseaa21c002007-12-08 20:04:36 +0000427 if (priv->cur_cmd) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200428 /* Clean up and Put current command back to cmdfreeq */
David Woodhouse183aeac2007-12-15 01:52:54 -0500429 lbs_complete_command(priv, priv->cur_cmd, result);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200430 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000431 spin_unlock_irqrestore(&priv->driver_lock, flags);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200432
433done:
David Woodhouseaa21c002007-12-08 20:04:36 +0000434 mutex_unlock(&priv->lock);
Holger Schuriga6c87002007-08-02 11:54:10 -0400435 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200436 return ret;
437}
438
David Woodhouseb47ef242007-12-17 13:26:42 -0500439static int lbs_send_confirmwake(struct lbs_private *priv)
440{
Holger Schurigf539f2e2008-03-26 13:22:11 +0100441 struct cmd_header cmd;
David Woodhouseb47ef242007-12-17 13:26:42 -0500442 int ret = 0;
443
444 lbs_deb_enter(LBS_DEB_HOST);
445
Holger Schurigf539f2e2008-03-26 13:22:11 +0100446 cmd.command = cpu_to_le16(CMD_802_11_WAKEUP_CONFIRM);
447 cmd.size = cpu_to_le16(sizeof(cmd));
448 cmd.seqnum = cpu_to_le16(++priv->seqnum);
449 cmd.result = 0;
David Woodhouseb47ef242007-12-17 13:26:42 -0500450
Holger Schurigf539f2e2008-03-26 13:22:11 +0100451 lbs_deb_hex(LBS_DEB_HOST, "wake confirm", (u8 *) &cmd,
452 sizeof(cmd));
David Woodhouseb47ef242007-12-17 13:26:42 -0500453
Holger Schurigf539f2e2008-03-26 13:22:11 +0100454 ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) &cmd, sizeof(cmd));
David Woodhouseb47ef242007-12-17 13:26:42 -0500455 if (ret)
456 lbs_pr_alert("SEND_WAKEC_CMD: Host to Card failed for Confirm Wake\n");
457
458 lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
459 return ret;
460}
461
Holger Schurig7919b892008-04-01 14:50:43 +0200462int lbs_process_event(struct lbs_private *priv, u32 event)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200463{
464 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200465
Holger Schurig9556d212007-08-02 13:14:07 -0400466 lbs_deb_enter(LBS_DEB_CMD);
467
Holger Schurig7919b892008-04-01 14:50:43 +0200468 switch (event) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200469 case MACREG_INT_CODE_LINK_SENSED:
Holger Schurigd4ff0ef2008-03-19 14:25:18 +0100470 lbs_deb_cmd("EVENT: link sensed\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200471 break;
472
473 case MACREG_INT_CODE_DEAUTHENTICATED:
Holger Schuriga6c87002007-08-02 11:54:10 -0400474 lbs_deb_cmd("EVENT: deauthenticated\n");
Holger Schurig10078322007-11-15 18:05:47 -0500475 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200476 break;
477
478 case MACREG_INT_CODE_DISASSOCIATED:
Holger Schuriga6c87002007-08-02 11:54:10 -0400479 lbs_deb_cmd("EVENT: disassociated\n");
Holger Schurig10078322007-11-15 18:05:47 -0500480 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200481 break;
482
Holger Schurig0b3c07f2007-11-28 09:15:11 +0100483 case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
Holger Schuriga6c87002007-08-02 11:54:10 -0400484 lbs_deb_cmd("EVENT: link lost\n");
Holger Schurig10078322007-11-15 18:05:47 -0500485 lbs_mac_event_disconnected(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200486 break;
487
488 case MACREG_INT_CODE_PS_SLEEP:
Holger Schurigd4ff0ef2008-03-19 14:25:18 +0100489 lbs_deb_cmd("EVENT: ps sleep\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200490
491 /* handle unexpected PS SLEEP event */
David Woodhouseaa21c002007-12-08 20:04:36 +0000492 if (priv->psstate == PS_STATE_FULL_POWER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400493 lbs_deb_cmd(
Holger Schuriga6c87002007-08-02 11:54:10 -0400494 "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200495 break;
496 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000497 priv->psstate = PS_STATE_PRE_SLEEP;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200498
Holger Schurigd4ff0ef2008-03-19 14:25:18 +0100499 lbs_ps_confirm_sleep(priv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200500
501 break;
502
David Woodhouseb47ef242007-12-17 13:26:42 -0500503 case MACREG_INT_CODE_HOST_AWAKE:
Holger Schurigd4ff0ef2008-03-19 14:25:18 +0100504 lbs_deb_cmd("EVENT: host awake\n");
Amitkumar Karwar49125452009-09-30 20:04:38 -0700505 if (priv->reset_deep_sleep_wakeup)
506 priv->reset_deep_sleep_wakeup(priv);
507 priv->is_deep_sleep = 0;
David Woodhouseb47ef242007-12-17 13:26:42 -0500508 lbs_send_confirmwake(priv);
509 break;
510
Amitkumar Karwar49125452009-09-30 20:04:38 -0700511 case MACREG_INT_CODE_DEEP_SLEEP_AWAKE:
512 if (priv->reset_deep_sleep_wakeup)
513 priv->reset_deep_sleep_wakeup(priv);
514 lbs_deb_cmd("EVENT: ds awake\n");
515 priv->is_deep_sleep = 0;
516 priv->wakeup_dev_required = 0;
517 wake_up_interruptible(&priv->ds_awake_q);
518 break;
519
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200520 case MACREG_INT_CODE_PS_AWAKE:
Holger Schurigd4ff0ef2008-03-19 14:25:18 +0100521 lbs_deb_cmd("EVENT: ps awake\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200522 /* handle unexpected PS AWAKE event */
David Woodhouseaa21c002007-12-08 20:04:36 +0000523 if (priv->psstate == PS_STATE_FULL_POWER) {
Holger Schurig9012b282007-05-25 11:27:16 -0400524 lbs_deb_cmd(
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200525 "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
526 break;
527 }
528
David Woodhouseaa21c002007-12-08 20:04:36 +0000529 priv->psstate = PS_STATE_AWAKE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200530
David Woodhouseaa21c002007-12-08 20:04:36 +0000531 if (priv->needtowakeup) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200532 /*
533 * wait for the command processing to finish
534 * before resuming sending
David Woodhouseaa21c002007-12-08 20:04:36 +0000535 * priv->needtowakeup will be set to FALSE
Holger Schurig10078322007-11-15 18:05:47 -0500536 * in lbs_ps_wakeup()
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200537 */
Holger Schuriga6c87002007-08-02 11:54:10 -0400538 lbs_deb_cmd("waking up ...\n");
Holger Schurig10078322007-11-15 18:05:47 -0500539 lbs_ps_wakeup(priv, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200540 }
541 break;
542
543 case MACREG_INT_CODE_MIC_ERR_UNICAST:
Holger Schurig9012b282007-05-25 11:27:16 -0400544 lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200545 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
546 break;
547
548 case MACREG_INT_CODE_MIC_ERR_MULTICAST:
Holger Schurig9012b282007-05-25 11:27:16 -0400549 lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200550 handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_MULTICAST);
551 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200552
Holger Schurigd4ff0ef2008-03-19 14:25:18 +0100553 case MACREG_INT_CODE_MIB_CHANGED:
554 lbs_deb_cmd("EVENT: MIB CHANGED\n");
555 break;
556 case MACREG_INT_CODE_INIT_DONE:
557 lbs_deb_cmd("EVENT: INIT DONE\n");
558 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200559 case MACREG_INT_CODE_ADHOC_BCN_LOST:
Holger Schuriga6c87002007-08-02 11:54:10 -0400560 lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200561 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200562 case MACREG_INT_CODE_RSSI_LOW:
Holger Schuriga6c87002007-08-02 11:54:10 -0400563 lbs_pr_alert("EVENT: rssi low\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200564 break;
565 case MACREG_INT_CODE_SNR_LOW:
Holger Schuriga6c87002007-08-02 11:54:10 -0400566 lbs_pr_alert("EVENT: snr low\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200567 break;
568 case MACREG_INT_CODE_MAX_FAIL:
Holger Schuriga6c87002007-08-02 11:54:10 -0400569 lbs_pr_alert("EVENT: max fail\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200570 break;
571 case MACREG_INT_CODE_RSSI_HIGH:
Holger Schuriga6c87002007-08-02 11:54:10 -0400572 lbs_pr_alert("EVENT: rssi high\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200573 break;
574 case MACREG_INT_CODE_SNR_HIGH:
Holger Schuriga6c87002007-08-02 11:54:10 -0400575 lbs_pr_alert("EVENT: snr high\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200576 break;
577
Luis Carlos Cobo Rus7d8d28b2007-05-25 23:12:19 -0400578 case MACREG_INT_CODE_MESH_AUTO_STARTED:
Dan Williams5612c012007-08-02 13:20:12 -0400579 /* Ignore spurious autostart events if autostart is disabled */
580 if (!priv->mesh_autostart_enabled) {
581 lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
582 break;
583 }
Luis Carlos Cobo9cdc6d22007-08-02 11:55:22 -0400584 lbs_pr_info("EVENT: MESH_AUTO_STARTED\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000585 priv->mesh_connect_status = LBS_CONNECTED;
David Woodhousea27b9f92007-12-12 00:41:51 -0500586 if (priv->mesh_open) {
Luis Carlos Cobo9cdc6d22007-08-02 11:55:22 -0400587 netif_carrier_on(priv->mesh_dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500588 if (!priv->tx_pending_len)
589 netif_wake_queue(priv->mesh_dev);
Luis Carlos Cobo Rus7d8d28b2007-05-25 23:12:19 -0400590 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000591 priv->mode = IW_MODE_ADHOC;
Luis Carlos Cobo Rusb8bedef2007-05-30 12:14:34 -0400592 schedule_work(&priv->sync_channel);
Luis Carlos Cobo Rus7d8d28b2007-05-25 23:12:19 -0400593 break;
594
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200595 default:
Holger Schurig7919b892008-04-01 14:50:43 +0200596 lbs_pr_alert("EVENT: unknown event id %d\n", event);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200597 break;
598 }
599
Holger Schurig9556d212007-08-02 13:14:07 -0400600 lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200601 return ret;
602}