wl1271_ps.c 3.8 KB

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