ps.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. if (delayed_work_pending(&wl->elp_work))
  58. cancel_delayed_work(&wl->elp_work);
  59. if (!wl->elp)
  60. return 0;
  61. wl1251_debug(DEBUG_PSM, "waking up chip from elp");
  62. start = jiffies;
  63. timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
  64. wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
  65. elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  66. /*
  67. * FIXME: we should wait for irq from chip but, as a temporary
  68. * solution to simplify locking, let's poll instead
  69. */
  70. while (!(elp_reg & ELPCTRL_WLAN_READY)) {
  71. if (time_after(jiffies, timeout)) {
  72. wl1251_error("elp wakeup timeout");
  73. return -ETIMEDOUT;
  74. }
  75. msleep(1);
  76. elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
  77. }
  78. wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
  79. jiffies_to_msecs(jiffies - start));
  80. wl->elp = false;
  81. return 0;
  82. }
  83. int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_station_mode mode)
  84. {
  85. int ret;
  86. switch (mode) {
  87. case STATION_POWER_SAVE_MODE:
  88. wl1251_debug(DEBUG_PSM, "entering psm");
  89. /* enable beacon filtering */
  90. ret = wl1251_acx_beacon_filter_opt(wl, true);
  91. if (ret < 0)
  92. return ret;
  93. ret = wl1251_acx_wake_up_conditions(wl,
  94. WAKE_UP_EVENT_DTIM_BITMAP,
  95. wl->listen_int);
  96. if (ret < 0)
  97. return ret;
  98. ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_ENABLE,
  99. WL1251_DEFAULT_BET_CONSECUTIVE);
  100. if (ret < 0)
  101. return ret;
  102. ret = wl1251_cmd_ps_mode(wl, CHIP_POWER_SAVE_MODE);
  103. if (ret < 0)
  104. return ret;
  105. ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
  106. if (ret < 0)
  107. return ret;
  108. break;
  109. case STATION_IDLE:
  110. wl1251_debug(DEBUG_PSM, "entering idle");
  111. ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
  112. if (ret < 0)
  113. return ret;
  114. ret = wl1251_cmd_template_set(wl, CMD_DISCONNECT, NULL, 0);
  115. if (ret < 0)
  116. return ret;
  117. break;
  118. case STATION_ACTIVE_MODE:
  119. default:
  120. wl1251_debug(DEBUG_PSM, "leaving psm");
  121. ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
  122. if (ret < 0)
  123. return ret;
  124. /* disable BET */
  125. ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_DISABLE,
  126. WL1251_DEFAULT_BET_CONSECUTIVE);
  127. if (ret < 0)
  128. return ret;
  129. /* disable beacon filtering */
  130. ret = wl1251_acx_beacon_filter_opt(wl, false);
  131. if (ret < 0)
  132. return ret;
  133. ret = wl1251_acx_wake_up_conditions(wl,
  134. WAKE_UP_EVENT_DTIM_BITMAP,
  135. wl->listen_int);
  136. if (ret < 0)
  137. return ret;
  138. ret = wl1251_cmd_ps_mode(wl, CHIP_ACTIVE_MODE);
  139. if (ret < 0)
  140. return ret;
  141. break;
  142. }
  143. wl->station_mode = mode;
  144. return ret;
  145. }