cfg.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. if (IS_ERR(err))
  46. return PTR_ERR(err);
  47. return 0;
  48. }
  49. static int
  50. ieee802154_del_iface(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev)
  51. {
  52. ieee802154_if_remove(IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev));
  53. return 0;
  54. }
  55. static int
  56. ieee802154_set_channel(struct wpan_phy *wpan_phy, u8 page, u8 channel)
  57. {
  58. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  59. int ret;
  60. ASSERT_RTNL();
  61. /* check if phy support this setting */
  62. if (!(wpan_phy->channels_supported[page] & BIT(channel)))
  63. return -EINVAL;
  64. ret = drv_set_channel(local, page, channel);
  65. if (!ret) {
  66. wpan_phy->current_page = page;
  67. wpan_phy->current_channel = channel;
  68. }
  69. return ret;
  70. }
  71. static int
  72. ieee802154_set_cca_mode(struct wpan_phy *wpan_phy,
  73. const struct wpan_phy_cca *cca)
  74. {
  75. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  76. int ret;
  77. ASSERT_RTNL();
  78. /* check if phy support this setting */
  79. if (!(local->hw.flags & IEEE802154_HW_CCA_MODE))
  80. return -EOPNOTSUPP;
  81. ret = drv_set_cca_mode(local, cca);
  82. if (!ret)
  83. wpan_phy->cca = *cca;
  84. return ret;
  85. }
  86. static int
  87. ieee802154_set_pan_id(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  88. __le16 pan_id)
  89. {
  90. ASSERT_RTNL();
  91. /* TODO
  92. * I am not sure about to check here on broadcast pan_id.
  93. * Broadcast is a valid setting, comment from 802.15.4:
  94. * If this value is 0xffff, the device is not associated.
  95. *
  96. * This could useful to simple deassociate an device.
  97. */
  98. if (pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST))
  99. return -EINVAL;
  100. wpan_dev->pan_id = pan_id;
  101. return 0;
  102. }
  103. static int
  104. ieee802154_set_backoff_exponent(struct wpan_phy *wpan_phy,
  105. struct wpan_dev *wpan_dev,
  106. u8 min_be, u8 max_be)
  107. {
  108. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  109. ASSERT_RTNL();
  110. if (!(local->hw.flags & IEEE802154_HW_CSMA_PARAMS))
  111. return -EOPNOTSUPP;
  112. wpan_dev->min_be = min_be;
  113. wpan_dev->max_be = max_be;
  114. return 0;
  115. }
  116. static int
  117. ieee802154_set_short_addr(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  118. __le16 short_addr)
  119. {
  120. ASSERT_RTNL();
  121. /* TODO
  122. * I am not sure about to check here on broadcast short_addr.
  123. * Broadcast is a valid setting, comment from 802.15.4:
  124. * A value of 0xfffe indicates that the device has
  125. * associated but has not been allocated an address. A
  126. * value of 0xffff indicates that the device does not
  127. * have a short address.
  128. *
  129. * I think we should allow to set these settings but
  130. * don't allow to allow socket communication with it.
  131. */
  132. if (short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC) ||
  133. short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST))
  134. return -EINVAL;
  135. wpan_dev->short_addr = short_addr;
  136. return 0;
  137. }
  138. static int
  139. ieee802154_set_max_csma_backoffs(struct wpan_phy *wpan_phy,
  140. struct wpan_dev *wpan_dev,
  141. u8 max_csma_backoffs)
  142. {
  143. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  144. ASSERT_RTNL();
  145. if (!(local->hw.flags & IEEE802154_HW_CSMA_PARAMS))
  146. return -EOPNOTSUPP;
  147. wpan_dev->csma_retries = max_csma_backoffs;
  148. return 0;
  149. }
  150. static int
  151. ieee802154_set_max_frame_retries(struct wpan_phy *wpan_phy,
  152. struct wpan_dev *wpan_dev,
  153. s8 max_frame_retries)
  154. {
  155. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  156. ASSERT_RTNL();
  157. if (!(local->hw.flags & IEEE802154_HW_FRAME_RETRIES))
  158. return -EOPNOTSUPP;
  159. wpan_dev->frame_retries = max_frame_retries;
  160. return 0;
  161. }
  162. static int
  163. ieee802154_set_lbt_mode(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  164. bool mode)
  165. {
  166. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  167. ASSERT_RTNL();
  168. if (!(local->hw.flags & IEEE802154_HW_LBT))
  169. return -EOPNOTSUPP;
  170. wpan_dev->lbt = mode;
  171. return 0;
  172. }
  173. const struct cfg802154_ops mac802154_config_ops = {
  174. .add_virtual_intf_deprecated = ieee802154_add_iface_deprecated,
  175. .del_virtual_intf_deprecated = ieee802154_del_iface_deprecated,
  176. .add_virtual_intf = ieee802154_add_iface,
  177. .del_virtual_intf = ieee802154_del_iface,
  178. .set_channel = ieee802154_set_channel,
  179. .set_cca_mode = ieee802154_set_cca_mode,
  180. .set_pan_id = ieee802154_set_pan_id,
  181. .set_short_addr = ieee802154_set_short_addr,
  182. .set_backoff_exponent = ieee802154_set_backoff_exponent,
  183. .set_max_csma_backoffs = ieee802154_set_max_csma_backoffs,
  184. .set_max_frame_retries = ieee802154_set_max_frame_retries,
  185. .set_lbt_mode = ieee802154_set_lbt_mode,
  186. };