main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. /* init supported flags with 802.15.4 default ranges */
  95. phy->supported.max_minbe = 8;
  96. phy->supported.min_maxbe = 3;
  97. phy->supported.max_maxbe = 8;
  98. phy->supported.min_frame_retries = -1;
  99. phy->supported.max_frame_retries = 7;
  100. phy->supported.max_csma_backoffs = 5;
  101. phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE;
  102. /* always supported */
  103. phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE);
  104. return &local->hw;
  105. }
  106. EXPORT_SYMBOL(ieee802154_alloc_hw);
  107. void ieee802154_free_hw(struct ieee802154_hw *hw)
  108. {
  109. struct ieee802154_local *local = hw_to_local(hw);
  110. BUG_ON(!list_empty(&local->interfaces));
  111. mutex_destroy(&local->iflist_mtx);
  112. wpan_phy_free(local->phy);
  113. }
  114. EXPORT_SYMBOL(ieee802154_free_hw);
  115. static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy)
  116. {
  117. /* TODO warn on empty symbol_duration
  118. * Should be done when all drivers sets this value.
  119. */
  120. wpan_phy->lifs_period = IEEE802154_LIFS_PERIOD *
  121. wpan_phy->symbol_duration;
  122. wpan_phy->sifs_period = IEEE802154_SIFS_PERIOD *
  123. wpan_phy->symbol_duration;
  124. }
  125. int ieee802154_register_hw(struct ieee802154_hw *hw)
  126. {
  127. struct ieee802154_local *local = hw_to_local(hw);
  128. struct net_device *dev;
  129. int rc = -ENOSYS;
  130. local->workqueue =
  131. create_singlethread_workqueue(wpan_phy_name(local->phy));
  132. if (!local->workqueue) {
  133. rc = -ENOMEM;
  134. goto out;
  135. }
  136. hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  137. local->ifs_timer.function = ieee802154_xmit_ifs_timer;
  138. wpan_phy_set_dev(local->phy, local->hw.parent);
  139. ieee802154_setup_wpan_phy_pib(local->phy);
  140. if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) {
  141. local->phy->supported.min_csma_backoffs = 4;
  142. local->phy->supported.max_csma_backoffs = 4;
  143. local->phy->supported.min_maxbe = 5;
  144. local->phy->supported.max_maxbe = 5;
  145. local->phy->supported.min_minbe = 3;
  146. local->phy->supported.max_minbe = 3;
  147. }
  148. if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) {
  149. /* TODO should be 3, but our default value is -1 which means
  150. * no ARET handling.
  151. */
  152. local->phy->supported.min_frame_retries = -1;
  153. local->phy->supported.max_frame_retries = -1;
  154. }
  155. if (hw->flags & IEEE802154_HW_PROMISCUOUS)
  156. local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR);
  157. rc = wpan_phy_register(local->phy);
  158. if (rc < 0)
  159. goto out_wq;
  160. rtnl_lock();
  161. dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
  162. NL802154_IFTYPE_NODE,
  163. cpu_to_le64(0x0000000000000000ULL));
  164. if (IS_ERR(dev)) {
  165. rtnl_unlock();
  166. rc = PTR_ERR(dev);
  167. goto out_phy;
  168. }
  169. rtnl_unlock();
  170. return 0;
  171. out_phy:
  172. wpan_phy_unregister(local->phy);
  173. out_wq:
  174. destroy_workqueue(local->workqueue);
  175. out:
  176. return rc;
  177. }
  178. EXPORT_SYMBOL(ieee802154_register_hw);
  179. void ieee802154_unregister_hw(struct ieee802154_hw *hw)
  180. {
  181. struct ieee802154_local *local = hw_to_local(hw);
  182. tasklet_kill(&local->tasklet);
  183. flush_workqueue(local->workqueue);
  184. destroy_workqueue(local->workqueue);
  185. rtnl_lock();
  186. ieee802154_remove_interfaces(local);
  187. rtnl_unlock();
  188. wpan_phy_unregister(local->phy);
  189. }
  190. EXPORT_SYMBOL(ieee802154_unregister_hw);
  191. static int __init ieee802154_init(void)
  192. {
  193. return ieee802154_iface_init();
  194. }
  195. static void __exit ieee802154_exit(void)
  196. {
  197. ieee802154_iface_exit();
  198. rcu_barrier();
  199. }
  200. subsys_initcall(ieee802154_init);
  201. module_exit(ieee802154_exit);
  202. MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
  203. MODULE_LICENSE("GPL v2");