wpan-class.c 5.1 KB

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