blob: db719f7d26928c4012a1020a050edbd3a9560980 [file] [log] [blame]
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001/*
Kalle Valo80301cd2009-06-12 14:17:39 +03002 * This file is part of wl1251
Kalle Valo2f01a1f2009-04-29 23:33:31 +03003 *
4 * Copyright (C) 2008 Nokia Corporation
5 *
Kalle Valo2f01a1f2009-04-29 23:33:31 +03006 * 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
Kalle Valo9bc67722010-10-10 11:28:32 +030022#include "reg.h"
23#include "ps.h"
24#include "cmd.h"
25#include "io.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030026
Kalle Valob5a16792009-11-30 10:17:59 +020027/* in ms */
28#define WL1251_WAKEUP_TIMEOUT 100
Kalle Valo2f01a1f2009-04-29 23:33:31 +030029
Juuso Oikarinend5da79a2009-11-17 18:48:37 +020030void wl1251_elp_work(struct work_struct *work)
31{
32 struct delayed_work *dwork;
33 struct wl1251 *wl;
34
35 dwork = container_of(work, struct delayed_work, work);
36 wl = container_of(dwork, struct wl1251, elp_work);
37
38 wl1251_debug(DEBUG_PSM, "elp work");
39
40 mutex_lock(&wl->mutex);
41
Jarkko Nikulaa0bbb582011-04-04 11:04:57 +030042 if (wl->elp || wl->station_mode == STATION_ACTIVE_MODE)
Juuso Oikarinend5da79a2009-11-17 18:48:37 +020043 goto out;
44
45 wl1251_debug(DEBUG_PSM, "chip to elp");
Grazvydas Ignotas3f9e7502010-03-11 17:44:57 +020046 wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
Juuso Oikarinend5da79a2009-11-17 18:48:37 +020047 wl->elp = true;
48
49out:
50 mutex_unlock(&wl->mutex);
51}
52
53#define ELP_ENTRY_DELAY 5
54
Kalle Valo2f01a1f2009-04-29 23:33:31 +030055/* Routines to toggle sleep mode while in ELP */
Kalle Valo80301cd2009-06-12 14:17:39 +030056void wl1251_ps_elp_sleep(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030057{
Juuso Oikarinend5da79a2009-11-17 18:48:37 +020058 unsigned long delay;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030059
Jarkko Nikulaa0bbb582011-04-04 11:04:57 +030060 if (wl->station_mode != STATION_ACTIVE_MODE) {
Juuso Oikarinend5da79a2009-11-17 18:48:37 +020061 delay = msecs_to_jiffies(ELP_ENTRY_DELAY);
62 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work, delay);
63 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +030064}
65
Kalle Valo80301cd2009-06-12 14:17:39 +030066int wl1251_ps_elp_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030067{
Kalle Valo7fa62822009-11-30 10:18:06 +020068 unsigned long timeout, start;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030069 u32 elp_reg;
70
Grazvydas Ignotas5f6722e2011-03-06 19:23:37 +020071 if (delayed_work_pending(&wl->elp_work))
72 cancel_delayed_work(&wl->elp_work);
73
Kalle Valo2f01a1f2009-04-29 23:33:31 +030074 if (!wl->elp)
75 return 0;
76
Kalle Valo80301cd2009-06-12 14:17:39 +030077 wl1251_debug(DEBUG_PSM, "waking up chip from elp");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030078
Kalle Valo7fa62822009-11-30 10:18:06 +020079 start = jiffies;
Kalle Valo80301cd2009-06-12 14:17:39 +030080 timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030081
Grazvydas Ignotas3f9e7502010-03-11 17:44:57 +020082 wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030083
Grazvydas Ignotas3f9e7502010-03-11 17:44:57 +020084 elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030085
86 /*
87 * FIXME: we should wait for irq from chip but, as a temporary
88 * solution to simplify locking, let's poll instead
89 */
90 while (!(elp_reg & ELPCTRL_WLAN_READY)) {
91 if (time_after(jiffies, timeout)) {
Kalle Valo80301cd2009-06-12 14:17:39 +030092 wl1251_error("elp wakeup timeout");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030093 return -ETIMEDOUT;
94 }
95 msleep(1);
Grazvydas Ignotas3f9e7502010-03-11 17:44:57 +020096 elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030097 }
98
Kalle Valo80301cd2009-06-12 14:17:39 +030099 wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
Kalle Valo7fa62822009-11-30 10:18:06 +0200100 jiffies_to_msecs(jiffies - start));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300101
102 wl->elp = false;
103
104 return 0;
105}
106
Jarkko Nikulaa0bbb582011-04-04 11:04:57 +0300107int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_station_mode mode)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300108{
109 int ret;
110
111 switch (mode) {
112 case STATION_POWER_SAVE_MODE:
Kalle Valo80301cd2009-06-12 14:17:39 +0300113 wl1251_debug(DEBUG_PSM, "entering psm");
Kalle Valo4a818922009-08-07 13:34:56 +0300114
Juuso Oikarinen6b21a2c2009-11-17 18:48:30 +0200115 /* enable beacon filtering */
116 ret = wl1251_acx_beacon_filter_opt(wl, true);
117 if (ret < 0)
118 return ret;
119
Kalle Valo4a818922009-08-07 13:34:56 +0300120 ret = wl1251_acx_wake_up_conditions(wl,
121 WAKE_UP_EVENT_DTIM_BITMAP,
122 wl->listen_int);
123 if (ret < 0)
124 return ret;
125
David Gnedtc3e334d2011-01-30 20:10:57 +0100126 ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_ENABLE,
127 WL1251_DEFAULT_BET_CONSECUTIVE);
128 if (ret < 0)
129 return ret;
130
Jarkko Nikulaa0bbb582011-04-04 11:04:57 +0300131 ret = wl1251_cmd_ps_mode(wl, CHIP_POWER_SAVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300132 if (ret < 0)
133 return ret;
134
Grazvydas Ignotasd0c331a2011-03-06 19:23:36 +0200135 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300136 if (ret < 0)
137 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300138 break;
Jarkko Nikula1e5f52d2011-04-04 11:04:58 +0300139 case STATION_IDLE:
140 wl1251_debug(DEBUG_PSM, "entering idle");
141
142 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
143 if (ret < 0)
144 return ret;
145
146 ret = wl1251_cmd_template_set(wl, CMD_DISCONNECT, NULL, 0);
147 if (ret < 0)
148 return ret;
149 break;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300150 case STATION_ACTIVE_MODE:
151 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300152 wl1251_debug(DEBUG_PSM, "leaving psm");
Grazvydas Ignotasd0c331a2011-03-06 19:23:36 +0200153
154 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300155 if (ret < 0)
156 return ret;
157
David Gnedtc3e334d2011-01-30 20:10:57 +0100158 /* disable BET */
159 ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_DISABLE,
160 WL1251_DEFAULT_BET_CONSECUTIVE);
161 if (ret < 0)
162 return ret;
163
Juuso Oikarinen6b21a2c2009-11-17 18:48:30 +0200164 /* disable beacon filtering */
165 ret = wl1251_acx_beacon_filter_opt(wl, false);
166 if (ret < 0)
167 return ret;
168
Kalle Valo4a818922009-08-07 13:34:56 +0300169 ret = wl1251_acx_wake_up_conditions(wl,
170 WAKE_UP_EVENT_DTIM_BITMAP,
171 wl->listen_int);
172 if (ret < 0)
173 return ret;
174
Jarkko Nikulaa0bbb582011-04-04 11:04:57 +0300175 ret = wl1251_cmd_ps_mode(wl, CHIP_ACTIVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300176 if (ret < 0)
177 return ret;
178
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300179 break;
180 }
Jarkko Nikulaa0bbb582011-04-04 11:04:57 +0300181 wl->station_mode = mode;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300182
183 return ret;
184}
185