ieee80211softmac_event.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #include "ieee80211softmac_priv.h"
  28. /*
  29. * Each event has associated to it
  30. * - an event type (see constants in public header)
  31. * - an event context (see below)
  32. * - the function to be called
  33. * - a context (extra parameter to call the function with)
  34. * - and the softmac struct
  35. *
  36. * The event context is private and can only be used from
  37. * within this module. Its meaning varies with the event
  38. * type:
  39. * SCAN_FINISHED,
  40. * DISASSOCIATED: NULL
  41. * ASSOCIATED,
  42. * ASSOCIATE_FAILED,
  43. * ASSOCIATE_TIMEOUT,
  44. * AUTHENTICATED,
  45. * AUTH_FAILED,
  46. * AUTH_TIMEOUT: a pointer to the network struct
  47. * ...
  48. * Code within this module can use the event context to be only
  49. * called when the event is true for that specific context
  50. * as per above table.
  51. * If the event context is NULL, then the notification is always called,
  52. * regardless of the event context. The event context is not passed to
  53. * the callback, it is assumed that the context suffices.
  54. *
  55. * You can also use the event context only by setting the event type
  56. * to -1 (private use only), in which case you'll be notified
  57. * whenever the event context matches.
  58. */
  59. static char *event_descriptions[IEEE80211SOFTMAC_EVENT_LAST+1] = {
  60. NULL, /* scan finished */
  61. NULL, /* associated */
  62. "associating failed",
  63. "associating timed out",
  64. "authenticated",
  65. "authenticating failed",
  66. "authenticating timed out",
  67. "associating failed because no suitable network was found",
  68. NULL, /* disassociated */
  69. };
  70. static void
  71. ieee80211softmac_notify_callback(void *d)
  72. {
  73. struct ieee80211softmac_event event = *(struct ieee80211softmac_event*) d;
  74. kfree(d);
  75. event.fun(event.mac->dev, event.event_type, event.context);
  76. }
  77. int
  78. ieee80211softmac_notify_internal(struct ieee80211softmac_device *mac,
  79. int event, void *event_context, notify_function_ptr fun, void *context, gfp_t gfp_mask)
  80. {
  81. struct ieee80211softmac_event *eventptr;
  82. unsigned long flags;
  83. if (event < -1 || event > IEEE80211SOFTMAC_EVENT_LAST)
  84. return -ENOSYS;
  85. if (!fun)
  86. return -EINVAL;
  87. eventptr = kmalloc(sizeof(struct ieee80211softmac_event), gfp_mask);
  88. if (!eventptr)
  89. return -ENOMEM;
  90. eventptr->event_type = event;
  91. INIT_WORK(&eventptr->work, ieee80211softmac_notify_callback, eventptr);
  92. eventptr->fun = fun;
  93. eventptr->context = context;
  94. eventptr->mac = mac;
  95. eventptr->event_context = event_context;
  96. spin_lock_irqsave(&mac->lock, flags);
  97. list_add(&eventptr->list, &mac->events);
  98. spin_unlock_irqrestore(&mac->lock, flags);
  99. return 0;
  100. }
  101. int
  102. ieee80211softmac_notify_gfp(struct net_device *dev,
  103. int event, notify_function_ptr fun, void *context, gfp_t gfp_mask)
  104. {
  105. struct ieee80211softmac_device *mac = ieee80211_priv(dev);
  106. if (event < 0 || event > IEEE80211SOFTMAC_EVENT_LAST)
  107. return -ENOSYS;
  108. return ieee80211softmac_notify_internal(mac, event, NULL, fun, context, gfp_mask);
  109. }
  110. EXPORT_SYMBOL_GPL(ieee80211softmac_notify_gfp);
  111. /* private -- calling all callbacks that were specified */
  112. void
  113. ieee80211softmac_call_events_locked(struct ieee80211softmac_device *mac, int event, void *event_ctx)
  114. {
  115. struct ieee80211softmac_event *eventptr, *tmp;
  116. struct ieee80211softmac_network *network;
  117. if (event >= 0) {
  118. union iwreq_data wrqu;
  119. int we_event;
  120. char *msg = NULL;
  121. memset(&wrqu, '\0', sizeof (union iwreq_data));
  122. switch(event) {
  123. case IEEE80211SOFTMAC_EVENT_ASSOCIATED:
  124. network = (struct ieee80211softmac_network *)event_ctx;
  125. memcpy(wrqu.ap_addr.sa_data, &network->bssid[0], ETH_ALEN);
  126. /* fall through */
  127. case IEEE80211SOFTMAC_EVENT_DISASSOCIATED:
  128. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  129. we_event = SIOCGIWAP;
  130. break;
  131. case IEEE80211SOFTMAC_EVENT_SCAN_FINISHED:
  132. we_event = SIOCGIWSCAN;
  133. break;
  134. default:
  135. msg = event_descriptions[event];
  136. if (!msg)
  137. msg = "SOFTMAC EVENT BUG";
  138. wrqu.data.length = strlen(msg);
  139. we_event = IWEVCUSTOM;
  140. break;
  141. }
  142. wireless_send_event(mac->dev, we_event, &wrqu, msg);
  143. }
  144. if (!list_empty(&mac->events))
  145. list_for_each_entry_safe(eventptr, tmp, &mac->events, list) {
  146. if ((eventptr->event_type == event || eventptr->event_type == -1)
  147. && (eventptr->event_context == NULL || eventptr->event_context == event_ctx)) {
  148. list_del(&eventptr->list);
  149. /* User may have subscribed to ANY event, so
  150. * we tell them which event triggered it. */
  151. eventptr->event_type = event;
  152. schedule_work(&eventptr->work);
  153. }
  154. }
  155. }
  156. void
  157. ieee80211softmac_call_events(struct ieee80211softmac_device *mac, int event, void *event_ctx)
  158. {
  159. unsigned long flags;
  160. spin_lock_irqsave(&mac->lock, flags);
  161. ieee80211softmac_call_events_locked(mac, event, event_ctx);
  162. spin_unlock_irqrestore(&mac->lock, flags);
  163. }