wpan-class.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #include "ieee802154.h"
  23. #define MASTER_SHOW_COMPLEX(name, format_string, args...) \
  24. static ssize_t name ## _show(struct device *dev, \
  25. struct device_attribute *attr, char *buf) \
  26. { \
  27. struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); \
  28. int ret; \
  29. \
  30. mutex_lock(&phy->pib_lock); \
  31. ret = snprintf(buf, PAGE_SIZE, format_string "\n", args); \
  32. mutex_unlock(&phy->pib_lock); \
  33. return ret; \
  34. }
  35. #define MASTER_SHOW(field, format_string) \
  36. MASTER_SHOW_COMPLEX(field, format_string, phy->field)
  37. MASTER_SHOW(current_channel, "%d");
  38. MASTER_SHOW(current_page, "%d");
  39. MASTER_SHOW_COMPLEX(transmit_power, "%d +- %d dB",
  40. ((signed char) (phy->transmit_power << 2)) >> 2,
  41. (phy->transmit_power >> 6) ? (phy->transmit_power >> 6) * 3 : 1 );
  42. MASTER_SHOW(cca_mode, "%d");
  43. static ssize_t channels_supported_show(struct device *dev,
  44. struct device_attribute *attr, char *buf)
  45. {
  46. struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
  47. int ret;
  48. int i, len = 0;
  49. mutex_lock(&phy->pib_lock);
  50. for (i = 0; i < 32; i++) {
  51. ret = snprintf(buf + len, PAGE_SIZE - len,
  52. "%#09x\n", phy->channels_supported[i]);
  53. if (ret < 0)
  54. break;
  55. len += ret;
  56. }
  57. mutex_unlock(&phy->pib_lock);
  58. return len;
  59. }
  60. static struct device_attribute pmib_attrs[] = {
  61. __ATTR_RO(current_channel),
  62. __ATTR_RO(current_page),
  63. __ATTR_RO(channels_supported),
  64. __ATTR_RO(transmit_power),
  65. __ATTR_RO(cca_mode),
  66. {},
  67. };
  68. static void wpan_phy_release(struct device *d)
  69. {
  70. struct wpan_phy *phy = container_of(d, struct wpan_phy, dev);
  71. kfree(phy);
  72. }
  73. static struct class wpan_phy_class = {
  74. .name = "ieee802154",
  75. .dev_release = wpan_phy_release,
  76. .dev_attrs = pmib_attrs,
  77. };
  78. static DEFINE_MUTEX(wpan_phy_mutex);
  79. static int wpan_phy_idx;
  80. static int wpan_phy_match(struct device *dev, void *data)
  81. {
  82. return !strcmp(dev_name(dev), (const char *)data);
  83. }
  84. struct wpan_phy *wpan_phy_find(const char *str)
  85. {
  86. struct device *dev;
  87. if (WARN_ON(!str))
  88. return NULL;
  89. dev = class_find_device(&wpan_phy_class, NULL,
  90. (void *)str, wpan_phy_match);
  91. if (!dev)
  92. return NULL;
  93. return container_of(dev, struct wpan_phy, dev);
  94. }
  95. EXPORT_SYMBOL(wpan_phy_find);
  96. struct wpan_phy_iter_data {
  97. int (*fn)(struct wpan_phy *phy, void *data);
  98. void *data;
  99. };
  100. static int wpan_phy_iter(struct device *dev, void *_data)
  101. {
  102. struct wpan_phy_iter_data *wpid = _data;
  103. struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
  104. return wpid->fn(phy, wpid->data);
  105. }
  106. int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
  107. void *data)
  108. {
  109. struct wpan_phy_iter_data wpid = {
  110. .fn = fn,
  111. .data = data,
  112. };
  113. return class_for_each_device(&wpan_phy_class, NULL,
  114. &wpid, wpan_phy_iter);
  115. }
  116. EXPORT_SYMBOL(wpan_phy_for_each);
  117. static int wpan_phy_idx_valid(int idx)
  118. {
  119. return idx >= 0;
  120. }
  121. struct wpan_phy *wpan_phy_alloc(size_t priv_size)
  122. {
  123. struct wpan_phy *phy = kzalloc(sizeof(*phy) + priv_size,
  124. GFP_KERNEL);
  125. mutex_lock(&wpan_phy_mutex);
  126. phy->idx = wpan_phy_idx++;
  127. if (unlikely(!wpan_phy_idx_valid(phy->idx))) {
  128. wpan_phy_idx--;
  129. mutex_unlock(&wpan_phy_mutex);
  130. kfree(phy);
  131. return NULL;
  132. }
  133. mutex_unlock(&wpan_phy_mutex);
  134. mutex_init(&phy->pib_lock);
  135. device_initialize(&phy->dev);
  136. dev_set_name(&phy->dev, "wpan-phy%d", phy->idx);
  137. phy->dev.class = &wpan_phy_class;
  138. phy->current_channel = -1; /* not initialised */
  139. phy->current_page = 0; /* for compatibility */
  140. return phy;
  141. }
  142. EXPORT_SYMBOL(wpan_phy_alloc);
  143. int wpan_phy_register(struct wpan_phy *phy)
  144. {
  145. return device_add(&phy->dev);
  146. }
  147. EXPORT_SYMBOL(wpan_phy_register);
  148. void wpan_phy_unregister(struct wpan_phy *phy)
  149. {
  150. device_del(&phy->dev);
  151. }
  152. EXPORT_SYMBOL(wpan_phy_unregister);
  153. void wpan_phy_free(struct wpan_phy *phy)
  154. {
  155. put_device(&phy->dev);
  156. }
  157. EXPORT_SYMBOL(wpan_phy_free);
  158. static int __init wpan_phy_class_init(void)
  159. {
  160. int rc;
  161. rc = class_register(&wpan_phy_class);
  162. if (rc)
  163. goto err;
  164. rc = ieee802154_nl_init();
  165. if (rc)
  166. goto err_nl;
  167. return 0;
  168. err_nl:
  169. class_unregister(&wpan_phy_class);
  170. err:
  171. return rc;
  172. }
  173. subsys_initcall(wpan_phy_class_init);
  174. static void __exit wpan_phy_class_exit(void)
  175. {
  176. ieee802154_nl_exit();
  177. class_unregister(&wpan_phy_class);
  178. }
  179. module_exit(wpan_phy_class_exit);
  180. MODULE_LICENSE("GPL v2");
  181. MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
  182. MODULE_AUTHOR("Dmitry Eremin-Solenikov");