cmdresp.c 9.3 KB

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