main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (C) 2007-2012 Siemens AG
  3. *
  4. * Written by:
  5. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  6. *
  7. * Based on the code from 'linux-zigbee.sourceforge.net' project.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/netdevice.h>
  21. #include <net/netlink.h>
  22. #include <linux/nl802154.h>
  23. #include <net/mac802154.h>
  24. #include <net/ieee802154_netdev.h>
  25. #include <net/route.h>
  26. #include <net/cfg802154.h>
  27. #include "ieee802154_i.h"
  28. #include "cfg.h"
  29. static int
  30. mac802154_netdev_register(struct wpan_phy *phy, struct net_device *dev)
  31. {
  32. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  33. struct ieee802154_local *local;
  34. int err;
  35. local = wpan_phy_priv(phy);
  36. sdata->dev = dev;
  37. sdata->local = local;
  38. dev->needed_headroom = local->hw.extra_tx_headroom;
  39. SET_NETDEV_DEV(dev, &local->phy->dev);
  40. err = register_netdev(dev);
  41. if (err < 0)
  42. return err;
  43. rtnl_lock();
  44. mutex_lock(&local->iflist_mtx);
  45. list_add_tail_rcu(&sdata->list, &local->interfaces);
  46. mutex_unlock(&local->iflist_mtx);
  47. rtnl_unlock();
  48. return 0;
  49. }
  50. void mac802154_del_iface(struct wpan_phy *phy, struct net_device *dev)
  51. {
  52. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  53. ASSERT_RTNL();
  54. BUG_ON(sdata->local->phy != phy);
  55. mutex_lock(&sdata->local->iflist_mtx);
  56. list_del_rcu(&sdata->list);
  57. mutex_unlock(&sdata->local->iflist_mtx);
  58. synchronize_rcu();
  59. unregister_netdevice(sdata->dev);
  60. }
  61. struct net_device *
  62. mac802154_add_iface(struct wpan_phy *phy, const char *name, int type)
  63. {
  64. struct net_device *dev;
  65. int err = -ENOMEM;
  66. switch (type) {
  67. case IEEE802154_DEV_MONITOR:
  68. dev = alloc_netdev(sizeof(struct ieee802154_sub_if_data),
  69. name, NET_NAME_UNKNOWN,
  70. mac802154_monitor_setup);
  71. break;
  72. case IEEE802154_DEV_WPAN:
  73. dev = alloc_netdev(sizeof(struct ieee802154_sub_if_data),
  74. name, NET_NAME_UNKNOWN,
  75. mac802154_wpan_setup);
  76. break;
  77. default:
  78. dev = NULL;
  79. err = -EINVAL;
  80. break;
  81. }
  82. if (!dev)
  83. goto err;
  84. err = mac802154_netdev_register(phy, dev);
  85. if (err)
  86. goto err_free;
  87. dev_hold(dev); /* we return an incremented device refcount */
  88. return dev;
  89. err_free:
  90. free_netdev(dev);
  91. err:
  92. return ERR_PTR(err);
  93. }
  94. static void ieee802154_tasklet_handler(unsigned long data)
  95. {
  96. struct ieee802154_local *local = (struct ieee802154_local *)data;
  97. struct sk_buff *skb;
  98. while ((skb = skb_dequeue(&local->skb_queue))) {
  99. switch (skb->pkt_type) {
  100. case IEEE802154_RX_MSG:
  101. /* Clear skb->pkt_type in order to not confuse kernel
  102. * netstack.
  103. */
  104. skb->pkt_type = 0;
  105. ieee802154_rx(&local->hw, skb);
  106. break;
  107. default:
  108. WARN(1, "mac802154: Packet is of unknown type %d\n",
  109. skb->pkt_type);
  110. kfree_skb(skb);
  111. break;
  112. }
  113. }
  114. }
  115. struct ieee802154_hw *
  116. ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
  117. {
  118. struct wpan_phy *phy;
  119. struct ieee802154_local *local;
  120. size_t priv_size;
  121. if (!ops || !(ops->xmit_async || ops->xmit_sync) || !ops->ed ||
  122. !ops->start || !ops->stop || !ops->set_channel) {
  123. pr_err("undefined IEEE802.15.4 device operations\n");
  124. return NULL;
  125. }
  126. /* Ensure 32-byte alignment of our private data and hw private data.
  127. * We use the wpan_phy priv data for both our ieee802154_local and for
  128. * the driver's private data
  129. *
  130. * in memory it'll be like this:
  131. *
  132. * +-------------------------+
  133. * | struct wpan_phy |
  134. * +-------------------------+
  135. * | struct ieee802154_local |
  136. * +-------------------------+
  137. * | driver's private data |
  138. * +-------------------------+
  139. *
  140. * Due to ieee802154 layer isn't aware of driver and MAC structures,
  141. * so lets align them here.
  142. */
  143. priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
  144. phy = wpan_phy_alloc(&mac802154_config_ops, priv_size);
  145. if (!phy) {
  146. pr_err("failure to allocate master IEEE802.15.4 device\n");
  147. return NULL;
  148. }
  149. local = wpan_phy_priv(phy);
  150. local->phy = phy;
  151. local->hw.phy = local->phy;
  152. local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
  153. local->ops = ops;
  154. INIT_LIST_HEAD(&local->interfaces);
  155. mutex_init(&local->iflist_mtx);
  156. tasklet_init(&local->tasklet,
  157. ieee802154_tasklet_handler,
  158. (unsigned long)local);
  159. skb_queue_head_init(&local->skb_queue);
  160. return &local->hw;
  161. }
  162. EXPORT_SYMBOL(ieee802154_alloc_hw);
  163. void ieee802154_free_hw(struct ieee802154_hw *hw)
  164. {
  165. struct ieee802154_local *local = hw_to_local(hw);
  166. BUG_ON(!list_empty(&local->interfaces));
  167. mutex_destroy(&local->iflist_mtx);
  168. wpan_phy_free(local->phy);
  169. }
  170. EXPORT_SYMBOL(ieee802154_free_hw);
  171. int ieee802154_register_hw(struct ieee802154_hw *hw)
  172. {
  173. struct ieee802154_local *local = hw_to_local(hw);
  174. int rc = -ENOSYS;
  175. local->workqueue =
  176. create_singlethread_workqueue(wpan_phy_name(local->phy));
  177. if (!local->workqueue) {
  178. rc = -ENOMEM;
  179. goto out;
  180. }
  181. wpan_phy_set_dev(local->phy, local->hw.parent);
  182. rc = wpan_phy_register(local->phy);
  183. if (rc < 0)
  184. goto out_wq;
  185. return 0;
  186. out_wq:
  187. destroy_workqueue(local->workqueue);
  188. out:
  189. return rc;
  190. }
  191. EXPORT_SYMBOL(ieee802154_register_hw);
  192. void ieee802154_unregister_hw(struct ieee802154_hw *hw)
  193. {
  194. struct ieee802154_local *local = hw_to_local(hw);
  195. struct ieee802154_sub_if_data *sdata, *next;
  196. tasklet_kill(&local->tasklet);
  197. flush_workqueue(local->workqueue);
  198. destroy_workqueue(local->workqueue);
  199. rtnl_lock();
  200. list_for_each_entry_safe(sdata, next, &local->interfaces, list) {
  201. mutex_lock(&sdata->local->iflist_mtx);
  202. list_del(&sdata->list);
  203. mutex_unlock(&sdata->local->iflist_mtx);
  204. unregister_netdevice(sdata->dev);
  205. }
  206. rtnl_unlock();
  207. wpan_phy_unregister(local->phy);
  208. }
  209. EXPORT_SYMBOL(ieee802154_unregister_hw);
  210. MODULE_DESCRIPTION("IEEE 802.15.4 implementation");
  211. MODULE_LICENSE("GPL v2");