]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/brcm80211/sys/wlc_event.c
Staging: Add initial release of brcm80211 - Broadcom 802.11n wireless LAN driver.
[net-next-2.6.git] / drivers / staging / brcm80211 / sys / wlc_event.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <typedefs.h>
18 #include <bcmutils.h>
19 #include <siutils.h>
20 #include <bcmendian.h>
21 #include <wlioctl.h>
22 #include <wl_dbg.h>
23 #include <wlc_cfg.h>
24 #include <wlc_pub.h>
25 #include <wlc_key.h>
26 #include <wl_export.h>
27 #include <wlc_event.h>
28
29 #include <d11.h>
30 #include <wlc_rate.h>
31 #include <wlc_mac80211.h>
32 #ifdef MSGTRACE
33 #include <msgtrace.h>
34 #endif
35
36 /* Local prototypes */
37 static void wlc_timer_cb(void *arg);
38
39 /* Private data structures */
40 struct wlc_eventq {
41         wlc_event_t *head;
42         wlc_event_t *tail;
43         struct wlc_info *wlc;
44         void *wl;
45         wlc_pub_t *pub;
46         bool tpending;
47         bool workpending;
48         struct wl_timer *timer;
49         wlc_eventq_cb_t cb;
50         uint8 event_inds_mask[ROUNDUP(WLC_E_LAST, NBBY) / NBBY];
51 };
52
53 /*
54  * Export functions
55  */
56 wlc_eventq_t *BCMATTACHFN(wlc_eventq_attach) (wlc_pub_t * pub,
57                                               struct wlc_info * wlc, void *wl,
58                                               wlc_eventq_cb_t cb) {
59         wlc_eventq_t *eq;
60
61         eq = (wlc_eventq_t *) MALLOC(pub->osh, sizeof(wlc_eventq_t));
62         if (eq == NULL)
63                 return NULL;
64
65         bzero(eq, sizeof(wlc_eventq_t));
66
67         eq->cb = cb;
68         eq->wlc = wlc;
69         eq->wl = wl;
70         eq->pub = pub;
71
72         if (!(eq->timer = wl_init_timer(eq->wl, wlc_timer_cb, eq, "eventq"))) {
73                 WL_ERROR(("wl%d: wlc_eventq_attach: timer failed\n",
74                           pub->unit));
75                 MFREE(eq->pub->osh, eq, sizeof(wlc_eventq_t));
76                 return NULL;
77         }
78
79         return eq;
80 }
81
82 int BCMATTACHFN(wlc_eventq_detach) (wlc_eventq_t * eq) {
83         /* Clean up pending events */
84         wlc_eventq_down(eq);
85
86         if (eq->timer) {
87                 if (eq->tpending) {
88                         wl_del_timer(eq->wl, eq->timer);
89                         eq->tpending = FALSE;
90                 }
91                 wl_free_timer(eq->wl, eq->timer);
92                 eq->timer = NULL;
93         }
94
95         ASSERT(wlc_eventq_avail(eq) == FALSE);
96         MFREE(eq->pub->osh, eq, sizeof(wlc_eventq_t));
97         return 0;
98 }
99
100 int BCMUNINITFN(wlc_eventq_down) (wlc_eventq_t * eq) {
101         int callbacks = 0;
102         if (eq->tpending && !eq->workpending) {
103                 if (!wl_del_timer(eq->wl, eq->timer))
104                         callbacks++;
105
106                 ASSERT(wlc_eventq_avail(eq) == TRUE);
107                 ASSERT(eq->workpending == FALSE);
108                 eq->workpending = TRUE;
109                 if (eq->cb)
110                         eq->cb(eq->wlc);
111
112                 ASSERT(eq->workpending == TRUE);
113                 eq->workpending = FALSE;
114                 eq->tpending = FALSE;
115         } else {
116                 ASSERT(eq->workpending || wlc_eventq_avail(eq) == FALSE);
117         }
118         return callbacks;
119 }
120
121 wlc_event_t *wlc_event_alloc(wlc_eventq_t * eq)
122 {
123         wlc_event_t *e;
124
125         e = MALLOC(eq->pub->osh, sizeof(wlc_event_t));
126
127         if (e == NULL)
128                 return NULL;
129
130         bzero(e, sizeof(wlc_event_t));
131         return e;
132 }
133
134 void wlc_event_free(wlc_eventq_t * eq, wlc_event_t * e)
135 {
136         ASSERT(e->data == NULL);
137         ASSERT(e->next == NULL);
138         MFREE(eq->pub->osh, e, sizeof(wlc_event_t));
139 }
140
141 void wlc_eventq_enq(wlc_eventq_t * eq, wlc_event_t * e)
142 {
143         ASSERT(e->next == NULL);
144         e->next = NULL;
145
146         if (eq->tail) {
147                 eq->tail->next = e;
148                 eq->tail = e;
149         } else
150                 eq->head = eq->tail = e;
151
152         if (!eq->tpending) {
153                 eq->tpending = TRUE;
154                 /* Use a zero-delay timer to trigger
155                  * delayed processing of the event.
156                  */
157                 wl_add_timer(eq->wl, eq->timer, 0, 0);
158         }
159 }
160
161 wlc_event_t *wlc_eventq_deq(wlc_eventq_t * eq)
162 {
163         wlc_event_t *e;
164
165         e = eq->head;
166         if (e) {
167                 eq->head = e->next;
168                 e->next = NULL;
169
170                 if (eq->head == NULL)
171                         eq->tail = eq->head;
172         }
173         return e;
174 }
175
176 wlc_event_t *wlc_eventq_next(wlc_eventq_t * eq, wlc_event_t * e)
177 {
178 #ifdef BCMDBG
179         wlc_event_t *etmp;
180
181         for (etmp = eq->head; etmp; etmp = etmp->next) {
182                 if (etmp == e)
183                         break;
184         }
185         ASSERT(etmp != NULL);
186 #endif
187
188         return e->next;
189 }
190
191 int wlc_eventq_cnt(wlc_eventq_t * eq)
192 {
193         wlc_event_t *etmp;
194         int cnt = 0;
195
196         for (etmp = eq->head; etmp; etmp = etmp->next)
197                 cnt++;
198
199         return cnt;
200 }
201
202 bool wlc_eventq_avail(wlc_eventq_t * eq)
203 {
204         return (eq->head != NULL);
205 }
206
207 /*
208  * Local Functions
209  */
210 static void wlc_timer_cb(void *arg)
211 {
212         struct wlc_eventq *eq = (struct wlc_eventq *)arg;
213
214         ASSERT(eq->tpending == TRUE);
215         ASSERT(wlc_eventq_avail(eq) == TRUE);
216         ASSERT(eq->workpending == FALSE);
217         eq->workpending = TRUE;
218
219         if (eq->cb)
220                 eq->cb(eq->wlc);
221
222         ASSERT(wlc_eventq_avail(eq) == FALSE);
223         ASSERT(eq->tpending == TRUE);
224         eq->workpending = FALSE;
225         eq->tpending = FALSE;
226 }