wl1271: Implement beacon early termination support
[linux-2.6.git] / drivers / net / wireless / wl12xx / wl1271_ps.c
1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 2008-2009 Nokia Corporation
5  *
6  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7  *
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.
11  *
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.
16  *
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
21  *
22  */
23
24 #include "wl1271_reg.h"
25 #include "wl1271_ps.h"
26 #include "wl1271_spi.h"
27
28 #define WL1271_WAKEUP_TIMEOUT 500
29
30 void wl1271_elp_work(struct work_struct *work)
31 {
32         struct delayed_work *dwork;
33         struct wl1271 *wl;
34
35         dwork = container_of(work, struct delayed_work, work);
36         wl = container_of(dwork, struct wl1271, elp_work);
37
38         wl1271_debug(DEBUG_PSM, "elp work");
39
40         mutex_lock(&wl->mutex);
41
42         if (wl->elp || !wl->psm)
43                 goto out;
44
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;
48
49 out:
50         mutex_unlock(&wl->mutex);
51 }
52
53 #define ELP_ENTRY_DELAY  5
54
55 /* Routines to toggle sleep mode while in ELP */
56 void wl1271_ps_elp_sleep(struct wl1271 *wl)
57 {
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));
62         }
63 }
64
65 int wl1271_ps_elp_wakeup(struct wl1271 *wl, bool chip_awake)
66 {
67         DECLARE_COMPLETION_ONSTACK(compl);
68         unsigned long flags;
69         int ret;
70         u32 start_time = jiffies;
71         bool pending = false;
72
73         if (!wl->elp)
74                 return 0;
75
76         wl1271_debug(DEBUG_PSM, "waking up chip from elp");
77
78         /*
79          * The spinlock is required here to synchronize both the work and
80          * the completion variable in one entity.
81          */
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);
88
89         wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
90
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;
101                 }
102         }
103
104         wl->elp = false;
105
106         wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
107                      jiffies_to_msecs(jiffies - start_time));
108         goto out;
109
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;
115
116 out:
117         return 0;
118 }
119
120 int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode)
121 {
122         int ret;
123
124         switch (mode) {
125         case STATION_POWER_SAVE_MODE:
126                 wl1271_debug(DEBUG_PSM, "entering psm");
127
128                 /* enable beacon filtering */
129                 ret = wl1271_acx_beacon_filter_opt(wl, true);
130                 if (ret < 0)
131                         return ret;
132
133                 /* enable beacon early termination */
134                 ret = wl1271_acx_bet_enable(wl, true);
135                 if (ret < 0)
136                         return ret;
137
138                 ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
139                 if (ret < 0)
140                         return ret;
141
142                 wl1271_ps_elp_sleep(wl);
143                 if (ret < 0)
144                         return ret;
145
146                 wl->psm = 1;
147                 break;
148         case STATION_ACTIVE_MODE:
149         default:
150                 wl1271_debug(DEBUG_PSM, "leaving psm");
151                 ret = wl1271_ps_elp_wakeup(wl, false);
152                 if (ret < 0)
153                         return ret;
154
155                 /* disable beacon early termination */
156                 ret = wl1271_acx_bet_enable(wl, false);
157                 if (ret < 0)
158                         return ret;
159
160                 /* disable beacon filtering */
161                 ret = wl1271_acx_beacon_filter_opt(wl, false);
162                 if (ret < 0)
163                         return ret;
164
165                 ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
166                 if (ret < 0)
167                         return ret;
168
169                 wl->psm = 0;
170                 break;
171         }
172
173         return ret;
174 }
175
176