core.c 7.2 KB

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