cmdresp.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 <asm/unaligned.h>
  9. #include <net/cfg80211.h>
  10. #include "cfg.h"
  11. #include "cmd.h"
  12. /**
  13. * @brief This function handles disconnect event. it
  14. * reports disconnect to upper layer, clean tx/rx packets,
  15. * reset link state etc.
  16. *
  17. * @param priv A pointer to struct lbs_private structure
  18. * @return n/a
  19. */
  20. void lbs_mac_event_disconnected(struct lbs_private *priv)
  21. {
  22. if (priv->connect_status != LBS_CONNECTED)
  23. return;
  24. lbs_deb_enter(LBS_DEB_ASSOC);
  25. /*
  26. * Cisco AP sends EAP failure and de-auth in less than 0.5 ms.
  27. * It causes problem in the Supplicant
  28. */
  29. msleep_interruptible(1000);
  30. if (priv->wdev->iftype == NL80211_IFTYPE_STATION)
  31. lbs_send_disconnect_notification(priv);
  32. /* report disconnect to upper layer */
  33. netif_stop_queue(priv->dev);
  34. netif_carrier_off(priv->dev);
  35. /* Free Tx and Rx packets */
  36. kfree_skb(priv->currenttxskb);
  37. priv->currenttxskb = NULL;
  38. priv->tx_pending_len = 0;
  39. priv->connect_status = LBS_DISCONNECTED;
  40. if (priv->psstate != PS_STATE_FULL_POWER) {
  41. /* make firmware to exit PS mode */
  42. lbs_deb_cmd("disconnected, so exit PS mode\n");
  43. lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, false);
  44. }
  45. lbs_deb_leave(LBS_DEB_ASSOC);
  46. }
  47. int lbs_process_command_response(struct lbs_private *priv, u8 *data, u32 len)
  48. {
  49. uint16_t respcmd, curcmd;
  50. struct cmd_header *resp;
  51. int ret = 0;
  52. unsigned long flags;
  53. uint16_t result;
  54. lbs_deb_enter(LBS_DEB_HOST);
  55. mutex_lock(&priv->lock);
  56. spin_lock_irqsave(&priv->driver_lock, flags);
  57. if (!priv->cur_cmd) {
  58. lbs_deb_host("CMD_RESP: cur_cmd is NULL\n");
  59. ret = -1;
  60. spin_unlock_irqrestore(&priv->driver_lock, flags);
  61. goto done;
  62. }
  63. resp = (void *)data;
  64. curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
  65. respcmd = le16_to_cpu(resp->command);
  66. result = le16_to_cpu(resp->result);
  67. lbs_deb_cmd("CMD_RESP: response 0x%04x, seq %d, size %d\n",
  68. respcmd, le16_to_cpu(resp->seqnum), len);
  69. lbs_deb_hex(LBS_DEB_CMD, "CMD_RESP", (void *) resp, len);
  70. if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
  71. lbs_pr_info("Received CMD_RESP with invalid sequence %d (expected %d)\n",
  72. le16_to_cpu(resp->seqnum), le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
  73. spin_unlock_irqrestore(&priv->driver_lock, flags);
  74. ret = -1;
  75. goto done;
  76. }
  77. if (respcmd != CMD_RET(curcmd) &&
  78. respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
  79. lbs_pr_info("Invalid CMD_RESP %x to command %x!\n", respcmd, curcmd);
  80. spin_unlock_irqrestore(&priv->driver_lock, flags);
  81. ret = -1;
  82. goto done;
  83. }
  84. if (resp->result == cpu_to_le16(0x0004)) {
  85. /* 0x0004 means -EAGAIN. Drop the response, let it time out
  86. and be resubmitted */
  87. lbs_pr_info("Firmware returns DEFER to command %x. Will let it time out...\n",
  88. le16_to_cpu(resp->command));
  89. spin_unlock_irqrestore(&priv->driver_lock, flags);
  90. ret = -1;
  91. goto done;
  92. }
  93. /* Now we got response from FW, cancel the command timer */
  94. del_timer(&priv->command_timer);
  95. priv->cmd_timed_out = 0;
  96. if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
  97. struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
  98. u16 action = le16_to_cpu(psmode->action);
  99. lbs_deb_host(
  100. "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
  101. result, action);
  102. if (result) {
  103. lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
  104. result);
  105. /*
  106. * We should not re-try enter-ps command in
  107. * ad-hoc mode. It takes place in
  108. * lbs_execute_next_command().
  109. */
  110. if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR &&
  111. action == PS_MODE_ACTION_ENTER_PS)
  112. priv->psmode = LBS802_11POWERMODECAM;
  113. } else if (action == PS_MODE_ACTION_ENTER_PS) {
  114. priv->needtowakeup = 0;
  115. priv->psstate = PS_STATE_AWAKE;
  116. lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
  117. if (priv->connect_status != LBS_CONNECTED) {
  118. /*
  119. * When Deauth Event received before Enter_PS command
  120. * response, We need to wake up the firmware.
  121. */
  122. lbs_deb_host(
  123. "disconnected, invoking lbs_ps_wakeup\n");
  124. spin_unlock_irqrestore(&priv->driver_lock, flags);
  125. mutex_unlock(&priv->lock);
  126. lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS,
  127. false);
  128. mutex_lock(&priv->lock);
  129. spin_lock_irqsave(&priv->driver_lock, flags);
  130. }
  131. } else if (action == PS_MODE_ACTION_EXIT_PS) {
  132. priv->needtowakeup = 0;
  133. priv->psstate = PS_STATE_FULL_POWER;
  134. lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
  135. } else {
  136. lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
  137. }
  138. lbs_complete_command(priv, priv->cur_cmd, result);
  139. spin_unlock_irqrestore(&priv->driver_lock, flags);
  140. ret = 0;
  141. goto done;
  142. }
  143. /* If the command is not successful, cleanup and return failure */
  144. if ((result != 0 || !(respcmd & 0x8000))) {
  145. lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
  146. result, respcmd);
  147. /*
  148. * Handling errors here
  149. */
  150. switch (respcmd) {
  151. case CMD_RET(CMD_GET_HW_SPEC):
  152. case CMD_RET(CMD_802_11_RESET):
  153. lbs_deb_host("CMD_RESP: reset failed\n");
  154. break;
  155. }
  156. lbs_complete_command(priv, priv->cur_cmd, result);
  157. spin_unlock_irqrestore(&priv->driver_lock, flags);
  158. ret = -1;
  159. goto done;
  160. }
  161. spin_unlock_irqrestore(&priv->driver_lock, flags);
  162. if (priv->cur_cmd && priv->cur_cmd->callback) {
  163. ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
  164. resp);
  165. }
  166. spin_lock_irqsave(&priv->driver_lock, flags);
  167. if (priv->cur_cmd) {
  168. /* Clean up and Put current command back to cmdfreeq */
  169. lbs_complete_command(priv, priv->cur_cmd, result);
  170. }
  171. spin_unlock_irqrestore(&priv->driver_lock, flags);
  172. done:
  173. mutex_unlock(&priv->lock);
  174. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  175. return ret;
  176. }
  177. int lbs_process_event(struct lbs_private *priv, u32 event)
  178. {
  179. int ret = 0;
  180. struct cmd_header cmd;
  181. lbs_deb_enter(LBS_DEB_CMD);
  182. switch (event) {
  183. case MACREG_INT_CODE_LINK_SENSED:
  184. lbs_deb_cmd("EVENT: link sensed\n");
  185. break;
  186. case MACREG_INT_CODE_DEAUTHENTICATED:
  187. lbs_deb_cmd("EVENT: deauthenticated\n");
  188. lbs_mac_event_disconnected(priv);
  189. break;
  190. case MACREG_INT_CODE_DISASSOCIATED:
  191. lbs_deb_cmd("EVENT: disassociated\n");
  192. lbs_mac_event_disconnected(priv);
  193. break;
  194. case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
  195. lbs_deb_cmd("EVENT: link lost\n");
  196. lbs_mac_event_disconnected(priv);
  197. break;
  198. case MACREG_INT_CODE_PS_SLEEP:
  199. lbs_deb_cmd("EVENT: ps sleep\n");
  200. /* handle unexpected PS SLEEP event */
  201. if (priv->psstate == PS_STATE_FULL_POWER) {
  202. lbs_deb_cmd(
  203. "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
  204. break;
  205. }
  206. priv->psstate = PS_STATE_PRE_SLEEP;
  207. lbs_ps_confirm_sleep(priv);
  208. break;
  209. case MACREG_INT_CODE_HOST_AWAKE:
  210. lbs_deb_cmd("EVENT: host awake\n");
  211. if (priv->reset_deep_sleep_wakeup)
  212. priv->reset_deep_sleep_wakeup(priv);
  213. priv->is_deep_sleep = 0;
  214. lbs_cmd_async(priv, CMD_802_11_WAKEUP_CONFIRM, &cmd,
  215. sizeof(cmd));
  216. priv->is_host_sleep_activated = 0;
  217. wake_up_interruptible(&priv->host_sleep_q);
  218. break;
  219. case MACREG_INT_CODE_DEEP_SLEEP_AWAKE:
  220. if (priv->reset_deep_sleep_wakeup)
  221. priv->reset_deep_sleep_wakeup(priv);
  222. lbs_deb_cmd("EVENT: ds awake\n");
  223. priv->is_deep_sleep = 0;
  224. priv->wakeup_dev_required = 0;
  225. wake_up_interruptible(&priv->ds_awake_q);
  226. break;
  227. case MACREG_INT_CODE_PS_AWAKE:
  228. lbs_deb_cmd("EVENT: ps awake\n");
  229. /* handle unexpected PS AWAKE event */
  230. if (priv->psstate == PS_STATE_FULL_POWER) {
  231. lbs_deb_cmd(
  232. "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
  233. break;
  234. }
  235. priv->psstate = PS_STATE_AWAKE;
  236. if (priv->needtowakeup) {
  237. /*
  238. * wait for the command processing to finish
  239. * before resuming sending
  240. * priv->needtowakeup will be set to FALSE
  241. * in lbs_ps_wakeup()
  242. */
  243. lbs_deb_cmd("waking up ...\n");
  244. lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, false);
  245. }
  246. break;
  247. case MACREG_INT_CODE_MIC_ERR_UNICAST:
  248. lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
  249. lbs_send_mic_failureevent(priv, event);
  250. break;
  251. case MACREG_INT_CODE_MIC_ERR_MULTICAST:
  252. lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
  253. lbs_send_mic_failureevent(priv, event);
  254. break;
  255. case MACREG_INT_CODE_MIB_CHANGED:
  256. lbs_deb_cmd("EVENT: MIB CHANGED\n");
  257. break;
  258. case MACREG_INT_CODE_INIT_DONE:
  259. lbs_deb_cmd("EVENT: INIT DONE\n");
  260. break;
  261. case MACREG_INT_CODE_ADHOC_BCN_LOST:
  262. lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
  263. break;
  264. case MACREG_INT_CODE_RSSI_LOW:
  265. lbs_pr_alert("EVENT: rssi low\n");
  266. break;
  267. case MACREG_INT_CODE_SNR_LOW:
  268. lbs_pr_alert("EVENT: snr low\n");
  269. break;
  270. case MACREG_INT_CODE_MAX_FAIL:
  271. lbs_pr_alert("EVENT: max fail\n");
  272. break;
  273. case MACREG_INT_CODE_RSSI_HIGH:
  274. lbs_pr_alert("EVENT: rssi high\n");
  275. break;
  276. case MACREG_INT_CODE_SNR_HIGH:
  277. lbs_pr_alert("EVENT: snr high\n");
  278. break;
  279. case MACREG_INT_CODE_MESH_AUTO_STARTED:
  280. /* Ignore spurious autostart events */
  281. lbs_pr_info("EVENT: MESH_AUTO_STARTED (ignoring)\n");
  282. break;
  283. default:
  284. lbs_pr_alert("EVENT: unknown event id %d\n", event);
  285. break;
  286. }
  287. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  288. return ret;
  289. }