ps.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2008-2009 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include "reg.h"
  24. #include "ps.h"
  25. #include "io.h"
  26. #include "tx.h"
  27. #define WL1271_WAKEUP_TIMEOUT 500
  28. void wl1271_elp_work(struct work_struct *work)
  29. {
  30. struct delayed_work *dwork;
  31. struct wl1271 *wl;
  32. dwork = container_of(work, struct delayed_work, work);
  33. wl = container_of(dwork, struct wl1271, elp_work);
  34. wl1271_debug(DEBUG_PSM, "elp work");
  35. mutex_lock(&wl->mutex);
  36. if (unlikely(wl->state == WL1271_STATE_OFF))
  37. goto out;
  38. if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags) ||
  39. (!test_bit(WL1271_FLAG_PSM, &wl->flags) &&
  40. !test_bit(WL1271_FLAG_IDLE, &wl->flags)))
  41. goto out;
  42. wl1271_debug(DEBUG_PSM, "chip to elp");
  43. wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
  44. set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
  45. out:
  46. mutex_unlock(&wl->mutex);
  47. }
  48. #define ELP_ENTRY_DELAY 5
  49. /* Routines to toggle sleep mode while in ELP */
  50. void wl1271_ps_elp_sleep(struct wl1271 *wl)
  51. {
  52. if (test_bit(WL1271_FLAG_PSM, &wl->flags) ||
  53. test_bit(WL1271_FLAG_IDLE, &wl->flags)) {
  54. cancel_delayed_work(&wl->elp_work);
  55. ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
  56. msecs_to_jiffies(ELP_ENTRY_DELAY));
  57. }
  58. }
  59. int wl1271_ps_elp_wakeup(struct wl1271 *wl)
  60. {
  61. DECLARE_COMPLETION_ONSTACK(compl);
  62. unsigned long flags;
  63. int ret;
  64. u32 start_time = jiffies;
  65. bool pending = false;
  66. if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
  67. return 0;
  68. wl1271_debug(DEBUG_PSM, "waking up chip from elp");
  69. /*
  70. * The spinlock is required here to synchronize both the work and
  71. * the completion variable in one entity.
  72. */
  73. spin_lock_irqsave(&wl->wl_lock, flags);
  74. if (test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags))
  75. pending = true;
  76. else
  77. wl->elp_compl = &compl;
  78. spin_unlock_irqrestore(&wl->wl_lock, flags);
  79. wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
  80. if (!pending) {
  81. ret = wait_for_completion_timeout(
  82. &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
  83. if (ret == 0) {
  84. wl1271_error("ELP wakeup timeout!");
  85. ieee80211_queue_work(wl->hw, &wl->recovery_work);
  86. ret = -ETIMEDOUT;
  87. goto err;
  88. } else if (ret < 0) {
  89. wl1271_error("ELP wakeup completion error.");
  90. goto err;
  91. }
  92. }
  93. clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
  94. wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
  95. jiffies_to_msecs(jiffies - start_time));
  96. goto out;
  97. err:
  98. spin_lock_irqsave(&wl->wl_lock, flags);
  99. wl->elp_compl = NULL;
  100. spin_unlock_irqrestore(&wl->wl_lock, flags);
  101. return ret;
  102. out:
  103. return 0;
  104. }
  105. int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode,
  106. u32 rates, bool send)
  107. {
  108. int ret;
  109. switch (mode) {
  110. case STATION_POWER_SAVE_MODE:
  111. wl1271_debug(DEBUG_PSM, "entering psm");
  112. ret = wl1271_acx_wake_up_conditions(wl);
  113. if (ret < 0) {
  114. wl1271_error("couldn't set wake up conditions");
  115. return ret;
  116. }
  117. ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
  118. if (ret < 0)
  119. return ret;
  120. set_bit(WL1271_FLAG_PSM, &wl->flags);
  121. break;
  122. case STATION_ACTIVE_MODE:
  123. default:
  124. wl1271_debug(DEBUG_PSM, "leaving psm");
  125. /* disable beacon early termination */
  126. ret = wl1271_acx_bet_enable(wl, false);
  127. if (ret < 0)
  128. return ret;
  129. /* disable beacon filtering */
  130. ret = wl1271_acx_beacon_filter_opt(wl, false);
  131. if (ret < 0)
  132. return ret;
  133. ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
  134. if (ret < 0)
  135. return ret;
  136. clear_bit(WL1271_FLAG_PSM, &wl->flags);
  137. break;
  138. }
  139. return ret;
  140. }
  141. static void wl1271_ps_filter_frames(struct wl1271 *wl, u8 hlid)
  142. {
  143. int i, filtered = 0;
  144. struct sk_buff *skb;
  145. struct ieee80211_tx_info *info;
  146. unsigned long flags;
  147. /* filter all frames currently the low level queus for this hlid */
  148. for (i = 0; i < NUM_TX_QUEUES; i++) {
  149. while ((skb = skb_dequeue(&wl->links[hlid].tx_queue[i]))) {
  150. info = IEEE80211_SKB_CB(skb);
  151. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  152. info->status.rates[0].idx = -1;
  153. ieee80211_tx_status(wl->hw, skb);
  154. filtered++;
  155. }
  156. }
  157. spin_lock_irqsave(&wl->wl_lock, flags);
  158. wl->tx_queue_count -= filtered;
  159. spin_unlock_irqrestore(&wl->wl_lock, flags);
  160. wl1271_handle_tx_low_watermark(wl);
  161. }
  162. void wl1271_ps_link_start(struct wl1271 *wl, u8 hlid, bool clean_queues)
  163. {
  164. struct ieee80211_sta *sta;
  165. if (test_bit(hlid, &wl->ap_ps_map))
  166. return;
  167. wl1271_debug(DEBUG_PSM, "start mac80211 PSM on hlid %d blks %d "
  168. "clean_queues %d", hlid, wl->links[hlid].allocated_blks,
  169. clean_queues);
  170. rcu_read_lock();
  171. sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr);
  172. if (!sta) {
  173. wl1271_error("could not find sta %pM for starting ps",
  174. wl->links[hlid].addr);
  175. rcu_read_unlock();
  176. return;
  177. }
  178. ieee80211_sta_ps_transition_ni(sta, true);
  179. rcu_read_unlock();
  180. /* do we want to filter all frames from this link's queues? */
  181. if (clean_queues)
  182. wl1271_ps_filter_frames(wl, hlid);
  183. __set_bit(hlid, &wl->ap_ps_map);
  184. }
  185. void wl1271_ps_link_end(struct wl1271 *wl, u8 hlid)
  186. {
  187. struct ieee80211_sta *sta;
  188. if (!test_bit(hlid, &wl->ap_ps_map))
  189. return;
  190. wl1271_debug(DEBUG_PSM, "end mac80211 PSM on hlid %d", hlid);
  191. __clear_bit(hlid, &wl->ap_ps_map);
  192. rcu_read_lock();
  193. sta = ieee80211_find_sta(wl->vif, wl->links[hlid].addr);
  194. if (!sta) {
  195. wl1271_error("could not find sta %pM for ending ps",
  196. wl->links[hlid].addr);
  197. goto end;
  198. }
  199. ieee80211_sta_ps_transition_ni(sta, false);
  200. end:
  201. rcu_read_unlock();
  202. }