cfg.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License version 2
  3. * as published by the Free Software Foundation.
  4. *
  5. * This program is distributed in the hope that it will be useful,
  6. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. * GNU General Public License for more details.
  9. *
  10. * Authors:
  11. * Alexander Aring <aar@pengutronix.de>
  12. *
  13. * Based on: net/mac80211/cfg.c
  14. */
  15. #include <net/rtnetlink.h>
  16. #include <net/cfg802154.h>
  17. #include "ieee802154_i.h"
  18. #include "driver-ops.h"
  19. #include "cfg.h"
  20. static struct net_device *
  21. ieee802154_add_iface_deprecated(struct wpan_phy *wpan_phy,
  22. const char *name, int type)
  23. {
  24. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  25. struct net_device *dev;
  26. rtnl_lock();
  27. dev = ieee802154_if_add(local, name, type,
  28. cpu_to_le64(0x0000000000000000ULL));
  29. rtnl_unlock();
  30. return dev;
  31. }
  32. static void ieee802154_del_iface_deprecated(struct wpan_phy *wpan_phy,
  33. struct net_device *dev)
  34. {
  35. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  36. ieee802154_if_remove(sdata);
  37. }
  38. static int
  39. ieee802154_add_iface(struct wpan_phy *phy, const char *name,
  40. enum nl802154_iftype type, __le64 extended_addr)
  41. {
  42. struct ieee802154_local *local = wpan_phy_priv(phy);
  43. struct net_device *err;
  44. err = ieee802154_if_add(local, name, type, extended_addr);
  45. return PTR_ERR_OR_ZERO(err);
  46. }
  47. static int
  48. ieee802154_del_iface(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev)
  49. {
  50. ieee802154_if_remove(IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev));
  51. return 0;
  52. }
  53. static int
  54. ieee802154_set_channel(struct wpan_phy *wpan_phy, u8 page, u8 channel)
  55. {
  56. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  57. int ret;
  58. ASSERT_RTNL();
  59. /* check if phy support this setting */
  60. if (!(wpan_phy->channels_supported[page] & BIT(channel)))
  61. return -EINVAL;
  62. ret = drv_set_channel(local, page, channel);
  63. if (!ret) {
  64. wpan_phy->current_page = page;
  65. wpan_phy->current_channel = channel;
  66. }
  67. return ret;
  68. }
  69. static int
  70. ieee802154_set_cca_mode(struct wpan_phy *wpan_phy,
  71. const struct wpan_phy_cca *cca)
  72. {
  73. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  74. int ret;
  75. ASSERT_RTNL();
  76. /* check if phy support this setting */
  77. if (!(local->hw.flags & IEEE802154_HW_CCA_MODE))
  78. return -EOPNOTSUPP;
  79. ret = drv_set_cca_mode(local, cca);
  80. if (!ret)
  81. wpan_phy->cca = *cca;
  82. return ret;
  83. }
  84. static int
  85. ieee802154_set_pan_id(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  86. __le16 pan_id)
  87. {
  88. ASSERT_RTNL();
  89. /* TODO
  90. * I am not sure about to check here on broadcast pan_id.
  91. * Broadcast is a valid setting, comment from 802.15.4:
  92. * If this value is 0xffff, the device is not associated.
  93. *
  94. * This could useful to simple deassociate an device.
  95. */
  96. if (pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST))
  97. return -EINVAL;
  98. wpan_dev->pan_id = pan_id;
  99. return 0;
  100. }
  101. static int
  102. ieee802154_set_backoff_exponent(struct wpan_phy *wpan_phy,
  103. struct wpan_dev *wpan_dev,
  104. u8 min_be, u8 max_be)
  105. {
  106. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  107. ASSERT_RTNL();
  108. if (!(local->hw.flags & IEEE802154_HW_CSMA_PARAMS))
  109. return -EOPNOTSUPP;
  110. wpan_dev->min_be = min_be;
  111. wpan_dev->max_be = max_be;
  112. return 0;
  113. }
  114. static int
  115. ieee802154_set_short_addr(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  116. __le16 short_addr)
  117. {
  118. ASSERT_RTNL();
  119. /* TODO
  120. * I am not sure about to check here on broadcast short_addr.
  121. * Broadcast is a valid setting, comment from 802.15.4:
  122. * A value of 0xfffe indicates that the device has
  123. * associated but has not been allocated an address. A
  124. * value of 0xffff indicates that the device does not
  125. * have a short address.
  126. *
  127. * I think we should allow to set these settings but
  128. * don't allow to allow socket communication with it.
  129. */
  130. if (short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC) ||
  131. short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST))
  132. return -EINVAL;
  133. wpan_dev->short_addr = short_addr;
  134. return 0;
  135. }
  136. static int
  137. ieee802154_set_max_csma_backoffs(struct wpan_phy *wpan_phy,
  138. struct wpan_dev *wpan_dev,
  139. u8 max_csma_backoffs)
  140. {
  141. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  142. ASSERT_RTNL();
  143. if (!(local->hw.flags & IEEE802154_HW_CSMA_PARAMS))
  144. return -EOPNOTSUPP;
  145. wpan_dev->csma_retries = max_csma_backoffs;
  146. return 0;
  147. }
  148. static int
  149. ieee802154_set_max_frame_retries(struct wpan_phy *wpan_phy,
  150. struct wpan_dev *wpan_dev,
  151. s8 max_frame_retries)
  152. {
  153. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  154. ASSERT_RTNL();
  155. if (!(local->hw.flags & IEEE802154_HW_FRAME_RETRIES))
  156. return -EOPNOTSUPP;
  157. wpan_dev->frame_retries = max_frame_retries;
  158. return 0;
  159. }
  160. static int
  161. ieee802154_set_lbt_mode(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  162. bool mode)
  163. {
  164. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  165. ASSERT_RTNL();
  166. if (!(local->hw.flags & IEEE802154_HW_LBT))
  167. return -EOPNOTSUPP;
  168. wpan_dev->lbt = mode;
  169. return 0;
  170. }
  171. const struct cfg802154_ops mac802154_config_ops = {
  172. .add_virtual_intf_deprecated = ieee802154_add_iface_deprecated,
  173. .del_virtual_intf_deprecated = ieee802154_del_iface_deprecated,
  174. .add_virtual_intf = ieee802154_add_iface,
  175. .del_virtual_intf = ieee802154_del_iface,
  176. .set_channel = ieee802154_set_channel,
  177. .set_cca_mode = ieee802154_set_cca_mode,
  178. .set_pan_id = ieee802154_set_pan_id,
  179. .set_short_addr = ieee802154_set_short_addr,
  180. .set_backoff_exponent = ieee802154_set_backoff_exponent,
  181. .set_max_csma_backoffs = ieee802154_set_max_csma_backoffs,
  182. .set_max_frame_retries = ieee802154_set_max_frame_retries,
  183. .set_lbt_mode = ieee802154_set_lbt_mode,
  184. };