ps.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #define WL1271_WAKEUP_TIMEOUT 500
  27. void wl1271_elp_work(struct work_struct *work)
  28. {
  29. struct delayed_work *dwork;
  30. struct wl1271 *wl;
  31. dwork = container_of(work, struct delayed_work, work);
  32. wl = container_of(dwork, struct wl1271, elp_work);
  33. wl1271_debug(DEBUG_PSM, "elp work");
  34. mutex_lock(&wl->mutex);
  35. if (unlikely(wl->state == WL1271_STATE_OFF))
  36. goto out;
  37. if (test_bit(WL1271_FLAG_IN_ELP, &wl->flags) ||
  38. (!test_bit(WL1271_FLAG_PSM, &wl->flags) &&
  39. !test_bit(WL1271_FLAG_IDLE, &wl->flags)))
  40. goto out;
  41. wl1271_debug(DEBUG_PSM, "chip to elp");
  42. wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
  43. set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
  44. out:
  45. mutex_unlock(&wl->mutex);
  46. }
  47. #define ELP_ENTRY_DELAY 5
  48. /* Routines to toggle sleep mode while in ELP */
  49. void wl1271_ps_elp_sleep(struct wl1271 *wl)
  50. {
  51. if (test_bit(WL1271_FLAG_PSM, &wl->flags) ||
  52. test_bit(WL1271_FLAG_IDLE, &wl->flags)) {
  53. cancel_delayed_work(&wl->elp_work);
  54. ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
  55. msecs_to_jiffies(ELP_ENTRY_DELAY));
  56. }
  57. }
  58. int wl1271_ps_elp_wakeup(struct wl1271 *wl, bool chip_awake)
  59. {
  60. DECLARE_COMPLETION_ONSTACK(compl);
  61. unsigned long flags;
  62. int ret;
  63. u32 start_time = jiffies;
  64. bool pending = false;
  65. if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
  66. return 0;
  67. wl1271_debug(DEBUG_PSM, "waking up chip from elp");
  68. /*
  69. * The spinlock is required here to synchronize both the work and
  70. * the completion variable in one entity.
  71. */
  72. spin_lock_irqsave(&wl->wl_lock, flags);
  73. if (work_pending(&wl->irq_work) || chip_awake)
  74. pending = true;
  75. else
  76. wl->elp_compl = &compl;
  77. spin_unlock_irqrestore(&wl->wl_lock, flags);
  78. wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
  79. if (!pending) {
  80. ret = wait_for_completion_timeout(
  81. &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
  82. if (ret == 0) {
  83. wl1271_error("ELP wakeup timeout!");
  84. ieee80211_queue_work(wl->hw, &wl->recovery_work);
  85. ret = -ETIMEDOUT;
  86. goto err;
  87. } else if (ret < 0) {
  88. wl1271_error("ELP wakeup completion error.");
  89. goto err;
  90. }
  91. }
  92. clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
  93. wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
  94. jiffies_to_msecs(jiffies - start_time));
  95. goto out;
  96. err:
  97. spin_lock_irqsave(&wl->wl_lock, flags);
  98. wl->elp_compl = NULL;
  99. spin_unlock_irqrestore(&wl->wl_lock, flags);
  100. return ret;
  101. out:
  102. return 0;
  103. }
  104. int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode,
  105. u32 rates, bool send)
  106. {
  107. int ret;
  108. switch (mode) {
  109. case STATION_POWER_SAVE_MODE:
  110. wl1271_debug(DEBUG_PSM, "entering psm");
  111. ret = wl1271_acx_wake_up_conditions(wl);
  112. if (ret < 0) {
  113. wl1271_error("couldn't set wake up conditions");
  114. return ret;
  115. }
  116. ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE,
  117. rates, send);
  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. ret = wl1271_ps_elp_wakeup(wl, false);
  126. if (ret < 0)
  127. return ret;
  128. /* disable beacon early termination */
  129. ret = wl1271_acx_bet_enable(wl, false);
  130. if (ret < 0)
  131. return ret;
  132. /* disable beacon filtering */
  133. ret = wl1271_acx_beacon_filter_opt(wl, false);
  134. if (ret < 0)
  135. return ret;
  136. ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE,
  137. rates, send);
  138. if (ret < 0)
  139. return ret;
  140. clear_bit(WL1271_FLAG_PSM, &wl->flags);
  141. break;
  142. }
  143. return ret;
  144. }