ieee802154_dev.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. default:
  116. dev = NULL;
  117. err = -EINVAL;
  118. break;
  119. }
  120. if (!dev)
  121. goto err;
  122. err = mac802154_netdev_register(phy, dev);
  123. if (err)
  124. goto err_free;
  125. dev_hold(dev); /* we return an incremented device refcount */
  126. return dev;
  127. err_free:
  128. free_netdev(dev);
  129. err:
  130. return ERR_PTR(err);
  131. }
  132. struct ieee802154_dev *
  133. ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops)
  134. {
  135. struct wpan_phy *phy;
  136. struct mac802154_priv *priv;
  137. size_t priv_size;
  138. if (!ops || !ops->xmit || !ops->ed || !ops->start ||
  139. !ops->stop || !ops->set_channel) {
  140. printk(KERN_ERR
  141. "undefined IEEE802.15.4 device operations\n");
  142. return NULL;
  143. }
  144. /* Ensure 32-byte alignment of our private data and hw private data.
  145. * We use the wpan_phy priv data for both our mac802154_priv and for
  146. * the driver's private data
  147. *
  148. * in memory it'll be like this:
  149. *
  150. * +-----------------------+
  151. * | struct wpan_phy |
  152. * +-----------------------+
  153. * | struct mac802154_priv |
  154. * +-----------------------+
  155. * | driver's private data |
  156. * +-----------------------+
  157. *
  158. * Due to ieee802154 layer isn't aware of driver and MAC structures,
  159. * so lets allign them here.
  160. */
  161. priv_size = ALIGN(sizeof(*priv), NETDEV_ALIGN) + priv_data_len;
  162. phy = wpan_phy_alloc(priv_size);
  163. if (!phy) {
  164. printk(KERN_ERR
  165. "failure to allocate master IEEE802.15.4 device\n");
  166. return NULL;
  167. }
  168. priv = wpan_phy_priv(phy);
  169. priv->hw.phy = priv->phy = phy;
  170. priv->hw.priv = (char *)priv + ALIGN(sizeof(*priv), NETDEV_ALIGN);
  171. priv->ops = ops;
  172. INIT_LIST_HEAD(&priv->slaves);
  173. mutex_init(&priv->slaves_mtx);
  174. return &priv->hw;
  175. }
  176. EXPORT_SYMBOL(ieee802154_alloc_device);
  177. void ieee802154_free_device(struct ieee802154_dev *hw)
  178. {
  179. struct mac802154_priv *priv = mac802154_to_priv(hw);
  180. BUG_ON(!list_empty(&priv->slaves));
  181. wpan_phy_free(priv->phy);
  182. mutex_destroy(&priv->slaves_mtx);
  183. }
  184. EXPORT_SYMBOL(ieee802154_free_device);
  185. int ieee802154_register_device(struct ieee802154_dev *dev)
  186. {
  187. struct mac802154_priv *priv = mac802154_to_priv(dev);
  188. int rc = -ENOMEM;
  189. priv->dev_workqueue =
  190. create_singlethread_workqueue(wpan_phy_name(priv->phy));
  191. if (!priv->dev_workqueue)
  192. goto out;
  193. wpan_phy_set_dev(priv->phy, priv->hw.parent);
  194. priv->phy->add_iface = mac802154_add_iface;
  195. priv->phy->del_iface = mac802154_del_iface;
  196. rc = wpan_phy_register(priv->phy);
  197. if (rc < 0)
  198. goto out_wq;
  199. rtnl_lock();
  200. mutex_lock(&priv->slaves_mtx);
  201. priv->running = MAC802154_DEVICE_RUN;
  202. mutex_unlock(&priv->slaves_mtx);
  203. rtnl_unlock();
  204. return 0;
  205. out_wq:
  206. destroy_workqueue(priv->dev_workqueue);
  207. out:
  208. return rc;
  209. }
  210. EXPORT_SYMBOL(ieee802154_register_device);
  211. void ieee802154_unregister_device(struct ieee802154_dev *dev)
  212. {
  213. struct mac802154_priv *priv = mac802154_to_priv(dev);
  214. struct mac802154_sub_if_data *sdata, *next;
  215. flush_workqueue(priv->dev_workqueue);
  216. destroy_workqueue(priv->dev_workqueue);
  217. rtnl_lock();
  218. mutex_lock(&priv->slaves_mtx);
  219. priv->running = MAC802154_DEVICE_STOPPED;
  220. mutex_unlock(&priv->slaves_mtx);
  221. list_for_each_entry_safe(sdata, next, &priv->slaves, list) {
  222. mutex_lock(&sdata->hw->slaves_mtx);
  223. list_del(&sdata->list);
  224. mutex_unlock(&sdata->hw->slaves_mtx);
  225. unregister_netdevice(sdata->dev);
  226. }
  227. rtnl_unlock();
  228. wpan_phy_unregister(priv->phy);
  229. }
  230. EXPORT_SYMBOL(ieee802154_unregister_device);
  231. MODULE_DESCRIPTION("IEEE 802.15.4 implementation");
  232. MODULE_LICENSE("GPL v2");