wow.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 void ath9k_wow_map_triggers(struct ath_softc *sc,
  24. struct cfg80211_wowlan *wowlan,
  25. u32 *wow_triggers)
  26. {
  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. sc->wow_enabled = *wow_triggers;
  35. }
  36. static void 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 i, byte_cnt;
  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. /* Create Disassociate Pattern first */
  74. byte_cnt = 0;
  75. /* Fill out the mask with all FF's */
  76. for (i = 0; i < MAX_PATTERN_MASK_SIZE; i++)
  77. dis_deauth_mask[i] = 0xff;
  78. /* copy the first byte of frame control field */
  79. dis_deauth_pattern[byte_cnt] = 0xa0;
  80. byte_cnt++;
  81. /* skip 2nd byte of frame control and Duration field */
  82. byte_cnt += 3;
  83. /*
  84. * need not match the destination mac address, it can be a broadcast
  85. * mac address or an unicast to this station
  86. */
  87. byte_cnt += 6;
  88. /* copy the source mac address */
  89. memcpy((dis_deauth_pattern + byte_cnt), common->curbssid, ETH_ALEN);
  90. byte_cnt += 6;
  91. /* copy the bssid, its same as the source mac address */
  92. memcpy((dis_deauth_pattern + byte_cnt), common->curbssid, ETH_ALEN);
  93. /* Create Disassociate pattern mask */
  94. dis_deauth_mask[0] = 0xfe;
  95. dis_deauth_mask[1] = 0x03;
  96. dis_deauth_mask[2] = 0xc0;
  97. ath_dbg(common, WOW, "Adding disassoc/deauth patterns for WoW\n");
  98. ath9k_hw_wow_apply_pattern(ah, dis_deauth_pattern, dis_deauth_mask,
  99. pattern_count, byte_cnt);
  100. pattern_count++;
  101. /*
  102. * for de-authenticate pattern, only the first byte of the frame
  103. * control field gets changed from 0xA0 to 0xC0
  104. */
  105. dis_deauth_pattern[0] = 0xC0;
  106. ath9k_hw_wow_apply_pattern(ah, dis_deauth_pattern, dis_deauth_mask,
  107. pattern_count, byte_cnt);
  108. }
  109. static void ath9k_wow_add_pattern(struct ath_softc *sc,
  110. struct cfg80211_wowlan *wowlan)
  111. {
  112. struct ath_hw *ah = sc->sc_ah;
  113. struct ath9k_wow_pattern *wow_pattern = NULL;
  114. struct cfg80211_pkt_pattern *patterns = wowlan->patterns;
  115. int mask_len;
  116. s8 i = 0;
  117. if (!wowlan->n_patterns)
  118. return;
  119. /*
  120. * Add the new user configured patterns
  121. */
  122. for (i = 0; i < wowlan->n_patterns; i++) {
  123. wow_pattern = kzalloc(sizeof(*wow_pattern), GFP_KERNEL);
  124. if (!wow_pattern)
  125. return;
  126. /*
  127. * TODO: convert the generic user space pattern to
  128. * appropriate chip specific/802.11 pattern.
  129. */
  130. mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
  131. memset(wow_pattern->pattern_bytes, 0, MAX_PATTERN_SIZE);
  132. memset(wow_pattern->mask_bytes, 0, MAX_PATTERN_SIZE);
  133. memcpy(wow_pattern->pattern_bytes, patterns[i].pattern,
  134. patterns[i].pattern_len);
  135. memcpy(wow_pattern->mask_bytes, patterns[i].mask, mask_len);
  136. wow_pattern->pattern_len = patterns[i].pattern_len;
  137. /*
  138. * just need to take care of deauth and disssoc pattern,
  139. * make sure we don't overwrite them.
  140. */
  141. ath9k_hw_wow_apply_pattern(ah, wow_pattern->pattern_bytes,
  142. wow_pattern->mask_bytes,
  143. i + 2,
  144. wow_pattern->pattern_len);
  145. kfree(wow_pattern);
  146. }
  147. }
  148. int ath9k_suspend(struct ieee80211_hw *hw,
  149. struct cfg80211_wowlan *wowlan)
  150. {
  151. struct ath_softc *sc = hw->priv;
  152. struct ath_hw *ah = sc->sc_ah;
  153. struct ath_common *common = ath9k_hw_common(ah);
  154. u32 wow_triggers_enabled = 0;
  155. int ret = 0;
  156. ath9k_deinit_channel_context(sc);
  157. mutex_lock(&sc->mutex);
  158. ath_cancel_work(sc);
  159. ath_stop_ani(sc);
  160. if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
  161. ath_dbg(common, ANY, "Device not present\n");
  162. ret = -EINVAL;
  163. goto fail_wow;
  164. }
  165. if (WARN_ON(!wowlan)) {
  166. ath_dbg(common, WOW, "None of the WoW triggers enabled\n");
  167. ret = -EINVAL;
  168. goto fail_wow;
  169. }
  170. if (!device_can_wakeup(sc->dev)) {
  171. ath_dbg(common, WOW, "device_can_wakeup failed, WoW is not enabled\n");
  172. ret = 1;
  173. goto fail_wow;
  174. }
  175. /*
  176. * none of the sta vifs are associated
  177. * and we are not currently handling multivif
  178. * cases, for instance we have to seperately
  179. * configure 'keep alive frame' for each
  180. * STA.
  181. */
  182. if (!test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
  183. ath_dbg(common, WOW, "None of the STA vifs are associated\n");
  184. ret = 1;
  185. goto fail_wow;
  186. }
  187. if (sc->cur_chan->nvifs > 1) {
  188. ath_dbg(common, WOW, "WoW for multivif is not yet supported\n");
  189. ret = 1;
  190. goto fail_wow;
  191. }
  192. ath9k_wow_map_triggers(sc, wowlan, &wow_triggers_enabled);
  193. ath_dbg(common, WOW, "WoW triggers enabled 0x%x\n",
  194. wow_triggers_enabled);
  195. ath9k_ps_wakeup(sc);
  196. ath9k_stop_btcoex(sc);
  197. /*
  198. * Enable wake up on recieving disassoc/deauth
  199. * frame by default.
  200. */
  201. ath9k_wow_add_disassoc_deauth_pattern(sc);
  202. if (wow_triggers_enabled & AH_WOW_USER_PATTERN_EN)
  203. ath9k_wow_add_pattern(sc, wowlan);
  204. spin_lock_bh(&sc->sc_pcu_lock);
  205. /*
  206. * To avoid false wake, we enable beacon miss interrupt only
  207. * when we go to sleep. We save the current interrupt mask
  208. * so we can restore it after the system wakes up
  209. */
  210. sc->wow_intr_before_sleep = ah->imask;
  211. ah->imask &= ~ATH9K_INT_GLOBAL;
  212. ath9k_hw_disable_interrupts(ah);
  213. ah->imask = ATH9K_INT_BMISS | ATH9K_INT_GLOBAL;
  214. ath9k_hw_set_interrupts(ah);
  215. ath9k_hw_enable_interrupts(ah);
  216. spin_unlock_bh(&sc->sc_pcu_lock);
  217. /*
  218. * we can now sync irq and kill any running tasklets, since we already
  219. * disabled interrupts and not holding a spin lock
  220. */
  221. synchronize_irq(sc->irq);
  222. tasklet_kill(&sc->intr_tq);
  223. ath9k_hw_wow_enable(ah, wow_triggers_enabled);
  224. ath9k_ps_restore(sc);
  225. ath_dbg(common, ANY, "WoW enabled in ath9k\n");
  226. atomic_inc(&sc->wow_sleep_proc_intr);
  227. fail_wow:
  228. mutex_unlock(&sc->mutex);
  229. return ret;
  230. }
  231. int ath9k_resume(struct ieee80211_hw *hw)
  232. {
  233. struct ath_softc *sc = hw->priv;
  234. struct ath_hw *ah = sc->sc_ah;
  235. struct ath_common *common = ath9k_hw_common(ah);
  236. u32 wow_status;
  237. mutex_lock(&sc->mutex);
  238. ath9k_ps_wakeup(sc);
  239. spin_lock_bh(&sc->sc_pcu_lock);
  240. ath9k_hw_disable_interrupts(ah);
  241. ah->imask = sc->wow_intr_before_sleep;
  242. ath9k_hw_set_interrupts(ah);
  243. ath9k_hw_enable_interrupts(ah);
  244. spin_unlock_bh(&sc->sc_pcu_lock);
  245. wow_status = ath9k_hw_wow_wakeup(ah);
  246. if (atomic_read(&sc->wow_got_bmiss_intr) == 0) {
  247. /*
  248. * some devices may not pick beacon miss
  249. * as the reason they woke up so we add
  250. * that here for that shortcoming.
  251. */
  252. wow_status |= AH_WOW_BEACON_MISS;
  253. atomic_dec(&sc->wow_got_bmiss_intr);
  254. ath_dbg(common, ANY, "Beacon miss interrupt picked up during WoW sleep\n");
  255. }
  256. atomic_dec(&sc->wow_sleep_proc_intr);
  257. if (wow_status) {
  258. ath_dbg(common, ANY, "Waking up due to WoW triggers %s with WoW status = %x\n",
  259. ath9k_hw_wow_event_to_string(wow_status), wow_status);
  260. }
  261. ath_restart_work(sc);
  262. ath9k_start_btcoex(sc);
  263. ath9k_ps_restore(sc);
  264. mutex_unlock(&sc->mutex);
  265. return 0;
  266. }
  267. void ath9k_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  268. {
  269. struct ath_softc *sc = hw->priv;
  270. mutex_lock(&sc->mutex);
  271. device_init_wakeup(sc->dev, 1);
  272. device_set_wakeup_enable(sc->dev, enabled);
  273. mutex_unlock(&sc->mutex);
  274. }
  275. void ath9k_init_wow(struct ieee80211_hw *hw)
  276. {
  277. struct ath_softc *sc = hw->priv;
  278. if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_WOW_DEVICE_CAPABLE) &&
  279. (sc->driver_data & ATH9K_PCI_WOW) &&
  280. device_can_wakeup(sc->dev))
  281. hw->wiphy->wowlan = &ath9k_wowlan_support;
  282. atomic_set(&sc->wow_sleep_proc_intr, -1);
  283. atomic_set(&sc->wow_got_bmiss_intr, -1);
  284. }