cmdresp.c 10 KB

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