sysfs.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file provides /sys/class/ieee80211/<wiphy name>/
  4. * and some default attributes.
  5. *
  6. * Copyright 2005-2006 Jiri Benc <[email protected]>
  7. * Copyright 2006 Johannes Berg <[email protected]>
  8. * Copyright (C) 2020-2021, 2023 Intel Corporation
  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. {
  39. struct wiphy *wiphy = &dev_to_rdev(dev)->wiphy;
  40. return sprintf(buf, "%s\n", wiphy_name(wiphy));
  41. }
  42. static DEVICE_ATTR_RO(name);
  43. static ssize_t addresses_show(struct device *dev,
  44. struct device_attribute *attr,
  45. char *buf)
  46. {
  47. struct wiphy *wiphy = &dev_to_rdev(dev)->wiphy;
  48. char *start = buf;
  49. int i;
  50. if (!wiphy->addresses)
  51. return sprintf(buf, "%pM\n", wiphy->perm_addr);
  52. for (i = 0; i < wiphy->n_addresses; i++)
  53. buf += sprintf(buf, "%pM\n", wiphy->addresses[i].addr);
  54. return buf - start;
  55. }
  56. static DEVICE_ATTR_RO(addresses);
  57. static struct attribute *ieee80211_attrs[] = {
  58. &dev_attr_index.attr,
  59. &dev_attr_macaddress.attr,
  60. &dev_attr_address_mask.attr,
  61. &dev_attr_addresses.attr,
  62. &dev_attr_name.attr,
  63. NULL,
  64. };
  65. ATTRIBUTE_GROUPS(ieee80211);
  66. static void wiphy_dev_release(struct device *dev)
  67. {
  68. struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
  69. cfg80211_dev_free(rdev);
  70. }
  71. #ifdef CONFIG_PM_SLEEP
  72. static void cfg80211_leave_all(struct cfg80211_registered_device *rdev)
  73. {
  74. struct wireless_dev *wdev;
  75. list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
  76. cfg80211_leave(rdev, wdev);
  77. }
  78. static int wiphy_suspend(struct device *dev)
  79. {
  80. struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
  81. int ret = 0;
  82. rdev->suspend_at = ktime_get_boottime_seconds();
  83. rtnl_lock();
  84. wiphy_lock(&rdev->wiphy);
  85. if (rdev->wiphy.registered) {
  86. if (!rdev->wiphy.wowlan_config) {
  87. cfg80211_leave_all(rdev);
  88. cfg80211_process_rdev_events(rdev);
  89. }
  90. cfg80211_process_wiphy_works(rdev, NULL);
  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. cfg80211_process_rdev_events(rdev);
  97. cfg80211_process_wiphy_works(rdev, NULL);
  98. ret = rdev_suspend(rdev, NULL);
  99. }
  100. if (ret == 0)
  101. rdev->suspended = true;
  102. }
  103. wiphy_unlock(&rdev->wiphy);
  104. rtnl_unlock();
  105. return ret;
  106. }
  107. static int wiphy_resume(struct device *dev)
  108. {
  109. struct cfg80211_registered_device *rdev = dev_to_rdev(dev);
  110. int ret = 0;
  111. /* Age scan results with time spent in suspend */
  112. cfg80211_bss_age(rdev, ktime_get_boottime_seconds() - rdev->suspend_at);
  113. rtnl_lock();
  114. wiphy_lock(&rdev->wiphy);
  115. if (rdev->wiphy.registered && rdev->ops->resume)
  116. ret = rdev_resume(rdev);
  117. rdev->suspended = false;
  118. schedule_work(&rdev->wiphy_work);
  119. wiphy_unlock(&rdev->wiphy);
  120. if (ret)
  121. cfg80211_shutdown_all_interfaces(&rdev->wiphy);
  122. rtnl_unlock();
  123. return ret;
  124. }
  125. static SIMPLE_DEV_PM_OPS(wiphy_pm_ops, wiphy_suspend, wiphy_resume);
  126. #define WIPHY_PM_OPS (&wiphy_pm_ops)
  127. #else
  128. #define WIPHY_PM_OPS NULL
  129. #endif
  130. static const void *wiphy_namespace(struct device *d)
  131. {
  132. struct wiphy *wiphy = container_of(d, struct wiphy, dev);
  133. return wiphy_net(wiphy);
  134. }
  135. struct class ieee80211_class = {
  136. .name = "ieee80211",
  137. .owner = THIS_MODULE,
  138. .dev_release = wiphy_dev_release,
  139. .dev_groups = ieee80211_groups,
  140. .pm = WIPHY_PM_OPS,
  141. .ns_type = &net_ns_type_operations,
  142. .namespace = wiphy_namespace,
  143. };
  144. int wiphy_sysfs_init(void)
  145. {
  146. return class_register(&ieee80211_class);
  147. }
  148. void wiphy_sysfs_exit(void)
  149. {
  150. class_unregister(&ieee80211_class);
  151. }