core.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. INIT_LIST_HEAD(&rdev->wpan_dev_list);
  99. device_initialize(&rdev->wpan_phy.dev);
  100. dev_set_name(&rdev->wpan_phy.dev, PHY_NAME "%d", rdev->wpan_phy_idx);
  101. rdev->wpan_phy.dev.class = &wpan_phy_class;
  102. rdev->wpan_phy.dev.platform_data = rdev;
  103. init_waitqueue_head(&rdev->dev_wait);
  104. return &rdev->wpan_phy;
  105. }
  106. EXPORT_SYMBOL(wpan_phy_new);
  107. int wpan_phy_register(struct wpan_phy *phy)
  108. {
  109. struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
  110. int ret;
  111. rtnl_lock();
  112. ret = device_add(&phy->dev);
  113. if (ret) {
  114. rtnl_unlock();
  115. return ret;
  116. }
  117. list_add_rcu(&rdev->list, &cfg802154_rdev_list);
  118. cfg802154_rdev_list_generation++;
  119. /* TODO phy registered lock */
  120. rtnl_unlock();
  121. /* TODO nl802154 phy notify */
  122. return 0;
  123. }
  124. EXPORT_SYMBOL(wpan_phy_register);
  125. void wpan_phy_unregister(struct wpan_phy *phy)
  126. {
  127. struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
  128. wait_event(rdev->dev_wait, ({
  129. int __count;
  130. rtnl_lock();
  131. __count = rdev->opencount;
  132. rtnl_unlock();
  133. __count == 0; }));
  134. rtnl_lock();
  135. /* TODO nl802154 phy notify */
  136. /* TODO phy registered lock */
  137. WARN_ON(!list_empty(&rdev->wpan_dev_list));
  138. /* First remove the hardware from everywhere, this makes
  139. * it impossible to find from userspace.
  140. */
  141. list_del_rcu(&rdev->list);
  142. synchronize_rcu();
  143. cfg802154_rdev_list_generation++;
  144. device_del(&phy->dev);
  145. rtnl_unlock();
  146. }
  147. EXPORT_SYMBOL(wpan_phy_unregister);
  148. void wpan_phy_free(struct wpan_phy *phy)
  149. {
  150. put_device(&phy->dev);
  151. }
  152. EXPORT_SYMBOL(wpan_phy_free);
  153. void cfg802154_dev_free(struct cfg802154_registered_device *rdev)
  154. {
  155. kfree(rdev);
  156. }
  157. static void
  158. cfg802154_update_iface_num(struct cfg802154_registered_device *rdev,
  159. int iftype, int num)
  160. {
  161. ASSERT_RTNL();
  162. rdev->num_running_ifaces += num;
  163. }
  164. static int cfg802154_netdev_notifier_call(struct notifier_block *nb,
  165. unsigned long state, void *ptr)
  166. {
  167. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  168. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  169. struct cfg802154_registered_device *rdev;
  170. if (!wpan_dev)
  171. return NOTIFY_DONE;
  172. rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
  173. /* TODO WARN_ON unspec type */
  174. switch (state) {
  175. /* TODO NETDEV_DEVTYPE */
  176. case NETDEV_REGISTER:
  177. dev->features |= NETIF_F_NETNS_LOCAL;
  178. wpan_dev->identifier = ++rdev->wpan_dev_id;
  179. list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list);
  180. rdev->devlist_generation++;
  181. wpan_dev->netdev = dev;
  182. break;
  183. case NETDEV_DOWN:
  184. cfg802154_update_iface_num(rdev, wpan_dev->iftype, -1);
  185. rdev->opencount--;
  186. wake_up(&rdev->dev_wait);
  187. break;
  188. case NETDEV_UP:
  189. cfg802154_update_iface_num(rdev, wpan_dev->iftype, 1);
  190. rdev->opencount++;
  191. break;
  192. case NETDEV_UNREGISTER:
  193. /* It is possible to get NETDEV_UNREGISTER
  194. * multiple times. To detect that, check
  195. * that the interface is still on the list
  196. * of registered interfaces, and only then
  197. * remove and clean it up.
  198. */
  199. if (!list_empty(&wpan_dev->list)) {
  200. list_del_rcu(&wpan_dev->list);
  201. rdev->devlist_generation++;
  202. }
  203. /* synchronize (so that we won't find this netdev
  204. * from other code any more) and then clear the list
  205. * head so that the above code can safely check for
  206. * !list_empty() to avoid double-cleanup.
  207. */
  208. synchronize_rcu();
  209. INIT_LIST_HEAD(&wpan_dev->list);
  210. break;
  211. default:
  212. return NOTIFY_DONE;
  213. }
  214. return NOTIFY_OK;
  215. }
  216. static struct notifier_block cfg802154_netdev_notifier = {
  217. .notifier_call = cfg802154_netdev_notifier_call,
  218. };
  219. static int __init wpan_phy_class_init(void)
  220. {
  221. int rc;
  222. rc = wpan_phy_sysfs_init();
  223. if (rc)
  224. goto err;
  225. rc = register_netdevice_notifier(&cfg802154_netdev_notifier);
  226. if (rc)
  227. goto err_nl;
  228. rc = ieee802154_nl_init();
  229. if (rc)
  230. goto err_notifier;
  231. rc = nl802154_init();
  232. if (rc)
  233. goto err_ieee802154_nl;
  234. return 0;
  235. err_ieee802154_nl:
  236. ieee802154_nl_exit();
  237. err_notifier:
  238. unregister_netdevice_notifier(&cfg802154_netdev_notifier);
  239. err_nl:
  240. wpan_phy_sysfs_exit();
  241. err:
  242. return rc;
  243. }
  244. subsys_initcall(wpan_phy_class_init);
  245. static void __exit wpan_phy_class_exit(void)
  246. {
  247. nl802154_exit();
  248. ieee802154_nl_exit();
  249. unregister_netdevice_notifier(&cfg802154_netdev_notifier);
  250. wpan_phy_sysfs_exit();
  251. }
  252. module_exit(wpan_phy_class_exit);
  253. MODULE_LICENSE("GPL v2");
  254. MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
  255. MODULE_AUTHOR("Dmitry Eremin-Solenikov");