core.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 <net/rtnetlink.h>
  20. #include "ieee802154.h"
  21. #include "nl802154.h"
  22. #include "sysfs.h"
  23. #include "core.h"
  24. /* RCU-protected (and RTNL for writers) */
  25. LIST_HEAD(cfg802154_rdev_list);
  26. int cfg802154_rdev_list_generation;
  27. static int wpan_phy_match(struct device *dev, const void *data)
  28. {
  29. return !strcmp(dev_name(dev), (const char *)data);
  30. }
  31. struct wpan_phy *wpan_phy_find(const char *str)
  32. {
  33. struct device *dev;
  34. if (WARN_ON(!str))
  35. return NULL;
  36. dev = class_find_device(&wpan_phy_class, NULL, str, wpan_phy_match);
  37. if (!dev)
  38. return NULL;
  39. return container_of(dev, struct wpan_phy, dev);
  40. }
  41. EXPORT_SYMBOL(wpan_phy_find);
  42. struct wpan_phy_iter_data {
  43. int (*fn)(struct wpan_phy *phy, void *data);
  44. void *data;
  45. };
  46. static int wpan_phy_iter(struct device *dev, void *_data)
  47. {
  48. struct wpan_phy_iter_data *wpid = _data;
  49. struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
  50. return wpid->fn(phy, wpid->data);
  51. }
  52. int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data),
  53. void *data)
  54. {
  55. struct wpan_phy_iter_data wpid = {
  56. .fn = fn,
  57. .data = data,
  58. };
  59. return class_for_each_device(&wpan_phy_class, NULL,
  60. &wpid, wpan_phy_iter);
  61. }
  62. EXPORT_SYMBOL(wpan_phy_for_each);
  63. struct cfg802154_registered_device *
  64. cfg802154_rdev_by_wpan_phy_idx(int wpan_phy_idx)
  65. {
  66. struct cfg802154_registered_device *result = NULL, *rdev;
  67. ASSERT_RTNL();
  68. list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
  69. if (rdev->wpan_phy_idx == wpan_phy_idx) {
  70. result = rdev;
  71. break;
  72. }
  73. }
  74. return result;
  75. }
  76. struct wpan_phy *
  77. wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size)
  78. {
  79. static atomic_t wpan_phy_counter = ATOMIC_INIT(0);
  80. struct cfg802154_registered_device *rdev;
  81. size_t alloc_size;
  82. alloc_size = sizeof(*rdev) + priv_size;
  83. rdev = kzalloc(alloc_size, GFP_KERNEL);
  84. if (!rdev)
  85. return NULL;
  86. rdev->ops = ops;
  87. rdev->wpan_phy_idx = atomic_inc_return(&wpan_phy_counter);
  88. if (unlikely(rdev->wpan_phy_idx < 0)) {
  89. /* ugh, wrapped! */
  90. atomic_dec(&wpan_phy_counter);
  91. kfree(rdev);
  92. return NULL;
  93. }
  94. /* atomic_inc_return makes it start at 1, make it start at 0 */
  95. rdev->wpan_phy_idx--;
  96. mutex_init(&rdev->wpan_phy.pib_lock);
  97. INIT_LIST_HEAD(&rdev->wpan_dev_list);
  98. device_initialize(&rdev->wpan_phy.dev);
  99. dev_set_name(&rdev->wpan_phy.dev, "wpan-phy%d", rdev->wpan_phy_idx);
  100. rdev->wpan_phy.dev.class = &wpan_phy_class;
  101. rdev->wpan_phy.dev.platform_data = rdev;
  102. init_waitqueue_head(&rdev->dev_wait);
  103. return &rdev->wpan_phy;
  104. }
  105. EXPORT_SYMBOL(wpan_phy_new);
  106. int wpan_phy_register(struct wpan_phy *phy)
  107. {
  108. struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
  109. int ret;
  110. rtnl_lock();
  111. ret = device_add(&phy->dev);
  112. if (ret) {
  113. rtnl_unlock();
  114. return ret;
  115. }
  116. list_add_rcu(&rdev->list, &cfg802154_rdev_list);
  117. cfg802154_rdev_list_generation++;
  118. /* TODO phy registered lock */
  119. rtnl_unlock();
  120. /* TODO nl802154 phy notify */
  121. return 0;
  122. }
  123. EXPORT_SYMBOL(wpan_phy_register);
  124. void wpan_phy_unregister(struct wpan_phy *phy)
  125. {
  126. struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
  127. wait_event(rdev->dev_wait, ({
  128. int __count;
  129. rtnl_lock();
  130. __count = rdev->opencount;
  131. rtnl_unlock();
  132. __count == 0; }));
  133. rtnl_lock();
  134. /* TODO nl802154 phy notify */
  135. /* TODO phy registered lock */
  136. WARN_ON(!list_empty(&rdev->wpan_dev_list));
  137. /* First remove the hardware from everywhere, this makes
  138. * it impossible to find from userspace.
  139. */
  140. list_del_rcu(&rdev->list);
  141. synchronize_rcu();
  142. cfg802154_rdev_list_generation++;
  143. device_del(&phy->dev);
  144. rtnl_unlock();
  145. }
  146. EXPORT_SYMBOL(wpan_phy_unregister);
  147. void wpan_phy_free(struct wpan_phy *phy)
  148. {
  149. put_device(&phy->dev);
  150. }
  151. EXPORT_SYMBOL(wpan_phy_free);
  152. void cfg802154_dev_free(struct cfg802154_registered_device *rdev)
  153. {
  154. kfree(rdev);
  155. }
  156. static void
  157. cfg802154_update_iface_num(struct cfg802154_registered_device *rdev,
  158. int iftype, int num)
  159. {
  160. ASSERT_RTNL();
  161. rdev->num_running_ifaces += num;
  162. }
  163. static int cfg802154_netdev_notifier_call(struct notifier_block *nb,
  164. unsigned long state, void *ptr)
  165. {
  166. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  167. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  168. struct cfg802154_registered_device *rdev;
  169. if (!wpan_dev)
  170. return NOTIFY_DONE;
  171. rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
  172. /* TODO WARN_ON unspec type */
  173. switch (state) {
  174. /* TODO NETDEV_DEVTYPE */
  175. case NETDEV_REGISTER:
  176. wpan_dev->identifier = ++rdev->wpan_dev_id;
  177. list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list);
  178. rdev->devlist_generation++;
  179. wpan_dev->netdev = dev;
  180. break;
  181. case NETDEV_DOWN:
  182. cfg802154_update_iface_num(rdev, wpan_dev->iftype, -1);
  183. rdev->opencount--;
  184. wake_up(&rdev->dev_wait);
  185. break;
  186. case NETDEV_UP:
  187. cfg802154_update_iface_num(rdev, wpan_dev->iftype, 1);
  188. rdev->opencount++;
  189. break;
  190. case NETDEV_UNREGISTER:
  191. /* It is possible to get NETDEV_UNREGISTER
  192. * multiple times. To detect that, check
  193. * that the interface is still on the list
  194. * of registered interfaces, and only then
  195. * remove and clean it up.
  196. */
  197. if (!list_empty(&wpan_dev->list)) {
  198. list_del_rcu(&wpan_dev->list);
  199. rdev->devlist_generation++;
  200. }
  201. /* synchronize (so that we won't find this netdev
  202. * from other code any more) and then clear the list
  203. * head so that the above code can safely check for
  204. * !list_empty() to avoid double-cleanup.
  205. */
  206. synchronize_rcu();
  207. INIT_LIST_HEAD(&wpan_dev->list);
  208. break;
  209. default:
  210. return NOTIFY_DONE;
  211. }
  212. return NOTIFY_OK;
  213. }
  214. static struct notifier_block cfg802154_netdev_notifier = {
  215. .notifier_call = cfg802154_netdev_notifier_call,
  216. };
  217. static int __init wpan_phy_class_init(void)
  218. {
  219. int rc;
  220. rc = wpan_phy_sysfs_init();
  221. if (rc)
  222. goto err;
  223. rc = register_netdevice_notifier(&cfg802154_netdev_notifier);
  224. if (rc)
  225. goto err_nl;
  226. rc = ieee802154_nl_init();
  227. if (rc)
  228. goto err_notifier;
  229. rc = nl802154_init();
  230. if (rc)
  231. goto err_ieee802154_nl;
  232. return 0;
  233. err_ieee802154_nl:
  234. ieee802154_nl_exit();
  235. err_notifier:
  236. unregister_netdevice_notifier(&cfg802154_netdev_notifier);
  237. err_nl:
  238. wpan_phy_sysfs_exit();
  239. err:
  240. return rc;
  241. }
  242. subsys_initcall(wpan_phy_class_init);
  243. static void __exit wpan_phy_class_exit(void)
  244. {
  245. nl802154_exit();
  246. ieee802154_nl_exit();
  247. unregister_netdevice_notifier(&cfg802154_netdev_notifier);
  248. wpan_phy_sysfs_exit();
  249. }
  250. module_exit(wpan_phy_class_exit);
  251. MODULE_LICENSE("GPL v2");
  252. MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
  253. MODULE_AUTHOR("Dmitry Eremin-Solenikov");