cfg.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #ifdef CONFIG_PM
  40. static int ieee802154_suspend(struct wpan_phy *wpan_phy)
  41. {
  42. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  43. if (!local->open_count)
  44. goto suspend;
  45. ieee802154_stop_queue(&local->hw);
  46. synchronize_net();
  47. /* stop hardware - this must stop RX */
  48. ieee802154_stop_device(local);
  49. suspend:
  50. local->suspended = true;
  51. return 0;
  52. }
  53. static int ieee802154_resume(struct wpan_phy *wpan_phy)
  54. {
  55. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  56. int ret;
  57. /* nothing to do if HW shouldn't run */
  58. if (!local->open_count)
  59. goto wake_up;
  60. /* restart hardware */
  61. ret = drv_start(local);
  62. if (ret)
  63. return ret;
  64. wake_up:
  65. ieee802154_wake_queue(&local->hw);
  66. local->suspended = false;
  67. return 0;
  68. }
  69. #else
  70. #define ieee802154_suspend NULL
  71. #define ieee802154_resume NULL
  72. #endif
  73. static int
  74. ieee802154_add_iface(struct wpan_phy *phy, const char *name,
  75. unsigned char name_assign_type,
  76. enum nl802154_iftype type, __le64 extended_addr)
  77. {
  78. struct ieee802154_local *local = wpan_phy_priv(phy);
  79. struct net_device *err;
  80. err = ieee802154_if_add(local, name, name_assign_type, type,
  81. extended_addr);
  82. return PTR_ERR_OR_ZERO(err);
  83. }
  84. static int
  85. ieee802154_del_iface(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev)
  86. {
  87. ieee802154_if_remove(IEEE802154_WPAN_DEV_TO_SUB_IF(wpan_dev));
  88. return 0;
  89. }
  90. static int
  91. ieee802154_set_channel(struct wpan_phy *wpan_phy, u8 page, u8 channel)
  92. {
  93. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  94. int ret;
  95. ASSERT_RTNL();
  96. if (wpan_phy->current_page == page &&
  97. wpan_phy->current_channel == channel)
  98. return 0;
  99. ret = drv_set_channel(local, page, channel);
  100. if (!ret) {
  101. wpan_phy->current_page = page;
  102. wpan_phy->current_channel = channel;
  103. }
  104. return ret;
  105. }
  106. static int
  107. ieee802154_set_cca_mode(struct wpan_phy *wpan_phy,
  108. const struct wpan_phy_cca *cca)
  109. {
  110. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  111. int ret;
  112. ASSERT_RTNL();
  113. if (wpan_phy_cca_cmp(&wpan_phy->cca, cca))
  114. return 0;
  115. ret = drv_set_cca_mode(local, cca);
  116. if (!ret)
  117. wpan_phy->cca = *cca;
  118. return ret;
  119. }
  120. static int
  121. ieee802154_set_cca_ed_level(struct wpan_phy *wpan_phy, s32 ed_level)
  122. {
  123. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  124. int ret;
  125. ASSERT_RTNL();
  126. if (wpan_phy->cca_ed_level == ed_level)
  127. return 0;
  128. ret = drv_set_cca_ed_level(local, ed_level);
  129. if (!ret)
  130. wpan_phy->cca_ed_level = ed_level;
  131. return ret;
  132. }
  133. static int
  134. ieee802154_set_tx_power(struct wpan_phy *wpan_phy, s32 power)
  135. {
  136. struct ieee802154_local *local = wpan_phy_priv(wpan_phy);
  137. int ret;
  138. ASSERT_RTNL();
  139. if (wpan_phy->transmit_power == power)
  140. return 0;
  141. ret = drv_set_tx_power(local, power);
  142. if (!ret)
  143. wpan_phy->transmit_power = power;
  144. return ret;
  145. }
  146. static int
  147. ieee802154_set_pan_id(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  148. __le16 pan_id)
  149. {
  150. int ret;
  151. ASSERT_RTNL();
  152. if (wpan_dev->pan_id == pan_id)
  153. return 0;
  154. ret = mac802154_wpan_update_llsec(wpan_dev->netdev);
  155. if (!ret)
  156. wpan_dev->pan_id = pan_id;
  157. return ret;
  158. }
  159. static int
  160. ieee802154_set_backoff_exponent(struct wpan_phy *wpan_phy,
  161. struct wpan_dev *wpan_dev,
  162. u8 min_be, u8 max_be)
  163. {
  164. ASSERT_RTNL();
  165. wpan_dev->min_be = min_be;
  166. wpan_dev->max_be = max_be;
  167. return 0;
  168. }
  169. static int
  170. ieee802154_set_short_addr(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  171. __le16 short_addr)
  172. {
  173. ASSERT_RTNL();
  174. wpan_dev->short_addr = short_addr;
  175. return 0;
  176. }
  177. static int
  178. ieee802154_set_max_csma_backoffs(struct wpan_phy *wpan_phy,
  179. struct wpan_dev *wpan_dev,
  180. u8 max_csma_backoffs)
  181. {
  182. ASSERT_RTNL();
  183. wpan_dev->csma_retries = max_csma_backoffs;
  184. return 0;
  185. }
  186. static int
  187. ieee802154_set_max_frame_retries(struct wpan_phy *wpan_phy,
  188. struct wpan_dev *wpan_dev,
  189. s8 max_frame_retries)
  190. {
  191. ASSERT_RTNL();
  192. wpan_dev->frame_retries = max_frame_retries;
  193. return 0;
  194. }
  195. static int
  196. ieee802154_set_lbt_mode(struct wpan_phy *wpan_phy, struct wpan_dev *wpan_dev,
  197. bool mode)
  198. {
  199. ASSERT_RTNL();
  200. wpan_dev->lbt = mode;
  201. return 0;
  202. }
  203. static int
  204. ieee802154_set_ackreq_default(struct wpan_phy *wpan_phy,
  205. struct wpan_dev *wpan_dev, bool ackreq)
  206. {
  207. ASSERT_RTNL();
  208. wpan_dev->ackreq = ackreq;
  209. return 0;
  210. }
  211. const struct cfg802154_ops mac802154_config_ops = {
  212. .add_virtual_intf_deprecated = ieee802154_add_iface_deprecated,
  213. .del_virtual_intf_deprecated = ieee802154_del_iface_deprecated,
  214. .suspend = ieee802154_suspend,
  215. .resume = ieee802154_resume,
  216. .add_virtual_intf = ieee802154_add_iface,
  217. .del_virtual_intf = ieee802154_del_iface,
  218. .set_channel = ieee802154_set_channel,
  219. .set_cca_mode = ieee802154_set_cca_mode,
  220. .set_cca_ed_level = ieee802154_set_cca_ed_level,
  221. .set_tx_power = ieee802154_set_tx_power,
  222. .set_pan_id = ieee802154_set_pan_id,
  223. .set_short_addr = ieee802154_set_short_addr,
  224. .set_backoff_exponent = ieee802154_set_backoff_exponent,
  225. .set_max_csma_backoffs = ieee802154_set_max_csma_backoffs,
  226. .set_max_frame_retries = ieee802154_set_max_frame_retries,
  227. .set_lbt_mode = ieee802154_set_lbt_mode,
  228. .set_ackreq_default = ieee802154_set_ackreq_default,
  229. };