iwl-hcmd.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * Contact Information:
  25. * Tomas Winkler <tomas.winkler@intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *****************************************************************************/
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/version.h>
  31. #include <net/mac80211.h>
  32. #include "iwl-4965.h" /* FIXME: remove */
  33. #include "iwl-debug.h"
  34. #include "iwl-eeprom.h"
  35. #include "iwl-core.h"
  36. #define IWL_CMD(x) case x : return #x
  37. const char *get_cmd_string(u8 cmd)
  38. {
  39. switch (cmd) {
  40. IWL_CMD(REPLY_ALIVE);
  41. IWL_CMD(REPLY_ERROR);
  42. IWL_CMD(REPLY_RXON);
  43. IWL_CMD(REPLY_RXON_ASSOC);
  44. IWL_CMD(REPLY_QOS_PARAM);
  45. IWL_CMD(REPLY_RXON_TIMING);
  46. IWL_CMD(REPLY_ADD_STA);
  47. IWL_CMD(REPLY_REMOVE_STA);
  48. IWL_CMD(REPLY_REMOVE_ALL_STA);
  49. IWL_CMD(REPLY_TX);
  50. IWL_CMD(REPLY_RATE_SCALE);
  51. IWL_CMD(REPLY_LEDS_CMD);
  52. IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
  53. IWL_CMD(RADAR_NOTIFICATION);
  54. IWL_CMD(REPLY_QUIET_CMD);
  55. IWL_CMD(REPLY_CHANNEL_SWITCH);
  56. IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
  57. IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
  58. IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
  59. IWL_CMD(POWER_TABLE_CMD);
  60. IWL_CMD(PM_SLEEP_NOTIFICATION);
  61. IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
  62. IWL_CMD(REPLY_SCAN_CMD);
  63. IWL_CMD(REPLY_SCAN_ABORT_CMD);
  64. IWL_CMD(SCAN_START_NOTIFICATION);
  65. IWL_CMD(SCAN_RESULTS_NOTIFICATION);
  66. IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
  67. IWL_CMD(BEACON_NOTIFICATION);
  68. IWL_CMD(REPLY_TX_BEACON);
  69. IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
  70. IWL_CMD(QUIET_NOTIFICATION);
  71. IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
  72. IWL_CMD(MEASURE_ABORT_NOTIFICATION);
  73. IWL_CMD(REPLY_BT_CONFIG);
  74. IWL_CMD(REPLY_STATISTICS_CMD);
  75. IWL_CMD(STATISTICS_NOTIFICATION);
  76. IWL_CMD(REPLY_CARD_STATE_CMD);
  77. IWL_CMD(CARD_STATE_NOTIFICATION);
  78. IWL_CMD(MISSED_BEACONS_NOTIFICATION);
  79. IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
  80. IWL_CMD(SENSITIVITY_CMD);
  81. IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
  82. IWL_CMD(REPLY_RX_PHY_CMD);
  83. IWL_CMD(REPLY_RX_MPDU_CMD);
  84. IWL_CMD(REPLY_RX);
  85. IWL_CMD(REPLY_COMPRESSED_BA);
  86. default:
  87. return "UNKNOWN";
  88. }
  89. }
  90. EXPORT_SYMBOL(get_cmd_string);
  91. #define HOST_COMPLETE_TIMEOUT (HZ / 2)
  92. static int iwl_send_cmd_async(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  93. {
  94. int ret;
  95. BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
  96. /* An asynchronous command can not expect an SKB to be set. */
  97. BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
  98. /* An asynchronous command MUST have a callback. */
  99. BUG_ON(!cmd->meta.u.callback);
  100. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  101. return -EBUSY;
  102. ret = priv->cfg->ops->utils->enqueue_hcmd(priv, cmd);
  103. if (ret < 0) {
  104. IWL_ERROR("Error sending %s: enqueue_hcmd failed: %d\n",
  105. get_cmd_string(cmd->id), ret);
  106. return ret;
  107. }
  108. return 0;
  109. }
  110. int iwl_send_cmd_sync(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  111. {
  112. int cmd_idx;
  113. int ret;
  114. static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
  115. BUG_ON(cmd->meta.flags & CMD_ASYNC);
  116. /* A synchronous command can not have a callback set. */
  117. BUG_ON(cmd->meta.u.callback != NULL);
  118. if (atomic_xchg(&entry, 1)) {
  119. IWL_ERROR("Error sending %s: Already sending a host command\n",
  120. get_cmd_string(cmd->id));
  121. return -EBUSY;
  122. }
  123. set_bit(STATUS_HCMD_ACTIVE, &priv->status);
  124. if (cmd->meta.flags & CMD_WANT_SKB)
  125. cmd->meta.source = &cmd->meta;
  126. cmd_idx = priv->cfg->ops->utils->enqueue_hcmd(priv, cmd);
  127. if (cmd_idx < 0) {
  128. ret = cmd_idx;
  129. IWL_ERROR("Error sending %s: enqueue_hcmd failed: %d\n",
  130. get_cmd_string(cmd->id), ret);
  131. goto out;
  132. }
  133. ret = wait_event_interruptible_timeout(priv->wait_command_queue,
  134. !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
  135. HOST_COMPLETE_TIMEOUT);
  136. if (!ret) {
  137. if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
  138. IWL_ERROR("Error sending %s: time out after %dms.\n",
  139. get_cmd_string(cmd->id),
  140. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  141. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  142. ret = -ETIMEDOUT;
  143. goto cancel;
  144. }
  145. }
  146. if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
  147. IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
  148. get_cmd_string(cmd->id));
  149. ret = -ECANCELED;
  150. goto fail;
  151. }
  152. if (test_bit(STATUS_FW_ERROR, &priv->status)) {
  153. IWL_DEBUG_INFO("Command %s failed: FW Error\n",
  154. get_cmd_string(cmd->id));
  155. ret = -EIO;
  156. goto fail;
  157. }
  158. if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
  159. IWL_ERROR("Error: Response NULL in '%s'\n",
  160. get_cmd_string(cmd->id));
  161. ret = -EIO;
  162. goto out;
  163. }
  164. ret = 0;
  165. goto out;
  166. cancel:
  167. if (cmd->meta.flags & CMD_WANT_SKB) {
  168. struct iwl_cmd *qcmd;
  169. /* Cancel the CMD_WANT_SKB flag for the cmd in the
  170. * TX cmd queue. Otherwise in case the cmd comes
  171. * in later, it will possibly set an invalid
  172. * address (cmd->meta.source). */
  173. qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
  174. qcmd->meta.flags &= ~CMD_WANT_SKB;
  175. }
  176. fail:
  177. if (cmd->meta.u.skb) {
  178. dev_kfree_skb_any(cmd->meta.u.skb);
  179. cmd->meta.u.skb = NULL;
  180. }
  181. out:
  182. atomic_set(&entry, 0);
  183. return ret;
  184. }
  185. EXPORT_SYMBOL(iwl_send_cmd_sync);
  186. int iwl_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  187. {
  188. if (cmd->meta.flags & CMD_ASYNC)
  189. return iwl_send_cmd_async(priv, cmd);
  190. return iwl_send_cmd_sync(priv, cmd);
  191. }
  192. EXPORT_SYMBOL(iwl_send_cmd);
  193. int iwl_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
  194. {
  195. struct iwl_host_cmd cmd = {
  196. .id = id,
  197. .len = len,
  198. .data = data,
  199. };
  200. return iwl_send_cmd_sync(priv, &cmd);
  201. }
  202. EXPORT_SYMBOL(iwl_send_cmd_pdu);
  203. int iwl_send_cmd_pdu_async(struct iwl_priv *priv,
  204. u8 id, u16 len, const void *data,
  205. int (*callback)(struct iwl_priv *priv,
  206. struct iwl_cmd *cmd,
  207. struct sk_buff *skb))
  208. {
  209. struct iwl_host_cmd cmd = {
  210. .id = id,
  211. .len = len,
  212. .data = data,
  213. };
  214. cmd.meta.flags |= CMD_ASYNC;
  215. cmd.meta.u.callback = callback;
  216. return iwl_send_cmd_async(priv, &cmd);
  217. }
  218. EXPORT_SYMBOL(iwl_send_cmd_pdu_async);