core.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. wpan_phy_net_set(&rdev->wpan_phy, &init_net);
  113. init_waitqueue_head(&rdev->dev_wait);
  114. return &rdev->wpan_phy;
  115. }
  116. EXPORT_SYMBOL(wpan_phy_new);
  117. int wpan_phy_register(struct wpan_phy *phy)
  118. {
  119. struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
  120. int ret;
  121. rtnl_lock();
  122. ret = device_add(&phy->dev);
  123. if (ret) {
  124. rtnl_unlock();
  125. return ret;
  126. }
  127. list_add_rcu(&rdev->list, &cfg802154_rdev_list);
  128. cfg802154_rdev_list_generation++;
  129. /* TODO phy registered lock */
  130. rtnl_unlock();
  131. /* TODO nl802154 phy notify */
  132. return 0;
  133. }
  134. EXPORT_SYMBOL(wpan_phy_register);
  135. void wpan_phy_unregister(struct wpan_phy *phy)
  136. {
  137. struct cfg802154_registered_device *rdev = wpan_phy_to_rdev(phy);
  138. wait_event(rdev->dev_wait, ({
  139. int __count;
  140. rtnl_lock();
  141. __count = rdev->opencount;
  142. rtnl_unlock();
  143. __count == 0; }));
  144. rtnl_lock();
  145. /* TODO nl802154 phy notify */
  146. /* TODO phy registered lock */
  147. WARN_ON(!list_empty(&rdev->wpan_dev_list));
  148. /* First remove the hardware from everywhere, this makes
  149. * it impossible to find from userspace.
  150. */
  151. list_del_rcu(&rdev->list);
  152. synchronize_rcu();
  153. cfg802154_rdev_list_generation++;
  154. device_del(&phy->dev);
  155. rtnl_unlock();
  156. }
  157. EXPORT_SYMBOL(wpan_phy_unregister);
  158. void wpan_phy_free(struct wpan_phy *phy)
  159. {
  160. put_device(&phy->dev);
  161. }
  162. EXPORT_SYMBOL(wpan_phy_free);
  163. int cfg802154_switch_netns(struct cfg802154_registered_device *rdev,
  164. struct net *net)
  165. {
  166. struct wpan_dev *wpan_dev;
  167. int err = 0;
  168. list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
  169. if (!wpan_dev->netdev)
  170. continue;
  171. wpan_dev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
  172. err = dev_change_net_namespace(wpan_dev->netdev, net, "wpan%d");
  173. if (err)
  174. break;
  175. wpan_dev->netdev->features |= NETIF_F_NETNS_LOCAL;
  176. }
  177. if (err) {
  178. /* failed -- clean up to old netns */
  179. net = wpan_phy_net(&rdev->wpan_phy);
  180. list_for_each_entry_continue_reverse(wpan_dev,
  181. &rdev->wpan_dev_list,
  182. list) {
  183. if (!wpan_dev->netdev)
  184. continue;
  185. wpan_dev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
  186. err = dev_change_net_namespace(wpan_dev->netdev, net,
  187. "wpan%d");
  188. WARN_ON(err);
  189. wpan_dev->netdev->features |= NETIF_F_NETNS_LOCAL;
  190. }
  191. return err;
  192. }
  193. wpan_phy_net_set(&rdev->wpan_phy, net);
  194. err = device_rename(&rdev->wpan_phy.dev, dev_name(&rdev->wpan_phy.dev));
  195. WARN_ON(err);
  196. return 0;
  197. }
  198. void cfg802154_dev_free(struct cfg802154_registered_device *rdev)
  199. {
  200. kfree(rdev);
  201. }
  202. static void
  203. cfg802154_update_iface_num(struct cfg802154_registered_device *rdev,
  204. int iftype, int num)
  205. {
  206. ASSERT_RTNL();
  207. rdev->num_running_ifaces += num;
  208. }
  209. static int cfg802154_netdev_notifier_call(struct notifier_block *nb,
  210. unsigned long state, void *ptr)
  211. {
  212. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  213. struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
  214. struct cfg802154_registered_device *rdev;
  215. if (!wpan_dev)
  216. return NOTIFY_DONE;
  217. rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
  218. /* TODO WARN_ON unspec type */
  219. switch (state) {
  220. /* TODO NETDEV_DEVTYPE */
  221. case NETDEV_REGISTER:
  222. dev->features |= NETIF_F_NETNS_LOCAL;
  223. wpan_dev->identifier = ++rdev->wpan_dev_id;
  224. list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list);
  225. rdev->devlist_generation++;
  226. wpan_dev->netdev = dev;
  227. break;
  228. case NETDEV_DOWN:
  229. cfg802154_update_iface_num(rdev, wpan_dev->iftype, -1);
  230. rdev->opencount--;
  231. wake_up(&rdev->dev_wait);
  232. break;
  233. case NETDEV_UP:
  234. cfg802154_update_iface_num(rdev, wpan_dev->iftype, 1);
  235. rdev->opencount++;
  236. break;
  237. case NETDEV_UNREGISTER:
  238. /* It is possible to get NETDEV_UNREGISTER
  239. * multiple times. To detect that, check
  240. * that the interface is still on the list
  241. * of registered interfaces, and only then
  242. * remove and clean it up.
  243. */
  244. if (!list_empty(&wpan_dev->list)) {
  245. list_del_rcu(&wpan_dev->list);
  246. rdev->devlist_generation++;
  247. }
  248. /* synchronize (so that we won't find this netdev
  249. * from other code any more) and then clear the list
  250. * head so that the above code can safely check for
  251. * !list_empty() to avoid double-cleanup.
  252. */
  253. synchronize_rcu();
  254. INIT_LIST_HEAD(&wpan_dev->list);
  255. break;
  256. default:
  257. return NOTIFY_DONE;
  258. }
  259. return NOTIFY_OK;
  260. }
  261. static struct notifier_block cfg802154_netdev_notifier = {
  262. .notifier_call = cfg802154_netdev_notifier_call,
  263. };
  264. static void __net_exit cfg802154_pernet_exit(struct net *net)
  265. {
  266. struct cfg802154_registered_device *rdev;
  267. rtnl_lock();
  268. list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
  269. if (net_eq(wpan_phy_net(&rdev->wpan_phy), net))
  270. WARN_ON(cfg802154_switch_netns(rdev, &init_net));
  271. }
  272. rtnl_unlock();
  273. }
  274. static struct pernet_operations cfg802154_pernet_ops = {
  275. .exit = cfg802154_pernet_exit,
  276. };
  277. static int __init wpan_phy_class_init(void)
  278. {
  279. int rc;
  280. rc = register_pernet_device(&cfg802154_pernet_ops);
  281. if (rc)
  282. goto err;
  283. rc = wpan_phy_sysfs_init();
  284. if (rc)
  285. goto err_sysfs;
  286. rc = register_netdevice_notifier(&cfg802154_netdev_notifier);
  287. if (rc)
  288. goto err_nl;
  289. rc = ieee802154_nl_init();
  290. if (rc)
  291. goto err_notifier;
  292. rc = nl802154_init();
  293. if (rc)
  294. goto err_ieee802154_nl;
  295. return 0;
  296. err_ieee802154_nl:
  297. ieee802154_nl_exit();
  298. err_notifier:
  299. unregister_netdevice_notifier(&cfg802154_netdev_notifier);
  300. err_nl:
  301. wpan_phy_sysfs_exit();
  302. err_sysfs:
  303. unregister_pernet_device(&cfg802154_pernet_ops);
  304. err:
  305. return rc;
  306. }
  307. subsys_initcall(wpan_phy_class_init);
  308. static void __exit wpan_phy_class_exit(void)
  309. {
  310. nl802154_exit();
  311. ieee802154_nl_exit();
  312. unregister_netdevice_notifier(&cfg802154_netdev_notifier);
  313. wpan_phy_sysfs_exit();
  314. unregister_pernet_device(&cfg802154_pernet_ops);
  315. }
  316. module_exit(wpan_phy_class_exit);
  317. MODULE_LICENSE("GPL v2");
  318. MODULE_DESCRIPTION("IEEE 802.15.4 configuration interface");
  319. MODULE_AUTHOR("Dmitry Eremin-Solenikov");