cmdresp.c 27.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/**
  * This file contains the handling of command
  * responses as well as events generated by firmware.
  */
#include <linux/delay.h>
#include <linux/if_arp.h>
#include <linux/netdevice.h>

#include <net/iw_handler.h>

#include "host.h"
#include "decl.h"
#include "defs.h"
#include "dev.h"
#include "join.h"
#include "wext.h"

/**
 *  @brief This function handles disconnect event. it
 *  reports disconnect to upper layer, clean tx/rx packets,
 *  reset link state etc.
 *
23
 *  @param priv    A pointer to lbs_private structure
24 25
 *  @return 	   n/a
 */
26
void lbs_mac_event_disconnected(lbs_private * priv)
27
{
28
	lbs_adapter *adapter = priv->adapter;
29 30
	union iwreq_data wrqu;

31
	if (adapter->connect_status != LBS_CONNECTED)
32 33
		return;

34
	lbs_deb_enter(LBS_DEB_CMD);
35 36 37 38 39 40 41 42 43 44

	memset(wrqu.ap_addr.sa_data, 0x00, ETH_ALEN);
	wrqu.ap_addr.sa_family = ARPHRD_ETHER;

	/*
	 * Cisco AP sends EAP failure and de-auth in less than 0.5 ms.
	 * It causes problem in the Supplicant
	 */

	msleep_interruptible(1000);
45
	wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
46 47 48 49 50 51

	/* Free Tx and Rx packets */
	kfree_skb(priv->adapter->currenttxskb);
	priv->adapter->currenttxskb = NULL;

	/* report disconnect to upper layer */
52 53
	netif_stop_queue(priv->dev);
	netif_carrier_off(priv->dev);
54 55 56 57 58 59 60 61 62

	/* reset SNR/NF/RSSI values */
	memset(adapter->SNR, 0x00, sizeof(adapter->SNR));
	memset(adapter->NF, 0x00, sizeof(adapter->NF));
	memset(adapter->RSSI, 0x00, sizeof(adapter->RSSI));
	memset(adapter->rawSNR, 0x00, sizeof(adapter->rawSNR));
	memset(adapter->rawNF, 0x00, sizeof(adapter->rawNF));
	adapter->nextSNRNF = 0;
	adapter->numSNRNF = 0;
63
	lbs_deb_cmd("current SSID '%s', length %u\n",
64 65 66
	            escape_essid(adapter->curbssparams.ssid,
	                         adapter->curbssparams.ssid_len),
	            adapter->curbssparams.ssid_len);
67

68
	adapter->connect_status = LBS_DISCONNECTED;
69

70 71 72 73
	/* Clear out associated SSID and BSSID since connection is
	 * no longer valid.
	 */
	memset(&adapter->curbssparams.bssid, 0, ETH_ALEN);
74 75
	memset(&adapter->curbssparams.ssid, 0, IW_ESSID_MAX_SIZE);
	adapter->curbssparams.ssid_len = 0;
76 77 78

	if (adapter->psstate != PS_STATE_FULL_POWER) {
		/* make firmware to exit PS mode */
79
		lbs_deb_cmd("disconnected, so exit PS mode\n");
80
		lbs_ps_wakeup(priv, 0);
81
	}
82
	lbs_deb_leave(LBS_DEB_CMD);
83 84 85 86 87
}

/**
 *  @brief This function handles MIC failure event.
 *
88
 *  @param priv    A pointer to lbs_private structure
89 90 91
 *  @para  event   the event id
 *  @return 	   n/a
 */
92
static void handle_mic_failureevent(lbs_private * priv, u32 event)
93 94 95
{
	char buf[50];

96
	lbs_deb_enter(LBS_DEB_CMD);
97 98 99 100 101 102 103 104 105 106
	memset(buf, 0, sizeof(buf));

	sprintf(buf, "%s", "MLME-MICHAELMICFAILURE.indication ");

	if (event == MACREG_INT_CODE_MIC_ERR_UNICAST) {
		strcat(buf, "unicast ");
	} else {
		strcat(buf, "multicast ");
	}

107
	lbs_send_iwevcustom_event(priv, buf);
108
	lbs_deb_leave(LBS_DEB_CMD);
109 110
}

111
static int lbs_ret_reg_access(lbs_private * priv,
112 113
			       u16 type, struct cmd_ds_command *resp)
{
114
	int ret = 0;
115
	lbs_adapter *adapter = priv->adapter;
116

117
	lbs_deb_enter(LBS_DEB_CMD);
118 119

	switch (type) {
120
	case CMD_RET(CMD_MAC_REG_ACCESS):
121
		{
122
			struct cmd_ds_mac_reg_access *reg = &resp->params.macreg;
123

124 125
			adapter->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
			adapter->offsetvalue.value = le32_to_cpu(reg->value);
126 127 128
			break;
		}

129
	case CMD_RET(CMD_BBP_REG_ACCESS):
130
		{
131
			struct cmd_ds_bbp_reg_access *reg = &resp->params.bbpreg;
132

133
			adapter->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
134 135 136 137
			adapter->offsetvalue.value = reg->value;
			break;
		}

138
	case CMD_RET(CMD_RF_REG_ACCESS):
139
		{
140
			struct cmd_ds_rf_reg_access *reg = &resp->params.rfreg;
141

142
			adapter->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
143 144 145 146 147
			adapter->offsetvalue.value = reg->value;
			break;
		}

	default:
148
		ret = -1;
149 150
	}

151
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
152
	return ret;
153 154
}

155
static int lbs_ret_get_hw_spec(lbs_private * priv,
156 157 158 159
				struct cmd_ds_command *resp)
{
	u32 i;
	struct cmd_ds_get_hw_spec *hwspec = &resp->params.hwspec;
160
	lbs_adapter *adapter = priv->adapter;
161
	int ret = 0;
162
	DECLARE_MAC_BUF(mac);
163

164
	lbs_deb_enter(LBS_DEB_CMD);
165 166 167

	adapter->fwcapinfo = le32_to_cpu(hwspec->fwcapinfo);

168
	memcpy(adapter->fwreleasenumber, hwspec->fwreleasenumber, 4);
169

170
	lbs_deb_cmd("GET_HW_SPEC: firmware release %u.%u.%up%u\n",
171 172
		    adapter->fwreleasenumber[2], adapter->fwreleasenumber[1],
		    adapter->fwreleasenumber[0], adapter->fwreleasenumber[3]);
173 174
	lbs_deb_cmd("GET_HW_SPEC: MAC addr %s\n",
		    print_mac(mac, hwspec->permanentaddr));
175
	lbs_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
176 177
	       hwspec->hwifversion, hwspec->version);

178 179 180 181 182
	/* Clamp region code to 8-bit since FW spec indicates that it should
	 * only ever be 8-bit, even though the field size is 16-bit.  Some firmware
	 * returns non-zero high 8 bits here.
	 */
	adapter->regioncode = le16_to_cpu(hwspec->regioncode) & 0xFF;
183 184 185

	for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
		/* use the region code to search for the index */
186
		if (adapter->regioncode == lbs_region_code_to_index[i]) {
187 188 189 190 191 192 193
			break;
		}
	}

	/* if it's unidentified region code, use the default (USA) */
	if (i >= MRVDRV_MAX_REGION_CODE) {
		adapter->regioncode = 0x10;
194
		lbs_pr_info("unidentified region code; using the default (USA)\n");
195 196
	}

197 198
	if (adapter->current_addr[0] == 0xff)
		memmove(adapter->current_addr, hwspec->permanentaddr, ETH_ALEN);
199

200
	memcpy(priv->dev->dev_addr, adapter->current_addr, ETH_ALEN);
201
	if (priv->mesh_dev)
202
		memcpy(priv->mesh_dev->dev_addr, adapter->current_addr, ETH_ALEN);
203

204
	if (lbs_set_regiontable(priv, adapter->regioncode, 0)) {
205 206 207 208
		ret = -1;
		goto done;
	}

209
	if (lbs_set_universaltable(priv, 0)) {
210 211 212 213
		ret = -1;
		goto done;
	}

214 215
done:
	lbs_deb_enter_args(LBS_DEB_CMD, "ret %d", ret);
216 217 218
	return ret;
}

219
static int lbs_ret_802_11_sleep_params(lbs_private * priv,
220 221 222
					struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_sleep_params *sp = &resp->params.sleep_params;
223
	lbs_adapter *adapter = priv->adapter;
224

225
	lbs_deb_enter(LBS_DEB_CMD);
226

227 228
	lbs_deb_cmd("error 0x%x, offset 0x%x, stabletime 0x%x, calcontrol 0x%x "
		    "extsleepclk 0x%x\n", le16_to_cpu(sp->error),
229 230 231
		    le16_to_cpu(sp->offset), le16_to_cpu(sp->stabletime),
		    sp->calcontrol, sp->externalsleepclk);

232 233 234
	adapter->sp.sp_error = le16_to_cpu(sp->error);
	adapter->sp.sp_offset = le16_to_cpu(sp->offset);
	adapter->sp.sp_stabletime = le16_to_cpu(sp->stabletime);
235 236
	adapter->sp.sp_calcontrol = sp->calcontrol;
	adapter->sp.sp_extsleepclk = sp->externalsleepclk;
237 238
	adapter->sp.sp_reserved = le16_to_cpu(sp->reserved);

239
	lbs_deb_enter(LBS_DEB_CMD);
240 241 242
	return 0;
}

243
static int lbs_ret_802_11_stat(lbs_private * priv,
244 245
				struct cmd_ds_command *resp)
{
246
	lbs_deb_enter(LBS_DEB_CMD);
247 248 249
/*	currently adapter->wlan802_11Stat is unused

	struct cmd_ds_802_11_get_stat *p11Stat = &resp->params.gstat;
250
	lbs_adapter *adapter = priv->adapter;
251 252 253 254 255

	// TODO Convert it to Big endian befor copy
	memcpy(&adapter->wlan802_11Stat,
	       p11Stat, sizeof(struct cmd_ds_802_11_get_stat));
*/
256
	lbs_deb_leave(LBS_DEB_CMD);
257 258 259
	return 0;
}

260
static int lbs_ret_802_11_snmp_mib(lbs_private * priv,
261 262 263 264 265 266
				    struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_snmp_mib *smib = &resp->params.smib;
	u16 oid = le16_to_cpu(smib->oid);
	u16 querytype = le16_to_cpu(smib->querytype);

267
	lbs_deb_enter(LBS_DEB_CMD);
268

269
	lbs_deb_cmd("SNMP_RESP: oid 0x%x, querytype 0x%x\n", oid,
270
	       querytype);
271
	lbs_deb_cmd("SNMP_RESP: Buf size %d\n", le16_to_cpu(smib->bufsize));
272

273
	if (querytype == CMD_ACT_GET) {
274
		switch (oid) {
275
		case FRAGTHRESH_I:
276
			priv->adapter->fragthsd =
277
				le16_to_cpu(*((__le16 *)(smib->value)));
278
			lbs_deb_cmd("SNMP_RESP: frag threshold %u\n",
279
				    priv->adapter->fragthsd);
280
			break;
281
		case RTSTHRESH_I:
282
			priv->adapter->rtsthsd =
283
				le16_to_cpu(*((__le16 *)(smib->value)));
284
			lbs_deb_cmd("SNMP_RESP: rts threshold %u\n",
285
				    priv->adapter->rtsthsd);
286
			break;
287
		case SHORT_RETRYLIM_I:
288
			priv->adapter->txretrycount =
289
				le16_to_cpu(*((__le16 *)(smib->value)));
290
			lbs_deb_cmd("SNMP_RESP: tx retry count %u\n",
291
				    priv->adapter->rtsthsd);
292 293 294 295 296 297
			break;
		default:
			break;
		}
	}

298
	lbs_deb_enter(LBS_DEB_CMD);
299 300 301
	return 0;
}

302
static int lbs_ret_802_11_key_material(lbs_private * priv,
303 304 305 306
					struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_key_material *pkeymaterial =
	    &resp->params.keymaterial;
307
	lbs_adapter *adapter = priv->adapter;
308 309
	u16 action = le16_to_cpu(pkeymaterial->action);

310
	lbs_deb_enter(LBS_DEB_CMD);
311 312

	/* Copy the returned key to driver private data */
313
	if (action == CMD_ACT_GET) {
314 315 316 317 318 319
		u8 * buf_ptr = (u8 *) &pkeymaterial->keyParamSet;
		u8 * resp_end = (u8 *) (resp + le16_to_cpu(resp->size));

		while (buf_ptr < resp_end) {
			struct MrvlIEtype_keyParamSet * pkeyparamset =
			    (struct MrvlIEtype_keyParamSet *) buf_ptr;
320
			struct enc_key * pkey;
321 322
			u16 param_set_len = le16_to_cpu(pkeyparamset->length);
			u16 key_len = le16_to_cpu(pkeyparamset->keylen);
323 324 325
			u16 key_flags = le16_to_cpu(pkeyparamset->keyinfo);
			u16 key_type = le16_to_cpu(pkeyparamset->keytypeid);
			u8 * end;
326 327 328 329 330 331 332 333

			end = (u8 *) pkeyparamset + sizeof (pkeyparamset->type)
			                          + sizeof (pkeyparamset->length)
			                          + param_set_len;
			/* Make sure we don't access past the end of the IEs */
			if (end > resp_end)
				break;

334
			if (key_flags & KEY_INFO_WPA_UNICAST)
335
				pkey = &adapter->wpa_unicast_key;
336
			else if (key_flags & KEY_INFO_WPA_MCAST)
337 338 339 340 341
				pkey = &adapter->wpa_mcast_key;
			else
				break;

			/* Copy returned key into driver */
342
			memset(pkey, 0, sizeof(struct enc_key));
343 344
			if (key_len > sizeof(pkey->key))
				break;
345 346 347
			pkey->type = key_type;
			pkey->flags = key_flags;
			pkey->len = key_len;
348 349 350 351 352 353
			memcpy(pkey->key, pkeyparamset->key, pkey->len);

			buf_ptr = end + 1;
		}
	}

354
	lbs_deb_enter(LBS_DEB_CMD);
355 356 357
	return 0;
}

358
static int lbs_ret_802_11_mac_address(lbs_private * priv,
359 360 361
				       struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_mac_address *macadd = &resp->params.macadd;
362
	lbs_adapter *adapter = priv->adapter;
363

364
	lbs_deb_enter(LBS_DEB_CMD);
365 366 367

	memcpy(adapter->current_addr, macadd->macadd, ETH_ALEN);

368
	lbs_deb_enter(LBS_DEB_CMD);
369 370 371
	return 0;
}

372
static int lbs_ret_802_11_rf_tx_power(lbs_private * priv,
373 374 375
				       struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_rf_tx_power *rtp = &resp->params.txp;
376
	lbs_adapter *adapter = priv->adapter;
377

378
	lbs_deb_enter(LBS_DEB_CMD);
379 380 381

	adapter->txpowerlevel = le16_to_cpu(rtp->currentlevel);

382
	lbs_deb_cmd("TX power currently %d\n", adapter->txpowerlevel);
383

384
	lbs_deb_leave(LBS_DEB_CMD);
385 386 387
	return 0;
}

388
static int lbs_ret_802_11_rate_adapt_rateset(lbs_private * priv,
389 390
					      struct cmd_ds_command *resp)
{
391
	struct cmd_ds_802_11_rate_adapt_rateset *rates = &resp->params.rateset;
392
	lbs_adapter *adapter = priv->adapter;
393

394
	lbs_deb_enter(LBS_DEB_CMD);
395

396
	if (rates->action == CMD_ACT_GET) {
397 398
		adapter->enablehwauto = le16_to_cpu(rates->enablehwauto);
		adapter->ratebitmap = le16_to_cpu(rates->bitmap);
399 400
	}

401
	lbs_deb_leave(LBS_DEB_CMD);
402 403 404
	return 0;
}

405
static int lbs_ret_802_11_data_rate(lbs_private * priv,
406 407 408
				     struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_data_rate *pdatarate = &resp->params.drate;
409
	lbs_adapter *adapter = priv->adapter;
410

411
	lbs_deb_enter(LBS_DEB_CMD);
412

413
	lbs_deb_hex(LBS_DEB_CMD, "DATA_RATE_RESP", (u8 *) pdatarate,
414
		sizeof(struct cmd_ds_802_11_data_rate));
415

416 417 418
	/* FIXME: get actual rates FW can do if this command actually returns
	 * all data rates supported.
	 */
419
	adapter->cur_rate = lbs_fw_index_to_data_rate(pdatarate->rates[0]);
420
	lbs_deb_cmd("DATA_RATE: current rate 0x%02x\n", adapter->cur_rate);
421

422
	lbs_deb_leave(LBS_DEB_CMD);
423 424 425
	return 0;
}

426
static int lbs_ret_802_11_rf_channel(lbs_private * priv,
427 428
				      struct cmd_ds_command *resp)
{
429
	struct cmd_ds_802_11_rf_channel *rfchannel = &resp->params.rfchannel;
430
	lbs_adapter *adapter = priv->adapter;
431 432 433
	u16 action = le16_to_cpu(rfchannel->action);
	u16 newchannel = le16_to_cpu(rfchannel->currentchannel);

434
	lbs_deb_enter(LBS_DEB_CMD);
435

436
	if (action == CMD_OPT_802_11_RF_CHANNEL_GET
437
	    && adapter->curbssparams.channel != newchannel) {
438
		lbs_deb_cmd("channel switch from %d to %d\n",
439 440 441 442 443 444
		       adapter->curbssparams.channel, newchannel);

		/* Update the channel again */
		adapter->curbssparams.channel = newchannel;
	}

445
	lbs_deb_enter(LBS_DEB_CMD);
446 447 448
	return 0;
}

449
static int lbs_ret_802_11_rssi(lbs_private * priv,
450 451 452
				struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_rssi_rsp *rssirsp = &resp->params.rssirsp;
453
	lbs_adapter *adapter = priv->adapter;
454

455 456
	lbs_deb_enter(LBS_DEB_CMD);

457 458
	/* store the non average value */
	adapter->SNR[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->SNR);
459
	adapter->NF[TYPE_BEACON][TYPE_NOAVG] = le16_to_cpu(rssirsp->noisefloor);
460 461

	adapter->SNR[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgSNR);
462
	adapter->NF[TYPE_BEACON][TYPE_AVG] = le16_to_cpu(rssirsp->avgnoisefloor);
463 464 465 466 467 468 469 470 471

	adapter->RSSI[TYPE_BEACON][TYPE_NOAVG] =
	    CAL_RSSI(adapter->SNR[TYPE_BEACON][TYPE_NOAVG],
		     adapter->NF[TYPE_BEACON][TYPE_NOAVG]);

	adapter->RSSI[TYPE_BEACON][TYPE_AVG] =
	    CAL_RSSI(adapter->SNR[TYPE_BEACON][TYPE_AVG] / AVG_SCALE,
		     adapter->NF[TYPE_BEACON][TYPE_AVG] / AVG_SCALE);

472 473
	lbs_deb_cmd("RSSI: beacon %d, avg %d\n",
	       adapter->RSSI[TYPE_BEACON][TYPE_NOAVG],
474 475
	       adapter->RSSI[TYPE_BEACON][TYPE_AVG]);

476
	lbs_deb_leave(LBS_DEB_CMD);
477 478 479
	return 0;
}

480
static int lbs_ret_802_11_eeprom_access(lbs_private * priv,
481 482
				  struct cmd_ds_command *resp)
{
483 484 485
	lbs_adapter *adapter = priv->adapter;
	struct lbs_ioctl_regrdwr *pbuf;
	pbuf = (struct lbs_ioctl_regrdwr *) adapter->prdeeprom;
486

487
	lbs_deb_enter_args(LBS_DEB_CMD, "len %d",
488 489 490
	       le16_to_cpu(resp->params.rdeeprom.bytecount));
	if (pbuf->NOB < le16_to_cpu(resp->params.rdeeprom.bytecount)) {
		pbuf->NOB = 0;
491
		lbs_deb_cmd("EEPROM read length too big\n");
492 493 494 495 496 497 498
		return -1;
	}
	pbuf->NOB = le16_to_cpu(resp->params.rdeeprom.bytecount);
	if (pbuf->NOB > 0) {

		memcpy(&pbuf->value, (u8 *) & resp->params.rdeeprom.value,
		       le16_to_cpu(resp->params.rdeeprom.bytecount));
499
		lbs_deb_hex(LBS_DEB_CMD, "EEPROM", (char *)&pbuf->value,
500 501
			le16_to_cpu(resp->params.rdeeprom.bytecount));
	}
502
	lbs_deb_leave(LBS_DEB_CMD);
503 504 505
	return 0;
}

506
static int lbs_ret_get_log(lbs_private * priv,
507 508
			    struct cmd_ds_command *resp)
{
509
	struct cmd_ds_802_11_get_log *logmessage = &resp->params.glog;
510
	lbs_adapter *adapter = priv->adapter;
511

512
	lbs_deb_enter(LBS_DEB_CMD);
513

514 515
	/* Stored little-endian */
	memcpy(&adapter->logmsg, logmessage, sizeof(struct cmd_ds_802_11_get_log));
516

517
	lbs_deb_leave(LBS_DEB_CMD);
518 519 520
	return 0;
}

521
static int lbs_ret_802_11_enable_rsn(lbs_private * priv,
522 523 524
                                          struct cmd_ds_command *resp)
{
	struct cmd_ds_802_11_enable_rsn *enable_rsn = &resp->params.enbrsn;
525
	lbs_adapter *adapter = priv->adapter;
526 527 528 529
	u32 * pdata_buf = adapter->cur_cmd->pdata_buf;

	lbs_deb_enter(LBS_DEB_CMD);

530
	if (enable_rsn->action == cpu_to_le16(CMD_ACT_GET)) {
531 532 533 534
		if (pdata_buf)
			*pdata_buf = (u32) le16_to_cpu(enable_rsn->enable);
	}

535
	lbs_deb_leave(LBS_DEB_CMD);
536 537 538
	return 0;
}

539 540
static inline int handle_cmd_response(u16 respcmd,
				      struct cmd_ds_command *resp,
541
				      lbs_private *priv)
542 543 544
{
	int ret = 0;
	unsigned long flags;
545
	lbs_adapter *adapter = priv->adapter;
546

547 548
	lbs_deb_enter(LBS_DEB_HOST);

549
	switch (respcmd) {
550 551 552
	case CMD_RET(CMD_MAC_REG_ACCESS):
	case CMD_RET(CMD_BBP_REG_ACCESS):
	case CMD_RET(CMD_RF_REG_ACCESS):
553
		ret = lbs_ret_reg_access(priv, respcmd, resp);
554 555
		break;

556
	case CMD_RET(CMD_GET_HW_SPEC):
557
		ret = lbs_ret_get_hw_spec(priv, resp);
558 559
		break;

560
	case CMD_RET(CMD_802_11_SCAN):
561
		ret = lbs_ret_80211_scan(priv, resp);
562 563
		break;

564
	case CMD_RET(CMD_802_11_GET_LOG):
565
		ret = lbs_ret_get_log(priv, resp);
566 567
		break;

568
	case CMD_RET_802_11_ASSOCIATE:
569 570
	case CMD_RET(CMD_802_11_ASSOCIATE):
	case CMD_RET(CMD_802_11_REASSOCIATE):
571
		ret = lbs_ret_80211_associate(priv, resp);
572 573
		break;

574 575
	case CMD_RET(CMD_802_11_DISASSOCIATE):
	case CMD_RET(CMD_802_11_DEAUTHENTICATE):
576
		ret = lbs_ret_80211_disassociate(priv, resp);
577 578
		break;

579 580
	case CMD_RET(CMD_802_11_AD_HOC_START):
	case CMD_RET(CMD_802_11_AD_HOC_JOIN):
581
		ret = lbs_ret_80211_ad_hoc_start(priv, resp);
582 583
		break;

584
	case CMD_RET(CMD_802_11_GET_STAT):
585
		ret = lbs_ret_802_11_stat(priv, resp);
586 587
		break;

588
	case CMD_RET(CMD_802_11_SNMP_MIB):
589
		ret = lbs_ret_802_11_snmp_mib(priv, resp);
590 591
		break;

592
	case CMD_RET(CMD_802_11_RF_TX_POWER):
593
		ret = lbs_ret_802_11_rf_tx_power(priv, resp);
594 595
		break;

596 597
	case CMD_RET(CMD_802_11_SET_AFC):
	case CMD_RET(CMD_802_11_GET_AFC):
598
		spin_lock_irqsave(&adapter->driver_lock, flags);
599
		memmove(adapter->cur_cmd->pdata_buf, &resp->params.afc,
600 601 602 603 604
			sizeof(struct cmd_ds_802_11_afc));
		spin_unlock_irqrestore(&adapter->driver_lock, flags);

		break;

605 606 607 608 609 610 611
	case CMD_RET(CMD_MAC_MULTICAST_ADR):
	case CMD_RET(CMD_MAC_CONTROL):
	case CMD_RET(CMD_802_11_SET_WEP):
	case CMD_RET(CMD_802_11_RESET):
	case CMD_RET(CMD_802_11_AUTHENTICATE):
	case CMD_RET(CMD_802_11_RADIO_CONTROL):
	case CMD_RET(CMD_802_11_BEACON_STOP):
612 613
		break;

614
	case CMD_RET(CMD_802_11_ENABLE_RSN):
615
		ret = lbs_ret_802_11_enable_rsn(priv, resp);
616 617
		break;

618
	case CMD_RET(CMD_802_11_DATA_RATE):
619
		ret = lbs_ret_802_11_data_rate(priv, resp);
620
		break;
621
	case CMD_RET(CMD_802_11_RATE_ADAPT_RATESET):
622
		ret = lbs_ret_802_11_rate_adapt_rateset(priv, resp);
623
		break;
624
	case CMD_RET(CMD_802_11_RF_CHANNEL):
625
		ret = lbs_ret_802_11_rf_channel(priv, resp);
626 627
		break;

628
	case CMD_RET(CMD_802_11_RSSI):
629
		ret = lbs_ret_802_11_rssi(priv, resp);
630 631
		break;

632
	case CMD_RET(CMD_802_11_MAC_ADDRESS):
633
		ret = lbs_ret_802_11_mac_address(priv, resp);
634 635
		break;

636
	case CMD_RET(CMD_802_11_AD_HOC_STOP):
637
		ret = lbs_ret_80211_ad_hoc_stop(priv, resp);
638 639
		break;

640
	case CMD_RET(CMD_802_11_KEY_MATERIAL):
641
		ret = lbs_ret_802_11_key_material(priv, resp);
642 643
		break;

644
	case CMD_RET(CMD_802_11_EEPROM_ACCESS):
645
		ret = lbs_ret_802_11_eeprom_access(priv, resp);
646 647
		break;

648
	case CMD_RET(CMD_802_11D_DOMAIN_INFO):
649
		ret = lbs_ret_802_11d_domain_info(priv, resp);
650 651
		break;

652
	case CMD_RET(CMD_802_11_SLEEP_PARAMS):
653
		ret = lbs_ret_802_11_sleep_params(priv, resp);
654
		break;
655
	case CMD_RET(CMD_802_11_INACTIVITY_TIMEOUT):
656 657 658 659 660 661
		spin_lock_irqsave(&adapter->driver_lock, flags);
		*((u16 *) adapter->cur_cmd->pdata_buf) =
		    le16_to_cpu(resp->params.inactivity_timeout.timeout);
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		break;

662
	case CMD_RET(CMD_802_11_TPC_CFG):
663
		spin_lock_irqsave(&adapter->driver_lock, flags);
664
		memmove(adapter->cur_cmd->pdata_buf, &resp->params.tpccfg,
665 666 667
			sizeof(struct cmd_ds_802_11_tpc_cfg));
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		break;
668
	case CMD_RET(CMD_802_11_LED_GPIO_CTRL):
669
		spin_lock_irqsave(&adapter->driver_lock, flags);
670
		memmove(adapter->cur_cmd->pdata_buf, &resp->params.ledgpio,
671 672 673
			sizeof(struct cmd_ds_802_11_led_ctrl));
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		break;
674
	case CMD_RET(CMD_802_11_PWR_CFG):
675
		spin_lock_irqsave(&adapter->driver_lock, flags);
676
		memmove(adapter->cur_cmd->pdata_buf, &resp->params.pwrcfg,
677 678 679 680 681
			sizeof(struct cmd_ds_802_11_pwr_cfg));
		spin_unlock_irqrestore(&adapter->driver_lock, flags);

		break;

682
	case CMD_RET(CMD_GET_TSF):
683 684 685 686 687
		spin_lock_irqsave(&adapter->driver_lock, flags);
		memcpy(priv->adapter->cur_cmd->pdata_buf,
		       &resp->params.gettsf.tsfvalue, sizeof(u64));
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		break;
688
	case CMD_RET(CMD_BT_ACCESS):
689 690 691 692 693 694
		spin_lock_irqsave(&adapter->driver_lock, flags);
		if (adapter->cur_cmd->pdata_buf)
			memcpy(adapter->cur_cmd->pdata_buf,
			       &resp->params.bt.addr1, 2 * ETH_ALEN);
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		break;
695
	case CMD_RET(CMD_FWT_ACCESS):
696 697
		spin_lock_irqsave(&adapter->driver_lock, flags);
		if (adapter->cur_cmd->pdata_buf)
698 699
			memcpy(adapter->cur_cmd->pdata_buf, &resp->params.fwt,
			       sizeof(resp->params.fwt));
700 701
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		break;
702
	case CMD_RET(CMD_MESH_ACCESS):
703
		if (adapter->cur_cmd->pdata_buf)
704
			memcpy(adapter->cur_cmd->pdata_buf, &resp->params.mesh,
705 706 707
			       sizeof(resp->params.mesh));
		break;
	default:
708
		lbs_deb_host("CMD_RESP: unknown cmd response 0x%04x\n",
709
			    resp->command);
710 711
		break;
	}
712
	lbs_deb_leave(LBS_DEB_HOST);
713 714 715
	return ret;
}

716
int lbs_process_rx_command(lbs_private * priv)
717 718 719
{
	u16 respcmd;
	struct cmd_ds_command *resp;
720
	lbs_adapter *adapter = priv->adapter;
721 722 723 724
	int ret = 0;
	ulong flags;
	u16 result;

725
	lbs_deb_enter(LBS_DEB_HOST);
726 727 728 729 730 731 732 733

	/* Now we got response from FW, cancel the command timer */
	del_timer(&adapter->command_timer);

	mutex_lock(&adapter->lock);
	spin_lock_irqsave(&adapter->driver_lock, flags);

	if (!adapter->cur_cmd) {
734
		lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
735 736 737 738 739 740 741 742 743
		ret = -1;
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		goto done;
	}
	resp = (struct cmd_ds_command *)(adapter->cur_cmd->bufvirtualaddr);

	respcmd = le16_to_cpu(resp->command);
	result = le16_to_cpu(resp->result);

744 745 746 747
	lbs_deb_host("CMD_RESP: response 0x%04x, size %d, jiffies %lu\n",
		respcmd, priv->upld_len, jiffies);
	lbs_deb_hex(LBS_DEB_HOST, "CMD_RESP", adapter->cur_cmd->bufvirtualaddr,
		    priv->upld_len);
748 749

	if (!(respcmd & 0x8000)) {
750
		lbs_deb_host("invalid response!\n");
751
		adapter->cur_cmd_retcode = -1;
752
		__lbs_cleanup_and_insert_cmd(priv, adapter->cur_cmd);
753 754 755 756 757 758 759 760
		adapter->nr_cmd_pending--;
		adapter->cur_cmd = NULL;
		spin_unlock_irqrestore(&adapter->driver_lock, flags);
		ret = -1;
		goto done;
	}

	/* Store the response code to cur_cmd_retcode. */
761
	adapter->cur_cmd_retcode = result;;
762

763
	if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
764 765
		struct cmd_ds_802_11_ps_mode *psmode = &resp->params.psmode;
		u16 action = le16_to_cpu(psmode->action);
766

767 768
		lbs_deb_host(
		       "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
769
		       result, action);
770 771

		if (result) {
772
			lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
773 774 775 776
				    result);
			/*
			 * We should not re-try enter-ps command in
			 * ad-hoc mode. It takes place in
777
			 * lbs_execute_next_command().
778 779
			 */
			if (adapter->mode == IW_MODE_ADHOC &&
780
			    action == CMD_SUBCMD_ENTER_PS)
781
				adapter->psmode = LBS802_11POWERMODECAM;
782
		} else if (action == CMD_SUBCMD_ENTER_PS) {
783 784 785
			adapter->needtowakeup = 0;
			adapter->psstate = PS_STATE_AWAKE;

786
			lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
787
			if (adapter->connect_status != LBS_CONNECTED) {
788 789 790 791
				/*
				 * When Deauth Event received before Enter_PS command
				 * response, We need to wake up the firmware.
				 */
792
				lbs_deb_host(
793
				       "disconnected, invoking lbs_ps_wakeup\n");
794 795

				spin_unlock_irqrestore(&adapter->driver_lock, flags);
796
				mutex_unlock(&adapter->lock);
797
				lbs_ps_wakeup(priv, 0);
798 799 800
				mutex_lock(&adapter->lock);
				spin_lock_irqsave(&adapter->driver_lock, flags);
			}
801
		} else if (action == CMD_SUBCMD_EXIT_PS) {
802 803
			adapter->needtowakeup = 0;
			adapter->psstate = PS_STATE_FULL_POWER;
804
			lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
805
		} else {
806
			lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
807 808
		}

809
		__lbs_cleanup_and_insert_cmd(priv, adapter->cur_cmd);
810 811 812 813 814 815 816 817 818 819
		adapter->nr_cmd_pending--;
		adapter->cur_cmd = NULL;
		spin_unlock_irqrestore(&adapter->driver_lock, flags);

		ret = 0;
		goto done;
	}

	if (adapter->cur_cmd->cmdflags & CMD_F_HOSTCMD) {
		/* Copy the response back to response buffer */
820 821
		memcpy(adapter->cur_cmd->pdata_buf, resp,
		       le16_to_cpu(resp->size));
822 823 824 825 826
		adapter->cur_cmd->cmdflags &= ~CMD_F_HOSTCMD;
	}

	/* If the command is not successful, cleanup and return failure */
	if ((result != 0 || !(respcmd & 0x8000))) {
827 828
		lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
		       result, respcmd);
829 830 831 832
		/*
		 * Handling errors here
		 */
		switch (respcmd) {
833 834
		case CMD_RET(CMD_GET_HW_SPEC):
		case CMD_RET(CMD_802_11_RESET):
835
			lbs_deb_host("CMD_RESP: reset failed\n");
836 837 838 839
			break;

		}

840
		__lbs_cleanup_and_insert_cmd(priv, adapter->cur_cmd);
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
		adapter->nr_cmd_pending--;
		adapter->cur_cmd = NULL;
		spin_unlock_irqrestore(&adapter->driver_lock, flags);

		ret = -1;
		goto done;
	}

	spin_unlock_irqrestore(&adapter->driver_lock, flags);

	ret = handle_cmd_response(respcmd, resp, priv);

	spin_lock_irqsave(&adapter->driver_lock, flags);
	if (adapter->cur_cmd) {
		/* Clean up and Put current command back to cmdfreeq */
856
		__lbs_cleanup_and_insert_cmd(priv, adapter->cur_cmd);
857 858 859 860 861 862 863 864
		adapter->nr_cmd_pending--;
		WARN_ON(adapter->nr_cmd_pending > 128);
		adapter->cur_cmd = NULL;
	}
	spin_unlock_irqrestore(&adapter->driver_lock, flags);

done:
	mutex_unlock(&adapter->lock);
865
	lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
866 867 868
	return ret;
}

869
int lbs_process_event(lbs_private * priv)
870 871
{
	int ret = 0;
872
	lbs_adapter *adapter = priv->adapter;
873 874
	u32 eventcause;

875 876
	lbs_deb_enter(LBS_DEB_CMD);

877 878 879 880
	spin_lock_irq(&adapter->driver_lock);
	eventcause = adapter->eventcause;
	spin_unlock_irq(&adapter->driver_lock);

881
	lbs_deb_cmd("event cause 0x%x\n", eventcause);
882 883 884

	switch (eventcause >> SBI_EVENT_CAUSE_SHIFT) {
	case MACREG_INT_CODE_LINK_SENSED:
885
		lbs_deb_cmd("EVENT: MACREG_INT_CODE_LINK_SENSED\n");
886 887 888
		break;

	case MACREG_INT_CODE_DEAUTHENTICATED:
889
		lbs_deb_cmd("EVENT: deauthenticated\n");
890
		lbs_mac_event_disconnected(priv);
891 892 893
		break;

	case MACREG_INT_CODE_DISASSOCIATED:
894
		lbs_deb_cmd("EVENT: disassociated\n");
895
		lbs_mac_event_disconnected(priv);
896 897 898
		break;

	case MACREG_INT_CODE_LINK_LOSE_NO_SCAN:
899
		lbs_deb_cmd("EVENT: link lost\n");
900
		lbs_mac_event_disconnected(priv);
901 902 903
		break;

	case MACREG_INT_CODE_PS_SLEEP:
904
		lbs_deb_cmd("EVENT: sleep\n");
905 906 907

		/* handle unexpected PS SLEEP event */
		if (adapter->psstate == PS_STATE_FULL_POWER) {
908
			lbs_deb_cmd(
909
			       "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
910 911 912 913
			break;
		}
		adapter->psstate = PS_STATE_PRE_SLEEP;

914
		lbs_ps_confirm_sleep(priv, (u16) adapter->psmode);
915 916 917 918

		break;

	case MACREG_INT_CODE_PS_AWAKE:
919
		lbs_deb_cmd("EVENT: awake\n");
920 921 922

		/* handle unexpected PS AWAKE event */
		if (adapter->psstate == PS_STATE_FULL_POWER) {
923
			lbs_deb_cmd(
924 925 926 927 928 929 930 931 932 933 934
			       "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
			break;
		}

		adapter->psstate = PS_STATE_AWAKE;

		if (adapter->needtowakeup) {
			/*
			 * wait for the command processing to finish
			 * before resuming sending
			 * adapter->needtowakeup will be set to FALSE
935
			 * in lbs_ps_wakeup()
936
			 */
937
			lbs_deb_cmd("waking up ...\n");
938
			lbs_ps_wakeup(priv, 0);
939 940 941 942
		}
		break;

	case MACREG_INT_CODE_MIC_ERR_UNICAST:
943
		lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
944 945 946 947
		handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_UNICAST);
		break;

	case MACREG_INT_CODE_MIC_ERR_MULTICAST:
948
		lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
949 950 951 952 953 954 955
		handle_mic_failureevent(priv, MACREG_INT_CODE_MIC_ERR_MULTICAST);
		break;
	case MACREG_INT_CODE_MIB_CHANGED:
	case MACREG_INT_CODE_INIT_DONE:
		break;

	case MACREG_INT_CODE_ADHOC_BCN_LOST:
956
		lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
957 958 959
		break;

	case MACREG_INT_CODE_RSSI_LOW:
960
		lbs_pr_alert("EVENT: rssi low\n");
961 962
		break;
	case MACREG_INT_CODE_SNR_LOW:
963
		lbs_pr_alert("EVENT: snr low\n");
964 965
		break;
	case MACREG_INT_CODE_MAX_FAIL:
966
		lbs_pr_alert("EVENT: max fail\n");
967 968
		break;
	case MACREG_INT_CODE_RSSI_HIGH:
969
		lbs_pr_alert("EVENT: rssi high\n");
970 971
		break;
	case MACREG_INT_CODE_SNR_HIGH:
972
		lbs_pr_alert("EVENT: snr high\n");
973 974
		break;

975
	case MACREG_INT_CODE_MESH_AUTO_STARTED:
976 977 978 979 980
		/* Ignore spurious autostart events if autostart is disabled */
		if (!priv->mesh_autostart_enabled) {
			lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
			break;
		}
981
		lbs_pr_info("EVENT: MESH_AUTO_STARTED\n");
982
		adapter->connect_status = LBS_CONNECTED;
983
		if (priv->mesh_open == 1) {
984 985
			netif_wake_queue(priv->mesh_dev);
			netif_carrier_on(priv->mesh_dev);
986
		}
987
		adapter->mode = IW_MODE_ADHOC;
988
		schedule_work(&priv->sync_channel);
989 990
		break;

991
	default:
992
		lbs_pr_alert("EVENT: unknown event id 0x%04x\n",
993 994 995 996 997 998 999
		       eventcause >> SBI_EVENT_CAUSE_SHIFT);
		break;
	}

	spin_lock_irq(&adapter->driver_lock);
	adapter->eventcause = 0;
	spin_unlock_irq(&adapter->driver_lock);
1000

1001
	lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
1002 1003
	return ret;
}