]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/net/wireless/wl12xx/wl1271_ps.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs...
[mv-sheeva.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 /* Routines to toggle sleep mode while in ELP */
31 void wl1271_ps_elp_sleep(struct wl1271 *wl)
32 {
33         /*
34          * FIXME: due to a problem in the firmware (causing a firmware
35          * crash), ELP entry is prevented below. Remove the "true" to
36          * re-enable ELP entry.
37          */
38         if (true || wl->elp || !wl->psm)
39                 return;
40
41         /*
42          * Go to ELP unless there is work already pending - pending work
43          * will immediately wakeup the chipset anyway.
44          */
45         if (!work_pending(&wl->irq_work) && !work_pending(&wl->tx_work)) {
46                 wl1271_debug(DEBUG_PSM, "chip to elp");
47                 wl1271_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_SLEEP);
48                 wl->elp = true;
49         }
50 }
51
52 int wl1271_ps_elp_wakeup(struct wl1271 *wl, bool chip_awake)
53 {
54         DECLARE_COMPLETION_ONSTACK(compl);
55         unsigned long flags;
56         int ret;
57         u32 start_time = jiffies;
58         bool pending = false;
59
60         if (!wl->elp)
61                 return 0;
62
63         wl1271_debug(DEBUG_PSM, "waking up chip from elp");
64
65         /*
66          * The spinlock is required here to synchronize both the work and
67          * the completion variable in one entity.
68          */
69         spin_lock_irqsave(&wl->wl_lock, flags);
70         if (work_pending(&wl->irq_work) || chip_awake)
71                 pending = true;
72         else
73                 wl->elp_compl = &compl;
74         spin_unlock_irqrestore(&wl->wl_lock, flags);
75
76         wl1271_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, ELPCTRL_WAKE_UP);
77
78         if (!pending) {
79                 ret = wait_for_completion_timeout(
80                         &compl, msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
81                 if (ret == 0) {
82                         wl1271_error("ELP wakeup timeout!");
83                         ret = -ETIMEDOUT;
84                         goto err;
85                 } else if (ret < 0) {
86                         wl1271_error("ELP wakeup completion error.");
87                         goto err;
88                 }
89         }
90
91         wl->elp = false;
92
93         wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
94                      jiffies_to_msecs(jiffies - start_time));
95         goto out;
96
97 err:
98         spin_lock_irqsave(&wl->wl_lock, flags);
99         wl->elp_compl = NULL;
100         spin_unlock_irqrestore(&wl->wl_lock, flags);
101         return ret;
102
103 out:
104         return 0;
105 }
106
107 int wl1271_ps_set_mode(struct wl1271 *wl, enum wl1271_cmd_ps_mode mode)
108 {
109         int ret;
110
111         switch (mode) {
112         case STATION_POWER_SAVE_MODE:
113                 wl1271_debug(DEBUG_PSM, "entering psm");
114                 ret = wl1271_cmd_ps_mode(wl, STATION_POWER_SAVE_MODE);
115                 if (ret < 0)
116                         return ret;
117
118                 wl1271_ps_elp_sleep(wl);
119                 if (ret < 0)
120                         return ret;
121
122                 wl->psm = 1;
123                 break;
124         case STATION_ACTIVE_MODE:
125         default:
126                 wl1271_debug(DEBUG_PSM, "leaving psm");
127                 ret = wl1271_ps_elp_wakeup(wl, false);
128                 if (ret < 0)
129                         return ret;
130
131                 ret = wl1271_cmd_ps_mode(wl, STATION_ACTIVE_MODE);
132                 if (ret < 0)
133                         return ret;
134
135                 wl->psm = 0;
136                 break;
137         }
138
139         return ret;
140 }
141
142