core.c 7.0 KB

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