cmdresp.c 9.0 KB

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