wl1271_ps.c 3.9 KB

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