wow.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. mutex_lock(&sc->mutex);
  157. ath_cancel_work(sc);
  158. ath_stop_ani(sc);
  159. if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
  160. ath_dbg(common, ANY, "Device not present\n");
  161. ret = -EINVAL;
  162. goto fail_wow;
  163. }
  164. if (WARN_ON(!wowlan)) {
  165. ath_dbg(common, WOW, "None of the WoW triggers enabled\n");
  166. ret = -EINVAL;
  167. goto fail_wow;
  168. }
  169. if (!device_can_wakeup(sc->dev)) {
  170. ath_dbg(common, WOW, "device_can_wakeup failed, WoW is not enabled\n");
  171. ret = 1;
  172. goto fail_wow;
  173. }
  174. /*
  175. * none of the sta vifs are associated
  176. * and we are not currently handling multivif
  177. * cases, for instance we have to seperately
  178. * configure 'keep alive frame' for each
  179. * STA.
  180. */
  181. if (!test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
  182. ath_dbg(common, WOW, "None of the STA vifs are associated\n");
  183. ret = 1;
  184. goto fail_wow;
  185. }
  186. if (sc->nvifs > 1) {
  187. ath_dbg(common, WOW, "WoW for multivif is not yet supported\n");
  188. ret = 1;
  189. goto fail_wow;
  190. }
  191. ath9k_wow_map_triggers(sc, wowlan, &wow_triggers_enabled);
  192. ath_dbg(common, WOW, "WoW triggers enabled 0x%x\n",
  193. wow_triggers_enabled);
  194. ath9k_ps_wakeup(sc);
  195. ath9k_stop_btcoex(sc);
  196. /*
  197. * Enable wake up on recieving disassoc/deauth
  198. * frame by default.
  199. */
  200. ath9k_wow_add_disassoc_deauth_pattern(sc);
  201. if (wow_triggers_enabled & AH_WOW_USER_PATTERN_EN)
  202. ath9k_wow_add_pattern(sc, wowlan);
  203. spin_lock_bh(&sc->sc_pcu_lock);
  204. /*
  205. * To avoid false wake, we enable beacon miss interrupt only
  206. * when we go to sleep. We save the current interrupt mask
  207. * so we can restore it after the system wakes up
  208. */
  209. sc->wow_intr_before_sleep = ah->imask;
  210. ah->imask &= ~ATH9K_INT_GLOBAL;
  211. ath9k_hw_disable_interrupts(ah);
  212. ah->imask = ATH9K_INT_BMISS | ATH9K_INT_GLOBAL;
  213. ath9k_hw_set_interrupts(ah);
  214. ath9k_hw_enable_interrupts(ah);
  215. spin_unlock_bh(&sc->sc_pcu_lock);
  216. /*
  217. * we can now sync irq and kill any running tasklets, since we already
  218. * disabled interrupts and not holding a spin lock
  219. */
  220. synchronize_irq(sc->irq);
  221. tasklet_kill(&sc->intr_tq);
  222. ath9k_hw_wow_enable(ah, wow_triggers_enabled);
  223. ath9k_ps_restore(sc);
  224. ath_dbg(common, ANY, "WoW enabled in ath9k\n");
  225. atomic_inc(&sc->wow_sleep_proc_intr);
  226. fail_wow:
  227. mutex_unlock(&sc->mutex);
  228. return ret;
  229. }
  230. int ath9k_resume(struct ieee80211_hw *hw)
  231. {
  232. struct ath_softc *sc = hw->priv;
  233. struct ath_hw *ah = sc->sc_ah;
  234. struct ath_common *common = ath9k_hw_common(ah);
  235. u32 wow_status;
  236. mutex_lock(&sc->mutex);
  237. ath9k_ps_wakeup(sc);
  238. spin_lock_bh(&sc->sc_pcu_lock);
  239. ath9k_hw_disable_interrupts(ah);
  240. ah->imask = sc->wow_intr_before_sleep;
  241. ath9k_hw_set_interrupts(ah);
  242. ath9k_hw_enable_interrupts(ah);
  243. spin_unlock_bh(&sc->sc_pcu_lock);
  244. wow_status = ath9k_hw_wow_wakeup(ah);
  245. if (atomic_read(&sc->wow_got_bmiss_intr) == 0) {
  246. /*
  247. * some devices may not pick beacon miss
  248. * as the reason they woke up so we add
  249. * that here for that shortcoming.
  250. */
  251. wow_status |= AH_WOW_BEACON_MISS;
  252. atomic_dec(&sc->wow_got_bmiss_intr);
  253. ath_dbg(common, ANY, "Beacon miss interrupt picked up during WoW sleep\n");
  254. }
  255. atomic_dec(&sc->wow_sleep_proc_intr);
  256. if (wow_status) {
  257. ath_dbg(common, ANY, "Waking up due to WoW triggers %s with WoW status = %x\n",
  258. ath9k_hw_wow_event_to_string(wow_status), wow_status);
  259. }
  260. ath_restart_work(sc);
  261. ath9k_start_btcoex(sc);
  262. ath9k_ps_restore(sc);
  263. mutex_unlock(&sc->mutex);
  264. return 0;
  265. }
  266. void ath9k_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  267. {
  268. struct ath_softc *sc = hw->priv;
  269. mutex_lock(&sc->mutex);
  270. device_init_wakeup(sc->dev, 1);
  271. device_set_wakeup_enable(sc->dev, enabled);
  272. mutex_unlock(&sc->mutex);
  273. }
  274. void ath9k_init_wow(struct ieee80211_hw *hw)
  275. {
  276. struct ath_softc *sc = hw->priv;
  277. if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_WOW_DEVICE_CAPABLE) &&
  278. (sc->driver_data & ATH9K_PCI_WOW) &&
  279. device_can_wakeup(sc->dev))
  280. hw->wiphy->wowlan = &ath9k_wowlan_support;
  281. atomic_set(&sc->wow_sleep_proc_intr, -1);
  282. atomic_set(&sc->wow_got_bmiss_intr, -1);
  283. }