sysfs.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <net/cfg802154.h>
  17. #include "core.h"
  18. #include "sysfs.h"
  19. static inline struct cfg802154_registered_device *
  20. dev_to_rdev(struct device *dev)
  21. {
  22. return container_of(dev, struct cfg802154_registered_device,
  23. wpan_phy.dev);
  24. }
  25. #define MASTER_SHOW_COMPLEX(name, format_string, args...) \
  26. static ssize_t name ## _show(struct device *dev, \
  27. struct device_attribute *attr, char *buf) \
  28. { \
  29. struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); \
  30. int ret; \
  31. \
  32. mutex_lock(&phy->pib_lock); \
  33. ret = snprintf(buf, PAGE_SIZE, format_string "\n", args); \
  34. mutex_unlock(&phy->pib_lock); \
  35. return ret; \
  36. } \
  37. static DEVICE_ATTR_RO(name)
  38. #define MASTER_SHOW(field, format_string) \
  39. MASTER_SHOW_COMPLEX(field, format_string, phy->field)
  40. MASTER_SHOW(current_channel, "%d");
  41. MASTER_SHOW(current_page, "%d");
  42. MASTER_SHOW(transmit_power, "%d +- 1 dB");
  43. MASTER_SHOW(cca_mode, "%d");
  44. static ssize_t channels_supported_show(struct device *dev,
  45. struct device_attribute *attr,
  46. char *buf)
  47. {
  48. struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
  49. int ret;
  50. int i, len = 0;
  51. mutex_lock(&phy->pib_lock);
  52. for (i = 0; i < 32; i++) {
  53. ret = snprintf(buf + len, PAGE_SIZE - len,
  54. "%#09x\n", phy->channels_supported[i]);
  55. if (ret < 0)
  56. break;
  57. len += ret;
  58. }
  59. mutex_unlock(&phy->pib_lock);
  60. return len;
  61. }
  62. static DEVICE_ATTR_RO(channels_supported);
  63. static void wpan_phy_release(struct device *dev)
  64. {
  65. struct cfg802154_registered_device *rdev = dev_to_rdev(dev);
  66. cfg802154_dev_free(rdev);
  67. }
  68. static struct attribute *pmib_attrs[] = {
  69. &dev_attr_current_channel.attr,
  70. &dev_attr_current_page.attr,
  71. &dev_attr_channels_supported.attr,
  72. &dev_attr_transmit_power.attr,
  73. &dev_attr_cca_mode.attr,
  74. NULL,
  75. };
  76. ATTRIBUTE_GROUPS(pmib);
  77. struct class wpan_phy_class = {
  78. .name = "ieee802154",
  79. .dev_release = wpan_phy_release,
  80. .dev_groups = pmib_groups,
  81. };
  82. int wpan_phy_sysfs_init(void)
  83. {
  84. return class_register(&wpan_phy_class);
  85. }
  86. void wpan_phy_sysfs_exit(void)
  87. {
  88. class_unregister(&wpan_phy_class);
  89. }