sysfs.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * This file provides /sys/class/ieee80211/<wiphy name>/
  3. * and some default attributes.
  4. *
  5. * Copyright 2005-2006 Jiri Benc <jbenc@suse.cz>
  6. * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
  7. *
  8. * This file is GPLv2 as found in COPYING.
  9. */
  10. #include <linux/device.h>
  11. #include <linux/module.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/nl80211.h>
  14. #include <linux/rtnetlink.h>
  15. #include <net/cfg80211.h>
  16. #include "sysfs.h"
  17. #include "core.h"
  18. #include "rdev-ops.h"
  19. static inline struct cfg80211_registered_device *dev_to_rdev(
  20. struct device *dev)
  21. {
  22. return container_of(dev, struct cfg80211_registered_device, wiphy.dev);
  23. }
  24. #define SHOW_FMT(name, fmt, member) \
  25. static ssize_t name ## _show(struct device *dev, \
  26. struct device_attribute *attr, \
  27. char *buf) \
  28. { \
  29. return sprintf(buf, fmt "\n", dev_to_rdev(dev)->member); \
  30. } \
  31. static DEVICE_ATTR_RO(name)
  32. SHOW_FMT(index, "%d", wiphy_idx);
  33. SHOW_FMT(macaddress, "%pM", wiphy.perm_addr);
  34. SHOW_FMT(address_mask, "%pM", wiphy.addr_mask);
  35. static ssize_t name_show(struct device *dev,
  36. struct device_attribute *attr,
  37. char *buf) {
  38. struct wiphy *wiphy = &dev_to_rdev(dev)->wiphy;
  39. return sprintf(buf, "%s\n", dev_name(&wiphy->dev));
  40. }
  41. static DEVICE_ATTR_RO(name);
  42. static ssize_t addresses_show(struct device *dev,
  43. struct device_attribute *attr,
  44. char *buf)
  45. {
  46. struct wiphy *wiphy = &dev_to_rdev(dev)->wiphy;
  47. char *start = buf;
  48. int i;
  49. if (!wiphy->addresses)
  50. return sprintf(buf, "%pM\n", wiphy->perm_addr);
  51. for (i = 0; i < wiphy->n_addresses; i++)
  52. buf += sprintf(buf, "%pM\n", &wiphy->addresses[i].addr);
  53. return buf - start;
  54. }
  55. static DEVICE_ATTR_RO(addresses);
  56. static struct attribute *ieee80211_attrs[] = {
  57. &dev_attr_index.attr,
  58. &dev_attr_macaddress.attr,
  59. &dev_attr_address_mask.attr,
  60. &dev_attr_addresses.attr,
  61. &dev_attr_name.attr,
  62. NULL,
  63. };
  64. ATTRIBUTE_GROUPS(ieee80211);
  65. static void wiphy_dev_release(struct device *dev)
  66. {
  67. struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
  68. cfg80211_dev_free(rdev);
  69. }
  70. static int wiphy_uevent(struct device *dev, struct kobj_uevent_env *env)
  71. {
  72. /* TODO, we probably need stuff here */
  73. return 0;
  74. }
  75. #ifdef CONFIG_PM_SLEEP
  76. static void cfg80211_leave_all(struct cfg80211_registered_device *rdev)
  77. {
  78. struct wireless_dev *wdev;
  79. list_for_each_entry(wdev, &rdev->wdev_list, list)
  80. cfg80211_leave(rdev, wdev);
  81. }
  82. static int wiphy_suspend(struct device *dev)
  83. {
  84. struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
  85. int ret = 0;
  86. rdev->suspend_at = get_seconds();
  87. rtnl_lock();
  88. if (rdev->wiphy.registered) {
  89. if (!rdev->wiphy.wowlan_config)
  90. cfg80211_leave_all(rdev);
  91. if (rdev->ops->suspend)
  92. ret = rdev_suspend(rdev, rdev->wiphy.wowlan_config);
  93. if (ret == 1) {
  94. /* Driver refuse to configure wowlan */
  95. cfg80211_leave_all(rdev);
  96. ret = rdev_suspend(rdev, NULL);
  97. }
  98. }
  99. rtnl_unlock();
  100. return ret;
  101. }
  102. static int wiphy_resume(struct device *dev)
  103. {
  104. struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
  105. int ret = 0;
  106. /* Age scan results with time spent in suspend */
  107. cfg80211_bss_age(rdev, get_seconds() - rdev->suspend_at);
  108. if (rdev->ops->resume) {
  109. rtnl_lock();
  110. if (rdev->wiphy.registered)
  111. ret = rdev_resume(rdev);
  112. rtnl_unlock();
  113. }
  114. return ret;
  115. }
  116. static SIMPLE_DEV_PM_OPS(wiphy_pm_ops, wiphy_suspend, wiphy_resume);
  117. #define WIPHY_PM_OPS (&wiphy_pm_ops)
  118. #else
  119. #define WIPHY_PM_OPS NULL
  120. #endif
  121. static const void *wiphy_namespace(struct device *d)
  122. {
  123. struct wiphy *wiphy = container_of(d, struct wiphy, dev);
  124. return wiphy_net(wiphy);
  125. }
  126. struct class ieee80211_class = {
  127. .name = "ieee80211",
  128. .owner = THIS_MODULE,
  129. .dev_release = wiphy_dev_release,
  130. .dev_groups = ieee80211_groups,
  131. .dev_uevent = wiphy_uevent,
  132. .pm = WIPHY_PM_OPS,
  133. .ns_type = &net_ns_type_operations,
  134. .namespace = wiphy_namespace,
  135. };
  136. int wiphy_sysfs_init(void)
  137. {
  138. return class_register(&ieee80211_class);
  139. }
  140. void wiphy_sysfs_exit(void)
  141. {
  142. class_unregister(&ieee80211_class);
  143. }