wl1271_ps.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "wl1271_reg.h"
  24. #include "wl1271_ps.h"
  25. #include "wl1271_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 (test_bit(WL1271_FLAG_IN_ELP, &wl->flags) ||
  36. (!test_bit(WL1271_FLAG_PSM, &wl->flags) &&
  37. !test_bit(WL1271_FLAG_IDLE, &wl->flags)))
  38. goto out;
  39. wl1271_debug(DEBUG_PSM, "chip to elp");
  40. wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
  41. set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
  42. out:
  43. mutex_unlock(&wl->mutex);
  44. }
  45. #define ELP_ENTRY_DELAY 5
  46. /* Routines to toggle sleep mode while in ELP */
  47. void wl1271_ps_elp_sleep(struct wl1271 *wl)
  48. {
  49. if (test_bit(WL1271_FLAG_PSM, &wl->flags) ||
  50. test_bit(WL1271_FLAG_IDLE, &wl->flags)) {
  51. cancel_delayed_work(&wl->elp_work);
  52. ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
  53. msecs_to_jiffies(ELP_ENTRY_DELAY));
  54. }
  55. }
  56. int wl1271_ps_elp_wakeup(struct wl1271 *wl, bool chip_awake)
  57. {
  58. DECLARE_COMPLETION_ONSTACK(compl);
  59. unsigned long flags;
  60. int ret;
  61. u32 start_time = jiffies;
  62. bool pending = false;
  63. if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
  64. return 0;
  65. wl1271_debug(DEBUG_PSM, "waking up chip from elp");
  66. /*
  67. * The spinlock is required here to synchronize both the work and
  68. * the completion variable in one entity.
  69. */
  70. spin_lock_irqsave(&wl->wl_lock, flags);
  71. if (work_pending(&wl->irq_work) || chip_awake)
  72. pending = true;
  73. else
  74. wl->elp_compl = &compl;
  75. spin_unlock_irqrestore(&wl->wl_lock, flags);
  76. wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
  77. if (!pending) {
  78. ret = wait_for_completion_timeout(
  79. &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
  80. if (ret == 0) {
  81. wl1271_error("ELP wakeup timeout!");
  82. ret = -ETIMEDOUT;
  83. goto err;
  84. } else if (ret < 0) {
  85. wl1271_error("ELP wakeup completion error.");
  86. goto err;
  87. }
  88. }
  89. clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
  90. wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
  91. jiffies_to_msecs(jiffies - start_time));
  92. goto out;
  93. err:
  94. spin_lock_irqsave(&wl->wl_lock, flags);
  95. wl->elp_compl = NULL;
  96. spin_unlock_irqrestore(&wl->wl_lock, flags);
  97. return ret;
  98. out:
  99. return 0;
  100. }
  101. int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode,
  102. bool send)
  103. {
  104. int ret;
  105. switch (mode) {
  106. case STATION_POWER_SAVE_MODE:
  107. wl1271_debug(DEBUG_PSM, "entering psm");
  108. ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE, send);
  109. if (ret < 0)
  110. return ret;
  111. set_bit(WL1271_FLAG_PSM, &wl->flags);
  112. break;
  113. case STATION_ACTIVE_MODE:
  114. default:
  115. wl1271_debug(DEBUG_PSM, "leaving psm");
  116. ret = wl1271_ps_elp_wakeup(wl, false);
  117. if (ret < 0)
  118. return ret;
  119. /* disable beacon early termination */
  120. ret = wl1271_acx_bet_enable(wl, false);
  121. if (ret < 0)
  122. return ret;
  123. /* disable beacon filtering */
  124. ret = wl1271_acx_beacon_filter_opt(wl, false);
  125. if (ret < 0)
  126. return ret;
  127. ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE, send);
  128. if (ret < 0)
  129. return ret;
  130. clear_bit(WL1271_FLAG_PSM, &wl->flags);
  131. break;
  132. }
  133. return ret;
  134. }