sysfs.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License version 2
  3. * as published by the Free Software Foundation.
  4. *
  5. * This program is distributed in the hope that it will be useful,
  6. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. * GNU General Public License for more details.
  9. *
  10. * Authors:
  11. * Alexander Aring <aar@pengutronix.de>
  12. *
  13. * Based on: net/wireless/sysfs.c
  14. */
  15. #include <linux/device.h>
  16. #include <linux/rtnetlink.h>
  17. #include <net/cfg802154.h>
  18. #include "core.h"
  19. #include "sysfs.h"
  20. #include "rdev-ops.h"
  21. static inline struct cfg802154_registered_device *
  22. dev_to_rdev(struct device *dev)
  23. {
  24. return container_of(dev, struct cfg802154_registered_device,
  25. wpan_phy.dev);
  26. }
  27. #define SHOW_FMT(name, fmt, member) \
  28. static ssize_t name ## _show(struct device *dev, \
  29. struct device_attribute *attr, \
  30. char *buf) \
  31. { \
  32. return sprintf(buf, fmt "\n", dev_to_rdev(dev)->member); \
  33. } \
  34. static DEVICE_ATTR_RO(name)
  35. SHOW_FMT(index, "%d", wpan_phy_idx);
  36. static ssize_t name_show(struct device *dev,
  37. struct device_attribute *attr,
  38. char *buf)
  39. {
  40. struct wpan_phy *wpan_phy = &dev_to_rdev(dev)->wpan_phy;
  41. return sprintf(buf, "%s\n", dev_name(&wpan_phy->dev));
  42. }
  43. static DEVICE_ATTR_RO(name);
  44. static void wpan_phy_release(struct device *dev)
  45. {
  46. struct cfg802154_registered_device *rdev = dev_to_rdev(dev);
  47. cfg802154_dev_free(rdev);
  48. }
  49. static struct attribute *pmib_attrs[] = {
  50. &dev_attr_index.attr,
  51. &dev_attr_name.attr,
  52. NULL,
  53. };
  54. ATTRIBUTE_GROUPS(pmib);
  55. #ifdef CONFIG_PM_SLEEP
  56. static int wpan_phy_suspend(struct device *dev)
  57. {
  58. struct cfg802154_registered_device *rdev = dev_to_rdev(dev);
  59. int ret = 0;
  60. if (rdev->ops->suspend) {
  61. rtnl_lock();
  62. ret = rdev_suspend(rdev);
  63. rtnl_unlock();
  64. }
  65. return ret;
  66. }
  67. static int wpan_phy_resume(struct device *dev)
  68. {
  69. struct cfg802154_registered_device *rdev = dev_to_rdev(dev);
  70. int ret = 0;
  71. if (rdev->ops->resume) {
  72. rtnl_lock();
  73. ret = rdev_resume(rdev);
  74. rtnl_unlock();
  75. }
  76. return ret;
  77. }
  78. static SIMPLE_DEV_PM_OPS(wpan_phy_pm_ops, wpan_phy_suspend, wpan_phy_resume);
  79. #define WPAN_PHY_PM_OPS (&wpan_phy_pm_ops)
  80. #else
  81. #define WPAN_PHY_PM_OPS NULL
  82. #endif
  83. struct class wpan_phy_class = {
  84. .name = "ieee802154",
  85. .dev_release = wpan_phy_release,
  86. .dev_groups = pmib_groups,
  87. .pm = WPAN_PHY_PM_OPS,
  88. };
  89. int wpan_phy_sysfs_init(void)
  90. {
  91. return class_register(&wpan_phy_class);
  92. }
  93. void wpan_phy_sysfs_exit(void)
  94. {
  95. class_unregister(&wpan_phy_class);
  96. }