ieee80211softmac_event.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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(struct work_struct *work)
  72. {
  73. struct ieee80211softmac_event *pevent =
  74. container_of(work, struct ieee80211softmac_event, work.work);
  75. struct ieee80211softmac_event event = *pevent;
  76. kfree(pevent);
  77. event.fun(event.mac->dev, event.event_type, event.context);
  78. }
  79. int
  80. ieee80211softmac_notify_internal(struct ieee80211softmac_device *mac,
  81. int event, void *event_context, notify_function_ptr fun, void *context, gfp_t gfp_mask)
  82. {
  83. struct ieee80211softmac_event *eventptr;
  84. unsigned long flags;
  85. if (event < -1 || event > IEEE80211SOFTMAC_EVENT_LAST)
  86. return -ENOSYS;
  87. if (!fun)
  88. return -EINVAL;
  89. eventptr = kmalloc(sizeof(struct ieee80211softmac_event), gfp_mask);
  90. if (!eventptr)
  91. return -ENOMEM;
  92. eventptr->event_type = event;
  93. INIT_DELAYED_WORK(&eventptr->work, ieee80211softmac_notify_callback);
  94. eventptr->fun = fun;
  95. eventptr->context = context;
  96. eventptr->mac = mac;
  97. eventptr->event_context = event_context;
  98. spin_lock_irqsave(&mac->lock, flags);
  99. list_add(&eventptr->list, &mac->events);
  100. spin_unlock_irqrestore(&mac->lock, flags);
  101. return 0;
  102. }
  103. int
  104. ieee80211softmac_notify_gfp(struct net_device *dev,
  105. int event, notify_function_ptr fun, void *context, gfp_t gfp_mask)
  106. {
  107. struct ieee80211softmac_device *mac = ieee80211_priv(dev);
  108. if (event < 0 || event > IEEE80211SOFTMAC_EVENT_LAST)
  109. return -ENOSYS;
  110. return ieee80211softmac_notify_internal(mac, event, NULL, fun, context, gfp_mask);
  111. }
  112. EXPORT_SYMBOL_GPL(ieee80211softmac_notify_gfp);
  113. /* private -- calling all callbacks that were specified */
  114. void
  115. ieee80211softmac_call_events_locked(struct ieee80211softmac_device *mac, int event, void *event_ctx)
  116. {
  117. struct ieee80211softmac_event *eventptr, *tmp;
  118. struct ieee80211softmac_network *network;
  119. if (event >= 0) {
  120. union iwreq_data wrqu;
  121. int we_event;
  122. char *msg = NULL;
  123. memset(&wrqu, '\0', sizeof (union iwreq_data));
  124. switch(event) {
  125. case IEEE80211SOFTMAC_EVENT_ASSOCIATED:
  126. network = (struct ieee80211softmac_network *)event_ctx;
  127. memcpy(wrqu.ap_addr.sa_data, &network->bssid[0], ETH_ALEN);
  128. /* fall through */
  129. case IEEE80211SOFTMAC_EVENT_DISASSOCIATED:
  130. wrqu.ap_addr.sa_family = ARPHRD_ETHER;
  131. we_event = SIOCGIWAP;
  132. break;
  133. case IEEE80211SOFTMAC_EVENT_SCAN_FINISHED:
  134. we_event = SIOCGIWSCAN;
  135. break;
  136. default:
  137. msg = event_descriptions[event];
  138. if (!msg)
  139. msg = "SOFTMAC EVENT BUG";
  140. wrqu.data.length = strlen(msg);
  141. we_event = IWEVCUSTOM;
  142. break;
  143. }
  144. wireless_send_event(mac->dev, we_event, &wrqu, msg);
  145. }
  146. if (!list_empty(&mac->events))
  147. list_for_each_entry_safe(eventptr, tmp, &mac->events, list) {
  148. if ((eventptr->event_type == event || eventptr->event_type == -1)
  149. && (eventptr->event_context == NULL || eventptr->event_context == event_ctx)) {
  150. list_del(&eventptr->list);
  151. /* User may have subscribed to ANY event, so
  152. * we tell them which event triggered it. */
  153. eventptr->event_type = event;
  154. schedule_delayed_work(&eventptr->work, 0);
  155. }
  156. }
  157. }
  158. void
  159. ieee80211softmac_call_events(struct ieee80211softmac_device *mac, int event, void *event_ctx)
  160. {
  161. unsigned long flags;
  162. spin_lock_irqsave(&mac->lock, flags);
  163. ieee80211softmac_call_events_locked(mac, event, event_ctx);
  164. spin_unlock_irqrestore(&mac->lock, flags);
  165. }