mib.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2007-2012 Siemens AG
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Written by:
  18. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  19. * Sergey Lapin <slapin@ossfans.org>
  20. * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
  21. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  22. */
  23. #include <linux/if_arp.h>
  24. #include <net/mac802154.h>
  25. #include <net/wpan-phy.h>
  26. #include "mac802154.h"
  27. struct phy_chan_notify_work {
  28. struct work_struct work;
  29. struct net_device *dev;
  30. };
  31. struct hw_addr_filt_notify_work {
  32. struct work_struct work;
  33. struct net_device *dev;
  34. unsigned long changed;
  35. };
  36. static struct mac802154_priv *mac802154_slave_get_priv(struct net_device *dev)
  37. {
  38. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  39. BUG_ON(dev->type != ARPHRD_IEEE802154);
  40. return priv->hw;
  41. }
  42. static void hw_addr_notify(struct work_struct *work)
  43. {
  44. struct hw_addr_filt_notify_work *nw = container_of(work,
  45. struct hw_addr_filt_notify_work, work);
  46. struct mac802154_priv *hw = mac802154_slave_get_priv(nw->dev);
  47. int res;
  48. res = hw->ops->set_hw_addr_filt(&hw->hw,
  49. &hw->hw.hw_filt,
  50. nw->changed);
  51. if (res)
  52. pr_debug("failed changed mask %lx\n", nw->changed);
  53. kfree(nw);
  54. return;
  55. }
  56. static void set_hw_addr_filt(struct net_device *dev, unsigned long changed)
  57. {
  58. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  59. struct hw_addr_filt_notify_work *work;
  60. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  61. if (!work)
  62. return;
  63. INIT_WORK(&work->work, hw_addr_notify);
  64. work->dev = dev;
  65. work->changed = changed;
  66. queue_work(priv->hw->dev_workqueue, &work->work);
  67. return;
  68. }
  69. void mac802154_dev_set_short_addr(struct net_device *dev, u16 val)
  70. {
  71. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  72. BUG_ON(dev->type != ARPHRD_IEEE802154);
  73. spin_lock_bh(&priv->mib_lock);
  74. priv->short_addr = val;
  75. spin_unlock_bh(&priv->mib_lock);
  76. if ((priv->hw->ops->set_hw_addr_filt) &&
  77. (priv->hw->hw.hw_filt.short_addr != priv->short_addr)) {
  78. priv->hw->hw.hw_filt.short_addr = priv->short_addr;
  79. set_hw_addr_filt(dev, IEEE802515_AFILT_SADDR_CHANGED);
  80. }
  81. }
  82. u16 mac802154_dev_get_short_addr(const struct net_device *dev)
  83. {
  84. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  85. u16 ret;
  86. BUG_ON(dev->type != ARPHRD_IEEE802154);
  87. spin_lock_bh(&priv->mib_lock);
  88. ret = priv->short_addr;
  89. spin_unlock_bh(&priv->mib_lock);
  90. return ret;
  91. }
  92. void mac802154_dev_set_ieee_addr(struct net_device *dev)
  93. {
  94. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  95. struct mac802154_priv *mac = priv->hw;
  96. if (mac->ops->set_hw_addr_filt &&
  97. memcmp(mac->hw.hw_filt.ieee_addr,
  98. dev->dev_addr, IEEE802154_ADDR_LEN)) {
  99. memcpy(mac->hw.hw_filt.ieee_addr,
  100. dev->dev_addr, IEEE802154_ADDR_LEN);
  101. set_hw_addr_filt(dev, IEEE802515_AFILT_IEEEADDR_CHANGED);
  102. }
  103. }
  104. u16 mac802154_dev_get_pan_id(const struct net_device *dev)
  105. {
  106. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  107. u16 ret;
  108. BUG_ON(dev->type != ARPHRD_IEEE802154);
  109. spin_lock_bh(&priv->mib_lock);
  110. ret = priv->pan_id;
  111. spin_unlock_bh(&priv->mib_lock);
  112. return ret;
  113. }
  114. void mac802154_dev_set_pan_id(struct net_device *dev, u16 val)
  115. {
  116. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  117. BUG_ON(dev->type != ARPHRD_IEEE802154);
  118. spin_lock_bh(&priv->mib_lock);
  119. priv->pan_id = val;
  120. spin_unlock_bh(&priv->mib_lock);
  121. if ((priv->hw->ops->set_hw_addr_filt) &&
  122. (priv->hw->hw.hw_filt.pan_id != priv->pan_id)) {
  123. priv->hw->hw.hw_filt.pan_id = priv->pan_id;
  124. set_hw_addr_filt(dev, IEEE802515_AFILT_PANID_CHANGED);
  125. }
  126. }
  127. static void phy_chan_notify(struct work_struct *work)
  128. {
  129. struct phy_chan_notify_work *nw = container_of(work,
  130. struct phy_chan_notify_work, work);
  131. struct mac802154_priv *hw = mac802154_slave_get_priv(nw->dev);
  132. struct mac802154_sub_if_data *priv = netdev_priv(nw->dev);
  133. int res;
  134. res = hw->ops->set_channel(&hw->hw, priv->page, priv->chan);
  135. if (res)
  136. pr_debug("set_channel failed\n");
  137. kfree(nw);
  138. }
  139. void mac802154_dev_set_page_channel(struct net_device *dev, u8 page, u8 chan)
  140. {
  141. struct mac802154_sub_if_data *priv = netdev_priv(dev);
  142. struct phy_chan_notify_work *work;
  143. BUG_ON(dev->type != ARPHRD_IEEE802154);
  144. spin_lock_bh(&priv->mib_lock);
  145. priv->page = page;
  146. priv->chan = chan;
  147. spin_unlock_bh(&priv->mib_lock);
  148. if (priv->hw->phy->current_channel != priv->chan ||
  149. priv->hw->phy->current_page != priv->page) {
  150. work = kzalloc(sizeof(*work), GFP_ATOMIC);
  151. if (!work)
  152. return;
  153. INIT_WORK(&work->work, phy_chan_notify);
  154. work->dev = dev;
  155. queue_work(priv->hw->dev_workqueue, &work->work);
  156. }
  157. }