blob: 0b997bdfec09498f9ca5c562eac2becc202cdec0 [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 Valo29d904c2009-08-07 13:35:11 +030022#include "wl1251_reg.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030023#include "wl1251_ps.h"
Bob Copeland0764de62009-08-07 13:32:56 +030024#include "wl1251_cmd.h"
25#include "wl1251_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
42 if (wl->elp || !wl->psm)
43 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
Juuso Oikarinend5da79a2009-11-17 18:48:37 +020060 if (wl->psm) {
61 cancel_delayed_work(&wl->elp_work);
62 delay = msecs_to_jiffies(ELP_ENTRY_DELAY);
63 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work, delay);
64 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +030065}
66
Kalle Valo80301cd2009-06-12 14:17:39 +030067int wl1251_ps_elp_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030068{
Kalle Valo7fa62822009-11-30 10:18:06 +020069 unsigned long timeout, start;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030070 u32 elp_reg;
71
72 if (!wl->elp)
73 return 0;
74
Kalle Valo80301cd2009-06-12 14:17:39 +030075 wl1251_debug(DEBUG_PSM, "waking up chip from elp");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030076
Kalle Valo7fa62822009-11-30 10:18:06 +020077 start = jiffies;
Kalle Valo80301cd2009-06-12 14:17:39 +030078 timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030079
Grazvydas Ignotas3f9e7502010-03-11 17:44:57 +020080 wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030081
Grazvydas Ignotas3f9e7502010-03-11 17:44:57 +020082 elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030083
84 /*
85 * FIXME: we should wait for irq from chip but, as a temporary
86 * solution to simplify locking, let's poll instead
87 */
88 while (!(elp_reg & ELPCTRL_WLAN_READY)) {
89 if (time_after(jiffies, timeout)) {
Kalle Valo80301cd2009-06-12 14:17:39 +030090 wl1251_error("elp wakeup timeout");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030091 return -ETIMEDOUT;
92 }
93 msleep(1);
Grazvydas Ignotas3f9e7502010-03-11 17:44:57 +020094 elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030095 }
96
Kalle Valo80301cd2009-06-12 14:17:39 +030097 wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
Kalle Valo7fa62822009-11-30 10:18:06 +020098 jiffies_to_msecs(jiffies - start));
Kalle Valo2f01a1f2009-04-29 23:33:31 +030099
100 wl->elp = false;
101
102 return 0;
103}
104
Kalle Valo80301cd2009-06-12 14:17:39 +0300105static int wl1251_ps_set_elp(struct wl1251 *wl, bool enable)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300106{
107 int ret;
108
109 if (enable) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300110 wl1251_debug(DEBUG_PSM, "sleep auth psm/elp");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300111
Kalle Valo80301cd2009-06-12 14:17:39 +0300112 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300113 if (ret < 0)
114 return ret;
115
Kalle Valo80301cd2009-06-12 14:17:39 +0300116 wl1251_ps_elp_sleep(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300117 } else {
Kalle Valo80301cd2009-06-12 14:17:39 +0300118 wl1251_debug(DEBUG_PSM, "sleep auth cam");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300119
120 /*
121 * When the target is in ELP, we can only
122 * access the ELP control register. Thus,
123 * we have to wake the target up before
124 * changing the power authorization.
125 */
126
Kalle Valo80301cd2009-06-12 14:17:39 +0300127 wl1251_ps_elp_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300128
Kalle Valo80301cd2009-06-12 14:17:39 +0300129 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300130 if (ret < 0)
131 return ret;
132 }
133
134 return 0;
135}
136
Kalle Valo80301cd2009-06-12 14:17:39 +0300137int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_cmd_ps_mode mode)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300138{
139 int ret;
140
141 switch (mode) {
142 case STATION_POWER_SAVE_MODE:
Kalle Valo80301cd2009-06-12 14:17:39 +0300143 wl1251_debug(DEBUG_PSM, "entering psm");
Kalle Valo4a818922009-08-07 13:34:56 +0300144
Juuso Oikarinen6b21a2c2009-11-17 18:48:30 +0200145 /* enable beacon filtering */
146 ret = wl1251_acx_beacon_filter_opt(wl, true);
147 if (ret < 0)
148 return ret;
149
Kalle Valo4a818922009-08-07 13:34:56 +0300150 ret = wl1251_acx_wake_up_conditions(wl,
151 WAKE_UP_EVENT_DTIM_BITMAP,
152 wl->listen_int);
153 if (ret < 0)
154 return ret;
155
Kalle Valo80301cd2009-06-12 14:17:39 +0300156 ret = wl1251_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300157 if (ret < 0)
158 return ret;
159
Kalle Valo80301cd2009-06-12 14:17:39 +0300160 ret = wl1251_ps_set_elp(wl, true);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300161 if (ret < 0)
162 return ret;
163
164 wl->psm = 1;
165 break;
166 case STATION_ACTIVE_MODE:
167 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300168 wl1251_debug(DEBUG_PSM, "leaving psm");
169 ret = wl1251_ps_set_elp(wl, false);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300170 if (ret < 0)
171 return ret;
172
Juuso Oikarinen6b21a2c2009-11-17 18:48:30 +0200173 /* disable beacon filtering */
174 ret = wl1251_acx_beacon_filter_opt(wl, false);
175 if (ret < 0)
176 return ret;
177
Kalle Valo4a818922009-08-07 13:34:56 +0300178 ret = wl1251_acx_wake_up_conditions(wl,
179 WAKE_UP_EVENT_DTIM_BITMAP,
180 wl->listen_int);
181 if (ret < 0)
182 return ret;
183
Kalle Valo80301cd2009-06-12 14:17:39 +0300184 ret = wl1251_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300185 if (ret < 0)
186 return ret;
187
188 wl->psm = 0;
189 break;
190 }
191
192 return ret;
193}
194