| Johannes Berg | 4855d25 | 2006-01-12 21:12:59 +0100 | [diff] [blame] | 1 | /* | 
|  | 2 | * Event system | 
|  | 3 | * Also see comments in public header file and longer explanation below. | 
|  | 4 | * | 
| Johannes Berg | 7985905 | 2006-01-31 19:31:41 +0100 | [diff] [blame] | 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> | 
| Johannes Berg | 4855d25 | 2006-01-12 21:12:59 +0100 | [diff] [blame] | 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 |  | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 28 | #include "ieee80211softmac_priv.h" | 
|  | 29 |  | 
|  | 30 | /* | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 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: | 
| Johannes Berg | 921a91e | 2006-04-20 20:02:04 +0200 | [diff] [blame] | 41 | *  SCAN_FINISHED, | 
|  | 42 | *  DISASSOCIATED:	NULL | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 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] = { | 
| Johannes Berg | 921a91e | 2006-04-20 20:02:04 +0200 | [diff] [blame] | 63 | NULL, /* scan finished */ | 
|  | 64 | NULL, /* associated */ | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 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", | 
| Johannes Berg | 921a91e | 2006-04-20 20:02:04 +0200 | [diff] [blame] | 71 | NULL, /* disassociated */ | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 72 | }; | 
|  | 73 |  | 
|  | 74 |  | 
|  | 75 | static void | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 76 | ieee80211softmac_notify_callback(struct work_struct *work) | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 77 | { | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 78 | struct ieee80211softmac_event *pevent = | 
|  | 79 | container_of(work, struct ieee80211softmac_event, work.work); | 
|  | 80 | struct ieee80211softmac_event event = *pevent; | 
|  | 81 | kfree(pevent); | 
| YOSHIFUJI Hideaki | 6426565 | 2007-02-09 23:24:46 +0900 | [diff] [blame] | 82 |  | 
| Daniel Drake | 6ae15df | 2006-06-01 15:37:22 +0100 | [diff] [blame] | 83 | event.fun(event.mac->dev, event.event_type, event.context); | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
|  | 86 | int | 
|  | 87 | ieee80211softmac_notify_internal(struct ieee80211softmac_device *mac, | 
|  | 88 | int event, void *event_context, notify_function_ptr fun, void *context, gfp_t gfp_mask) | 
|  | 89 | { | 
|  | 90 | struct ieee80211softmac_event *eventptr; | 
|  | 91 | unsigned long flags; | 
|  | 92 |  | 
|  | 93 | if (event < -1 || event > IEEE80211SOFTMAC_EVENT_LAST) | 
|  | 94 | return -ENOSYS; | 
| YOSHIFUJI Hideaki | 6426565 | 2007-02-09 23:24:46 +0900 | [diff] [blame] | 95 |  | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 96 | if (!fun) | 
|  | 97 | return -EINVAL; | 
| YOSHIFUJI Hideaki | 6426565 | 2007-02-09 23:24:46 +0900 | [diff] [blame] | 98 |  | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 99 | eventptr = kmalloc(sizeof(struct ieee80211softmac_event), gfp_mask); | 
|  | 100 | if (!eventptr) | 
|  | 101 | return -ENOMEM; | 
| YOSHIFUJI Hideaki | 6426565 | 2007-02-09 23:24:46 +0900 | [diff] [blame] | 102 |  | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 103 | eventptr->event_type = event; | 
| David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 104 | INIT_DELAYED_WORK(&eventptr->work, ieee80211softmac_notify_callback); | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 105 | eventptr->fun = fun; | 
|  | 106 | eventptr->context = context; | 
|  | 107 | eventptr->mac = mac; | 
|  | 108 | eventptr->event_context = event_context; | 
|  | 109 |  | 
|  | 110 | spin_lock_irqsave(&mac->lock, flags); | 
|  | 111 | list_add(&eventptr->list, &mac->events); | 
|  | 112 | spin_unlock_irqrestore(&mac->lock, flags); | 
|  | 113 |  | 
|  | 114 | return 0; | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | int | 
|  | 118 | ieee80211softmac_notify_gfp(struct net_device *dev, | 
|  | 119 | int event, notify_function_ptr fun, void *context, gfp_t gfp_mask) | 
|  | 120 | { | 
|  | 121 | struct ieee80211softmac_device *mac = ieee80211_priv(dev); | 
|  | 122 |  | 
|  | 123 | if (event < 0 || event > IEEE80211SOFTMAC_EVENT_LAST) | 
|  | 124 | return -ENOSYS; | 
| YOSHIFUJI Hideaki | 6426565 | 2007-02-09 23:24:46 +0900 | [diff] [blame] | 125 |  | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 126 | return ieee80211softmac_notify_internal(mac, event, NULL, fun, context, gfp_mask); | 
|  | 127 | } | 
|  | 128 | EXPORT_SYMBOL_GPL(ieee80211softmac_notify_gfp); | 
|  | 129 |  | 
|  | 130 | /* private -- calling all callbacks that were specified */ | 
|  | 131 | void | 
|  | 132 | ieee80211softmac_call_events_locked(struct ieee80211softmac_device *mac, int event, void *event_ctx) | 
|  | 133 | { | 
|  | 134 | struct ieee80211softmac_event *eventptr, *tmp; | 
| Johannes Berg | feeeaa8 | 2006-04-13 02:42:42 +0200 | [diff] [blame] | 135 | struct ieee80211softmac_network *network; | 
| YOSHIFUJI Hideaki | 6426565 | 2007-02-09 23:24:46 +0900 | [diff] [blame] | 136 |  | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 137 | if (event >= 0) { | 
| Johannes Berg | feeeaa8 | 2006-04-13 02:42:42 +0200 | [diff] [blame] | 138 | union iwreq_data wrqu; | 
|  | 139 | int we_event; | 
|  | 140 | char *msg = NULL; | 
|  | 141 |  | 
| Johannes Berg | 921a91e | 2006-04-20 20:02:04 +0200 | [diff] [blame] | 142 | memset(&wrqu, '\0', sizeof (union iwreq_data)); | 
|  | 143 |  | 
| Johannes Berg | feeeaa8 | 2006-04-13 02:42:42 +0200 | [diff] [blame] | 144 | switch(event) { | 
|  | 145 | case IEEE80211SOFTMAC_EVENT_ASSOCIATED: | 
|  | 146 | network = (struct ieee80211softmac_network *)event_ctx; | 
| Johannes Berg | feeeaa8 | 2006-04-13 02:42:42 +0200 | [diff] [blame] | 147 | memcpy(wrqu.ap_addr.sa_data, &network->bssid[0], ETH_ALEN); | 
| Johannes Berg | 921a91e | 2006-04-20 20:02:04 +0200 | [diff] [blame] | 148 | /* fall through */ | 
| Johannes Berg | feeeaa8 | 2006-04-13 02:42:42 +0200 | [diff] [blame] | 149 | case IEEE80211SOFTMAC_EVENT_DISASSOCIATED: | 
| Johannes Berg | feeeaa8 | 2006-04-13 02:42:42 +0200 | [diff] [blame] | 150 | wrqu.ap_addr.sa_family = ARPHRD_ETHER; | 
|  | 151 | we_event = SIOCGIWAP; | 
|  | 152 | break; | 
| Johannes Berg | 6788a07 | 2006-04-13 11:41:28 +0200 | [diff] [blame] | 153 | case IEEE80211SOFTMAC_EVENT_SCAN_FINISHED: | 
| Johannes Berg | 6788a07 | 2006-04-13 11:41:28 +0200 | [diff] [blame] | 154 | we_event = SIOCGIWSCAN; | 
|  | 155 | break; | 
| Johannes Berg | feeeaa8 | 2006-04-13 02:42:42 +0200 | [diff] [blame] | 156 | default: | 
|  | 157 | msg = event_descriptions[event]; | 
| Johannes Berg | 921a91e | 2006-04-20 20:02:04 +0200 | [diff] [blame] | 158 | if (!msg) | 
|  | 159 | msg = "SOFTMAC EVENT BUG"; | 
| Johannes Berg | feeeaa8 | 2006-04-13 02:42:42 +0200 | [diff] [blame] | 160 | wrqu.data.length = strlen(msg); | 
|  | 161 | we_event = IWEVCUSTOM; | 
|  | 162 | break; | 
|  | 163 | } | 
|  | 164 | wireless_send_event(mac->dev, we_event, &wrqu, msg); | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
|  | 167 | if (!list_empty(&mac->events)) | 
|  | 168 | list_for_each_entry_safe(eventptr, tmp, &mac->events, list) { | 
|  | 169 | if ((eventptr->event_type == event || eventptr->event_type == -1) | 
|  | 170 | && (eventptr->event_context == NULL || eventptr->event_context == event_ctx)) { | 
|  | 171 | list_del(&eventptr->list); | 
| Daniel Drake | 6ae15df | 2006-06-01 15:37:22 +0100 | [diff] [blame] | 172 | /* User may have subscribed to ANY event, so | 
|  | 173 | * we tell them which event triggered it. */ | 
|  | 174 | eventptr->event_type = event; | 
| Johannes Berg | 501d857 | 2007-10-03 18:14:23 -0700 | [diff] [blame] | 175 | queue_delayed_work(mac->wq, &eventptr->work, 0); | 
| Johannes Berg | 370121e | 2006-01-04 16:32:16 +0100 | [diff] [blame] | 176 | } | 
|  | 177 | } | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | void | 
|  | 181 | ieee80211softmac_call_events(struct ieee80211softmac_device *mac, int event, void *event_ctx) | 
|  | 182 | { | 
|  | 183 | unsigned long flags; | 
|  | 184 |  | 
|  | 185 | spin_lock_irqsave(&mac->lock, flags); | 
|  | 186 | ieee80211softmac_call_events_locked(mac, event, event_ctx); | 
|  | 187 |  | 
|  | 188 | spin_unlock_irqrestore(&mac->lock, flags); | 
|  | 189 | } |