wl1271_ps.c 3.9 KB

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