f34fa2ef666b5fa56247f060bae06a2d94f7f06e
[safe/jmp/linux-2.6] / net / ieee80211 / softmac / ieee80211softmac_event.c
1 /*
2  * Event system
3  * Also see comments in public header file and longer explanation below.
4  *
5  * Copyright (c) 2005, 2006 Johannes Berg <johannes@sipsolutions.net>
6  *                          Joseph Jezak <josejx@gentoo.org>
7  *                          Larry Finger <Larry.Finger@lwfinger.net>
8  *                          Danny van Dyk <kugelfang@gentoo.org>
9  *                          Michael Buesch <mbuesch@freenet.de>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of version 2 of the GNU General Public License as
13  * published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18  * more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
23  *
24  * The full GNU General Public License is included in this distribution in the
25  * file called COPYING.
26  */
27
28 #include "ieee80211softmac_priv.h"
29
30 /*
31  * Each event has associated to it
32  *  - an event type (see constants in public header)
33  *  - an event context (see below)
34  *  - the function to be called
35  *  - a context (extra parameter to call the function with)
36  *  - and the softmac struct
37  *
38  * The event context is private and can only be used from
39  * within this module. Its meaning varies with the event
40  * type:
41  *  SCAN_FINISHED,
42  *  DISASSOCIATED:      NULL
43  *  ASSOCIATED,
44  *  ASSOCIATE_FAILED,
45  *  ASSOCIATE_TIMEOUT,
46  *  AUTHENTICATED,
47  *  AUTH_FAILED,
48  *  AUTH_TIMEOUT:       a pointer to the network struct
49  * ...
50  * Code within this module can use the event context to be only
51  * called when the event is true for that specific context
52  * as per above table.
53  * If the event context is NULL, then the notification is always called,
54  * regardless of the event context. The event context is not passed to
55  * the callback, it is assumed that the context suffices.
56  *
57  * You can also use the event context only by setting the event type
58  * to -1 (private use only), in which case you'll be notified
59  * whenever the event context matches.
60  */
61
62 static char *event_descriptions[IEEE80211SOFTMAC_EVENT_LAST+1] = {
63         NULL, /* scan finished */
64         NULL, /* associated */
65         "associating failed",
66         "associating timed out",
67         "authenticated",
68         "authenticating failed",
69         "authenticating timed out",
70         "associating failed because no suitable network was found",
71         NULL, /* disassociated */
72 };
73
74
75 static void
76 ieee80211softmac_notify_callback(void *d)
77 {
78         struct ieee80211softmac_event event = *(struct ieee80211softmac_event*) d;
79         kfree(d);
80         
81         event.fun(event.mac->dev, event.event_type, event.context);
82 }
83
84 int
85 ieee80211softmac_notify_internal(struct ieee80211softmac_device *mac,
86         int event, void *event_context, notify_function_ptr fun, void *context, gfp_t gfp_mask)
87 {
88         struct ieee80211softmac_event *eventptr;
89         unsigned long flags;
90
91         if (event < -1 || event > IEEE80211SOFTMAC_EVENT_LAST)
92                 return -ENOSYS;
93         
94         if (!fun)
95                 return -EINVAL;
96         
97         eventptr = kmalloc(sizeof(struct ieee80211softmac_event), gfp_mask);
98         if (!eventptr)
99                 return -ENOMEM;
100         
101         eventptr->event_type = event;
102         INIT_WORK(&eventptr->work, ieee80211softmac_notify_callback, eventptr);
103         eventptr->fun = fun;
104         eventptr->context = context;
105         eventptr->mac = mac;
106         eventptr->event_context = event_context;
107
108         spin_lock_irqsave(&mac->lock, flags);
109         list_add(&eventptr->list, &mac->events);
110         spin_unlock_irqrestore(&mac->lock, flags);
111
112         return 0;
113 }
114
115 int
116 ieee80211softmac_notify_gfp(struct net_device *dev,
117         int event, notify_function_ptr fun, void *context, gfp_t gfp_mask)
118 {
119         struct ieee80211softmac_device *mac = ieee80211_priv(dev);
120
121         if (event < 0 || event > IEEE80211SOFTMAC_EVENT_LAST)
122                 return -ENOSYS;
123         
124         return ieee80211softmac_notify_internal(mac, event, NULL, fun, context, gfp_mask);
125 }
126 EXPORT_SYMBOL_GPL(ieee80211softmac_notify_gfp);
127
128 /* private -- calling all callbacks that were specified */
129 void
130 ieee80211softmac_call_events_locked(struct ieee80211softmac_device *mac, int event, void *event_ctx)
131 {
132         struct ieee80211softmac_event *eventptr, *tmp;
133         struct ieee80211softmac_network *network;
134         
135         if (event >= 0) {
136                 union iwreq_data wrqu;
137                 int we_event;
138                 char *msg = NULL;
139
140                 memset(&wrqu, '\0', sizeof (union iwreq_data));
141
142                 switch(event) {
143                 case IEEE80211SOFTMAC_EVENT_ASSOCIATED:
144                         network = (struct ieee80211softmac_network *)event_ctx;
145                         memcpy(wrqu.ap_addr.sa_data, &network->bssid[0], ETH_ALEN);
146                         /* fall through */
147                 case IEEE80211SOFTMAC_EVENT_DISASSOCIATED:
148                         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
149                         we_event = SIOCGIWAP;
150                         break;
151                 case IEEE80211SOFTMAC_EVENT_SCAN_FINISHED:
152                         we_event = SIOCGIWSCAN;
153                         break;
154                 default:
155                         msg = event_descriptions[event];
156                         if (!msg)
157                                 msg = "SOFTMAC EVENT BUG";
158                         wrqu.data.length = strlen(msg);
159                         we_event = IWEVCUSTOM;
160                         break;
161                 }
162                 wireless_send_event(mac->dev, we_event, &wrqu, msg);
163         }
164
165         if (!list_empty(&mac->events))
166                 list_for_each_entry_safe(eventptr, tmp, &mac->events, list) {
167                         if ((eventptr->event_type == event || eventptr->event_type == -1)
168                                 && (eventptr->event_context == NULL || eventptr->event_context == event_ctx)) {
169                                 list_del(&eventptr->list);
170                                 /* User may have subscribed to ANY event, so
171                                  * we tell them which event triggered it. */
172                                 eventptr->event_type = event;
173                                 schedule_work(&eventptr->work);
174                         }
175                 }
176 }
177
178 void
179 ieee80211softmac_call_events(struct ieee80211softmac_device *mac, int event, void *event_ctx)
180 {
181         unsigned long flags;
182
183         spin_lock_irqsave(&mac->lock, flags);
184         ieee80211softmac_call_events_locked(mac, event, event_ctx);
185
186         spin_unlock_irqrestore(&mac->lock, flags);
187 }