main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2007-2012 Siemens AG
  3. *
  4. * Written by:
  5. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/netdevice.h>
  19. #include <net/netlink.h>
  20. #include <net/nl802154.h>
  21. #include <net/mac802154.h>
  22. #include <net/ieee802154_netdev.h>
  23. #include <net/route.h>
  24. #include <net/cfg802154.h>
  25. #include "ieee802154_i.h"
  26. #include "cfg.h"
  27. static void ieee802154_tasklet_handler(unsigned long data)
  28. {
  29. struct ieee802154_local *local = (struct ieee802154_local *)data;
  30. struct sk_buff *skb;
  31. while ((skb = skb_dequeue(&local->skb_queue))) {
  32. switch (skb->pkt_type) {
  33. case IEEE802154_RX_MSG:
  34. /* Clear skb->pkt_type in order to not confuse kernel
  35. * netstack.
  36. */
  37. skb->pkt_type = 0;
  38. ieee802154_rx(&local->hw, skb);
  39. break;
  40. default:
  41. WARN(1, "mac802154: Packet is of unknown type %d\n",
  42. skb->pkt_type);
  43. kfree_skb(skb);
  44. break;
  45. }
  46. }
  47. }
  48. struct ieee802154_hw *
  49. ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
  50. {
  51. struct wpan_phy *phy;
  52. struct ieee802154_local *local;
  53. size_t priv_size;
  54. if (!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
  55. !ops->start || !ops->stop || !ops->set_channel) {
  56. pr_err("undefined IEEE802.15.4 device operations\n");
  57. return NULL;
  58. }
  59. /* Ensure 32-byte alignment of our private data and hw private data.
  60. * We use the wpan_phy priv data for both our ieee802154_local and for
  61. * the driver's private data
  62. *
  63. * in memory it'll be like this:
  64. *
  65. * +-------------------------+
  66. * | struct wpan_phy |
  67. * +-------------------------+
  68. * | struct ieee802154_local |
  69. * +-------------------------+
  70. * | driver's private data |
  71. * +-------------------------+
  72. *
  73. * Due to ieee802154 layer isn't aware of driver and MAC structures,
  74. * so lets align them here.
  75. */
  76. priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
  77. phy = wpan_phy_new(&mac802154_config_ops, priv_size);
  78. if (!phy) {
  79. pr_err("failure to allocate master IEEE802.15.4 device\n");
  80. return NULL;
  81. }
  82. phy->privid = mac802154_wpan_phy_privid;
  83. local = wpan_phy_priv(phy);
  84. local->phy = phy;
  85. local->hw.phy = local->phy;
  86. local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
  87. local->ops = ops;
  88. INIT_LIST_HEAD(&local->interfaces);
  89. mutex_init(&local->iflist_mtx);
  90. tasklet_init(&local->tasklet,
  91. ieee802154_tasklet_handler,
  92. (unsigned long)local);
  93. skb_queue_head_init(&local->skb_queue);
  94. return &local->hw;
  95. }
  96. EXPORT_SYMBOL(ieee802154_alloc_hw);
  97. void ieee802154_free_hw(struct ieee802154_hw *hw)
  98. {
  99. struct ieee802154_local *local = hw_to_local(hw);
  100. BUG_ON(!list_empty(&local->interfaces));
  101. mutex_destroy(&local->iflist_mtx);
  102. wpan_phy_free(local->phy);
  103. }
  104. EXPORT_SYMBOL(ieee802154_free_hw);
  105. static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy)
  106. {
  107. /* TODO warn on empty symbol_duration
  108. * Should be done when all drivers sets this value.
  109. */
  110. wpan_phy->lifs_period = IEEE802154_LIFS_PERIOD *
  111. wpan_phy->symbol_duration;
  112. wpan_phy->sifs_period = IEEE802154_SIFS_PERIOD *
  113. wpan_phy->symbol_duration;
  114. }
  115. int ieee802154_register_hw(struct ieee802154_hw *hw)
  116. {
  117. struct ieee802154_local *local = hw_to_local(hw);
  118. struct net_device *dev;
  119. int rc = -ENOSYS;
  120. local->workqueue =
  121. create_singlethread_workqueue(wpan_phy_name(local->phy));
  122. if (!local->workqueue) {
  123. rc = -ENOMEM;
  124. goto out;
  125. }
  126. hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  127. local->ifs_timer.function = ieee802154_xmit_ifs_timer;
  128. wpan_phy_set_dev(local->phy, local->hw.parent);
  129. ieee802154_setup_wpan_phy_pib(local->phy);
  130. rc = wpan_phy_register(local->phy);
  131. if (rc < 0)
  132. goto out_wq;
  133. rtnl_lock();
  134. dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
  135. NL802154_IFTYPE_NODE,
  136. cpu_to_le64(0x0000000000000000ULL));
  137. if (IS_ERR(dev)) {
  138. rtnl_unlock();
  139. rc = PTR_ERR(dev);
  140. goto out_phy;
  141. }
  142. rtnl_unlock();
  143. return 0;
  144. out_phy:
  145. wpan_phy_unregister(local->phy);
  146. out_wq:
  147. destroy_workqueue(local->workqueue);
  148. out:
  149. return rc;
  150. }
  151. EXPORT_SYMBOL(ieee802154_register_hw);
  152. void ieee802154_unregister_hw(struct ieee802154_hw *hw)
  153. {
  154. struct ieee802154_local *local = hw_to_local(hw);
  155. tasklet_kill(&local->tasklet);
  156. flush_workqueue(local->workqueue);
  157. destroy_workqueue(local->workqueue);
  158. rtnl_lock();
  159. ieee802154_remove_interfaces(local);
  160. rtnl_unlock();
  161. wpan_phy_unregister(local->phy);
  162. }
  163. EXPORT_SYMBOL(ieee802154_unregister_hw);
  164. static int __init ieee802154_init(void)
  165. {
  166. return ieee802154_iface_init();
  167. }
  168. static void __exit ieee802154_exit(void)
  169. {
  170. ieee802154_iface_exit();
  171. rcu_barrier();
  172. }
  173. subsys_initcall(ieee802154_init);
  174. module_exit(ieee802154_exit);
  175. MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
  176. MODULE_LICENSE("GPL v2");