main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007-2012 Siemens AG
  4. *
  5. * Written by:
  6. * Alexander Smirnov <[email protected]>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/netdevice.h>
  11. #include <net/netlink.h>
  12. #include <net/nl802154.h>
  13. #include <net/mac802154.h>
  14. #include <net/ieee802154_netdev.h>
  15. #include <net/route.h>
  16. #include <net/cfg802154.h>
  17. #include "ieee802154_i.h"
  18. #include "cfg.h"
  19. static void ieee802154_tasklet_handler(struct tasklet_struct *t)
  20. {
  21. struct ieee802154_local *local = from_tasklet(local, t, tasklet);
  22. struct sk_buff *skb;
  23. while ((skb = skb_dequeue(&local->skb_queue))) {
  24. switch (skb->pkt_type) {
  25. case IEEE802154_RX_MSG:
  26. /* Clear skb->pkt_type in order to not confuse kernel
  27. * netstack.
  28. */
  29. skb->pkt_type = 0;
  30. ieee802154_rx(local, skb);
  31. break;
  32. default:
  33. WARN(1, "mac802154: Packet is of unknown type %d\n",
  34. skb->pkt_type);
  35. kfree_skb(skb);
  36. break;
  37. }
  38. }
  39. }
  40. struct ieee802154_hw *
  41. ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
  42. {
  43. struct wpan_phy *phy;
  44. struct ieee802154_local *local;
  45. size_t priv_size;
  46. if (WARN_ON(!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
  47. !ops->start || !ops->stop || !ops->set_channel))
  48. return NULL;
  49. /* Ensure 32-byte alignment of our private data and hw private data.
  50. * We use the wpan_phy priv data for both our ieee802154_local and for
  51. * the driver's private data
  52. *
  53. * in memory it'll be like this:
  54. *
  55. * +-------------------------+
  56. * | struct wpan_phy |
  57. * +-------------------------+
  58. * | struct ieee802154_local |
  59. * +-------------------------+
  60. * | driver's private data |
  61. * +-------------------------+
  62. *
  63. * Due to ieee802154 layer isn't aware of driver and MAC structures,
  64. * so lets align them here.
  65. */
  66. priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
  67. phy = wpan_phy_new(&mac802154_config_ops, priv_size);
  68. if (!phy) {
  69. pr_err("failure to allocate master IEEE802.15.4 device\n");
  70. return NULL;
  71. }
  72. phy->privid = mac802154_wpan_phy_privid;
  73. local = wpan_phy_priv(phy);
  74. local->phy = phy;
  75. local->hw.phy = local->phy;
  76. local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
  77. local->ops = ops;
  78. INIT_LIST_HEAD(&local->interfaces);
  79. mutex_init(&local->iflist_mtx);
  80. tasklet_setup(&local->tasklet, ieee802154_tasklet_handler);
  81. skb_queue_head_init(&local->skb_queue);
  82. INIT_WORK(&local->tx_work, ieee802154_xmit_worker);
  83. /* init supported flags with 802.15.4 default ranges */
  84. phy->supported.max_minbe = 8;
  85. phy->supported.min_maxbe = 3;
  86. phy->supported.max_maxbe = 8;
  87. phy->supported.min_frame_retries = 0;
  88. phy->supported.max_frame_retries = 7;
  89. phy->supported.max_csma_backoffs = 5;
  90. phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE;
  91. /* always supported */
  92. phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE);
  93. return &local->hw;
  94. }
  95. EXPORT_SYMBOL(ieee802154_alloc_hw);
  96. void ieee802154_configure_durations(struct wpan_phy *phy)
  97. {
  98. u32 duration = 0;
  99. switch (phy->current_page) {
  100. case 0:
  101. if (BIT(phy->current_channel) & 0x1)
  102. /* 868 MHz BPSK 802.15.4-2003: 20 ksym/s */
  103. duration = 50 * NSEC_PER_USEC;
  104. else if (BIT(phy->current_channel) & 0x7FE)
  105. /* 915 MHz BPSK 802.15.4-2003: 40 ksym/s */
  106. duration = 25 * NSEC_PER_USEC;
  107. else if (BIT(phy->current_channel) & 0x7FFF800)
  108. /* 2400 MHz O-QPSK 802.15.4-2006: 62.5 ksym/s */
  109. duration = 16 * NSEC_PER_USEC;
  110. break;
  111. case 2:
  112. if (BIT(phy->current_channel) & 0x1)
  113. /* 868 MHz O-QPSK 802.15.4-2006: 25 ksym/s */
  114. duration = 40 * NSEC_PER_USEC;
  115. else if (BIT(phy->current_channel) & 0x7FE)
  116. /* 915 MHz O-QPSK 802.15.4-2006: 62.5 ksym/s */
  117. duration = 16 * NSEC_PER_USEC;
  118. break;
  119. case 3:
  120. if (BIT(phy->current_channel) & 0x3FFF)
  121. /* 2.4 GHz CSS 802.15.4a-2007: 1/6 Msym/s */
  122. duration = 6 * NSEC_PER_USEC;
  123. break;
  124. default:
  125. break;
  126. }
  127. if (!duration) {
  128. pr_debug("Unknown PHY symbol duration\n");
  129. return;
  130. }
  131. phy->symbol_duration = duration;
  132. phy->lifs_period = (IEEE802154_LIFS_PERIOD * phy->symbol_duration) / NSEC_PER_SEC;
  133. phy->sifs_period = (IEEE802154_SIFS_PERIOD * phy->symbol_duration) / NSEC_PER_SEC;
  134. }
  135. EXPORT_SYMBOL(ieee802154_configure_durations);
  136. void ieee802154_free_hw(struct ieee802154_hw *hw)
  137. {
  138. struct ieee802154_local *local = hw_to_local(hw);
  139. BUG_ON(!list_empty(&local->interfaces));
  140. mutex_destroy(&local->iflist_mtx);
  141. wpan_phy_free(local->phy);
  142. }
  143. EXPORT_SYMBOL(ieee802154_free_hw);
  144. static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy)
  145. {
  146. /* TODO warn on empty symbol_duration
  147. * Should be done when all drivers sets this value.
  148. */
  149. wpan_phy->lifs_period =
  150. (IEEE802154_LIFS_PERIOD * wpan_phy->symbol_duration) / 1000;
  151. wpan_phy->sifs_period =
  152. (IEEE802154_SIFS_PERIOD * wpan_phy->symbol_duration) / 1000;
  153. }
  154. int ieee802154_register_hw(struct ieee802154_hw *hw)
  155. {
  156. struct ieee802154_local *local = hw_to_local(hw);
  157. struct net_device *dev;
  158. int rc = -ENOSYS;
  159. local->workqueue =
  160. create_singlethread_workqueue(wpan_phy_name(local->phy));
  161. if (!local->workqueue) {
  162. rc = -ENOMEM;
  163. goto out;
  164. }
  165. hrtimer_init(&local->ifs_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  166. local->ifs_timer.function = ieee802154_xmit_ifs_timer;
  167. wpan_phy_set_dev(local->phy, local->hw.parent);
  168. ieee802154_setup_wpan_phy_pib(local->phy);
  169. ieee802154_configure_durations(local->phy);
  170. if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) {
  171. local->phy->supported.min_csma_backoffs = 4;
  172. local->phy->supported.max_csma_backoffs = 4;
  173. local->phy->supported.min_maxbe = 5;
  174. local->phy->supported.max_maxbe = 5;
  175. local->phy->supported.min_minbe = 3;
  176. local->phy->supported.max_minbe = 3;
  177. }
  178. if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) {
  179. local->phy->supported.min_frame_retries = 3;
  180. local->phy->supported.max_frame_retries = 3;
  181. }
  182. if (hw->flags & IEEE802154_HW_PROMISCUOUS)
  183. local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR);
  184. rc = wpan_phy_register(local->phy);
  185. if (rc < 0)
  186. goto out_wq;
  187. rtnl_lock();
  188. dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
  189. NL802154_IFTYPE_NODE,
  190. cpu_to_le64(0x0000000000000000ULL));
  191. if (IS_ERR(dev)) {
  192. rtnl_unlock();
  193. rc = PTR_ERR(dev);
  194. goto out_phy;
  195. }
  196. rtnl_unlock();
  197. return 0;
  198. out_phy:
  199. wpan_phy_unregister(local->phy);
  200. out_wq:
  201. destroy_workqueue(local->workqueue);
  202. out:
  203. return rc;
  204. }
  205. EXPORT_SYMBOL(ieee802154_register_hw);
  206. void ieee802154_unregister_hw(struct ieee802154_hw *hw)
  207. {
  208. struct ieee802154_local *local = hw_to_local(hw);
  209. tasklet_kill(&local->tasklet);
  210. flush_workqueue(local->workqueue);
  211. rtnl_lock();
  212. ieee802154_remove_interfaces(local);
  213. rtnl_unlock();
  214. destroy_workqueue(local->workqueue);
  215. wpan_phy_unregister(local->phy);
  216. }
  217. EXPORT_SYMBOL(ieee802154_unregister_hw);
  218. static int __init ieee802154_init(void)
  219. {
  220. return ieee802154_iface_init();
  221. }
  222. static void __exit ieee802154_exit(void)
  223. {
  224. ieee802154_iface_exit();
  225. rcu_barrier();
  226. }
  227. subsys_initcall(ieee802154_init);
  228. module_exit(ieee802154_exit);
  229. MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
  230. MODULE_LICENSE("GPL v2");