ps.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "reg.h"
  22. #include "ps.h"
  23. #include "cmd.h"
  24. #include "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->station_mode == STATION_ACTIVE_MODE)
  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->station_mode != STATION_ACTIVE_MODE) {
  49. delay = msecs_to_jiffies(ELP_ENTRY_DELAY);
  50. ieee80211_queue_delayed_work(wl->hw, &wl->elp_work, delay);
  51. }
  52. }
  53. int wl1251_ps_elp_wakeup(struct wl1251 *wl)
  54. {
  55. unsigned long timeout, start;
  56. u32 elp_reg;
  57. cancel_delayed_work(&wl->elp_work);
  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. int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_station_mode mode)
  83. {
  84. int ret;
  85. switch (mode) {
  86. case STATION_POWER_SAVE_MODE:
  87. wl1251_debug(DEBUG_PSM, "entering psm");
  88. /* enable beacon filtering */
  89. ret = wl1251_acx_beacon_filter_opt(wl, true);
  90. if (ret < 0)
  91. return ret;
  92. ret = wl1251_acx_wake_up_conditions(wl,
  93. WAKE_UP_EVENT_DTIM_BITMAP,
  94. wl->listen_int);
  95. if (ret < 0)
  96. return ret;
  97. ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_ENABLE,
  98. WL1251_DEFAULT_BET_CONSECUTIVE);
  99. if (ret < 0)
  100. return ret;
  101. ret = wl1251_cmd_ps_mode(wl, CHIP_POWER_SAVE_MODE);
  102. if (ret < 0)
  103. return ret;
  104. ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
  105. if (ret < 0)
  106. return ret;
  107. break;
  108. case STATION_IDLE:
  109. wl1251_debug(DEBUG_PSM, "entering idle");
  110. ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
  111. if (ret < 0)
  112. return ret;
  113. ret = wl1251_cmd_template_set(wl, CMD_DISCONNECT, NULL, 0);
  114. if (ret < 0)
  115. return ret;
  116. break;
  117. case STATION_ACTIVE_MODE:
  118. default:
  119. wl1251_debug(DEBUG_PSM, "leaving psm");
  120. ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
  121. if (ret < 0)
  122. return ret;
  123. /* disable BET */
  124. ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_DISABLE,
  125. WL1251_DEFAULT_BET_CONSECUTIVE);
  126. if (ret < 0)
  127. return ret;
  128. /* disable beacon filtering */
  129. ret = wl1251_acx_beacon_filter_opt(wl, false);
  130. if (ret < 0)
  131. return ret;
  132. ret = wl1251_acx_wake_up_conditions(wl,
  133. WAKE_UP_EVENT_DTIM_BITMAP,
  134. wl->listen_int);
  135. if (ret < 0)
  136. return ret;
  137. ret = wl1251_cmd_ps_mode(wl, CHIP_ACTIVE_MODE);
  138. if (ret < 0)
  139. return ret;
  140. break;
  141. }
  142. wl->station_mode = mode;
  143. return ret;
  144. }