cmdresp.c 11.8 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
	lbs_send_disconnect_notification(priv);
35 36

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

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

45
	priv->connect_status = LBS_DISCONNECTED;
46

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

55
static int lbs_ret_reg_access(struct lbs_private *priv,
56 57
			       u16 type, struct cmd_ds_command *resp)
{
58
	int ret = 0;
59

60
	lbs_deb_enter(LBS_DEB_CMD);
61 62

	switch (type) {
63
	case CMD_RET(CMD_MAC_REG_ACCESS):
64
		{
65
			struct cmd_ds_mac_reg_access *reg = &resp->params.macreg;
66

67 68
			priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
			priv->offsetvalue.value = le32_to_cpu(reg->value);
69 70 71
			break;
		}

72
	case CMD_RET(CMD_BBP_REG_ACCESS):
73
		{
74
			struct cmd_ds_bbp_reg_access *reg = &resp->params.bbpreg;
75

76 77
			priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
			priv->offsetvalue.value = reg->value;
78 79 80
			break;
		}

81
	case CMD_RET(CMD_RF_REG_ACCESS):
82
		{
83
			struct cmd_ds_rf_reg_access *reg = &resp->params.rfreg;
84

85 86
			priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
			priv->offsetvalue.value = reg->value;
87 88 89 90
			break;
		}

	default:
91
		ret = -1;
92 93
	}

94
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
95
	return ret;
96 97
}

98
static inline int handle_cmd_response(struct lbs_private *priv,
99
				      struct cmd_header *cmd_response)
100
{
101
	struct cmd_ds_command *resp = (struct cmd_ds_command *) cmd_response;
102 103
	int ret = 0;
	unsigned long flags;
104
	uint16_t respcmd = le16_to_cpu(resp->command);
105

106 107
	lbs_deb_enter(LBS_DEB_HOST);

108
	switch (respcmd) {
109 110 111
	case CMD_RET(CMD_MAC_REG_ACCESS):
	case CMD_RET(CMD_BBP_REG_ACCESS):
	case CMD_RET(CMD_RF_REG_ACCESS):
112
		ret = lbs_ret_reg_access(priv, respcmd, resp);
113 114
		break;

115 116
	case CMD_RET(CMD_802_11_SET_AFC):
	case CMD_RET(CMD_802_11_GET_AFC):
117
		spin_lock_irqsave(&priv->driver_lock, flags);
118
		memmove((void *)priv->cur_cmd->callback_arg, &resp->params.afc,
119
			sizeof(struct cmd_ds_802_11_afc));
120
		spin_unlock_irqrestore(&priv->driver_lock, flags);
121 122 123

		break;

124
	case CMD_RET(CMD_802_11_BEACON_STOP):
125 126
		break;

127
	case CMD_RET(CMD_802_11_RSSI):
128
		ret = lbs_ret_802_11_rssi(priv, resp);
129 130
		break;

131
	case CMD_RET(CMD_802_11_TPC_CFG):
132
		spin_lock_irqsave(&priv->driver_lock, flags);
133
		memmove((void *)priv->cur_cmd->callback_arg, &resp->params.tpccfg,
134
			sizeof(struct cmd_ds_802_11_tpc_cfg));
135
		spin_unlock_irqrestore(&priv->driver_lock, flags);
136
		break;
137

138
	case CMD_RET(CMD_BT_ACCESS):
139
		spin_lock_irqsave(&priv->driver_lock, flags);
140 141
		if (priv->cur_cmd->callback_arg)
			memcpy((void *)priv->cur_cmd->callback_arg,
142
			       &resp->params.bt.addr1, 2 * ETH_ALEN);
143
		spin_unlock_irqrestore(&priv->driver_lock, flags);
144
		break;
145
	case CMD_RET(CMD_FWT_ACCESS):
146
		spin_lock_irqsave(&priv->driver_lock, flags);
147 148
		if (priv->cur_cmd->callback_arg)
			memcpy((void *)priv->cur_cmd->callback_arg, &resp->params.fwt,
149
			       sizeof(resp->params.fwt));
150
		spin_unlock_irqrestore(&priv->driver_lock, flags);
151
		break;
152 153 154 155
	case CMD_RET(CMD_802_11_BEACON_CTRL):
		ret = lbs_ret_802_11_bcn_ctrl(priv, resp);
		break;

156
	default:
157 158
		lbs_pr_err("CMD_RESP: unknown cmd response 0x%04x\n",
			   le16_to_cpu(resp->command));
159 160
		break;
	}
161
	lbs_deb_leave(LBS_DEB_HOST);
162 163 164
	return ret;
}

165
int lbs_process_command_response(struct lbs_private *priv, u8 *data, u32 len)
166
{
167
	uint16_t respcmd, curcmd;
168
	struct cmd_header *resp;
169
	int ret = 0;
170 171
	unsigned long flags;
	uint16_t result;
172

173
	lbs_deb_enter(LBS_DEB_HOST);
174

175 176
	mutex_lock(&priv->lock);
	spin_lock_irqsave(&priv->driver_lock, flags);
177

178
	if (!priv->cur_cmd) {
179
		lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
180
		ret = -1;
181
		spin_unlock_irqrestore(&priv->driver_lock, flags);
182 183
		goto done;
	}
184

185
	resp = (void *)data;
186
	curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
187 188 189
	respcmd = le16_to_cpu(resp->command);
	result = le16_to_cpu(resp->result);

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

194
	if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
195
		lbs_pr_info("Received CMD_RESP with invalid sequence %d (expected %d)\n",
196
			    le16_to_cpu(resp->seqnum), le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
197
		spin_unlock_irqrestore(&priv->driver_lock, flags);
198 199 200
		ret = -1;
		goto done;
	}
201
	if (respcmd != CMD_RET(curcmd) &&
202
	    respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
203 204 205 206 207 208
		lbs_pr_info("Invalid CMD_RESP %x to command %x!\n", respcmd, curcmd);
		spin_unlock_irqrestore(&priv->driver_lock, flags);
		ret = -1;
		goto done;
	}

209 210 211 212 213 214 215 216 217 218
	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;
	}

219 220
	/* Now we got response from FW, cancel the command timer */
	del_timer(&priv->command_timer);
221
	priv->cmd_timed_out = 0;
222 223

	/* Store the response code to cur_cmd_retcode. */
224
	priv->cur_cmd_retcode = result;
225

226
	if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
227
		struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
228
		u16 action = le16_to_cpu(psmode->action);
229

230 231
		lbs_deb_host(
		       "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
232
		       result, action);
233 234

		if (result) {
235
			lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
236 237 238 239
				    result);
			/*
			 * We should not re-try enter-ps command in
			 * ad-hoc mode. It takes place in
240
			 * lbs_execute_next_command().
241
			 */
K
Kiran Divekar 已提交
242
			if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR &&
243
			    action == CMD_SUBCMD_ENTER_PS)
244
				priv->psmode = LBS802_11POWERMODECAM;
245
		} else if (action == CMD_SUBCMD_ENTER_PS) {
246 247
			priv->needtowakeup = 0;
			priv->psstate = PS_STATE_AWAKE;
248

249
			lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
250
			if (priv->connect_status != LBS_CONNECTED) {
251 252 253 254
				/*
				 * When Deauth Event received before Enter_PS command
				 * response, We need to wake up the firmware.
				 */
255
				lbs_deb_host(
256
				       "disconnected, invoking lbs_ps_wakeup\n");
257

258 259
				spin_unlock_irqrestore(&priv->driver_lock, flags);
				mutex_unlock(&priv->lock);
260
				lbs_ps_wakeup(priv, 0);
261 262
				mutex_lock(&priv->lock);
				spin_lock_irqsave(&priv->driver_lock, flags);
263
			}
264
		} else if (action == CMD_SUBCMD_EXIT_PS) {
265 266
			priv->needtowakeup = 0;
			priv->psstate = PS_STATE_FULL_POWER;
267
			lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
268
		} else {
269
			lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
270 271
		}

272
		lbs_complete_command(priv, priv->cur_cmd, result);
273
		spin_unlock_irqrestore(&priv->driver_lock, flags);
274 275 276 277 278 279 280

		ret = 0;
		goto done;
	}

	/* If the command is not successful, cleanup and return failure */
	if ((result != 0 || !(respcmd & 0x8000))) {
281 282
		lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
		       result, respcmd);
283 284 285 286
		/*
		 * Handling errors here
		 */
		switch (respcmd) {
287 288
		case CMD_RET(CMD_GET_HW_SPEC):
		case CMD_RET(CMD_802_11_RESET):
289
			lbs_deb_host("CMD_RESP: reset failed\n");
290 291 292
			break;

		}
293
		lbs_complete_command(priv, priv->cur_cmd, result);
294
		spin_unlock_irqrestore(&priv->driver_lock, flags);
295 296 297 298 299

		ret = -1;
		goto done;
	}

300
	spin_unlock_irqrestore(&priv->driver_lock, flags);
301

302 303
	if (priv->cur_cmd && priv->cur_cmd->callback) {
		ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
304
				resp);
305
	} else
306
		ret = handle_cmd_response(priv, resp);
307

308
	spin_lock_irqsave(&priv->driver_lock, flags);
309

310
	if (priv->cur_cmd) {
311
		/* Clean up and Put current command back to cmdfreeq */
312
		lbs_complete_command(priv, priv->cur_cmd, result);
313
	}
314
	spin_unlock_irqrestore(&priv->driver_lock, flags);
315 316

done:
317
	mutex_unlock(&priv->lock);
318
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
319 320 321
	return ret;
}

322
int lbs_process_event(struct lbs_private *priv, u32 event)
323 324
{
	int ret = 0;
325
	struct cmd_header cmd;
326

327 328
	lbs_deb_enter(LBS_DEB_CMD);

329
	switch (event) {
330
	case MACREG_INT_CODE_LINK_SENSED:
331
		lbs_deb_cmd("EVENT: link sensed\n");
332 333 334
		break;

	case MACREG_INT_CODE_DEAUTHENTICATED:
335
		lbs_deb_cmd("EVENT: deauthenticated\n");
336
		lbs_mac_event_disconnected(priv);
337 338 339
		break;

	case MACREG_INT_CODE_DISASSOCIATED:
340
		lbs_deb_cmd("EVENT: disassociated\n");
341
		lbs_mac_event_disconnected(priv);
342 343
		break;

344
	case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
345
		lbs_deb_cmd("EVENT: link lost\n");
346
		lbs_mac_event_disconnected(priv);
347 348 349
		break;

	case MACREG_INT_CODE_PS_SLEEP:
350
		lbs_deb_cmd("EVENT: ps sleep\n");
351 352

		/* handle unexpected PS SLEEP event */
353
		if (priv->psstate == PS_STATE_FULL_POWER) {
354
			lbs_deb_cmd(
355
			       "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
356 357
			break;
		}
358
		priv->psstate = PS_STATE_PRE_SLEEP;
359

360
		lbs_ps_confirm_sleep(priv);
361 362 363

		break;

364
	case MACREG_INT_CODE_HOST_AWAKE:
365
		lbs_deb_cmd("EVENT: host awake\n");
366 367 368
		if (priv->reset_deep_sleep_wakeup)
			priv->reset_deep_sleep_wakeup(priv);
		priv->is_deep_sleep = 0;
369 370 371 372
		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);
373 374
		break;

375 376 377 378 379 380 381 382 383
	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;

384
	case MACREG_INT_CODE_PS_AWAKE:
385
		lbs_deb_cmd("EVENT: ps awake\n");
386
		/* handle unexpected PS AWAKE event */
387
		if (priv->psstate == PS_STATE_FULL_POWER) {
388
			lbs_deb_cmd(
389 390 391 392
			       "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
			break;
		}

393
		priv->psstate = PS_STATE_AWAKE;
394

395
		if (priv->needtowakeup) {
396 397 398
			/*
			 * wait for the command processing to finish
			 * before resuming sending
399
			 * priv->needtowakeup will be set to FALSE
400
			 * in lbs_ps_wakeup()
401
			 */
402
			lbs_deb_cmd("waking up ...\n");
403
			lbs_ps_wakeup(priv, 0);
404 405 406 407
		}
		break;

	case MACREG_INT_CODE_MIC_ERR_UNICAST:
408
		lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
409
		lbs_send_mic_failureevent(priv, event);
410 411 412
		break;

	case MACREG_INT_CODE_MIC_ERR_MULTICAST:
413
		lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
414
		lbs_send_mic_failureevent(priv, event);
415
		break;
416

417
	case MACREG_INT_CODE_MIB_CHANGED:
418 419
		lbs_deb_cmd("EVENT: MIB CHANGED\n");
		break;
420
	case MACREG_INT_CODE_INIT_DONE:
421
		lbs_deb_cmd("EVENT: INIT DONE\n");
422 423
		break;
	case MACREG_INT_CODE_ADHOC_BCN_LOST:
424
		lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
425 426
		break;
	case MACREG_INT_CODE_RSSI_LOW:
427
		lbs_pr_alert("EVENT: rssi low\n");
428 429
		break;
	case MACREG_INT_CODE_SNR_LOW:
430
		lbs_pr_alert("EVENT: snr low\n");
431 432
		break;
	case MACREG_INT_CODE_MAX_FAIL:
433
		lbs_pr_alert("EVENT: max fail\n");
434 435
		break;
	case MACREG_INT_CODE_RSSI_HIGH:
436
		lbs_pr_alert("EVENT: rssi high\n");
437 438
		break;
	case MACREG_INT_CODE_SNR_HIGH:
439
		lbs_pr_alert("EVENT: snr high\n");
440 441
		break;

442
	case MACREG_INT_CODE_MESH_AUTO_STARTED:
443 444
		/* Ignore spurious autostart events */
		lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
445 446
		break;

447
	default:
448
		lbs_pr_alert("EVENT: unknown event id %d\n", event);
449 450 451
		break;
	}

452
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
453 454
	return ret;
}