ps.c 4.1 KB
Newer Older
K
Kalle Valo 已提交
1
/*
2
 * This file is part of wl1251
K
Kalle Valo 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Copyright (C) 2008 Nokia Corporation
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

K
Kalle Valo 已提交
22 23 24 25
#include "reg.h"
#include "ps.h"
#include "cmd.h"
#include "io.h"
K
Kalle Valo 已提交
26

K
Kalle Valo 已提交
27 28
/* in ms */
#define WL1251_WAKEUP_TIMEOUT 100
K
Kalle Valo 已提交
29

30
void wl1251_elp_work(struct work_struct *work)
K
Kalle Valo 已提交
31
{
32 33 34
	struct delayed_work *dwork;
	struct wl1251 *wl;

G
Geliang Tang 已提交
35
	dwork = to_delayed_work(work);
36 37 38 39 40 41
	wl = container_of(dwork, struct wl1251, elp_work);

	wl1251_debug(DEBUG_PSM, "elp work");

	mutex_lock(&wl->mutex);

42
	if (wl->elp || wl->station_mode == STATION_ACTIVE_MODE)
43
		goto out;
K
Kalle Valo 已提交
44

45
	wl1251_debug(DEBUG_PSM, "chip to elp");
46
	wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
K
Kalle Valo 已提交
47
	wl->elp = true;
48 49 50 51 52 53 54 55 56 57 58 59

out:
	mutex_unlock(&wl->mutex);
}

#define ELP_ENTRY_DELAY  5

/* Routines to toggle sleep mode while in ELP */
void wl1251_ps_elp_sleep(struct wl1251 *wl)
{
	unsigned long delay;

60
	if (wl->station_mode != STATION_ACTIVE_MODE) {
61 62 63
		delay = msecs_to_jiffies(ELP_ENTRY_DELAY);
		ieee80211_queue_delayed_work(wl->hw, &wl->elp_work, delay);
	}
K
Kalle Valo 已提交
64 65
}

66
int wl1251_ps_elp_wakeup(struct wl1251 *wl)
K
Kalle Valo 已提交
67
{
68
	unsigned long timeout, start;
K
Kalle Valo 已提交
69 70
	u32 elp_reg;

71
	cancel_delayed_work(&wl->elp_work);
72

K
Kalle Valo 已提交
73 74 75
	if (!wl->elp)
		return 0;

76
	wl1251_debug(DEBUG_PSM, "waking up chip from elp");
K
Kalle Valo 已提交
77

78
	start = jiffies;
79
	timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
K
Kalle Valo 已提交
80

81
	wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
K
Kalle Valo 已提交
82

83
	elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
K
Kalle Valo 已提交
84 85 86 87 88 89 90

	/*
	 * FIXME: we should wait for irq from chip but, as a temporary
	 * solution to simplify locking, let's poll instead
	 */
	while (!(elp_reg & ELPCTRL_WLAN_READY)) {
		if (time_after(jiffies, timeout)) {
91
			wl1251_error("elp wakeup timeout");
K
Kalle Valo 已提交
92 93 94
			return -ETIMEDOUT;
		}
		msleep(1);
95
		elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
K
Kalle Valo 已提交
96 97
	}

98
	wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
99
		     jiffies_to_msecs(jiffies - start));
K
Kalle Valo 已提交
100 101 102 103 104 105

	wl->elp = false;

	return 0;
}

106
int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_station_mode mode)
K
Kalle Valo 已提交
107 108 109 110 111
{
	int ret;

	switch (mode) {
	case STATION_POWER_SAVE_MODE:
112
		wl1251_debug(DEBUG_PSM, "entering psm");
113

114 115 116 117 118
		/* enable beacon filtering */
		ret = wl1251_acx_beacon_filter_opt(wl, true);
		if (ret < 0)
			return ret;

119 120 121 122 123 124
		ret = wl1251_acx_wake_up_conditions(wl,
						    WAKE_UP_EVENT_DTIM_BITMAP,
						    wl->listen_int);
		if (ret < 0)
			return ret;

125 126 127 128 129
		ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_ENABLE,
					    WL1251_DEFAULT_BET_CONSECUTIVE);
		if (ret < 0)
			return ret;

130
		ret = wl1251_cmd_ps_mode(wl, CHIP_POWER_SAVE_MODE);
K
Kalle Valo 已提交
131 132 133
		if (ret < 0)
			return ret;

134
		ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
K
Kalle Valo 已提交
135 136 137
		if (ret < 0)
			return ret;
		break;
138 139 140 141 142 143 144 145 146 147 148
	case STATION_IDLE:
		wl1251_debug(DEBUG_PSM, "entering idle");

		ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
		if (ret < 0)
			return ret;

		ret = wl1251_cmd_template_set(wl, CMD_DISCONNECT, NULL, 0);
		if (ret < 0)
			return ret;
		break;
K
Kalle Valo 已提交
149 150
	case STATION_ACTIVE_MODE:
	default:
151
		wl1251_debug(DEBUG_PSM, "leaving psm");
152 153

		ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
K
Kalle Valo 已提交
154 155 156
		if (ret < 0)
			return ret;

157 158 159 160 161 162
		/* disable BET */
		ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_DISABLE,
					    WL1251_DEFAULT_BET_CONSECUTIVE);
		if (ret < 0)
			return ret;

163 164 165 166 167
		/* disable beacon filtering */
		ret = wl1251_acx_beacon_filter_opt(wl, false);
		if (ret < 0)
			return ret;

168 169 170 171 172 173
		ret = wl1251_acx_wake_up_conditions(wl,
						    WAKE_UP_EVENT_DTIM_BITMAP,
						    wl->listen_int);
		if (ret < 0)
			return ret;

174
		ret = wl1251_cmd_ps_mode(wl, CHIP_ACTIVE_MODE);
K
Kalle Valo 已提交
175 176 177 178 179
		if (ret < 0)
			return ret;

		break;
	}
180
	wl->station_mode = mode;
K
Kalle Valo 已提交
181 182 183 184

	return ret;
}