wow.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "ath9k.h"
  17. static const struct wiphy_wowlan_support ath9k_wowlan_support = {
  18. .flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT,
  19. .n_patterns = MAX_NUM_USER_PATTERN,
  20. .pattern_min_len = 1,
  21. .pattern_max_len = MAX_PATTERN_SIZE,
  22. };
  23. static u8 ath9k_wow_map_triggers(struct ath_softc *sc,
  24. struct cfg80211_wowlan *wowlan)
  25. {
  26. u8 wow_triggers = 0;
  27. if (wowlan->disconnect)
  28. wow_triggers |= AH_WOW_LINK_CHANGE |
  29. AH_WOW_BEACON_MISS;
  30. if (wowlan->magic_pkt)
  31. wow_triggers |= AH_WOW_MAGIC_PATTERN_EN;
  32. if (wowlan->n_patterns)
  33. wow_triggers |= AH_WOW_USER_PATTERN_EN;
  34. return wow_triggers;
  35. }
  36. static int ath9k_wow_add_disassoc_deauth_pattern(struct ath_softc *sc)
  37. {
  38. struct ath_hw *ah = sc->sc_ah;
  39. struct ath_common *common = ath9k_hw_common(ah);
  40. int pattern_count = 0;
  41. int ret, i, byte_cnt = 0;
  42. u8 dis_deauth_pattern[MAX_PATTERN_SIZE];
  43. u8 dis_deauth_mask[MAX_PATTERN_SIZE];
  44. memset(dis_deauth_pattern, 0, MAX_PATTERN_SIZE);
  45. memset(dis_deauth_mask, 0, MAX_PATTERN_SIZE);
  46. /*
  47. * Create Dissassociate / Deauthenticate packet filter
  48. *
  49. * 2 bytes 2 byte 6 bytes 6 bytes 6 bytes
  50. * +--------------+----------+---------+--------+--------+----
  51. * + Frame Control+ Duration + DA + SA + BSSID +
  52. * +--------------+----------+---------+--------+--------+----
  53. *
  54. * The above is the management frame format for disassociate/
  55. * deauthenticate pattern, from this we need to match the first byte
  56. * of 'Frame Control' and DA, SA, and BSSID fields
  57. * (skipping 2nd byte of FC and Duration feild.
  58. *
  59. * Disassociate pattern
  60. * --------------------
  61. * Frame control = 00 00 1010
  62. * DA, SA, BSSID = x:x:x:x:x:x
  63. * Pattern will be A0000000 | x:x:x:x:x:x | x:x:x:x:x:x
  64. * | x:x:x:x:x:x -- 22 bytes
  65. *
  66. * Deauthenticate pattern
  67. * ----------------------
  68. * Frame control = 00 00 1100
  69. * DA, SA, BSSID = x:x:x:x:x:x
  70. * Pattern will be C0000000 | x:x:x:x:x:x | x:x:x:x:x:x
  71. * | x:x:x:x:x:x -- 22 bytes
  72. */
  73. /* Fill out the mask with all FF's */
  74. for (i = 0; i < MAX_PATTERN_MASK_SIZE; i++)
  75. dis_deauth_mask[i] = 0xff;
  76. /* copy the first byte of frame control field */
  77. dis_deauth_pattern[byte_cnt] = 0xa0;
  78. byte_cnt++;
  79. /* skip 2nd byte of frame control and Duration field */
  80. byte_cnt += 3;
  81. /*
  82. * need not match the destination mac address, it can be a broadcast
  83. * mac address or an unicast to this station
  84. */
  85. byte_cnt += 6;
  86. /* copy the source mac address */
  87. memcpy((dis_deauth_pattern + byte_cnt), common->curbssid, ETH_ALEN);
  88. byte_cnt += 6;
  89. /* copy the bssid, its same as the source mac address */
  90. memcpy((dis_deauth_pattern + byte_cnt), common->curbssid, ETH_ALEN);
  91. /* Create Disassociate pattern mask */
  92. dis_deauth_mask[0] = 0xfe;
  93. dis_deauth_mask[1] = 0x03;
  94. dis_deauth_mask[2] = 0xc0;
  95. ret = ath9k_hw_wow_apply_pattern(ah, dis_deauth_pattern, dis_deauth_mask,
  96. pattern_count, byte_cnt);
  97. if (ret)
  98. goto exit;
  99. pattern_count++;
  100. /*
  101. * for de-authenticate pattern, only the first byte of the frame
  102. * control field gets changed from 0xA0 to 0xC0
  103. */
  104. dis_deauth_pattern[0] = 0xC0;
  105. ret = ath9k_hw_wow_apply_pattern(ah, dis_deauth_pattern, dis_deauth_mask,
  106. pattern_count, byte_cnt);
  107. exit:
  108. return ret;
  109. }
  110. static int ath9k_wow_add_pattern(struct ath_softc *sc,
  111. struct cfg80211_wowlan *wowlan)
  112. {
  113. struct ath_hw *ah = sc->sc_ah;
  114. struct cfg80211_pkt_pattern *patterns = wowlan->patterns;
  115. u8 wow_pattern[MAX_PATTERN_SIZE];
  116. u8 wow_mask[MAX_PATTERN_SIZE];
  117. int mask_len, ret = 0;
  118. s8 i = 0;
  119. for (i = 0; i < wowlan->n_patterns; i++) {
  120. mask_len = DIV_ROUND_UP(patterns[i].pattern_len, 8);
  121. memset(wow_pattern, 0, MAX_PATTERN_SIZE);
  122. memset(wow_mask, 0, MAX_PATTERN_SIZE);
  123. memcpy(wow_pattern, patterns[i].pattern, patterns[i].pattern_len);
  124. memcpy(wow_mask, patterns[i].mask, mask_len);
  125. ret = ath9k_hw_wow_apply_pattern(ah,
  126. wow_pattern,
  127. wow_mask,
  128. i + 2,
  129. patterns[i].pattern_len);
  130. if (ret)
  131. break;
  132. }
  133. return ret;
  134. }
  135. int ath9k_suspend(struct ieee80211_hw *hw,
  136. struct cfg80211_wowlan *wowlan)
  137. {
  138. struct ath_softc *sc = hw->priv;
  139. struct ath_hw *ah = sc->sc_ah;
  140. struct ath_common *common = ath9k_hw_common(ah);
  141. u8 triggers;
  142. int ret = 0;
  143. ath9k_deinit_channel_context(sc);
  144. mutex_lock(&sc->mutex);
  145. if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
  146. ath_err(common, "Device not present\n");
  147. ret = -ENODEV;
  148. goto fail_wow;
  149. }
  150. if (WARN_ON(!wowlan)) {
  151. ath_err(common, "None of the WoW triggers enabled\n");
  152. ret = -EINVAL;
  153. goto fail_wow;
  154. }
  155. if (sc->cur_chan->nvifs > 1) {
  156. ath_dbg(common, WOW, "WoW for multivif is not yet supported\n");
  157. ret = 1;
  158. goto fail_wow;
  159. }
  160. if (ath9k_is_chanctx_enabled()) {
  161. if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags)) {
  162. ath_dbg(common, WOW,
  163. "Multi-channel WOW is not supported\n");
  164. ret = 1;
  165. goto fail_wow;
  166. }
  167. }
  168. if (!test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
  169. ath_dbg(common, WOW, "None of the STA vifs are associated\n");
  170. ret = 1;
  171. goto fail_wow;
  172. }
  173. triggers = ath9k_wow_map_triggers(sc, wowlan);
  174. if (!triggers) {
  175. ath_dbg(common, WOW, "No valid WoW triggers\n");
  176. ret = 1;
  177. goto fail_wow;
  178. }
  179. ath_cancel_work(sc);
  180. ath_stop_ani(sc);
  181. ath9k_ps_wakeup(sc);
  182. ath9k_stop_btcoex(sc);
  183. /*
  184. * Enable wake up on recieving disassoc/deauth
  185. * frame by default.
  186. */
  187. ret = ath9k_wow_add_disassoc_deauth_pattern(sc);
  188. if (ret) {
  189. ath_err(common,
  190. "Unable to add disassoc/deauth pattern: %d\n", ret);
  191. goto fail_wow;
  192. }
  193. if (triggers & AH_WOW_USER_PATTERN_EN) {
  194. ret = ath9k_wow_add_pattern(sc, wowlan);
  195. if (ret) {
  196. ath_err(common,
  197. "Unable to add user pattern: %d\n", ret);
  198. goto fail_wow;
  199. }
  200. }
  201. spin_lock_bh(&sc->sc_pcu_lock);
  202. /*
  203. * To avoid false wake, we enable beacon miss interrupt only
  204. * when we go to sleep. We save the current interrupt mask
  205. * so we can restore it after the system wakes up
  206. */
  207. sc->wow_intr_before_sleep = ah->imask;
  208. ah->imask &= ~ATH9K_INT_GLOBAL;
  209. ath9k_hw_disable_interrupts(ah);
  210. ah->imask = ATH9K_INT_BMISS | ATH9K_INT_GLOBAL;
  211. ath9k_hw_set_interrupts(ah);
  212. ath9k_hw_enable_interrupts(ah);
  213. spin_unlock_bh(&sc->sc_pcu_lock);
  214. /*
  215. * we can now sync irq and kill any running tasklets, since we already
  216. * disabled interrupts and not holding a spin lock
  217. */
  218. synchronize_irq(sc->irq);
  219. tasklet_kill(&sc->intr_tq);
  220. ath9k_hw_wow_enable(ah, triggers);
  221. ath9k_ps_restore(sc);
  222. ath_dbg(common, WOW, "Suspend with WoW triggers: 0x%x\n", triggers);
  223. set_bit(ATH_OP_WOW_ENABLED, &common->op_flags);
  224. fail_wow:
  225. mutex_unlock(&sc->mutex);
  226. return ret;
  227. }
  228. int ath9k_resume(struct ieee80211_hw *hw)
  229. {
  230. struct ath_softc *sc = hw->priv;
  231. struct ath_hw *ah = sc->sc_ah;
  232. struct ath_common *common = ath9k_hw_common(ah);
  233. u8 status;
  234. mutex_lock(&sc->mutex);
  235. ath9k_ps_wakeup(sc);
  236. spin_lock_bh(&sc->sc_pcu_lock);
  237. ath9k_hw_disable_interrupts(ah);
  238. ah->imask = sc->wow_intr_before_sleep;
  239. ath9k_hw_set_interrupts(ah);
  240. ath9k_hw_enable_interrupts(ah);
  241. spin_unlock_bh(&sc->sc_pcu_lock);
  242. status = ath9k_hw_wow_wakeup(ah);
  243. ath_dbg(common, WOW, "Resume with WoW status: 0x%x\n", status);
  244. ath_restart_work(sc);
  245. ath9k_start_btcoex(sc);
  246. clear_bit(ATH_OP_WOW_ENABLED, &common->op_flags);
  247. ath9k_ps_restore(sc);
  248. mutex_unlock(&sc->mutex);
  249. return 0;
  250. }
  251. void ath9k_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  252. {
  253. struct ath_softc *sc = hw->priv;
  254. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  255. mutex_lock(&sc->mutex);
  256. device_set_wakeup_enable(sc->dev, enabled);
  257. mutex_unlock(&sc->mutex);
  258. ath_dbg(common, WOW, "WoW wakeup source is %s\n",
  259. (enabled) ? "enabled" : "disabled");
  260. }
  261. void ath9k_init_wow(struct ieee80211_hw *hw)
  262. {
  263. struct ath_softc *sc = hw->priv;
  264. if ((sc->driver_data & ATH9K_PCI_WOW) || sc->force_wow) {
  265. hw->wiphy->wowlan = &ath9k_wowlan_support;
  266. device_init_wakeup(sc->dev, 1);
  267. }
  268. }
  269. void ath9k_deinit_wow(struct ieee80211_hw *hw)
  270. {
  271. struct ath_softc *sc = hw->priv;
  272. if ((sc->driver_data & ATH9K_PCI_WOW) || sc->force_wow)
  273. device_init_wakeup(sc->dev, 0);
  274. }