wl1251_ps.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * This file is part of wl1251
  3. *
  4. * Copyright (C) 2008 Nokia Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. *
  20. */
  21. #include "wl1251_reg.h"
  22. #include "wl1251_ps.h"
  23. #include "wl1251_cmd.h"
  24. #include "wl1251_io.h"
  25. /* in ms */
  26. #define WL1251_WAKEUP_TIMEOUT 100
  27. void wl1251_elp_work(struct work_struct *work)
  28. {
  29. struct delayed_work *dwork;
  30. struct wl1251 *wl;
  31. dwork = container_of(work, struct delayed_work, work);
  32. wl = container_of(dwork, struct wl1251, elp_work);
  33. wl1251_debug(DEBUG_PSM, "elp work");
  34. mutex_lock(&wl->mutex);
  35. if (wl->elp || !wl->psm)
  36. goto out;
  37. wl1251_debug(DEBUG_PSM, "chip to elp");
  38. wl1251_write_elp(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 wl1251_ps_elp_sleep(struct wl1251 *wl)
  46. {
  47. unsigned long delay;
  48. if (wl->psm) {
  49. cancel_delayed_work(&wl->elp_work);
  50. delay = msecs_to_jiffies(ELP_ENTRY_DELAY);
  51. ieee80211_queue_delayed_work(wl->hw, &wl->elp_work, delay);
  52. }
  53. }
  54. int wl1251_ps_elp_wakeup(struct wl1251 *wl)
  55. {
  56. unsigned long timeout, start;
  57. u32 elp_reg;
  58. if (!wl->elp)
  59. return 0;
  60. wl1251_debug(DEBUG_PSM, "waking up chip from elp");
  61. start = jiffies;
  62. timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
  63. wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
  64. elp_reg = wl1251_read_elp(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_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  76. }
  77. wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
  78. jiffies_to_msecs(jiffies - start));
  79. wl->elp = false;
  80. return 0;
  81. }
  82. static int wl1251_ps_set_elp(struct wl1251 *wl, bool enable)
  83. {
  84. int ret;
  85. if (enable) {
  86. wl1251_debug(DEBUG_PSM, "sleep auth psm/elp");
  87. ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
  88. if (ret < 0)
  89. return ret;
  90. wl1251_ps_elp_sleep(wl);
  91. } else {
  92. wl1251_debug(DEBUG_PSM, "sleep auth cam");
  93. /*
  94. * When the target is in ELP, we can only
  95. * access the ELP control register. Thus,
  96. * we have to wake the target up before
  97. * changing the power authorization.
  98. */
  99. wl1251_ps_elp_wakeup(wl);
  100. ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
  101. if (ret < 0)
  102. return ret;
  103. }
  104. return 0;
  105. }
  106. int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_cmd_ps_mode mode)
  107. {
  108. int ret;
  109. switch (mode) {
  110. case STATION_POWER_SAVE_MODE:
  111. wl1251_debug(DEBUG_PSM, "entering psm");
  112. /* enable beacon filtering */
  113. ret = wl1251_acx_beacon_filter_opt(wl, true);
  114. if (ret < 0)
  115. return ret;
  116. ret = wl1251_acx_wake_up_conditions(wl,
  117. WAKE_UP_EVENT_DTIM_BITMAP,
  118. wl->listen_int);
  119. if (ret < 0)
  120. return ret;
  121. ret = wl1251_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
  122. if (ret < 0)
  123. return ret;
  124. ret = wl1251_ps_set_elp(wl, true);
  125. if (ret < 0)
  126. return ret;
  127. wl->psm = 1;
  128. break;
  129. case STATION_ACTIVE_MODE:
  130. default:
  131. wl1251_debug(DEBUG_PSM, "leaving psm");
  132. ret = wl1251_ps_set_elp(wl, false);
  133. if (ret < 0)
  134. return ret;
  135. /* disable beacon filtering */
  136. ret = wl1251_acx_beacon_filter_opt(wl, false);
  137. if (ret < 0)
  138. return ret;
  139. ret = wl1251_acx_wake_up_conditions(wl,
  140. WAKE_UP_EVENT_DTIM_BITMAP,
  141. wl->listen_int);
  142. if (ret < 0)
  143. return ret;
  144. ret = wl1251_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
  145. if (ret < 0)
  146. return ret;
  147. wl->psm = 0;
  148. break;
  149. }
  150. return ret;
  151. }