ieee80211softmac_event.c 4.9 KB

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