wpan-class.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (C) 2007, 2008, 2009 Siemens AG
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/device.h>
  21. #include <net/wpan-phy.h>
  22. #define MASTER_SHOW_COMPLEX(name, format_string, args...) \
  23. static ssize_t name ## _show(struct device *dev, \
  24. struct device_attribute *attr, char *buf) \
  25. { \
  26. struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); \
  27. int ret; \
  28. \
  29. mutex_lock(&phy->pib_lock); \
  30. ret = snprintf(buf, PAGE_SIZE, format_string "\n", args); \
  31. mutex_unlock(&phy->pib_lock); \
  32. return ret; \
  33. }
  34. #define MASTER_SHOW(field, format_string) \
  35. MASTER_SHOW_COMPLEX(field, format_string, phy->field)
  36. MASTER_SHOW(current_channel, "%d");
  37. MASTER_SHOW(current_page, "%d");
  38. MASTER_SHOW_COMPLEX(transmit_power, "%d +- %d dB",
  39. ((signed char) (phy->transmit_power << 2)) >> 2,
  40. (phy->transmit_power >> 6) ? (phy->transmit_power >> 6) * 3 : 1 );
  41. MASTER_SHOW(cca_mode, "%d");
  42. static ssize_t channels_supported_show(struct device *dev,
  43. struct device_attribute *attr, char *buf)
  44. {
  45. struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
  46. int ret;
  47. int i, len = 0;
  48. mutex_lock(&phy->pib_lock);
  49. for (i = 0; i < 32; i++) {
  50. ret = snprintf(buf + len, PAGE_SIZE - len,
  51. "%#09x\n", phy->channels_supported[i]);
  52. if (ret < 0)
  53. break;
  54. len += ret;
  55. }
  56. mutex_unlock(&phy->pib_lock);
  57. return len;
  58. }
  59. static struct device_attribute pmib_attrs[] = {
  60. __ATTR_RO(current_channel),
  61. __ATTR_RO(current_page),
  62. __ATTR_RO(channels_supported),
  63. __ATTR_RO(transmit_power),
  64. __ATTR_RO(cca_mode),
  65. {},
  66. };
  67. static void wpan_phy_release(struct device *d)
  68. {
  69. struct wpan_phy *phy = container_of(d, struct wpan_phy, dev);
  70. kfree(phy);
  71. }
  72. static struct class wpan_phy_class = {
  73. .name = "ieee802154",
  74. .dev_release = wpan_phy_release,
  75. .dev_attrs = pmib_attrs,
  76. };
  77. static DEFINE_MUTEX(wpan_phy_mutex);
  78. static int wpan_phy_idx;
  79. static int wpan_phy_match(struct device *dev, void *data)
  80. {
  81. return !strcmp(dev_name(dev), (const char *)data);
  82. }
  83. struct wpan_phy *wpan_phy_find(const char *str)
  84. {
  85. struct device *dev;
  86. if (WARN_ON(!str))
  87. return NULL;
  88. dev = class_find_device(&wpan_phy_class, NULL,
  89. (void *)str, wpan_phy_match);
  90. if (!dev)
  91. return NULL;
  92. return container_of(dev, struct wpan_phy, dev);
  93. }
  94. EXPORT_SYMBOL(wpan_phy_find);
  95. struct wpan_phy_iter_data {
  96. int (*fn)(struct wpan_phy *phy, void *data);
  97. void *data;
  98. };
  99. static int wpan_phy_iter(struct device *dev, void *_data)
  100. {
  101. struct wpan_phy_iter_data *wpid = _data;
  102. struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
  103. return wpid->fn(phy, wpid->data);
  104. }
  105. int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
  106. void *data)
  107. {
  108. struct wpan_phy_iter_data wpid = {
  109. .fn = fn,
  110. .data = data,
  111. };
  112. return class_for_each_device(&wpan_phy_class, NULL,
  113. &wpid, wpan_phy_iter);
  114. }
  115. EXPORT_SYMBOL(wpan_phy_for_each);
  116. static int wpan_phy_idx_valid(int idx)
  117. {
  118. return idx >= 0;
  119. }
  120. struct wpan_phy *wpan_phy_alloc(size_t priv_size)
  121. {
  122. struct wpan_phy *phy = kzalloc(sizeof(*phy) + priv_size,
  123. GFP_KERNEL);
  124. mutex_lock(&wpan_phy_mutex);
  125. phy->idx = wpan_phy_idx++;
  126. if (unlikely(!wpan_phy_idx_valid(phy->idx))) {
  127. wpan_phy_idx--;
  128. mutex_unlock(&wpan_phy_mutex);
  129. kfree(phy);
  130. return NULL;
  131. }
  132. mutex_unlock(&wpan_phy_mutex);
  133. mutex_init(&phy->pib_lock);
  134. device_initialize(&phy->dev);
  135. dev_set_name(&phy->dev, "wpan-phy%d", phy->idx);
  136. phy->dev.class = &wpan_phy_class;
  137. phy->current_channel = -1; /* not initialised */
  138. phy->current_page = 0; /* for compatibility */
  139. return phy;
  140. }
  141. EXPORT_SYMBOL(wpan_phy_alloc);
  142. int wpan_phy_register(struct wpan_phy *phy)
  143. {
  144. return device_add(&phy->dev);
  145. }
  146. EXPORT_SYMBOL(wpan_phy_register);
  147. void wpan_phy_unregister(struct wpan_phy *phy)
  148. {
  149. device_del(&phy->dev);
  150. }
  151. EXPORT_SYMBOL(wpan_phy_unregister);
  152. void wpan_phy_free(struct wpan_phy *phy)
  153. {
  154. put_device(&phy->dev);
  155. }
  156. EXPORT_SYMBOL(wpan_phy_free);
  157. static int __init wpan_phy_class_init(void)
  158. {
  159. return class_register(&wpan_phy_class);
  160. }
  161. subsys_initcall(wpan_phy_class_init);
  162. static void __exit wpan_phy_class_exit(void)
  163. {
  164. class_unregister(&wpan_phy_class);
  165. }
  166. module_exit(wpan_phy_class_exit);
  167. MODULE_DESCRIPTION("IEEE 802.15.4 device class");
  168. MODULE_LICENSE("GPL v2");