core.c 3.4 KB

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