core.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * This is the linux wireless configuration interface.
  3. *
  4. * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
  5. */
  6. #include <linux/if.h>
  7. #include <linux/module.h>
  8. #include <linux/err.h>
  9. #include <linux/mutex.h>
  10. #include <linux/list.h>
  11. #include <linux/nl80211.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/notifier.h>
  14. #include <linux/device.h>
  15. #include <net/genetlink.h>
  16. #include <net/cfg80211.h>
  17. #include <net/wireless.h>
  18. #include "core.h"
  19. #include "sysfs.h"
  20. /* name for sysfs, %d is appended */
  21. #define PHY_NAME "phy"
  22. MODULE_AUTHOR("Johannes Berg");
  23. MODULE_LICENSE("GPL");
  24. MODULE_DESCRIPTION("wireless configuration support");
  25. /* RCU might be appropriate here since we usually
  26. * only read the list, and that can happen quite
  27. * often because we need to do it for each command */
  28. LIST_HEAD(cfg80211_drv_list);
  29. DEFINE_MUTEX(cfg80211_drv_mutex);
  30. static int wiphy_counter;
  31. /* for debugfs */
  32. static struct dentry *ieee80211_debugfs_dir;
  33. /* exported functions */
  34. struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
  35. {
  36. struct cfg80211_registered_device *drv;
  37. int alloc_size;
  38. alloc_size = sizeof(*drv) + sizeof_priv;
  39. drv = kzalloc(alloc_size, GFP_KERNEL);
  40. if (!drv)
  41. return NULL;
  42. drv->ops = ops;
  43. mutex_lock(&cfg80211_drv_mutex);
  44. drv->idx = wiphy_counter;
  45. /* now increase counter for the next device unless
  46. * it has wrapped previously */
  47. if (wiphy_counter >= 0)
  48. wiphy_counter++;
  49. mutex_unlock(&cfg80211_drv_mutex);
  50. if (unlikely(drv->idx < 0)) {
  51. /* ugh, wrapped! */
  52. kfree(drv);
  53. return NULL;
  54. }
  55. /* give it a proper name */
  56. snprintf(drv->wiphy.dev.bus_id, BUS_ID_SIZE,
  57. PHY_NAME "%d", drv->idx);
  58. mutex_init(&drv->mtx);
  59. mutex_init(&drv->devlist_mtx);
  60. INIT_LIST_HEAD(&drv->netdev_list);
  61. device_initialize(&drv->wiphy.dev);
  62. drv->wiphy.dev.class = &ieee80211_class;
  63. drv->wiphy.dev.platform_data = drv;
  64. return &drv->wiphy;
  65. }
  66. EXPORT_SYMBOL(wiphy_new);
  67. int wiphy_register(struct wiphy *wiphy)
  68. {
  69. struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
  70. int res;
  71. mutex_lock(&cfg80211_drv_mutex);
  72. res = device_add(&drv->wiphy.dev);
  73. if (res)
  74. goto out_unlock;
  75. list_add(&drv->list, &cfg80211_drv_list);
  76. /* add to debugfs */
  77. drv->wiphy.debugfsdir =
  78. debugfs_create_dir(wiphy_name(&drv->wiphy),
  79. ieee80211_debugfs_dir);
  80. res = 0;
  81. out_unlock:
  82. mutex_unlock(&cfg80211_drv_mutex);
  83. return res;
  84. }
  85. EXPORT_SYMBOL(wiphy_register);
  86. void wiphy_unregister(struct wiphy *wiphy)
  87. {
  88. struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
  89. /* protect the device list */
  90. mutex_lock(&cfg80211_drv_mutex);
  91. BUG_ON(!list_empty(&drv->netdev_list));
  92. /*
  93. * Try to grab drv->mtx. If a command is still in progress,
  94. * hopefully the driver will refuse it since it's tearing
  95. * down the device already. We wait for this command to complete
  96. * before unlinking the item from the list.
  97. * Note: as codified by the BUG_ON above we cannot get here if
  98. * a virtual interface is still associated. Hence, we can only
  99. * get to lock contention here if userspace issues a command
  100. * that identified the hardware by wiphy index.
  101. */
  102. mutex_lock(&drv->mtx);
  103. /* unlock again before freeing */
  104. mutex_unlock(&drv->mtx);
  105. list_del(&drv->list);
  106. device_del(&drv->wiphy.dev);
  107. debugfs_remove(drv->wiphy.debugfsdir);
  108. mutex_unlock(&cfg80211_drv_mutex);
  109. }
  110. EXPORT_SYMBOL(wiphy_unregister);
  111. void cfg80211_dev_free(struct cfg80211_registered_device *drv)
  112. {
  113. mutex_destroy(&drv->mtx);
  114. mutex_destroy(&drv->devlist_mtx);
  115. kfree(drv);
  116. }
  117. void wiphy_free(struct wiphy *wiphy)
  118. {
  119. put_device(&wiphy->dev);
  120. }
  121. EXPORT_SYMBOL(wiphy_free);
  122. static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
  123. unsigned long state,
  124. void *ndev)
  125. {
  126. struct net_device *dev = ndev;
  127. struct cfg80211_registered_device *rdev;
  128. if (!dev->ieee80211_ptr)
  129. return 0;
  130. rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  131. switch (state) {
  132. case NETDEV_REGISTER:
  133. mutex_lock(&rdev->devlist_mtx);
  134. list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
  135. if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
  136. "phy80211")) {
  137. printk(KERN_ERR "wireless: failed to add phy80211 "
  138. "symlink to netdev!\n");
  139. }
  140. dev->ieee80211_ptr->netdev = dev;
  141. mutex_unlock(&rdev->devlist_mtx);
  142. break;
  143. case NETDEV_UNREGISTER:
  144. mutex_lock(&rdev->devlist_mtx);
  145. if (!list_empty(&dev->ieee80211_ptr->list)) {
  146. sysfs_remove_link(&dev->dev.kobj, "phy80211");
  147. list_del_init(&dev->ieee80211_ptr->list);
  148. }
  149. mutex_unlock(&rdev->devlist_mtx);
  150. break;
  151. }
  152. return 0;
  153. }
  154. static struct notifier_block cfg80211_netdev_notifier = {
  155. .notifier_call = cfg80211_netdev_notifier_call,
  156. };
  157. static int cfg80211_init(void)
  158. {
  159. int err = wiphy_sysfs_init();
  160. if (err)
  161. goto out_fail_sysfs;
  162. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  163. if (err)
  164. goto out_fail_notifier;
  165. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  166. return 0;
  167. out_fail_notifier:
  168. wiphy_sysfs_exit();
  169. out_fail_sysfs:
  170. return err;
  171. }
  172. module_init(cfg80211_init);
  173. static void cfg80211_exit(void)
  174. {
  175. debugfs_remove(ieee80211_debugfs_dir);
  176. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  177. wiphy_sysfs_exit();
  178. }
  179. module_exit(cfg80211_exit);