f5b90a1f87d849ad372ac0b7780ce65b3573d767
[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->psm)
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->psm) {
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         unsigned long timeout, start;
71         u32 elp_reg;
72
73         if (delayed_work_pending(&wl->elp_work))
74                 cancel_delayed_work(&wl->elp_work);
75
76         if (!wl->elp)
77                 return 0;
78
79         wl1251_debug(DEBUG_PSM, "waking up chip from elp");
80
81         start = jiffies;
82         timeout = jiffies + msecs_to_jiffies(WL1251_WAKEUP_TIMEOUT);
83
84         wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
85
86         elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
87
88         /*
89          * FIXME: we should wait for irq from chip but, as a temporary
90          * solution to simplify locking, let's poll instead
91          */
92         while (!(elp_reg & ELPCTRL_WLAN_READY)) {
93                 if (time_after(jiffies, timeout)) {
94                         wl1251_error("elp wakeup timeout");
95                         return -ETIMEDOUT;
96                 }
97                 msleep(1);
98                 elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
99         }
100
101         wl1251_debug(DEBUG_PSM, "wakeup time: %u ms",
102                      jiffies_to_msecs(jiffies - start));
103
104         wl->elp = false;
105
106         return 0;
107 }
108
109 int wl1251_ps_set_mode(struct wl1251 *wl, enum wl1251_cmd_ps_mode mode)
110 {
111         int ret;
112
113         switch (mode) {
114         case STATION_POWER_SAVE_MODE:
115                 wl1251_debug(DEBUG_PSM, "entering psm");
116
117                 /* enable beacon filtering */
118                 ret = wl1251_acx_beacon_filter_opt(wl, true);
119                 if (ret < 0)
120                         return ret;
121
122                 ret = wl1251_acx_wake_up_conditions(wl,
123                                                     WAKE_UP_EVENT_DTIM_BITMAP,
124                                                     wl->listen_int);
125                 if (ret < 0)
126                         return ret;
127
128                 ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_ENABLE,
129                                             WL1251_DEFAULT_BET_CONSECUTIVE);
130                 if (ret < 0)
131                         return ret;
132
133                 ret = wl1251_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
134                 if (ret < 0)
135                         return ret;
136
137                 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_ELP);
138                 if (ret < 0)
139                         return ret;
140
141                 wl->psm = 1;
142                 break;
143         case STATION_ACTIVE_MODE:
144         default:
145                 wl1251_debug(DEBUG_PSM, "leaving psm");
146
147                 ret = wl1251_acx_sleep_auth(wl, WL1251_PSM_CAM);
148                 if (ret < 0)
149                         return ret;
150
151                 /* disable BET */
152                 ret = wl1251_acx_bet_enable(wl, WL1251_ACX_BET_DISABLE,
153                                             WL1251_DEFAULT_BET_CONSECUTIVE);
154                 if (ret < 0)
155                         return ret;
156
157                 /* disable beacon filtering */
158                 ret = wl1251_acx_beacon_filter_opt(wl, false);
159                 if (ret < 0)
160                         return ret;
161
162                 ret = wl1251_acx_wake_up_conditions(wl,
163                                                     WAKE_UP_EVENT_DTIM_BITMAP,
164                                                     wl->listen_int);
165                 if (ret < 0)
166                         return ret;
167
168                 ret = wl1251_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
169                 if (ret < 0)
170                         return ret;
171
172                 wl->psm = 0;
173                 break;
174         }
175
176         return ret;
177 }
178