cmdresp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /**
  2. * This file contains the handling of command
  3. * responses as well as events generated by firmware.
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/delay.h>
  7. #include <linux/sched.h>
  8. #include <linux/if_arp.h>
  9. #include <linux/netdevice.h>
  10. #include <asm/unaligned.h>
  11. #include <net/iw_handler.h>
  12. #include "host.h"
  13. #include "decl.h"
  14. #include "cmd.h"
  15. #include "defs.h"
  16. #include "dev.h"
  17. #include "assoc.h"
  18. #include "wext.h"
  19. #include "cmd.h"
  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. *
  25. * @param priv A pointer to struct lbs_private structure
  26. * @return n/a
  27. */
  28. void lbs_mac_event_disconnected(struct lbs_private *priv)
  29. {
  30. if (priv->connect_status != LBS_CONNECTED)
  31. return;
  32. lbs_deb_enter(LBS_DEB_ASSOC);
  33. /*
  34. * Cisco AP sends EAP failure and de-auth in less than 0.5 ms.
  35. * It causes problem in the Supplicant
  36. */
  37. msleep_interruptible(1000);
  38. lbs_send_disconnect_notification(priv);
  39. /* report disconnect to upper layer */
  40. netif_stop_queue(priv->dev);
  41. netif_carrier_off(priv->dev);
  42. /* Free Tx and Rx packets */
  43. kfree_skb(priv->currenttxskb);
  44. priv->currenttxskb = NULL;
  45. priv->tx_pending_len = 0;
  46. /* reset SNR/NF/RSSI values */
  47. memset(priv->SNR, 0x00, sizeof(priv->SNR));
  48. memset(priv->NF, 0x00, sizeof(priv->NF));
  49. memset(priv->RSSI, 0x00, sizeof(priv->RSSI));
  50. memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
  51. memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
  52. priv->nextSNRNF = 0;
  53. priv->numSNRNF = 0;
  54. priv->connect_status = LBS_DISCONNECTED;
  55. /* Clear out associated SSID and BSSID since connection is
  56. * no longer valid.
  57. */
  58. memset(&priv->curbssparams.bssid, 0, ETH_ALEN);
  59. memset(&priv->curbssparams.ssid, 0, IEEE80211_MAX_SSID_LEN);
  60. priv->curbssparams.ssid_len = 0;
  61. if (priv->psstate != PS_STATE_FULL_POWER) {
  62. /* make firmware to exit PS mode */
  63. lbs_deb_cmd("disconnected, so exit PS mode\n");
  64. lbs_ps_wakeup(priv, 0);
  65. }
  66. lbs_deb_leave(LBS_DEB_ASSOC);
  67. }
  68. static int lbs_ret_reg_access(struct lbs_private *priv,
  69. u16 type, struct cmd_ds_command *resp)
  70. {
  71. int ret = 0;
  72. lbs_deb_enter(LBS_DEB_CMD);
  73. switch (type) {
  74. case CMD_RET(CMD_MAC_REG_ACCESS):
  75. {
  76. struct cmd_ds_mac_reg_access *reg = &resp->params.macreg;
  77. priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
  78. priv->offsetvalue.value = le32_to_cpu(reg->value);
  79. break;
  80. }
  81. case CMD_RET(CMD_BBP_REG_ACCESS):
  82. {
  83. struct cmd_ds_bbp_reg_access *reg = &resp->params.bbpreg;
  84. priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
  85. priv->offsetvalue.value = reg->value;
  86. break;
  87. }
  88. case CMD_RET(CMD_RF_REG_ACCESS):
  89. {
  90. struct cmd_ds_rf_reg_access *reg = &resp->params.rfreg;
  91. priv->offsetvalue.offset = (u32)le16_to_cpu(reg->offset);
  92. priv->offsetvalue.value = reg->value;
  93. break;
  94. }
  95. default:
  96. ret = -1;
  97. }
  98. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  99. return ret;
  100. }
  101. static inline int handle_cmd_response(struct lbs_private *priv,
  102. struct cmd_header *cmd_response)
  103. {
  104. struct cmd_ds_command *resp = (struct cmd_ds_command *) cmd_response;
  105. int ret = 0;
  106. unsigned long flags;
  107. uint16_t respcmd = le16_to_cpu(resp->command);
  108. lbs_deb_enter(LBS_DEB_HOST);
  109. switch (respcmd) {
  110. case CMD_RET(CMD_MAC_REG_ACCESS):
  111. case CMD_RET(CMD_BBP_REG_ACCESS):
  112. case CMD_RET(CMD_RF_REG_ACCESS):
  113. ret = lbs_ret_reg_access(priv, respcmd, resp);
  114. break;
  115. case CMD_RET(CMD_802_11_SET_AFC):
  116. 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. break;
  122. case CMD_RET(CMD_802_11_BEACON_STOP):
  123. break;
  124. case CMD_RET(CMD_802_11_RSSI):
  125. ret = lbs_ret_802_11_rssi(priv, resp);
  126. break;
  127. case CMD_RET(CMD_802_11_TPC_CFG):
  128. spin_lock_irqsave(&priv->driver_lock, flags);
  129. memmove((void *)priv->cur_cmd->callback_arg, &resp->params.tpccfg,
  130. sizeof(struct cmd_ds_802_11_tpc_cfg));
  131. spin_unlock_irqrestore(&priv->driver_lock, flags);
  132. break;
  133. case CMD_RET(CMD_BT_ACCESS):
  134. spin_lock_irqsave(&priv->driver_lock, flags);
  135. if (priv->cur_cmd->callback_arg)
  136. memcpy((void *)priv->cur_cmd->callback_arg,
  137. &resp->params.bt.addr1, 2 * ETH_ALEN);
  138. spin_unlock_irqrestore(&priv->driver_lock, flags);
  139. break;
  140. case CMD_RET(CMD_FWT_ACCESS):
  141. spin_lock_irqsave(&priv->driver_lock, flags);
  142. if (priv->cur_cmd->callback_arg)
  143. memcpy((void *)priv->cur_cmd->callback_arg, &resp->params.fwt,
  144. sizeof(resp->params.fwt));
  145. spin_unlock_irqrestore(&priv->driver_lock, flags);
  146. break;
  147. case CMD_RET(CMD_802_11_BEACON_CTRL):
  148. ret = lbs_ret_802_11_bcn_ctrl(priv, resp);
  149. break;
  150. default:
  151. lbs_pr_err("CMD_RESP: unknown cmd response 0x%04x\n",
  152. le16_to_cpu(resp->command));
  153. break;
  154. }
  155. lbs_deb_leave(LBS_DEB_HOST);
  156. return ret;
  157. }
  158. int lbs_process_command_response(struct lbs_private *priv, u8 *data, u32 len)
  159. {
  160. uint16_t respcmd, curcmd;
  161. struct cmd_header *resp;
  162. int ret = 0;
  163. unsigned long flags;
  164. uint16_t result;
  165. lbs_deb_enter(LBS_DEB_HOST);
  166. mutex_lock(&priv->lock);
  167. spin_lock_irqsave(&priv->driver_lock, flags);
  168. if (!priv->cur_cmd) {
  169. lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
  170. ret = -1;
  171. spin_unlock_irqrestore(&priv->driver_lock, flags);
  172. goto done;
  173. }
  174. resp = (void *)data;
  175. curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
  176. respcmd = le16_to_cpu(resp->command);
  177. result = le16_to_cpu(resp->result);
  178. lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d\n",
  179. respcmd, le16_to_cpu(resp->seqnum), len);
  180. lbs_deb_hex(LBS_DEB_CMD, "CMD_RESP", (void *) resp, len);
  181. if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
  182. lbs_pr_info("Received CMD_RESP with invalid sequence %d (expected %d)\n",
  183. le16_to_cpu(resp->seqnum), le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
  184. spin_unlock_irqrestore(&priv->driver_lock, flags);
  185. ret = -1;
  186. goto done;
  187. }
  188. if (respcmd != CMD_RET(curcmd) &&
  189. respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
  190. lbs_pr_info("Invalid CMD_RESP %x to command %x!\n", respcmd, curcmd);
  191. spin_unlock_irqrestore(&priv->driver_lock, flags);
  192. ret = -1;
  193. goto done;
  194. }
  195. if (resp->result == cpu_to_le16(0x0004)) {
  196. /* 0x0004 means -EAGAIN. Drop the response, let it time out
  197. and be resubmitted */
  198. lbs_pr_info("Firmware returns DEFER to command %x. Will let it time out...\n",
  199. le16_to_cpu(resp->command));
  200. spin_unlock_irqrestore(&priv->driver_lock, flags);
  201. ret = -1;
  202. goto done;
  203. }
  204. /* Now we got response from FW, cancel the command timer */
  205. del_timer(&priv->command_timer);
  206. priv->cmd_timed_out = 0;
  207. /* Store the response code to cur_cmd_retcode. */
  208. priv->cur_cmd_retcode = result;
  209. if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
  210. struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
  211. u16 action = le16_to_cpu(psmode->action);
  212. lbs_deb_host(
  213. "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
  214. result, action);
  215. if (result) {
  216. lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
  217. result);
  218. /*
  219. * We should not re-try enter-ps command in
  220. * ad-hoc mode. It takes place in
  221. * lbs_execute_next_command().
  222. */
  223. if (priv->mode == IW_MODE_ADHOC &&
  224. action == CMD_SUBCMD_ENTER_PS)
  225. priv->psmode = LBS802_11POWERMODECAM;
  226. } else if (action == CMD_SUBCMD_ENTER_PS) {
  227. priv->needtowakeup = 0;
  228. priv->psstate = PS_STATE_AWAKE;
  229. lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
  230. if (priv->connect_status != LBS_CONNECTED) {
  231. /*
  232. * When Deauth Event received before Enter_PS command
  233. * response, We need to wake up the firmware.
  234. */
  235. lbs_deb_host(
  236. "disconnected, invoking lbs_ps_wakeup\n");
  237. spin_unlock_irqrestore(&priv->driver_lock, flags);
  238. mutex_unlock(&priv->lock);
  239. lbs_ps_wakeup(priv, 0);
  240. mutex_lock(&priv->lock);
  241. spin_lock_irqsave(&priv->driver_lock, flags);
  242. }
  243. } else if (action == CMD_SUBCMD_EXIT_PS) {
  244. priv->needtowakeup = 0;
  245. priv->psstate = PS_STATE_FULL_POWER;
  246. lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
  247. } else {
  248. lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
  249. }
  250. lbs_complete_command(priv, priv->cur_cmd, result);
  251. spin_unlock_irqrestore(&priv->driver_lock, flags);
  252. ret = 0;
  253. goto done;
  254. }
  255. /* If the command is not successful, cleanup and return failure */
  256. if ((result != 0 || !(respcmd & 0x8000))) {
  257. lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
  258. result, respcmd);
  259. /*
  260. * Handling errors here
  261. */
  262. switch (respcmd) {
  263. case CMD_RET(CMD_GET_HW_SPEC):
  264. case CMD_RET(CMD_802_11_RESET):
  265. lbs_deb_host("CMD_RESP: reset failed\n");
  266. break;
  267. }
  268. lbs_complete_command(priv, priv->cur_cmd, result);
  269. spin_unlock_irqrestore(&priv->driver_lock, flags);
  270. ret = -1;
  271. goto done;
  272. }
  273. spin_unlock_irqrestore(&priv->driver_lock, flags);
  274. if (priv->cur_cmd && priv->cur_cmd->callback) {
  275. ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
  276. resp);
  277. } else
  278. ret = handle_cmd_response(priv, resp);
  279. spin_lock_irqsave(&priv->driver_lock, flags);
  280. if (priv->cur_cmd) {
  281. /* Clean up and Put current command back to cmdfreeq */
  282. lbs_complete_command(priv, priv->cur_cmd, result);
  283. }
  284. spin_unlock_irqrestore(&priv->driver_lock, flags);
  285. done:
  286. mutex_unlock(&priv->lock);
  287. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  288. return ret;
  289. }
  290. int lbs_process_event(struct lbs_private *priv, u32 event)
  291. {
  292. int ret = 0;
  293. struct cmd_header cmd;
  294. lbs_deb_enter(LBS_DEB_CMD);
  295. switch (event) {
  296. case MACREG_INT_CODE_LINK_SENSED:
  297. lbs_deb_cmd("EVENT: link sensed\n");
  298. break;
  299. case MACREG_INT_CODE_DEAUTHENTICATED:
  300. lbs_deb_cmd("EVENT: deauthenticated\n");
  301. lbs_mac_event_disconnected(priv);
  302. break;
  303. case MACREG_INT_CODE_DISASSOCIATED:
  304. lbs_deb_cmd("EVENT: disassociated\n");
  305. lbs_mac_event_disconnected(priv);
  306. break;
  307. case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
  308. lbs_deb_cmd("EVENT: link lost\n");
  309. lbs_mac_event_disconnected(priv);
  310. break;
  311. case MACREG_INT_CODE_PS_SLEEP:
  312. lbs_deb_cmd("EVENT: ps sleep\n");
  313. /* handle unexpected PS SLEEP event */
  314. if (priv->psstate == PS_STATE_FULL_POWER) {
  315. lbs_deb_cmd(
  316. "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
  317. break;
  318. }
  319. priv->psstate = PS_STATE_PRE_SLEEP;
  320. lbs_ps_confirm_sleep(priv);
  321. break;
  322. case MACREG_INT_CODE_HOST_AWAKE:
  323. lbs_deb_cmd("EVENT: host awake\n");
  324. if (priv->reset_deep_sleep_wakeup)
  325. priv->reset_deep_sleep_wakeup(priv);
  326. priv->is_deep_sleep = 0;
  327. lbs_cmd_async(priv, CMD_802_11_WAKEUP_CONFIRM, &cmd,
  328. sizeof(cmd));
  329. priv->is_host_sleep_activated = 0;
  330. wake_up_interruptible(&priv->host_sleep_q);
  331. break;
  332. case MACREG_INT_CODE_DEEP_SLEEP_AWAKE:
  333. if (priv->reset_deep_sleep_wakeup)
  334. priv->reset_deep_sleep_wakeup(priv);
  335. lbs_deb_cmd("EVENT: ds awake\n");
  336. priv->is_deep_sleep = 0;
  337. priv->wakeup_dev_required = 0;
  338. wake_up_interruptible(&priv->ds_awake_q);
  339. break;
  340. case MACREG_INT_CODE_PS_AWAKE:
  341. lbs_deb_cmd("EVENT: ps awake\n");
  342. /* handle unexpected PS AWAKE event */
  343. if (priv->psstate == PS_STATE_FULL_POWER) {
  344. lbs_deb_cmd(
  345. "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
  346. break;
  347. }
  348. priv->psstate = PS_STATE_AWAKE;
  349. if (priv->needtowakeup) {
  350. /*
  351. * wait for the command processing to finish
  352. * before resuming sending
  353. * priv->needtowakeup will be set to FALSE
  354. * in lbs_ps_wakeup()
  355. */
  356. lbs_deb_cmd("waking up ...\n");
  357. lbs_ps_wakeup(priv, 0);
  358. }
  359. break;
  360. case MACREG_INT_CODE_MIC_ERR_UNICAST:
  361. lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
  362. lbs_send_mic_failureevent(priv, event);
  363. break;
  364. case MACREG_INT_CODE_MIC_ERR_MULTICAST:
  365. lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
  366. lbs_send_mic_failureevent(priv, event);
  367. break;
  368. case MACREG_INT_CODE_MIB_CHANGED:
  369. lbs_deb_cmd("EVENT: MIB CHANGED\n");
  370. break;
  371. case MACREG_INT_CODE_INIT_DONE:
  372. lbs_deb_cmd("EVENT: INIT DONE\n");
  373. break;
  374. case MACREG_INT_CODE_ADHOC_BCN_LOST:
  375. lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
  376. break;
  377. case MACREG_INT_CODE_RSSI_LOW:
  378. lbs_pr_alert("EVENT: rssi low\n");
  379. break;
  380. case MACREG_INT_CODE_SNR_LOW:
  381. lbs_pr_alert("EVENT: snr low\n");
  382. break;
  383. case MACREG_INT_CODE_MAX_FAIL:
  384. lbs_pr_alert("EVENT: max fail\n");
  385. break;
  386. case MACREG_INT_CODE_RSSI_HIGH:
  387. lbs_pr_alert("EVENT: rssi high\n");
  388. break;
  389. case MACREG_INT_CODE_SNR_HIGH:
  390. lbs_pr_alert("EVENT: snr high\n");
  391. break;
  392. case MACREG_INT_CODE_MESH_AUTO_STARTED:
  393. /* Ignore spurious autostart events */
  394. lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
  395. break;
  396. default:
  397. lbs_pr_alert("EVENT: unknown event id %d\n", event);
  398. break;
  399. }
  400. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  401. return ret;
  402. }