blob: fa01b0a0f312abd20092478e6aa768997f326d26 [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
Geliang Tangd1162f02016-03-18 13:22:03 +110035 dwork = to_delayed_work(work);
Juuso Oikarinend5da79a2009-11-17 18:48:37 +020036 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
Tejun Heo02957f92012-12-21 17:56:59 -080071 cancel_delayed_work(&wl->elp_work);
Grazvydas Ignotas5f6722e2011-03-06 19:23:37 +020072
Kalle Valo2f01a1f2009-04-29 23:33:31 +030073 if (!wl->elp)
74 return 0;
75
Kalle Valo80301cd2009-06-12 14:17:39 +030076 wl1251_debug(DEBUG_PSM, "waking up chip from elp");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030077
Kalle Valo7fa62822009-11-30 10:18:06 +020078 start = jiffies;
Kalle Valo80301cd2009-06-12 14:17:39 +030079 timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030080
Grazvydas Ignotas3f9e7502010-03-11 17:44:57 +020081 wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030082
Grazvydas Ignotas3f9e7502010-03-11 17:44:57 +020083 elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030084
85 /*
86 * FIXME: we should wait for irq from chip but, as a temporary
87 * solution to simplify locking, let's poll instead
88 */
89 while (!(elp_reg & ELPCTRL_WLAN_READY)) {
90 if (time_after(jiffies, timeout)) {
Kalle Valo80301cd2009-06-12 14:17:39 +030091 wl1251_error("elp wakeup timeout");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030092 return -ETIMEDOUT;
93 }
94 msleep(1);
Grazvydas Ignotas3f9e7502010-03-11 17:44:57 +020095 elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030096 }
97
Kalle Valo80301cd2009-06-12 14:17:39 +030098 wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
Kalle Valo7fa62822009-11-30 10:18:06 +020099 jiffies_to_msecs(jiffies - start));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300100
101 wl->elp = false;
102
103 return 0;
104}
105
Jarkko Nikulaa0bbb582011-04-04 11:04:57 +0300106int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_station_mode mode)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300107{
108 int ret;
109
110 switch (mode) {
111 case STATION_POWER_SAVE_MODE:
Kalle Valo80301cd2009-06-12 14:17:39 +0300112 wl1251_debug(DEBUG_PSM, "entering psm");
Kalle Valo4a818922009-08-07 13:34:56 +0300113
Juuso Oikarinen6b21a2c2009-11-17 18:48:30 +0200114 /* enable beacon filtering */
115 ret = wl1251_acx_beacon_filter_opt(wl, true);
116 if (ret < 0)
117 return ret;
118
Kalle Valo4a818922009-08-07 13:34:56 +0300119 ret = wl1251_acx_wake_up_conditions(wl,
120 WAKE_UP_EVENT_DTIM_BITMAP,
121 wl->listen_int);
122 if (ret < 0)
123 return ret;
124
David Gnedtc3e334d2011-01-30 20:10:57 +0100125 ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_ENABLE,
126 WL1251_DEFAULT_BET_CONSECUTIVE);
127 if (ret < 0)
128 return ret;
129
Jarkko Nikulaa0bbb582011-04-04 11:04:57 +0300130 ret = wl1251_cmd_ps_mode(wl, CHIP_POWER_SAVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300131 if (ret < 0)
132 return ret;
133
Grazvydas Ignotasd0c331a2011-03-06 19:23:36 +0200134 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300135 if (ret < 0)
136 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300137 break;
Jarkko Nikula1e5f52d2011-04-04 11:04:58 +0300138 case STATION_IDLE:
139 wl1251_debug(DEBUG_PSM, "entering idle");
140
141 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
142 if (ret < 0)
143 return ret;
144
145 ret = wl1251_cmd_template_set(wl, CMD_DISCONNECT, NULL, 0);
146 if (ret < 0)
147 return ret;
148 break;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300149 case STATION_ACTIVE_MODE:
150 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300151 wl1251_debug(DEBUG_PSM, "leaving psm");
Grazvydas Ignotasd0c331a2011-03-06 19:23:36 +0200152
153 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300154 if (ret < 0)
155 return ret;
156
David Gnedtc3e334d2011-01-30 20:10:57 +0100157 /* disable BET */
158 ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_DISABLE,
159 WL1251_DEFAULT_BET_CONSECUTIVE);
160 if (ret < 0)
161 return ret;
162
Juuso Oikarinen6b21a2c2009-11-17 18:48:30 +0200163 /* disable beacon filtering */
164 ret = wl1251_acx_beacon_filter_opt(wl, false);
165 if (ret < 0)
166 return ret;
167
Kalle Valo4a818922009-08-07 13:34:56 +0300168 ret = wl1251_acx_wake_up_conditions(wl,
169 WAKE_UP_EVENT_DTIM_BITMAP,
170 wl->listen_int);
171 if (ret < 0)
172 return ret;
173
Jarkko Nikulaa0bbb582011-04-04 11:04:57 +0300174 ret = wl1251_cmd_ps_mode(wl, CHIP_ACTIVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300175 if (ret < 0)
176 return ret;
177
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300178 break;
179 }
Jarkko Nikulaa0bbb582011-04-04 11:04:57 +0300180 wl->station_mode = mode;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300181
182 return ret;
183}
184