sysfs.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Authors:
  5. * Alexander Aring <aar@pengutronix.de>
  6. *
  7. * Based on: net/wireless/sysfs.c
  8. */
  9. #include <linux/device.h>
  10. #include <linux/rtnetlink.h>
  11. #include <net/cfg802154.h>
  12. #include "core.h"
  13. #include "sysfs.h"
  14. #include "rdev-ops.h"
  15. static inline struct cfg802154_registered_device *
  16. dev_to_rdev(struct device *dev)
  17. {
  18. return container_of(dev, struct cfg802154_registered_device,
  19. wpan_phy.dev);
  20. }
  21. #define SHOW_FMT(name, fmt, member) \
  22. static ssize_t name ## _show(struct device *dev, \
  23. struct device_attribute *attr, \
  24. char *buf) \
  25. { \
  26. return sprintf(buf, fmt "\n", dev_to_rdev(dev)->member); \
  27. } \
  28. static DEVICE_ATTR_RO(name)
  29. SHOW_FMT(index, "%d", wpan_phy_idx);
  30. static ssize_t name_show(struct device *dev,
  31. struct device_attribute *attr,
  32. char *buf)
  33. {
  34. struct wpan_phy *wpan_phy = &dev_to_rdev(dev)->wpan_phy;
  35. return sprintf(buf, "%s\n", dev_name(&wpan_phy->dev));
  36. }
  37. static DEVICE_ATTR_RO(name);
  38. static void wpan_phy_release(struct device *dev)
  39. {
  40. struct cfg802154_registered_device *rdev = dev_to_rdev(dev);
  41. cfg802154_dev_free(rdev);
  42. }
  43. static struct attribute *pmib_attrs[] = {
  44. &dev_attr_index.attr,
  45. &dev_attr_name.attr,
  46. NULL,
  47. };
  48. ATTRIBUTE_GROUPS(pmib);
  49. #ifdef CONFIG_PM_SLEEP
  50. static int wpan_phy_suspend(struct device *dev)
  51. {
  52. struct cfg802154_registered_device *rdev = dev_to_rdev(dev);
  53. int ret = 0;
  54. if (rdev->ops->suspend) {
  55. rtnl_lock();
  56. ret = rdev_suspend(rdev);
  57. rtnl_unlock();
  58. }
  59. return ret;
  60. }
  61. static int wpan_phy_resume(struct device *dev)
  62. {
  63. struct cfg802154_registered_device *rdev = dev_to_rdev(dev);
  64. int ret = 0;
  65. if (rdev->ops->resume) {
  66. rtnl_lock();
  67. ret = rdev_resume(rdev);
  68. rtnl_unlock();
  69. }
  70. return ret;
  71. }
  72. static SIMPLE_DEV_PM_OPS(wpan_phy_pm_ops, wpan_phy_suspend, wpan_phy_resume);
  73. #define WPAN_PHY_PM_OPS (&wpan_phy_pm_ops)
  74. #else
  75. #define WPAN_PHY_PM_OPS NULL
  76. #endif
  77. struct class wpan_phy_class = {
  78. .name = "ieee802154",
  79. .dev_release = wpan_phy_release,
  80. .dev_groups = pmib_groups,
  81. .pm = WPAN_PHY_PM_OPS,
  82. };
  83. int wpan_phy_sysfs_init(void)
  84. {
  85. return class_register(&wpan_phy_class);
  86. }
  87. void wpan_phy_sysfs_exit(void)
  88. {
  89. class_unregister(&wpan_phy_class);
  90. }