core.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. */
  14. #include <linux/slab.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/device.h>
  18. #include <net/cfg802154.h>
  19. #include "ieee802154.h"
  20. #include "sysfs.h"
  21. #include "core.h"
  22. static DEFINE_MUTEX(wpan_phy_mutex);
  23. static int wpan_phy_idx;
  24. static int wpan_phy_match(struct device *dev, const void *data)
  25. {
  26. return !strcmp(dev_name(dev), (const char *)data);
  27. }
  28. struct wpan_phy *wpan_phy_find(const char *str)
  29. {
  30. struct device *dev;
  31. if (WARN_ON(!str))
  32. return NULL;
  33. dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match);
  34. if (!dev)
  35. return NULL;
  36. return container_of(dev, struct wpan_phy, dev);
  37. }
  38. EXPORT_SYMBOL(wpan_phy_find);
  39. struct wpan_phy_iter_data {
  40. int (*fn)(struct wpan_phy *phy, void *data);
  41. void *data;
  42. };
  43. static int wpan_phy_iter(struct device *dev, void *_data)
  44. {
  45. struct wpan_phy_iter_data *wpid = _data;
  46. struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
  47. return wpid->fn(phy, wpid->data);
  48. }
  49. int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
  50. void *data)
  51. {
  52. struct wpan_phy_iter_data wpid = {
  53. .fn = fn,
  54. .data = data,
  55. };
  56. return class_for_each_device(&wpan_phy_class, NULL,
  57. &wpid, wpan_phy_iter);
  58. }
  59. EXPORT_SYMBOL(wpan_phy_for_each);
  60. static int wpan_phy_idx_valid(int idx)
  61. {
  62. return idx >= 0;
  63. }
  64. struct wpan_phy *
  65. wpan_phy_alloc(const struct cfg802154_ops *ops, size_t priv_size)
  66. {
  67. struct cfg802154_registered_device *rdev;
  68. size_t alloc_size;
  69. alloc_size = sizeof(*rdev) + priv_size;
  70. rdev = kzalloc(alloc_size, GFP_KERNEL);
  71. if (!rdev)
  72. return NULL;
  73. rdev->ops = ops;
  74. mutex_lock(&wpan_phy_mutex);
  75. rdev->wpan_phy.idx = wpan_phy_idx++;
  76. if (unlikely(!wpan_phy_idx_valid(rdev->wpan_phy.idx))) {
  77. wpan_phy_idx--;
  78. mutex_unlock(&wpan_phy_mutex);
  79. kfree(rdev);
  80. goto out;
  81. }
  82. mutex_unlock(&wpan_phy_mutex);
  83. mutex_init(&rdev->wpan_phy.pib_lock);
  84. device_initialize(&rdev->wpan_phy.dev);
  85. dev_set_name(&rdev->wpan_phy.dev, "wpan-phy%d", rdev->wpan_phy.idx);
  86. rdev->wpan_phy.dev.class = &wpan_phy_class;
  87. rdev->wpan_phy.dev.platform_data = rdev;
  88. return &rdev->wpan_phy;
  89. out:
  90. return NULL;
  91. }
  92. EXPORT_SYMBOL(wpan_phy_alloc);
  93. int wpan_phy_register(struct wpan_phy *phy)
  94. {
  95. return device_add(&phy->dev);
  96. }
  97. EXPORT_SYMBOL(wpan_phy_register);
  98. void wpan_phy_unregister(struct wpan_phy *phy)
  99. {
  100. device_del(&phy->dev);
  101. }
  102. EXPORT_SYMBOL(wpan_phy_unregister);
  103. void wpan_phy_free(struct wpan_phy *phy)
  104. {
  105. put_device(&phy->dev);
  106. }
  107. EXPORT_SYMBOL(wpan_phy_free);
  108. void cfg802154_dev_free(struct cfg802154_registered_device *rdev)
  109. {
  110. kfree(rdev);
  111. }
  112. static int __init wpan_phy_class_init(void)
  113. {
  114. int rc;
  115. rc = wpan_phy_sysfs_init();
  116. if (rc)
  117. goto err;
  118. rc = ieee802154_nl_init();
  119. if (rc)
  120. goto err_nl;
  121. return 0;
  122. err_nl:
  123. wpan_phy_sysfs_exit();
  124. err:
  125. return rc;
  126. }
  127. subsys_initcall(wpan_phy_class_init);
  128. static void __exit wpan_phy_class_exit(void)
  129. {
  130. ieee802154_nl_exit();
  131. wpan_phy_sysfs_exit();
  132. }
  133. module_exit(wpan_phy_class_exit);
  134. MODULE_LICENSE("GPL v2");
  135. MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
  136. MODULE_AUTHOR("Dmitry Eremin-Solenikov");