wpan-class.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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_COMPLEX(transmit_power, "%d +- %d dB",
  42. ((signed char) (phy->transmit_power << 2)) >> 2,
  43. (phy->transmit_power >> 6) ? (phy->transmit_power >> 6) * 3 : 1 );
  44. MASTER_SHOW(cca_mode, "%d");
  45. static ssize_t channels_supported_show(struct device *dev,
  46. struct device_attribute *attr, 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 struct attribute *pmib_attrs[] = {
  64. &dev_attr_current_channel.attr,
  65. &dev_attr_current_page.attr,
  66. &dev_attr_channels_supported.attr,
  67. &dev_attr_transmit_power.attr,
  68. &dev_attr_cca_mode.attr,
  69. NULL,
  70. };
  71. ATTRIBUTE_GROUPS(pmib);
  72. static void wpan_phy_release(struct device *d)
  73. {
  74. struct wpan_phy *phy = container_of(d, struct wpan_phy, dev);
  75. kfree(phy);
  76. }
  77. static struct class wpan_phy_class = {
  78. .name = "ieee802154",
  79. .dev_release = wpan_phy_release,
  80. .dev_groups = pmib_groups,
  81. };
  82. static DEFINE_MUTEX(wpan_phy_mutex);
  83. static int wpan_phy_idx;
  84. static int wpan_phy_match(struct device *dev, const void *data)
  85. {
  86. return !strcmp(dev_name(dev), (const char *)data);
  87. }
  88. struct wpan_phy *wpan_phy_find(const char *str)
  89. {
  90. struct device *dev;
  91. if (WARN_ON(!str))
  92. return NULL;
  93. dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match);
  94. if (!dev)
  95. return NULL;
  96. return container_of(dev, struct wpan_phy, dev);
  97. }
  98. EXPORT_SYMBOL(wpan_phy_find);
  99. struct wpan_phy_iter_data {
  100. int (*fn)(struct wpan_phy *phy, void *data);
  101. void *data;
  102. };
  103. static int wpan_phy_iter(struct device *dev, void *_data)
  104. {
  105. struct wpan_phy_iter_data *wpid = _data;
  106. struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
  107. return wpid->fn(phy, wpid->data);
  108. }
  109. int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
  110. void *data)
  111. {
  112. struct wpan_phy_iter_data wpid = {
  113. .fn = fn,
  114. .data = data,
  115. };
  116. return class_for_each_device(&wpan_phy_class, NULL,
  117. &wpid, wpan_phy_iter);
  118. }
  119. EXPORT_SYMBOL(wpan_phy_for_each);
  120. static int wpan_phy_idx_valid(int idx)
  121. {
  122. return idx >= 0;
  123. }
  124. struct wpan_phy *wpan_phy_alloc(size_t priv_size)
  125. {
  126. struct wpan_phy *phy = kzalloc(sizeof(*phy) + priv_size,
  127. GFP_KERNEL);
  128. if (!phy)
  129. goto out;
  130. mutex_lock(&wpan_phy_mutex);
  131. phy->idx = wpan_phy_idx++;
  132. if (unlikely(!wpan_phy_idx_valid(phy->idx))) {
  133. wpan_phy_idx--;
  134. mutex_unlock(&wpan_phy_mutex);
  135. kfree(phy);
  136. goto out;
  137. }
  138. mutex_unlock(&wpan_phy_mutex);
  139. mutex_init(&phy->pib_lock);
  140. device_initialize(&phy->dev);
  141. dev_set_name(&phy->dev, "wpan-phy%d", phy->idx);
  142. phy->dev.class = &wpan_phy_class;
  143. phy->current_channel = -1; /* not initialised */
  144. phy->current_page = 0; /* for compatibility */
  145. return phy;
  146. out:
  147. return NULL;
  148. }
  149. EXPORT_SYMBOL(wpan_phy_alloc);
  150. int wpan_phy_register(struct wpan_phy *phy)
  151. {
  152. return device_add(&phy->dev);
  153. }
  154. EXPORT_SYMBOL(wpan_phy_register);
  155. void wpan_phy_unregister(struct wpan_phy *phy)
  156. {
  157. device_del(&phy->dev);
  158. }
  159. EXPORT_SYMBOL(wpan_phy_unregister);
  160. void wpan_phy_free(struct wpan_phy *phy)
  161. {
  162. put_device(&phy->dev);
  163. }
  164. EXPORT_SYMBOL(wpan_phy_free);
  165. static int __init wpan_phy_class_init(void)
  166. {
  167. int rc;
  168. rc = class_register(&wpan_phy_class);
  169. if (rc)
  170. goto err;
  171. rc = ieee802154_nl_init();
  172. if (rc)
  173. goto err_nl;
  174. return 0;
  175. err_nl:
  176. class_unregister(&wpan_phy_class);
  177. err:
  178. return rc;
  179. }
  180. subsys_initcall(wpan_phy_class_init);
  181. static void __exit wpan_phy_class_exit(void)
  182. {
  183. ieee802154_nl_exit();
  184. class_unregister(&wpan_phy_class);
  185. }
  186. module_exit(wpan_phy_class_exit);
  187. MODULE_LICENSE("GPL v2");
  188. MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
  189. MODULE_AUTHOR("Dmitry Eremin-Solenikov");