wl1251_ps.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * This file is part of wl1251
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. *
  6. * Contact: Kalle Valo <kalle.valo@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 "wl1251_reg.h"
  24. #include "wl1251_ps.h"
  25. #include "wl1251_cmd.h"
  26. #include "wl1251_io.h"
  27. #define WL1251_WAKEUP_TIMEOUT 2000
  28. void wl1251_elp_work(struct work_struct *work)
  29. {
  30. struct delayed_work *dwork;
  31. struct wl1251 *wl;
  32. dwork = container_of(work, struct delayed_work, work);
  33. wl = container_of(dwork, struct wl1251, elp_work);
  34. wl1251_debug(DEBUG_PSM, "elp work");
  35. mutex_lock(&wl->mutex);
  36. if (wl->elp || !wl->psm)
  37. goto out;
  38. wl1251_debug(DEBUG_PSM, "chip to elp");
  39. wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
  40. wl->elp = true;
  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 wl1251_ps_elp_sleep(struct wl1251 *wl)
  47. {
  48. unsigned long delay;
  49. if (wl->psm) {
  50. cancel_delayed_work(&wl->elp_work);
  51. delay = msecs_to_jiffies(ELP_ENTRY_DELAY);
  52. ieee80211_queue_delayed_work(wl->hw, &wl->elp_work, delay);
  53. }
  54. }
  55. int wl1251_ps_elp_wakeup(struct wl1251 *wl)
  56. {
  57. unsigned long timeout;
  58. u32 elp_reg;
  59. if (!wl->elp)
  60. return 0;
  61. wl1251_debug(DEBUG_PSM, "waking up chip from elp");
  62. timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
  63. wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
  64. elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  65. /*
  66. * FIXME: we should wait for irq from chip but, as a temporary
  67. * solution to simplify locking, let's poll instead
  68. */
  69. while (!(elp_reg & ELPCTRL_WLAN_READY)) {
  70. if (time_after(jiffies, timeout)) {
  71. wl1251_error("elp wakeup timeout");
  72. return -ETIMEDOUT;
  73. }
  74. msleep(1);
  75. elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  76. }
  77. wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
  78. jiffies_to_msecs(jiffies) -
  79. (jiffies_to_msecs(timeout) - WL1251_WAKEUP_TIMEOUT));
  80. wl->elp = false;
  81. return 0;
  82. }
  83. static int wl1251_ps_set_elp(struct wl1251 *wl, bool enable)
  84. {
  85. int ret;
  86. if (enable) {
  87. wl1251_debug(DEBUG_PSM, "sleep auth psm/elp");
  88. ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
  89. if (ret < 0)
  90. return ret;
  91. wl1251_ps_elp_sleep(wl);
  92. } else {
  93. wl1251_debug(DEBUG_PSM, "sleep auth cam");
  94. /*
  95. * When the target is in ELP, we can only
  96. * access the ELP control register. Thus,
  97. * we have to wake the target up before
  98. * changing the power authorization.
  99. */
  100. wl1251_ps_elp_wakeup(wl);
  101. ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
  102. if (ret < 0)
  103. return ret;
  104. }
  105. return 0;
  106. }
  107. int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_cmd_ps_mode mode)
  108. {
  109. int ret;
  110. switch (mode) {
  111. case STATION_POWER_SAVE_MODE:
  112. wl1251_debug(DEBUG_PSM, "entering psm");
  113. /* enable beacon filtering */
  114. ret = wl1251_acx_beacon_filter_opt(wl, true);
  115. if (ret < 0)
  116. return ret;
  117. ret = wl1251_acx_wake_up_conditions(wl,
  118. WAKE_UP_EVENT_DTIM_BITMAP,
  119. wl->listen_int);
  120. if (ret < 0)
  121. return ret;
  122. ret = wl1251_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
  123. if (ret < 0)
  124. return ret;
  125. ret = wl1251_ps_set_elp(wl, true);
  126. if (ret < 0)
  127. return ret;
  128. wl->psm = 1;
  129. break;
  130. case STATION_ACTIVE_MODE:
  131. default:
  132. wl1251_debug(DEBUG_PSM, "leaving psm");
  133. ret = wl1251_ps_set_elp(wl, false);
  134. if (ret < 0)
  135. return ret;
  136. /* disable beacon filtering */
  137. ret = wl1251_acx_beacon_filter_opt(wl, false);
  138. if (ret < 0)
  139. return ret;
  140. ret = wl1251_acx_wake_up_conditions(wl,
  141. WAKE_UP_EVENT_DTIM_BITMAP,
  142. wl->listen_int);
  143. if (ret < 0)
  144. return ret;
  145. ret = wl1251_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
  146. if (ret < 0)
  147. return ret;
  148. wl->psm = 0;
  149. break;
  150. }
  151. return ret;
  152. }