cmdresp.c 9.1 KB
Newer Older
1 2 3 4
/**
  * This file contains the handling of command
  * responses as well as events generated by firmware.
  */
5
#include <linux/slab.h>
6
#include <linux/delay.h>
A
Alan Cox 已提交
7
#include <linux/sched.h>
8
#include <asm/unaligned.h>
K
Kiran Divekar 已提交
9
#include <net/cfg80211.h>
10

K
Kiran Divekar 已提交
11
#include "cfg.h"
12
#include "cmd.h"
13 14 15 16 17 18

/**
 *  @brief This function handles disconnect event. it
 *  reports disconnect to upper layer, clean tx/rx packets,
 *  reset link state etc.
 *
19
 *  @param priv    A pointer to struct lbs_private structure
20 21
 *  @return 	   n/a
 */
22
void lbs_mac_event_disconnected(struct lbs_private *priv)
23
{
24
	if (priv->connect_status != LBS_CONNECTED)
25 26
		return;

27
	lbs_deb_enter(LBS_DEB_ASSOC);
28 29 30 31 32 33

	/*
	 * Cisco AP sends EAP failure and de-auth in less than 0.5 ms.
	 * It causes problem in the Supplicant
	 */
	msleep_interruptible(1000);
34 35 36

	if (priv->wdev->iftype == NL80211_IFTYPE_STATION)
		lbs_send_disconnect_notification(priv);
37 38

	/* report disconnect to upper layer */
39 40
	netif_stop_queue(priv->dev);
	netif_carrier_off(priv->dev);
41

42 43 44 45 46
	/* Free Tx and Rx packets */
	kfree_skb(priv->currenttxskb);
	priv->currenttxskb = NULL;
	priv->tx_pending_len = 0;

47
	priv->connect_status = LBS_DISCONNECTED;
48

49
	if (priv->psstate != PS_STATE_FULL_POWER) {
50
		/* make firmware to exit PS mode */
51
		lbs_deb_cmd("disconnected, so exit PS mode\n");
52
		lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, false);
53
	}
54
	lbs_deb_leave(LBS_DEB_ASSOC);
55 56
}

57
int lbs_process_command_response(struct lbs_private *priv, u8 *data, u32 len)
58
{
59
	uint16_t respcmd, curcmd;
60
	struct cmd_header *resp;
61
	int ret = 0;
62 63
	unsigned long flags;
	uint16_t result;
64

65
	lbs_deb_enter(LBS_DEB_HOST);
66

67 68
	mutex_lock(&priv->lock);
	spin_lock_irqsave(&priv->driver_lock, flags);
69

70
	if (!priv->cur_cmd) {
71
		lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
72
		ret = -1;
73
		spin_unlock_irqrestore(&priv->driver_lock, flags);
74 75
		goto done;
	}
76

77
	resp = (void *)data;
78
	curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
79 80 81
	respcmd = le16_to_cpu(resp->command);
	result = le16_to_cpu(resp->result);

H
Holger Schurig 已提交
82
	lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d\n",
83 84
		     respcmd, le16_to_cpu(resp->seqnum), len);
	lbs_deb_hex(LBS_DEB_CMD, "CMD_RESP", (void *) resp, len);
85

86
	if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
87
		lbs_pr_info("Received CMD_RESP with invalid sequence %d (expected %d)\n",
88
			    le16_to_cpu(resp->seqnum), le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
89
		spin_unlock_irqrestore(&priv->driver_lock, flags);
90 91 92
		ret = -1;
		goto done;
	}
93
	if (respcmd != CMD_RET(curcmd) &&
94
	    respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
95 96 97 98 99 100
		lbs_pr_info("Invalid CMD_RESP %x to command %x!\n", respcmd, curcmd);
		spin_unlock_irqrestore(&priv->driver_lock, flags);
		ret = -1;
		goto done;
	}

101 102 103 104 105 106 107 108 109 110
	if (resp->result == cpu_to_le16(0x0004)) {
		/* 0x0004 means -EAGAIN. Drop the response, let it time out
		   and be resubmitted */
		lbs_pr_info("Firmware returns DEFER to command %x. Will let it time out...\n",
			    le16_to_cpu(resp->command));
		spin_unlock_irqrestore(&priv->driver_lock, flags);
		ret = -1;
		goto done;
	}

111 112
	/* Now we got response from FW, cancel the command timer */
	del_timer(&priv->command_timer);
113
	priv->cmd_timed_out = 0;
114 115

	/* Store the response code to cur_cmd_retcode. */
116
	priv->cur_cmd_retcode = result;
117

118
	if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
119
		struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
120
		u16 action = le16_to_cpu(psmode->action);
121

122 123
		lbs_deb_host(
		       "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
124
		       result, action);
125 126

		if (result) {
127
			lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
128 129 130 131
				    result);
			/*
			 * We should not re-try enter-ps command in
			 * ad-hoc mode. It takes place in
132
			 * lbs_execute_next_command().
133
			 */
K
Kiran Divekar 已提交
134
			if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR &&
135
			    action == PS_MODE_ACTION_ENTER_PS)
136
				priv->psmode = LBS802_11POWERMODECAM;
137
		} else if (action == PS_MODE_ACTION_ENTER_PS) {
138 139
			priv->needtowakeup = 0;
			priv->psstate = PS_STATE_AWAKE;
140

141
			lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
142
			if (priv->connect_status != LBS_CONNECTED) {
143 144 145 146
				/*
				 * When Deauth Event received before Enter_PS command
				 * response, We need to wake up the firmware.
				 */
147
				lbs_deb_host(
148
				       "disconnected, invoking lbs_ps_wakeup\n");
149

150 151
				spin_unlock_irqrestore(&priv->driver_lock, flags);
				mutex_unlock(&priv->lock);
152 153
				lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS,
						false);
154 155
				mutex_lock(&priv->lock);
				spin_lock_irqsave(&priv->driver_lock, flags);
156
			}
157
		} else if (action == PS_MODE_ACTION_EXIT_PS) {
158 159
			priv->needtowakeup = 0;
			priv->psstate = PS_STATE_FULL_POWER;
160
			lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
161
		} else {
162
			lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
163 164
		}

165
		lbs_complete_command(priv, priv->cur_cmd, result);
166
		spin_unlock_irqrestore(&priv->driver_lock, flags);
167 168 169 170 171 172 173

		ret = 0;
		goto done;
	}

	/* If the command is not successful, cleanup and return failure */
	if ((result != 0 || !(respcmd & 0x8000))) {
174 175
		lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
		       result, respcmd);
176 177 178 179
		/*
		 * Handling errors here
		 */
		switch (respcmd) {
180 181
		case CMD_RET(CMD_GET_HW_SPEC):
		case CMD_RET(CMD_802_11_RESET):
182
			lbs_deb_host("CMD_RESP: reset failed\n");
183 184 185
			break;

		}
186
		lbs_complete_command(priv, priv->cur_cmd, result);
187
		spin_unlock_irqrestore(&priv->driver_lock, flags);
188 189 190 191 192

		ret = -1;
		goto done;
	}

193
	spin_unlock_irqrestore(&priv->driver_lock, flags);
194

195 196
	if (priv->cur_cmd && priv->cur_cmd->callback) {
		ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
197
				resp);
198
	}
199

200
	spin_lock_irqsave(&priv->driver_lock, flags);
201

202
	if (priv->cur_cmd) {
203
		/* Clean up and Put current command back to cmdfreeq */
204
		lbs_complete_command(priv, priv->cur_cmd, result);
205
	}
206
	spin_unlock_irqrestore(&priv->driver_lock, flags);
207 208

done:
209
	mutex_unlock(&priv->lock);
210
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
211 212 213
	return ret;
}

214
int lbs_process_event(struct lbs_private *priv, u32 event)
215 216
{
	int ret = 0;
217
	struct cmd_header cmd;
218

219 220
	lbs_deb_enter(LBS_DEB_CMD);

221
	switch (event) {
222
	case MACREG_INT_CODE_LINK_SENSED:
223
		lbs_deb_cmd("EVENT: link sensed\n");
224 225 226
		break;

	case MACREG_INT_CODE_DEAUTHENTICATED:
227
		lbs_deb_cmd("EVENT: deauthenticated\n");
228
		lbs_mac_event_disconnected(priv);
229 230 231
		break;

	case MACREG_INT_CODE_DISASSOCIATED:
232
		lbs_deb_cmd("EVENT: disassociated\n");
233
		lbs_mac_event_disconnected(priv);
234 235
		break;

236
	case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
237
		lbs_deb_cmd("EVENT: link lost\n");
238
		lbs_mac_event_disconnected(priv);
239 240 241
		break;

	case MACREG_INT_CODE_PS_SLEEP:
242
		lbs_deb_cmd("EVENT: ps sleep\n");
243 244

		/* handle unexpected PS SLEEP event */
245
		if (priv->psstate == PS_STATE_FULL_POWER) {
246
			lbs_deb_cmd(
247
			       "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
248 249
			break;
		}
250
		priv->psstate = PS_STATE_PRE_SLEEP;
251

252
		lbs_ps_confirm_sleep(priv);
253 254 255

		break;

256
	case MACREG_INT_CODE_HOST_AWAKE:
257
		lbs_deb_cmd("EVENT: host awake\n");
258 259 260
		if (priv->reset_deep_sleep_wakeup)
			priv->reset_deep_sleep_wakeup(priv);
		priv->is_deep_sleep = 0;
261 262 263 264
		lbs_cmd_async(priv, CMD_802_11_WAKEUP_CONFIRM, &cmd,
				sizeof(cmd));
		priv->is_host_sleep_activated = 0;
		wake_up_interruptible(&priv->host_sleep_q);
265 266
		break;

267 268 269 270 271 272 273 274 275
	case MACREG_INT_CODE_DEEP_SLEEP_AWAKE:
		if (priv->reset_deep_sleep_wakeup)
			priv->reset_deep_sleep_wakeup(priv);
		lbs_deb_cmd("EVENT: ds awake\n");
		priv->is_deep_sleep = 0;
		priv->wakeup_dev_required = 0;
		wake_up_interruptible(&priv->ds_awake_q);
		break;

276
	case MACREG_INT_CODE_PS_AWAKE:
277
		lbs_deb_cmd("EVENT: ps awake\n");
278
		/* handle unexpected PS AWAKE event */
279
		if (priv->psstate == PS_STATE_FULL_POWER) {
280
			lbs_deb_cmd(
281 282 283 284
			       "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
			break;
		}

285
		priv->psstate = PS_STATE_AWAKE;
286

287
		if (priv->needtowakeup) {
288 289 290
			/*
			 * wait for the command processing to finish
			 * before resuming sending
291
			 * priv->needtowakeup will be set to FALSE
292
			 * in lbs_ps_wakeup()
293
			 */
294
			lbs_deb_cmd("waking up ...\n");
295
			lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, false);
296 297 298 299
		}
		break;

	case MACREG_INT_CODE_MIC_ERR_UNICAST:
300
		lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
301
		lbs_send_mic_failureevent(priv, event);
302 303 304
		break;

	case MACREG_INT_CODE_MIC_ERR_MULTICAST:
305
		lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
306
		lbs_send_mic_failureevent(priv, event);
307
		break;
308

309
	case MACREG_INT_CODE_MIB_CHANGED:
310 311
		lbs_deb_cmd("EVENT: MIB CHANGED\n");
		break;
312
	case MACREG_INT_CODE_INIT_DONE:
313
		lbs_deb_cmd("EVENT: INIT DONE\n");
314 315
		break;
	case MACREG_INT_CODE_ADHOC_BCN_LOST:
316
		lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
317 318
		break;
	case MACREG_INT_CODE_RSSI_LOW:
319
		lbs_pr_alert("EVENT: rssi low\n");
320 321
		break;
	case MACREG_INT_CODE_SNR_LOW:
322
		lbs_pr_alert("EVENT: snr low\n");
323 324
		break;
	case MACREG_INT_CODE_MAX_FAIL:
325
		lbs_pr_alert("EVENT: max fail\n");
326 327
		break;
	case MACREG_INT_CODE_RSSI_HIGH:
328
		lbs_pr_alert("EVENT: rssi high\n");
329 330
		break;
	case MACREG_INT_CODE_SNR_HIGH:
331
		lbs_pr_alert("EVENT: snr high\n");
332 333
		break;

334
	case MACREG_INT_CODE_MESH_AUTO_STARTED:
335 336
		/* Ignore spurious autostart events */
		lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
337 338
		break;

339
	default:
340
		lbs_pr_alert("EVENT: unknown event id %d\n", event);
341 342 343
		break;
	}

344
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
345 346
	return ret;
}