main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007-2012 Siemens AG
  4. *
  5. * Written by:
  6. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  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. INIT_LIST_HEAD(&local->rx_beacon_list);
  80. INIT_LIST_HEAD(&local->rx_mac_cmd_list);
  81. mutex_init(&local->iflist_mtx);
  82. tasklet_setup(&local->tasklet, ieee802154_tasklet_handler);
  83. skb_queue_head_init(&local->skb_queue);
  84. INIT_WORK(&local->sync_tx_work, ieee802154_xmit_sync_worker);
  85. INIT_DELAYED_WORK(&local->scan_work, mac802154_scan_worker);
  86. INIT_WORK(&local->rx_beacon_work, mac802154_rx_beacon_worker);
  87. INIT_DELAYED_WORK(&local->beacon_work, mac802154_beacon_worker);
  88. INIT_WORK(&local->rx_mac_cmd_work, mac802154_rx_mac_cmd_worker);
  89. init_completion(&local->assoc_done);
  90. /* init supported flags with 802.15.4 default ranges */
  91. phy->supported.max_minbe = 8;
  92. phy->supported.min_maxbe = 3;
  93. phy->supported.max_maxbe = 8;
  94. phy->supported.min_frame_retries = 0;
  95. phy->supported.max_frame_retries = 7;
  96. phy->supported.max_csma_backoffs = 5;
  97. phy->supported.lbt = NL802154_SUPPORTED_BOOL_FALSE;
  98. /* always supported */
  99. phy->supported.iftypes = BIT(NL802154_IFTYPE_NODE) | BIT(NL802154_IFTYPE_COORD);
  100. return &local->hw;
  101. }
  102. EXPORT_SYMBOL(ieee802154_alloc_hw);
  103. void ieee802154_configure_durations(struct wpan_phy *phy,
  104. unsigned int page, unsigned int channel)
  105. {
  106. u32 duration = 0;
  107. switch (page) {
  108. case 0:
  109. if (BIT(channel) & 0x1)
  110. /* 868 MHz BPSK 802.15.4-2003: 20 ksym/s */
  111. duration = 50 * NSEC_PER_USEC;
  112. else if (BIT(channel) & 0x7FE)
  113. /* 915 MHz BPSK 802.15.4-2003: 40 ksym/s */
  114. duration = 25 * NSEC_PER_USEC;
  115. else if (BIT(channel) & 0x7FFF800)
  116. /* 2400 MHz O-QPSK 802.15.4-2006: 62.5 ksym/s */
  117. duration = 16 * NSEC_PER_USEC;
  118. break;
  119. case 2:
  120. if (BIT(channel) & 0x1)
  121. /* 868 MHz O-QPSK 802.15.4-2006: 25 ksym/s */
  122. duration = 40 * NSEC_PER_USEC;
  123. else if (BIT(channel) & 0x7FE)
  124. /* 915 MHz O-QPSK 802.15.4-2006: 62.5 ksym/s */
  125. duration = 16 * NSEC_PER_USEC;
  126. break;
  127. case 3:
  128. if (BIT(channel) & 0x3FFF)
  129. /* 2.4 GHz CSS 802.15.4a-2007: 1/6 Msym/s */
  130. duration = 6 * NSEC_PER_USEC;
  131. break;
  132. default:
  133. break;
  134. }
  135. if (!duration) {
  136. pr_debug("Unknown PHY symbol duration\n");
  137. return;
  138. }
  139. phy->symbol_duration = duration;
  140. phy->lifs_period =
  141. (IEEE802154_LIFS_PERIOD * phy->symbol_duration) / NSEC_PER_USEC;
  142. phy->sifs_period =
  143. (IEEE802154_SIFS_PERIOD * phy->symbol_duration) / NSEC_PER_USEC;
  144. }
  145. EXPORT_SYMBOL(ieee802154_configure_durations);
  146. void ieee802154_free_hw(struct ieee802154_hw *hw)
  147. {
  148. struct ieee802154_local *local = hw_to_local(hw);
  149. BUG_ON(!list_empty(&local->interfaces));
  150. mutex_destroy(&local->iflist_mtx);
  151. wpan_phy_free(local->phy);
  152. }
  153. EXPORT_SYMBOL(ieee802154_free_hw);
  154. static void ieee802154_setup_wpan_phy_pib(struct wpan_phy *wpan_phy)
  155. {
  156. /* TODO warn on empty symbol_duration
  157. * Should be done when all drivers sets this value.
  158. */
  159. wpan_phy->lifs_period = (IEEE802154_LIFS_PERIOD *
  160. wpan_phy->symbol_duration) / NSEC_PER_USEC;
  161. wpan_phy->sifs_period = (IEEE802154_SIFS_PERIOD *
  162. wpan_phy->symbol_duration) / NSEC_PER_USEC;
  163. }
  164. int ieee802154_register_hw(struct ieee802154_hw *hw)
  165. {
  166. struct ieee802154_local *local = hw_to_local(hw);
  167. char mac_wq_name[IFNAMSIZ + 10] = {};
  168. struct net_device *dev;
  169. int rc = -ENOSYS;
  170. local->workqueue =
  171. create_singlethread_workqueue(wpan_phy_name(local->phy));
  172. if (!local->workqueue) {
  173. rc = -ENOMEM;
  174. goto out;
  175. }
  176. snprintf(mac_wq_name, IFNAMSIZ + 10, "%s-mac-cmds", wpan_phy_name(local->phy));
  177. local->mac_wq = create_singlethread_workqueue(mac_wq_name);
  178. if (!local->mac_wq) {
  179. rc = -ENOMEM;
  180. goto out_wq;
  181. }
  182. hrtimer_setup(&local->ifs_timer, ieee802154_xmit_ifs_timer, CLOCK_MONOTONIC,
  183. HRTIMER_MODE_REL);
  184. wpan_phy_set_dev(local->phy, local->hw.parent);
  185. ieee802154_setup_wpan_phy_pib(local->phy);
  186. ieee802154_configure_durations(local->phy, local->phy->current_page,
  187. local->phy->current_channel);
  188. if (!(hw->flags & IEEE802154_HW_CSMA_PARAMS)) {
  189. local->phy->supported.min_csma_backoffs = 4;
  190. local->phy->supported.max_csma_backoffs = 4;
  191. local->phy->supported.min_maxbe = 5;
  192. local->phy->supported.max_maxbe = 5;
  193. local->phy->supported.min_minbe = 3;
  194. local->phy->supported.max_minbe = 3;
  195. }
  196. if (!(hw->flags & IEEE802154_HW_FRAME_RETRIES)) {
  197. local->phy->supported.min_frame_retries = 3;
  198. local->phy->supported.max_frame_retries = 3;
  199. }
  200. if (hw->flags & IEEE802154_HW_PROMISCUOUS)
  201. local->phy->supported.iftypes |= BIT(NL802154_IFTYPE_MONITOR);
  202. rc = wpan_phy_register(local->phy);
  203. if (rc < 0)
  204. goto out_mac_wq;
  205. rtnl_lock();
  206. dev = ieee802154_if_add(local, "wpan%d", NET_NAME_ENUM,
  207. NL802154_IFTYPE_NODE,
  208. cpu_to_le64(0x0000000000000000ULL));
  209. if (IS_ERR(dev)) {
  210. rtnl_unlock();
  211. rc = PTR_ERR(dev);
  212. goto out_phy;
  213. }
  214. rtnl_unlock();
  215. return 0;
  216. out_phy:
  217. wpan_phy_unregister(local->phy);
  218. out_mac_wq:
  219. destroy_workqueue(local->mac_wq);
  220. out_wq:
  221. destroy_workqueue(local->workqueue);
  222. out:
  223. return rc;
  224. }
  225. EXPORT_SYMBOL(ieee802154_register_hw);
  226. void ieee802154_unregister_hw(struct ieee802154_hw *hw)
  227. {
  228. struct ieee802154_local *local = hw_to_local(hw);
  229. tasklet_kill(&local->tasklet);
  230. flush_workqueue(local->workqueue);
  231. rtnl_lock();
  232. ieee802154_remove_interfaces(local);
  233. rtnl_unlock();
  234. destroy_workqueue(local->mac_wq);
  235. destroy_workqueue(local->workqueue);
  236. wpan_phy_unregister(local->phy);
  237. }
  238. EXPORT_SYMBOL(ieee802154_unregister_hw);
  239. static int __init ieee802154_init(void)
  240. {
  241. return ieee802154_iface_init();
  242. }
  243. static void __exit ieee802154_exit(void)
  244. {
  245. ieee802154_iface_exit();
  246. rcu_barrier();
  247. }
  248. subsys_initcall(ieee802154_init);
  249. module_exit(ieee802154_exit);
  250. MODULE_DESCRIPTION("IEEE 802.15.4 subsystem");
  251. MODULE_LICENSE("GPL v2");