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