cmdresp.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. netdev_info(priv->dev,
  73. "Received CMD_RESP with invalid sequence %d (expected %d)\n",
  74. le16_to_cpu(resp->seqnum),
  75. le16_to_cpu(priv->cur_cmd->cmdbuf->seqnum));
  76. spin_unlock_irqrestore(&priv->driver_lock, flags);
  77. ret = -1;
  78. goto done;
  79. }
  80. if (respcmd != CMD_RET(curcmd) &&
  81. respcmd != CMD_RET_802_11_ASSOCIATE && curcmd != CMD_802_11_ASSOCIATE) {
  82. netdev_info(priv->dev, "Invalid CMD_RESP %x to command %x!\n",
  83. respcmd, curcmd);
  84. spin_unlock_irqrestore(&priv->driver_lock, flags);
  85. ret = -1;
  86. goto done;
  87. }
  88. if (resp->result == cpu_to_le16(0x0004)) {
  89. /* 0x0004 means -EAGAIN. Drop the response, let it time out
  90. and be resubmitted */
  91. netdev_info(priv->dev,
  92. "Firmware returns DEFER to command %x. Will let it time out...\n",
  93. le16_to_cpu(resp->command));
  94. spin_unlock_irqrestore(&priv->driver_lock, flags);
  95. ret = -1;
  96. goto done;
  97. }
  98. /* Now we got response from FW, cancel the command timer */
  99. del_timer(&priv->command_timer);
  100. priv->cmd_timed_out = 0;
  101. if (respcmd == CMD_RET(CMD_802_11_PS_MODE)) {
  102. struct cmd_ds_802_11_ps_mode *psmode = (void *) &resp[1];
  103. u16 action = le16_to_cpu(psmode->action);
  104. lbs_deb_host(
  105. "CMD_RESP: PS_MODE cmd reply result 0x%x, action 0x%x\n",
  106. result, action);
  107. if (result) {
  108. lbs_deb_host("CMD_RESP: PS command failed with 0x%x\n",
  109. result);
  110. /*
  111. * We should not re-try enter-ps command in
  112. * ad-hoc mode. It takes place in
  113. * lbs_execute_next_command().
  114. */
  115. if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR &&
  116. action == PS_MODE_ACTION_ENTER_PS)
  117. priv->psmode = LBS802_11POWERMODECAM;
  118. } else if (action == PS_MODE_ACTION_ENTER_PS) {
  119. priv->needtowakeup = 0;
  120. priv->psstate = PS_STATE_AWAKE;
  121. lbs_deb_host("CMD_RESP: ENTER_PS command response\n");
  122. if (priv->connect_status != LBS_CONNECTED) {
  123. /*
  124. * When Deauth Event received before Enter_PS command
  125. * response, We need to wake up the firmware.
  126. */
  127. lbs_deb_host(
  128. "disconnected, invoking lbs_ps_wakeup\n");
  129. spin_unlock_irqrestore(&priv->driver_lock, flags);
  130. mutex_unlock(&priv->lock);
  131. lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS,
  132. false);
  133. mutex_lock(&priv->lock);
  134. spin_lock_irqsave(&priv->driver_lock, flags);
  135. }
  136. } else if (action == PS_MODE_ACTION_EXIT_PS) {
  137. priv->needtowakeup = 0;
  138. priv->psstate = PS_STATE_FULL_POWER;
  139. lbs_deb_host("CMD_RESP: EXIT_PS command response\n");
  140. } else {
  141. lbs_deb_host("CMD_RESP: PS action 0x%X\n", action);
  142. }
  143. lbs_complete_command(priv, priv->cur_cmd, result);
  144. spin_unlock_irqrestore(&priv->driver_lock, flags);
  145. ret = 0;
  146. goto done;
  147. }
  148. /* If the command is not successful, cleanup and return failure */
  149. if ((result != 0 || !(respcmd & 0x8000))) {
  150. lbs_deb_host("CMD_RESP: error 0x%04x in command reply 0x%04x\n",
  151. result, respcmd);
  152. /*
  153. * Handling errors here
  154. */
  155. switch (respcmd) {
  156. case CMD_RET(CMD_GET_HW_SPEC):
  157. case CMD_RET(CMD_802_11_RESET):
  158. lbs_deb_host("CMD_RESP: reset failed\n");
  159. break;
  160. }
  161. lbs_complete_command(priv, priv->cur_cmd, result);
  162. spin_unlock_irqrestore(&priv->driver_lock, flags);
  163. ret = -1;
  164. goto done;
  165. }
  166. spin_unlock_irqrestore(&priv->driver_lock, flags);
  167. if (priv->cur_cmd && priv->cur_cmd->callback) {
  168. ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
  169. resp);
  170. }
  171. spin_lock_irqsave(&priv->driver_lock, flags);
  172. if (priv->cur_cmd) {
  173. /* Clean up and Put current command back to cmdfreeq */
  174. lbs_complete_command(priv, priv->cur_cmd, result);
  175. }
  176. spin_unlock_irqrestore(&priv->driver_lock, flags);
  177. done:
  178. mutex_unlock(&priv->lock);
  179. lbs_deb_leave_args(LBS_DEB_HOST, "ret %d", ret);
  180. return ret;
  181. }
  182. int lbs_process_event(struct lbs_private *priv, u32 event)
  183. {
  184. int ret = 0;
  185. struct cmd_header cmd;
  186. lbs_deb_enter(LBS_DEB_CMD);
  187. switch (event) {
  188. case MACREG_INT_CODE_LINK_SENSED:
  189. lbs_deb_cmd("EVENT: link sensed\n");
  190. break;
  191. case MACREG_INT_CODE_DEAUTHENTICATED:
  192. lbs_deb_cmd("EVENT: deauthenticated\n");
  193. lbs_mac_event_disconnected(priv);
  194. break;
  195. case MACREG_INT_CODE_DISASSOCIATED:
  196. lbs_deb_cmd("EVENT: disassociated\n");
  197. lbs_mac_event_disconnected(priv);
  198. break;
  199. case MACREG_INT_CODE_LINK_LOST_NO_SCAN:
  200. lbs_deb_cmd("EVENT: link lost\n");
  201. lbs_mac_event_disconnected(priv);
  202. break;
  203. case MACREG_INT_CODE_PS_SLEEP:
  204. lbs_deb_cmd("EVENT: ps sleep\n");
  205. /* handle unexpected PS SLEEP event */
  206. if (priv->psstate == PS_STATE_FULL_POWER) {
  207. lbs_deb_cmd(
  208. "EVENT: in FULL POWER mode, ignoreing PS_SLEEP\n");
  209. break;
  210. }
  211. priv->psstate = PS_STATE_PRE_SLEEP;
  212. lbs_ps_confirm_sleep(priv);
  213. break;
  214. case MACREG_INT_CODE_HOST_AWAKE:
  215. lbs_deb_cmd("EVENT: host awake\n");
  216. if (priv->reset_deep_sleep_wakeup)
  217. priv->reset_deep_sleep_wakeup(priv);
  218. priv->is_deep_sleep = 0;
  219. lbs_cmd_async(priv, CMD_802_11_WAKEUP_CONFIRM, &cmd,
  220. sizeof(cmd));
  221. priv->is_host_sleep_activated = 0;
  222. wake_up_interruptible(&priv->host_sleep_q);
  223. break;
  224. case MACREG_INT_CODE_DEEP_SLEEP_AWAKE:
  225. if (priv->reset_deep_sleep_wakeup)
  226. priv->reset_deep_sleep_wakeup(priv);
  227. lbs_deb_cmd("EVENT: ds awake\n");
  228. priv->is_deep_sleep = 0;
  229. priv->wakeup_dev_required = 0;
  230. wake_up_interruptible(&priv->ds_awake_q);
  231. break;
  232. case MACREG_INT_CODE_PS_AWAKE:
  233. lbs_deb_cmd("EVENT: ps awake\n");
  234. /* handle unexpected PS AWAKE event */
  235. if (priv->psstate == PS_STATE_FULL_POWER) {
  236. lbs_deb_cmd(
  237. "EVENT: In FULL POWER mode - ignore PS AWAKE\n");
  238. break;
  239. }
  240. priv->psstate = PS_STATE_AWAKE;
  241. if (priv->needtowakeup) {
  242. /*
  243. * wait for the command processing to finish
  244. * before resuming sending
  245. * priv->needtowakeup will be set to FALSE
  246. * in lbs_ps_wakeup()
  247. */
  248. lbs_deb_cmd("waking up ...\n");
  249. lbs_set_ps_mode(priv, PS_MODE_ACTION_EXIT_PS, false);
  250. }
  251. break;
  252. case MACREG_INT_CODE_MIC_ERR_UNICAST:
  253. lbs_deb_cmd("EVENT: UNICAST MIC ERROR\n");
  254. lbs_send_mic_failureevent(priv, event);
  255. break;
  256. case MACREG_INT_CODE_MIC_ERR_MULTICAST:
  257. lbs_deb_cmd("EVENT: MULTICAST MIC ERROR\n");
  258. lbs_send_mic_failureevent(priv, event);
  259. break;
  260. case MACREG_INT_CODE_MIB_CHANGED:
  261. lbs_deb_cmd("EVENT: MIB CHANGED\n");
  262. break;
  263. case MACREG_INT_CODE_INIT_DONE:
  264. lbs_deb_cmd("EVENT: INIT DONE\n");
  265. break;
  266. case MACREG_INT_CODE_ADHOC_BCN_LOST:
  267. lbs_deb_cmd("EVENT: ADHOC beacon lost\n");
  268. break;
  269. case MACREG_INT_CODE_RSSI_LOW:
  270. netdev_alert(priv->dev, "EVENT: rssi low\n");
  271. break;
  272. case MACREG_INT_CODE_SNR_LOW:
  273. netdev_alert(priv->dev, "EVENT: snr low\n");
  274. break;
  275. case MACREG_INT_CODE_MAX_FAIL:
  276. netdev_alert(priv->dev, "EVENT: max fail\n");
  277. break;
  278. case MACREG_INT_CODE_RSSI_HIGH:
  279. netdev_alert(priv->dev, "EVENT: rssi high\n");
  280. break;
  281. case MACREG_INT_CODE_SNR_HIGH:
  282. netdev_alert(priv->dev, "EVENT: snr high\n");
  283. break;
  284. case MACREG_INT_CODE_MESH_AUTO_STARTED:
  285. /* Ignore spurious autostart events */
  286. netdev_info(priv->dev, "EVENT: MESH_AUTO_STARTED (ignoring)\n");
  287. break;
  288. default:
  289. netdev_alert(priv->dev, "EVENT: unknown event id %d\n", event);
  290. break;
  291. }
  292. lbs_deb_leave_args(LBS_DEB_CMD, "ret %d", ret);
  293. return ret;
  294. }