On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / drivers / net / wireless / wl12xx / wl1271_ps.c
blobbb8745d9bd64764b066d65ca72eb0f740c12e3f4
1 /*
2 * This file is part of wl1271
4 * Copyright (C) 2008-2009 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
24 #include "wl1271_reg.h"
25 #include "wl1271_ps.h"
26 #include "wl1271_spi.h"
28 #define WL1271_WAKEUP_TIMEOUT 500
30 void wl1271_elp_work(struct work_struct *work)
32 struct delayed_work *dwork;
33 struct wl1271 *wl;
35 dwork = container_of(work, struct delayed_work, work);
36 wl = container_of(dwork, struct wl1271, elp_work);
38 wl1271_debug(DEBUG_PSM, "elp work");
40 mutex_lock(&wl->mutex);
42 if (wl->elp || !wl->psm)
43 goto out;
45 wl1271_debug(DEBUG_PSM, "chip to elp");
46 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
47 wl->elp = true;
49 out:
50 mutex_unlock(&wl->mutex);
53 #define ELP_ENTRY_DELAY 5
55 /* Routines to toggle sleep mode while in ELP */
56 void wl1271_ps_elp_sleep(struct wl1271 *wl)
58 if (wl->psm) {
59 cancel_delayed_work(&wl->elp_work);
60 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
61 msecs_to_jiffies(ELP_ENTRY_DELAY));
65 int wl1271_ps_elp_wakeup(struct wl1271 *wl, bool chip_awake)
67 DECLARE_COMPLETION_ONSTACK(compl);
68 unsigned long flags;
69 int ret;
70 u32 start_time = jiffies;
71 bool pending = false;
73 if (!wl->elp)
74 return 0;
76 wl1271_debug(DEBUG_PSM, "waking up chip from elp");
79 * The spinlock is required here to synchronize both the work and
80 * the completion variable in one entity.
82 spin_lock_irqsave(&wl->wl_lock, flags);
83 if (work_pending(&wl->irq_work) || chip_awake)
84 pending = true;
85 else
86 wl->elp_compl = &compl;
87 spin_unlock_irqrestore(&wl->wl_lock, flags);
89 wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
91 if (!pending) {
92 ret = wait_for_completion_timeout(
93 &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
94 if (ret == 0) {
95 wl1271_error("ELP wakeup timeout!");
96 ret = -ETIMEDOUT;
97 goto err;
98 } else if (ret < 0) {
99 wl1271_error("ELP wakeup completion error.");
100 goto err;
104 wl->elp = false;
106 wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
107 jiffies_to_msecs(jiffies - start_time));
108 goto out;
110 err:
111 spin_lock_irqsave(&wl->wl_lock, flags);
112 wl->elp_compl = NULL;
113 spin_unlock_irqrestore(&wl->wl_lock, flags);
114 return ret;
116 out:
117 return 0;
120 int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode)
122 int ret;
124 switch (mode) {
125 case STATION_POWER_SAVE_MODE:
126 wl1271_debug(DEBUG_PSM, "entering psm");
128 /* enable beacon filtering */
129 ret = wl1271_acx_beacon_filter_opt(wl, true);
130 if (ret < 0)
131 return ret;
133 ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
134 if (ret < 0)
135 return ret;
137 wl1271_ps_elp_sleep(wl);
138 if (ret < 0)
139 return ret;
141 wl->psm = 1;
142 break;
143 case STATION_ACTIVE_MODE:
144 default:
145 wl1271_debug(DEBUG_PSM, "leaving psm");
146 ret = wl1271_ps_elp_wakeup(wl, false);
147 if (ret < 0)
148 return ret;
150 /* disable beacon filtering */
151 ret = wl1271_acx_beacon_filter_opt(wl, false);
152 if (ret < 0)
153 return ret;
155 ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
156 if (ret < 0)
157 return ret;
159 wl->psm = 0;
160 break;
163 return ret;