wl1251_ps.c 4.2 KB

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