ieee802154_dev.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/netdevice.h>
  25. #include <net/netlink.h>
  26. #include <linux/nl802154.h>
  27. #include <net/mac802154.h>
  28. #include <net/route.h>
  29. #include <net/wpan-phy.h>
  30. #include "mac802154.h"
  31. int mac802154_slave_open(struct net_device *dev)
  32. {
  33. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  34. struct mac802154_priv *ipriv = priv->hw;
  35. int res = 0;
  36. if (ipriv->open_count++ == 0) {
  37. res = ipriv->ops->start(&ipriv->hw);
  38. WARN_ON(res);
  39. if (res)
  40. goto err;
  41. }
  42. if (ipriv->ops->ieee_addr) {
  43. res = ipriv->ops->ieee_addr(&ipriv->hw, dev->dev_addr);
  44. WARN_ON(res);
  45. if (res)
  46. goto err;
  47. mac802154_dev_set_ieee_addr(dev);
  48. }
  49. netif_start_queue(dev);
  50. return 0;
  51. err:
  52. priv->hw->open_count--;
  53. return res;
  54. }
  55. int mac802154_slave_close(struct net_device *dev)
  56. {
  57. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  58. struct mac802154_priv *ipriv = priv->hw;
  59. netif_stop_queue(dev);
  60. if (!--ipriv->open_count)
  61. ipriv->ops->stop(&ipriv->hw);
  62. return 0;
  63. }
  64. static int
  65. mac802154_netdev_register(struct wpan_phy *phy, struct net_device *dev)
  66. {
  67. struct mac802154_sub_if_data *priv;
  68. struct mac802154_priv *ipriv;
  69. int err;
  70. ipriv = wpan_phy_priv(phy);
  71. priv = netdev_priv(dev);
  72. priv->dev = dev;
  73. priv->hw = ipriv;
  74. dev->needed_headroom = ipriv->hw.extra_tx_headroom;
  75. SET_NETDEV_DEV(dev, &ipriv->phy->dev);
  76. mutex_lock(&ipriv->slaves_mtx);
  77. if (!ipriv->running) {
  78. mutex_unlock(&ipriv->slaves_mtx);
  79. return -ENODEV;
  80. }
  81. mutex_unlock(&ipriv->slaves_mtx);
  82. err = register_netdev(dev);
  83. if (err < 0)
  84. return err;
  85. rtnl_lock();
  86. mutex_lock(&ipriv->slaves_mtx);
  87. list_add_tail_rcu(&priv->list, &ipriv->slaves);
  88. mutex_unlock(&ipriv->slaves_mtx);
  89. rtnl_unlock();
  90. return 0;
  91. }
  92. static void
  93. mac802154_del_iface(struct wpan_phy *phy, struct net_device *dev)
  94. {
  95. struct mac802154_sub_if_data *sdata;
  96. ASSERT_RTNL();
  97. sdata = netdev_priv(dev);
  98. BUG_ON(sdata->hw->phy != phy);
  99. mutex_lock(&sdata->hw->slaves_mtx);
  100. list_del_rcu(&sdata->list);
  101. mutex_unlock(&sdata->hw->slaves_mtx);
  102. synchronize_rcu();
  103. unregister_netdevice(sdata->dev);
  104. }
  105. static struct net_device *
  106. mac802154_add_iface(struct wpan_phy *phy, const char *name, int type)
  107. {
  108. struct net_device *dev;
  109. int err = -ENOMEM;
  110. switch (type) {
  111. case IEEE802154_DEV_MONITOR:
  112. dev = alloc_netdev(sizeof(struct mac802154_sub_if_data),
  113. name, mac802154_monitor_setup);
  114. break;
  115. case IEEE802154_DEV_WPAN:
  116. dev = alloc_netdev(sizeof(struct mac802154_sub_if_data),
  117. name, mac802154_wpan_setup);
  118. break;
  119. default:
  120. dev = NULL;
  121. err = -EINVAL;
  122. break;
  123. }
  124. if (!dev)
  125. goto err;
  126. err = mac802154_netdev_register(phy, dev);
  127. if (err)
  128. goto err_free;
  129. dev_hold(dev); /* we return an incremented device refcount */
  130. return dev;
  131. err_free:
  132. free_netdev(dev);
  133. err:
  134. return ERR_PTR(err);
  135. }
  136. struct ieee802154_dev *
  137. ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops)
  138. {
  139. struct wpan_phy *phy;
  140. struct mac802154_priv *priv;
  141. size_t priv_size;
  142. if (!ops || !ops->xmit || !ops->ed || !ops->start ||
  143. !ops->stop || !ops->set_channel) {
  144. printk(KERN_ERR
  145. "undefined IEEE802.15.4 device operations\n");
  146. return NULL;
  147. }
  148. /* Ensure 32-byte alignment of our private data and hw private data.
  149. * We use the wpan_phy priv data for both our mac802154_priv and for
  150. * the driver's private data
  151. *
  152. * in memory it'll be like this:
  153. *
  154. * +-----------------------+
  155. * | struct wpan_phy |
  156. * +-----------------------+
  157. * | struct mac802154_priv |
  158. * +-----------------------+
  159. * | driver's private data |
  160. * +-----------------------+
  161. *
  162. * Due to ieee802154 layer isn't aware of driver and MAC structures,
  163. * so lets allign them here.
  164. */
  165. priv_size = ALIGN(sizeof(*priv), NETDEV_ALIGN) + priv_data_len;
  166. phy = wpan_phy_alloc(priv_size);
  167. if (!phy) {
  168. printk(KERN_ERR
  169. "failure to allocate master IEEE802.15.4 device\n");
  170. return NULL;
  171. }
  172. priv = wpan_phy_priv(phy);
  173. priv->hw.phy = priv->phy = phy;
  174. priv->hw.priv = (char *)priv + ALIGN(sizeof(*priv), NETDEV_ALIGN);
  175. priv->ops = ops;
  176. INIT_LIST_HEAD(&priv->slaves);
  177. mutex_init(&priv->slaves_mtx);
  178. return &priv->hw;
  179. }
  180. EXPORT_SYMBOL(ieee802154_alloc_device);
  181. void ieee802154_free_device(struct ieee802154_dev *hw)
  182. {
  183. struct mac802154_priv *priv = mac802154_to_priv(hw);
  184. BUG_ON(!list_empty(&priv->slaves));
  185. wpan_phy_free(priv->phy);
  186. mutex_destroy(&priv->slaves_mtx);
  187. }
  188. EXPORT_SYMBOL(ieee802154_free_device);
  189. int ieee802154_register_device(struct ieee802154_dev *dev)
  190. {
  191. struct mac802154_priv *priv = mac802154_to_priv(dev);
  192. int rc = -ENOMEM;
  193. priv->dev_workqueue =
  194. create_singlethread_workqueue(wpan_phy_name(priv->phy));
  195. if (!priv->dev_workqueue)
  196. goto out;
  197. wpan_phy_set_dev(priv->phy, priv->hw.parent);
  198. priv->phy->add_iface = mac802154_add_iface;
  199. priv->phy->del_iface = mac802154_del_iface;
  200. rc = wpan_phy_register(priv->phy);
  201. if (rc < 0)
  202. goto out_wq;
  203. rtnl_lock();
  204. mutex_lock(&priv->slaves_mtx);
  205. priv->running = MAC802154_DEVICE_RUN;
  206. mutex_unlock(&priv->slaves_mtx);
  207. rtnl_unlock();
  208. return 0;
  209. out_wq:
  210. destroy_workqueue(priv->dev_workqueue);
  211. out:
  212. return rc;
  213. }
  214. EXPORT_SYMBOL(ieee802154_register_device);
  215. void ieee802154_unregister_device(struct ieee802154_dev *dev)
  216. {
  217. struct mac802154_priv *priv = mac802154_to_priv(dev);
  218. struct mac802154_sub_if_data *sdata, *next;
  219. flush_workqueue(priv->dev_workqueue);
  220. destroy_workqueue(priv->dev_workqueue);
  221. rtnl_lock();
  222. mutex_lock(&priv->slaves_mtx);
  223. priv->running = MAC802154_DEVICE_STOPPED;
  224. mutex_unlock(&priv->slaves_mtx);
  225. list_for_each_entry_safe(sdata, next, &priv->slaves, list) {
  226. mutex_lock(&sdata->hw->slaves_mtx);
  227. list_del(&sdata->list);
  228. mutex_unlock(&sdata->hw->slaves_mtx);
  229. unregister_netdevice(sdata->dev);
  230. }
  231. rtnl_unlock();
  232. wpan_phy_unregister(priv->phy);
  233. }
  234. EXPORT_SYMBOL(ieee802154_unregister_device);
  235. MODULE_DESCRIPTION("IEEE 802.15.4 implementation");
  236. MODULE_LICENSE("GPL v2");