main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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, 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 (WARN_ON(!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
  55. !ops->start || !ops->stop || !ops->set_channel))
  56. return NULL;
  57. /* Ensure 32-byte alignment of our private data and hw private data.
  58. * We use the wpan_phy priv data for both our ieee802154_local and for
  59. * the driver's private data
  60. *
  61. * in memory it'll be like this:
  62. *
  63. * +-------------------------+
  64. * | struct wpan_phy |
  65. * +-------------------------+
  66. * | struct ieee802154_local |
  67. * +-------------------------+
  68. * | driver's private data |
  69. * +-------------------------+
  70. *
  71. * Due to ieee802154 layer isn't aware of driver and MAC structures,
  72. * so lets align them here.
  73. */
  74. priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
  75. phy = wpan_phy_new(&mac802154_config_ops, priv_size);
  76. if (!phy) {
  77. pr_err("failure to allocate master IEEE802.15.4 device\n");
  78. return NULL;
  79. }
  80. phy->privid = mac802154_wpan_phy_privid;
  81. local = wpan_phy_priv(phy);
  82. local->phy = phy;
  83. local->hw.phy = local->phy;
  84. local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
  85. local->ops = ops;
  86. INIT_LIST_HEAD(&local->interfaces);
  87. mutex_init(&local->iflist_mtx);
  88. tasklet_init(&local->tasklet,
  89. ieee802154_tasklet_handler,
  90. (unsigned long)local);
  91. skb_queue_head_init(&local->skb_queue);
  92. INIT_WORK(&local->tx_work, ieee802154_xmit_worker);
  93. /* init supported flags with 802.15.4 default ranges */
  94. phy->supported.max_minbe = 8;
  95. phy->supported.min_maxbe = 3;
  96. phy->supported.max_maxbe = 8;
  97. phy->supported.min_frame_retries = 0;
  98. phy->supported.max_frame_retries = 7;
  99. phy->supported.max_csma_backoffs = 5;
  100. phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE;
  101. /* always supported */
  102. phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE);
  103. return &local->hw;
  104. }
  105. EXPORT_SYMBOL(ieee802154_alloc_hw);
  106. void ieee802154_free_hw(struct ieee802154_hw *hw)
  107. {
  108. struct ieee802154_local *local = hw_to_local(hw);
  109. BUG_ON(!list_empty(&local->interfaces));
  110. mutex_destroy(&local->iflist_mtx);
  111. wpan_phy_free(local->phy);
  112. }
  113. EXPORT_SYMBOL(ieee802154_free_hw);
  114. static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy)
  115. {
  116. /* TODO warn on empty symbol_duration
  117. * Should be done when all drivers sets this value.
  118. */
  119. wpan_phy->lifs_period = IEEE802154_LIFS_PERIOD *
  120. wpan_phy->symbol_duration;
  121. wpan_phy->sifs_period = IEEE802154_SIFS_PERIOD *
  122. wpan_phy->symbol_duration;
  123. }
  124. int ieee802154_register_hw(struct ieee802154_hw *hw)
  125. {
  126. struct ieee802154_local *local = hw_to_local(hw);
  127. struct net_device *dev;
  128. int rc = -ENOSYS;
  129. local->workqueue =
  130. create_singlethread_workqueue(wpan_phy_name(local->phy));
  131. if (!local->workqueue) {
  132. rc = -ENOMEM;
  133. goto out;
  134. }
  135. hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  136. local->ifs_timer.function = ieee802154_xmit_ifs_timer;
  137. wpan_phy_set_dev(local->phy, local->hw.parent);
  138. ieee802154_setup_wpan_phy_pib(local->phy);
  139. if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) {
  140. local->phy->supported.min_csma_backoffs = 4;
  141. local->phy->supported.max_csma_backoffs = 4;
  142. local->phy->supported.min_maxbe = 5;
  143. local->phy->supported.max_maxbe = 5;
  144. local->phy->supported.min_minbe = 3;
  145. local->phy->supported.max_minbe = 3;
  146. }
  147. if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) {
  148. local->phy->supported.min_frame_retries = 3;
  149. local->phy->supported.max_frame_retries = 3;
  150. }
  151. if (hw->flags & IEEE802154_HW_PROMISCUOUS)
  152. local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR);
  153. rc = wpan_phy_register(local->phy);
  154. if (rc < 0)
  155. goto out_wq;
  156. rtnl_lock();
  157. dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
  158. NL802154_IFTYPE_NODE,
  159. cpu_to_le64(0x0000000000000000ULL));
  160. if (IS_ERR(dev)) {
  161. rtnl_unlock();
  162. rc = PTR_ERR(dev);
  163. goto out_phy;
  164. }
  165. rtnl_unlock();
  166. return 0;
  167. out_phy:
  168. wpan_phy_unregister(local->phy);
  169. out_wq:
  170. destroy_workqueue(local->workqueue);
  171. out:
  172. return rc;
  173. }
  174. EXPORT_SYMBOL(ieee802154_register_hw);
  175. void ieee802154_unregister_hw(struct ieee802154_hw *hw)
  176. {
  177. struct ieee802154_local *local = hw_to_local(hw);
  178. tasklet_kill(&local->tasklet);
  179. flush_workqueue(local->workqueue);
  180. destroy_workqueue(local->workqueue);
  181. rtnl_lock();
  182. ieee802154_remove_interfaces(local);
  183. rtnl_unlock();
  184. wpan_phy_unregister(local->phy);
  185. }
  186. EXPORT_SYMBOL(ieee802154_unregister_hw);
  187. static int __init ieee802154_init(void)
  188. {
  189. return ieee802154_iface_init();
  190. }
  191. static void __exit ieee802154_exit(void)
  192. {
  193. ieee802154_iface_exit();
  194. rcu_barrier();
  195. }
  196. subsys_initcall(ieee802154_init);
  197. module_exit(ieee802154_exit);
  198. MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
  199. MODULE_LICENSE("GPL v2");