5580e53d103a3b71088632aa67b0170005d4e29f
[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         /*
43          * FIXME: below, by means of the "true", ELP has been disabled for now
44          * to work around a firmware bug. To be enabled upon receiving a new
45          * firmware version.
46          */
47         if (true || wl->elp || !wl->psm)
48                 goto out;
49
50         wl1271_debug(DEBUG_PSM, "chip to elp");
51         wl1271_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
52         wl->elp = true;
53
54 out:
55         mutex_unlock(&wl->mutex);
56 }
57
58 #define ELP_ENTRY_DELAY  5
59
60 /* Routines to toggle sleep mode while in ELP */
61 void wl1271_ps_elp_sleep(struct wl1271 *wl)
62 {
63         if (wl->psm) {
64                 cancel_delayed_work(&wl->elp_work);
65                 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
66                                         msecs_to_jiffies(ELP_ENTRY_DELAY));
67         }
68 }
69
70 int wl1271_ps_elp_wakeup(struct wl1271 *wl, bool chip_awake)
71 {
72         DECLARE_COMPLETION_ONSTACK(compl);
73         unsigned long flags;
74         int ret;
75         u32 start_time = jiffies;
76         bool pending = false;
77
78         if (!wl->elp)
79                 return 0;
80
81         wl1271_debug(DEBUG_PSM, "waking up chip from elp");
82
83         /*
84          * The spinlock is required here to synchronize both the work and
85          * the completion variable in one entity.
86          */
87         spin_lock_irqsave(&wl->wl_lock, flags);
88         if (work_pending(&wl->irq_work) || chip_awake)
89                 pending = true;
90         else
91                 wl->elp_compl = &compl;
92         spin_unlock_irqrestore(&wl->wl_lock, flags);
93
94         wl1271_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
95
96         if (!pending) {
97                 ret = wait_for_completion_timeout(
98                         &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
99                 if (ret == 0) {
100                         wl1271_error("ELP wakeup timeout!");
101                         ret = -ETIMEDOUT;
102                         goto err;
103                 } else if (ret < 0) {
104                         wl1271_error("ELP wakeup completion error.");
105                         goto err;
106                 }
107         }
108
109         wl->elp = false;
110
111         wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
112                      jiffies_to_msecs(jiffies - start_time));
113         goto out;
114
115 err:
116         spin_lock_irqsave(&wl->wl_lock, flags);
117         wl->elp_compl = NULL;
118         spin_unlock_irqrestore(&wl->wl_lock, flags);
119         return ret;
120
121 out:
122         return 0;
123 }
124
125 int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode)
126 {
127         int ret;
128
129         switch (mode) {
130         case STATION_POWER_SAVE_MODE:
131                 wl1271_debug(DEBUG_PSM, "entering psm");
132
133                 /* enable beacon filtering */
134                 ret = wl1271_acx_beacon_filter_opt(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 filtering */
156                 ret = wl1271_acx_beacon_filter_opt(wl, false);
157                 if (ret < 0)
158                         return ret;
159
160                 ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
161                 if (ret < 0)
162                         return ret;
163
164                 wl->psm = 0;
165                 break;
166         }
167
168         return ret;
169 }
170
171