wl1251: Prepare for idle mode support
[pandora-wifi.git] / drivers / net / wireless / wl12xx / wl1251_event.c
1 /*
2  * This file is part of wl1251
3  *
4  * Copyright (c) 1998-2007 Texas Instruments Incorporated
5  * Copyright (C) 2008 Nokia Corporation
6  *
7  * Contact: Kalle Valo <kalle.valo@nokia.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include "wl1251.h"
26 #include "wl1251_reg.h"
27 #include "wl1251_io.h"
28 #include "wl1251_event.h"
29 #include "wl1251_ps.h"
30
31 static int wl1251_event_scan_complete(struct wl1251 *wl,
32                                       struct event_mailbox *mbox)
33 {
34         wl1251_debug(DEBUG_EVENT, "status: 0x%x, channels: %d",
35                      mbox->scheduled_scan_status,
36                      mbox->scheduled_scan_channels);
37
38         if (wl->scanning) {
39                 mutex_unlock(&wl->mutex);
40                 ieee80211_scan_completed(wl->hw, false);
41                 mutex_lock(&wl->mutex);
42                 wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan completed");
43                 wl->scanning = false;
44         }
45
46         return 0;
47 }
48
49 static void wl1251_event_mbox_dump(struct event_mailbox *mbox)
50 {
51         wl1251_debug(DEBUG_EVENT, "MBOX DUMP:");
52         wl1251_debug(DEBUG_EVENT, "\tvector: 0x%x", mbox->events_vector);
53         wl1251_debug(DEBUG_EVENT, "\tmask: 0x%x", mbox->events_mask);
54 }
55
56 static int wl1251_event_process(struct wl1251 *wl, struct event_mailbox *mbox)
57 {
58         int ret;
59         u32 vector;
60
61         wl1251_event_mbox_dump(mbox);
62
63         vector = mbox->events_vector & ~(mbox->events_mask);
64         wl1251_debug(DEBUG_EVENT, "vector: 0x%x", vector);
65
66         if (vector & SCAN_COMPLETE_EVENT_ID) {
67                 ret = wl1251_event_scan_complete(wl, mbox);
68                 if (ret < 0)
69                         return ret;
70         }
71
72         if (vector & BSS_LOSE_EVENT_ID) {
73                 wl1251_debug(DEBUG_EVENT, "BSS_LOSE_EVENT");
74
75                 if (wl->psm_requested &&
76                     wl->station_mode != STATION_ACTIVE_MODE) {
77                         ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
78                         if (ret < 0)
79                                 return ret;
80                 }
81         }
82
83         if (vector & SYNCHRONIZATION_TIMEOUT_EVENT_ID &&
84             wl->station_mode != STATION_ACTIVE_MODE) {
85                 wl1251_debug(DEBUG_EVENT, "SYNCHRONIZATION_TIMEOUT_EVENT");
86                 /* need to unlock mutex to avoid deadlocking with rtnl */
87                 mutex_unlock(&wl->mutex);
88
89                 /* indicate to the stack, that beacons have been lost */
90                 ieee80211_beacon_loss(wl->vif);
91                 mutex_lock(&wl->mutex);
92         }
93
94         if (vector & REGAINED_BSS_EVENT_ID) {
95                 if (wl->psm_requested) {
96                         ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
97                         if (ret < 0)
98                                 return ret;
99                 }
100         }
101
102         /* log rare events to help debugging some issues */
103         if (vector & ~(SCAN_COMPLETE_EVENT_ID | PS_REPORT_EVENT_ID |
104                         SYNCHRONIZATION_TIMEOUT_EVENT_ID | JOIN_EVENT_COMPLETE_ID |
105                         BSS_LOSE_EVENT_ID | RESET_BSS_EVENT_ID |
106                         BT_PTA_PREDICTION_EVENT_ID))
107                 wl1251_info("rare event: %08x", vector);
108
109         return 0;
110 }
111
112 /*
113  * Poll the mailbox event field until any of the bits in the mask is set or a
114  * timeout occurs (WL1251_EVENT_TIMEOUT in msecs)
115  */
116 int wl1251_event_wait(struct wl1251 *wl, u32 mask, int timeout_ms)
117 {
118         u32 events_vector, event;
119         unsigned long timeout;
120
121         timeout = jiffies + msecs_to_jiffies(timeout_ms);
122
123         do {
124                 if (time_after(jiffies, timeout))
125                         return -ETIMEDOUT;
126
127                 msleep(1);
128
129                 /* read from both event fields */
130                 wl1251_mem_read(wl, wl->mbox_ptr[0], &events_vector,
131                                 sizeof(events_vector));
132                 event = events_vector & mask;
133                 wl1251_mem_read(wl, wl->mbox_ptr[1], &events_vector,
134                                 sizeof(events_vector));
135                 event |= events_vector & mask;
136         } while (!event);
137
138         return 0;
139 }
140
141 int wl1251_event_unmask(struct wl1251 *wl)
142 {
143         int ret;
144
145         ret = wl1251_acx_event_mbox_mask(wl, ~(wl->event_mask));
146         if (ret < 0)
147                 return ret;
148
149         return 0;
150 }
151
152 void wl1251_event_mbox_config(struct wl1251 *wl)
153 {
154         wl->mbox_ptr[0] = wl1251_reg_read32(wl, REG_EVENT_MAILBOX_PTR);
155         wl->mbox_ptr[1] = wl->mbox_ptr[0] + sizeof(struct event_mailbox);
156
157         wl1251_debug(DEBUG_EVENT, "MBOX ptrs: 0x%x 0x%x",
158                      wl->mbox_ptr[0], wl->mbox_ptr[1]);
159 }
160
161 int wl1251_event_handle(struct wl1251 *wl, u8 mbox_num)
162 {
163         struct event_mailbox mbox;
164         int ret;
165
166         wl1251_debug(DEBUG_EVENT, "EVENT on mbox %d", mbox_num);
167
168         if (mbox_num > 1)
169                 return -EINVAL;
170
171         /* first we read the mbox descriptor */
172         wl1251_mem_read(wl, wl->mbox_ptr[mbox_num], &mbox,
173                             sizeof(struct event_mailbox));
174
175         /* process the descriptor */
176         ret = wl1251_event_process(wl, &mbox);
177         if (ret < 0)
178                 return ret;
179
180         /* then we let the firmware know it can go on...*/
181         wl1251_reg_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_EVENT_ACK);
182
183         return 0;
184 }