Commit | Line | Data |
---|---|---|
370121e5 JB |
1 | /* |
2 | * Event system | |
4855d25b JB |
3 | * Also see comments in public header file and longer explanation below. |
4 | * | |
5 | * Copyright (c) 2005 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. | |
370121e5 | 19 | * |
4855d25b JB |
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 | /* | |
370121e5 JB |
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: no special meaning | |
42 | * ASSOCIATED, | |
43 | * ASSOCIATE_FAILED, | |
44 | * ASSOCIATE_TIMEOUT, | |
45 | * AUTHENTICATED, | |
46 | * AUTH_FAILED, | |
47 | * AUTH_TIMEOUT: a pointer to the network struct | |
48 | * ... | |
49 | * Code within this module can use the event context to be only | |
50 | * called when the event is true for that specific context | |
51 | * as per above table. | |
52 | * If the event context is NULL, then the notification is always called, | |
53 | * regardless of the event context. The event context is not passed to | |
54 | * the callback, it is assumed that the context suffices. | |
55 | * | |
56 | * You can also use the event context only by setting the event type | |
57 | * to -1 (private use only), in which case you'll be notified | |
58 | * whenever the event context matches. | |
59 | */ | |
60 | ||
61 | static char *event_descriptions[IEEE80211SOFTMAC_EVENT_LAST+1] = { | |
62 | "scan finished", | |
63 | "associated", | |
64 | "associating failed", | |
65 | "associating timed out", | |
66 | "authenticated", | |
67 | "authenticating failed", | |
68 | "authenticating timed out", | |
69 | "associating failed because no suitable network was found", | |
70 | }; | |
71 | ||
72 | ||
73 | static void | |
74 | ieee80211softmac_notify_callback(void *d) | |
75 | { | |
76 | struct ieee80211softmac_event event = *(struct ieee80211softmac_event*) d; | |
77 | kfree(d); | |
78 | ||
79 | event.fun(event.mac->dev, event.context); | |
80 | } | |
81 | ||
82 | int | |
83 | ieee80211softmac_notify_internal(struct ieee80211softmac_device *mac, | |
84 | int event, void *event_context, notify_function_ptr fun, void *context, gfp_t gfp_mask) | |
85 | { | |
86 | struct ieee80211softmac_event *eventptr; | |
87 | unsigned long flags; | |
88 | ||
89 | if (event < -1 || event > IEEE80211SOFTMAC_EVENT_LAST) | |
90 | return -ENOSYS; | |
91 | ||
92 | if (!fun) | |
93 | return -EINVAL; | |
94 | ||
95 | eventptr = kmalloc(sizeof(struct ieee80211softmac_event), gfp_mask); | |
96 | if (!eventptr) | |
97 | return -ENOMEM; | |
98 | ||
99 | eventptr->event_type = event; | |
100 | INIT_WORK(&eventptr->work, ieee80211softmac_notify_callback, eventptr); | |
101 | eventptr->fun = fun; | |
102 | eventptr->context = context; | |
103 | eventptr->mac = mac; | |
104 | eventptr->event_context = event_context; | |
105 | ||
106 | spin_lock_irqsave(&mac->lock, flags); | |
107 | list_add(&eventptr->list, &mac->events); | |
108 | spin_unlock_irqrestore(&mac->lock, flags); | |
109 | ||
110 | return 0; | |
111 | } | |
112 | ||
113 | int | |
114 | ieee80211softmac_notify_gfp(struct net_device *dev, | |
115 | int event, notify_function_ptr fun, void *context, gfp_t gfp_mask) | |
116 | { | |
117 | struct ieee80211softmac_device *mac = ieee80211_priv(dev); | |
118 | ||
119 | if (event < 0 || event > IEEE80211SOFTMAC_EVENT_LAST) | |
120 | return -ENOSYS; | |
121 | ||
122 | return ieee80211softmac_notify_internal(mac, event, NULL, fun, context, gfp_mask); | |
123 | } | |
124 | EXPORT_SYMBOL_GPL(ieee80211softmac_notify_gfp); | |
125 | ||
126 | /* private -- calling all callbacks that were specified */ | |
127 | void | |
128 | ieee80211softmac_call_events_locked(struct ieee80211softmac_device *mac, int event, void *event_ctx) | |
129 | { | |
130 | struct ieee80211softmac_event *eventptr, *tmp; | |
131 | union iwreq_data wrqu; | |
132 | char *msg; | |
133 | ||
134 | if (event >= 0) { | |
135 | msg = event_descriptions[event]; | |
136 | wrqu.data.length = strlen(msg); | |
137 | wireless_send_event(mac->dev, IWEVCUSTOM, &wrqu, msg); | |
138 | } | |
139 | ||
140 | if (!list_empty(&mac->events)) | |
141 | list_for_each_entry_safe(eventptr, tmp, &mac->events, list) { | |
142 | if ((eventptr->event_type == event || eventptr->event_type == -1) | |
143 | && (eventptr->event_context == NULL || eventptr->event_context == event_ctx)) { | |
144 | list_del(&eventptr->list); | |
5c4df6da | 145 | schedule_work(&eventptr->work); |
370121e5 JB |
146 | } |
147 | } | |
148 | } | |
149 | ||
150 | void | |
151 | ieee80211softmac_call_events(struct ieee80211softmac_device *mac, int event, void *event_ctx) | |
152 | { | |
153 | unsigned long flags; | |
154 | ||
155 | spin_lock_irqsave(&mac->lock, flags); | |
156 | ieee80211softmac_call_events_locked(mac, event, event_ctx); | |
157 | ||
158 | spin_unlock_irqrestore(&mac->lock, flags); | |
159 | } |