wl1271: wait for disconnect command complete event
[safe/jmp/linux-2.6] / 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_io.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 (test_bit(WL1271_FLAG_IN_ELP, &wl->flags) ||
43             !test_bit(WL1271_FLAG_PSM, &wl->flags))
44                 goto out;
45
46         wl1271_debug(DEBUG_PSM, "chip to elp");
47         wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
48         set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
49
50 out:
51         mutex_unlock(&wl->mutex);
52 }
53
54 #define ELP_ENTRY_DELAY  5
55
56 /* Routines to toggle sleep mode while in ELP */
57 void wl1271_ps_elp_sleep(struct wl1271 *wl)
58 {
59         if (test_bit(WL1271_FLAG_PSM, &wl->flags)) {
60                 cancel_delayed_work(&wl->elp_work);
61                 ieee80211_queue_delayed_work(wl->hw, &wl->elp_work,
62                                         msecs_to_jiffies(ELP_ENTRY_DELAY));
63         }
64 }
65
66 int wl1271_ps_elp_wakeup(struct wl1271 *wl, bool chip_awake)
67 {
68         DECLARE_COMPLETION_ONSTACK(compl);
69         unsigned long flags;
70         int ret;
71         u32 start_time = jiffies;
72         bool pending = false;
73
74         if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
75                 return 0;
76
77         wl1271_debug(DEBUG_PSM, "waking up chip from elp");
78
79         /*
80          * The spinlock is required here to synchronize both the work and
81          * the completion variable in one entity.
82          */
83         spin_lock_irqsave(&wl->wl_lock, flags);
84         if (work_pending(&wl->irq_work) || chip_awake)
85                 pending = true;
86         else
87                 wl->elp_compl = &compl;
88         spin_unlock_irqrestore(&wl->wl_lock, flags);
89
90         wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
91
92         if (!pending) {
93                 ret = wait_for_completion_timeout(
94                         &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
95                 if (ret == 0) {
96                         wl1271_error("ELP wakeup timeout!");
97                         ret = -ETIMEDOUT;
98                         goto err;
99                 } else if (ret < 0) {
100                         wl1271_error("ELP wakeup completion error.");
101                         goto err;
102                 }
103         }
104
105         clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
106
107         wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
108                      jiffies_to_msecs(jiffies - start_time));
109         goto out;
110
111 err:
112         spin_lock_irqsave(&wl->wl_lock, flags);
113         wl->elp_compl = NULL;
114         spin_unlock_irqrestore(&wl->wl_lock, flags);
115         return ret;
116
117 out:
118         return 0;
119 }
120
121 int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode,
122                        bool send)
123 {
124         int ret;
125
126         switch (mode) {
127         case STATION_POWER_SAVE_MODE:
128                 wl1271_debug(DEBUG_PSM, "entering psm");
129
130                 ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE, send);
131                 if (ret < 0)
132                         return ret;
133
134                 set_bit(WL1271_FLAG_PSM, &wl->flags);
135                 break;
136         case STATION_ACTIVE_MODE:
137         default:
138                 wl1271_debug(DEBUG_PSM, "leaving psm");
139                 ret = wl1271_ps_elp_wakeup(wl, false);
140                 if (ret < 0)
141                         return ret;
142
143                 /* disable beacon early termination */
144                 ret = wl1271_acx_bet_enable(wl, false);
145                 if (ret < 0)
146                         return ret;
147
148                 /* disable beacon filtering */
149                 ret = wl1271_acx_beacon_filter_opt(wl, false);
150                 if (ret < 0)
151                         return ret;
152
153                 ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE, send);
154                 if (ret < 0)
155                         return ret;
156
157                 clear_bit(WL1271_FLAG_PSM, &wl->flags);
158                 break;
159         }
160
161         return ret;
162 }
163
164