wl1251: Prepare for idle mode support
[pandora-wifi.git] / drivers / net / wireless / wl12xx / wl1251_ps.c
1 /*
2  * This file is part of wl1251
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  *
6  * Contact: Kalle Valo <kalle.valo@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 "wl1251_reg.h"
25 #include "wl1251_ps.h"
26 #include "wl1251_cmd.h"
27 #include "wl1251_io.h"
28
29 /* in ms */
30 #define WL1251_WAKEUP_TIMEOUT 100
31
32 void wl1251_elp_work(struct work_struct *work)
33 {
34         struct delayed_work *dwork;
35         struct wl1251 *wl;
36
37         dwork = container_of(work, struct delayed_work, work);
38         wl = container_of(dwork, struct wl1251, elp_work);
39
40         wl1251_debug(DEBUG_PSM, "elp work");
41
42         mutex_lock(&wl->mutex);
43
44         if (wl->elp || wl->station_mode == STATION_ACTIVE_MODE)
45                 goto out;
46
47         wl1251_debug(DEBUG_PSM, "chip to elp");
48         wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
49         wl->elp = true;
50
51 out:
52         mutex_unlock(&wl->mutex);
53 }
54
55 #define ELP_ENTRY_DELAY  5
56
57 /* Routines to toggle sleep mode while in ELP */
58 void wl1251_ps_elp_sleep(struct wl1251 *wl)
59 {
60         unsigned long delay;
61
62         if (wl->station_mode != STATION_ACTIVE_MODE) {
63                 delay = msecs_to_jiffies(ELP_ENTRY_DELAY);
64                 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work, delay);
65         }
66 }
67
68 int wl1251_ps_elp_wakeup(struct wl1251 *wl)
69 {
70         DECLARE_COMPLETION_ONSTACK(compl);
71         unsigned long start;
72         u32 elp_reg;
73         int ret;
74
75         if (delayed_work_pending(&wl->elp_work))
76                 cancel_delayed_work(&wl->elp_work);
77
78         if (!wl->elp)
79                 return 0;
80
81         wl1251_debug(DEBUG_PSM, "waking up chip from elp");
82
83         start = jiffies;
84
85         elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
86
87         wl->elp_compl = &compl;
88         wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR,
89                          ELPCTRL_WAKE_UP | ELPCTRL_IRQ_SRC);
90
91         elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
92         if (!(elp_reg & ELPCTRL_WLAN_READY)) {
93                 ret = wait_for_completion_timeout(
94                         &compl, msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT));
95                 if (ret == 0) {
96                         wl1251_error("ELP wakeup timeout");
97                         ret = -ETIMEDOUT;
98                         goto err;
99                 } else if (ret < 0) {
100                         wl1251_error("ELP wakeup completion error.");
101                         goto err;
102                 }
103         }
104
105         wl->elp_compl = NULL;
106
107         /* clear IRQ_SRC to switch irq source back from WLAN_RDY to HOST_IRQ */
108         wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
109
110         wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
111                      jiffies_to_msecs(jiffies - start));
112
113         wl->elp = false;
114
115         return 0;
116
117 err:
118         wl->elp_compl = NULL;
119         return ret;
120 }
121
122 int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_station_mode mode)
123 {
124         int ret;
125
126         switch (mode) {
127         case STATION_POWER_SAVE_MODE:
128                 wl1251_debug(DEBUG_PSM, "entering psm");
129
130                 /* enable beacon filtering */
131                 ret = wl1251_acx_beacon_filter_opt(wl, true);
132                 if (ret < 0)
133                         return ret;
134
135                 ret = wl1251_acx_wake_up_conditions(wl,
136                                                     WAKE_UP_EVENT_DTIM_BITMAP,
137                                                     wl->listen_int);
138                 if (ret < 0)
139                         return ret;
140
141                 ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_ENABLE,
142                                             WL1251_DEFAULT_BET_CONSECUTIVE);
143                 if (ret < 0)
144                         return ret;
145
146                 ret = wl1251_cmd_ps_mode(wl, CHIP_POWER_SAVE_MODE);
147                 if (ret < 0)
148                         return ret;
149
150                 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
151                 if (ret < 0)
152                         return ret;
153                 break;
154         case STATION_ACTIVE_MODE:
155         default:
156                 wl1251_debug(DEBUG_PSM, "leaving psm");
157
158                 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
159                 if (ret < 0)
160                         return ret;
161
162                 /* disable BET */
163                 ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_DISABLE,
164                                             WL1251_DEFAULT_BET_CONSECUTIVE);
165                 if (ret < 0)
166                         return ret;
167
168                 /* disable beacon filtering */
169                 ret = wl1251_acx_beacon_filter_opt(wl, false);
170                 if (ret < 0)
171                         return ret;
172
173                 ret = wl1251_acx_wake_up_conditions(wl,
174                                                     WAKE_UP_EVENT_DTIM_BITMAP,
175                                                     wl->listen_int);
176                 if (ret < 0)
177                         return ret;
178
179                 ret = wl1251_cmd_ps_mode(wl, CHIP_ACTIVE_MODE);
180                 if (ret < 0)
181                         return ret;
182
183                 break;
184         }
185         wl->station_mode = mode;
186
187         return ret;
188 }
189