cfg802154.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2007, 2008, 2009 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. * Written by:
  14. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  15. */
  16. #ifndef __NET_CFG802154_H
  17. #define __NET_CFG802154_H
  18. #include <linux/ieee802154.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/mutex.h>
  21. #include <linux/bug.h>
  22. #include <net/nl802154.h>
  23. struct wpan_phy;
  24. struct wpan_phy_cca;
  25. struct cfg802154_ops {
  26. struct net_device * (*add_virtual_intf_deprecated)(struct wpan_phy *wpan_phy,
  27. const char *name,
  28. unsigned char name_assign_type,
  29. int type);
  30. void (*del_virtual_intf_deprecated)(struct wpan_phy *wpan_phy,
  31. struct net_device *dev);
  32. int (*add_virtual_intf)(struct wpan_phy *wpan_phy,
  33. const char *name,
  34. unsigned char name_assign_type,
  35. enum nl802154_iftype type,
  36. __le64 extended_addr);
  37. int (*del_virtual_intf)(struct wpan_phy *wpan_phy,
  38. struct wpan_dev *wpan_dev);
  39. int (*set_channel)(struct wpan_phy *wpan_phy, u8 page, u8 channel);
  40. int (*set_cca_mode)(struct wpan_phy *wpan_phy,
  41. const struct wpan_phy_cca *cca);
  42. int (*set_cca_ed_level)(struct wpan_phy *wpan_phy, s32 ed_level);
  43. int (*set_tx_power)(struct wpan_phy *wpan_phy, s32 power);
  44. int (*set_pan_id)(struct wpan_phy *wpan_phy,
  45. struct wpan_dev *wpan_dev, __le16 pan_id);
  46. int (*set_short_addr)(struct wpan_phy *wpan_phy,
  47. struct wpan_dev *wpan_dev, __le16 short_addr);
  48. int (*set_backoff_exponent)(struct wpan_phy *wpan_phy,
  49. struct wpan_dev *wpan_dev, u8 min_be,
  50. u8 max_be);
  51. int (*set_max_csma_backoffs)(struct wpan_phy *wpan_phy,
  52. struct wpan_dev *wpan_dev,
  53. u8 max_csma_backoffs);
  54. int (*set_max_frame_retries)(struct wpan_phy *wpan_phy,
  55. struct wpan_dev *wpan_dev,
  56. s8 max_frame_retries);
  57. int (*set_lbt_mode)(struct wpan_phy *wpan_phy,
  58. struct wpan_dev *wpan_dev, bool mode);
  59. };
  60. static inline bool
  61. wpan_phy_supported_bool(bool b, enum nl802154_supported_bool_states st)
  62. {
  63. switch (st) {
  64. case NL802154_SUPPORTED_BOOL_TRUE:
  65. return b;
  66. case NL802154_SUPPORTED_BOOL_FALSE:
  67. return !b;
  68. case NL802154_SUPPORTED_BOOL_BOTH:
  69. return true;
  70. default:
  71. WARN_ON(1);
  72. }
  73. return false;
  74. }
  75. struct wpan_phy_supported {
  76. u32 channels[IEEE802154_MAX_PAGE + 1],
  77. cca_modes, cca_opts, iftypes;
  78. enum nl802154_supported_bool_states lbt;
  79. u8 min_minbe, max_minbe, min_maxbe, max_maxbe,
  80. min_csma_backoffs, max_csma_backoffs;
  81. s8 min_frame_retries, max_frame_retries;
  82. size_t tx_powers_size, cca_ed_levels_size;
  83. const s32 *tx_powers, *cca_ed_levels;
  84. };
  85. struct wpan_phy_cca {
  86. enum nl802154_cca_modes mode;
  87. enum nl802154_cca_opts opt;
  88. };
  89. static inline bool
  90. wpan_phy_cca_cmp(const struct wpan_phy_cca *a, const struct wpan_phy_cca *b)
  91. {
  92. if (a->mode != b->mode)
  93. return false;
  94. if (a->mode == NL802154_CCA_ENERGY_CARRIER)
  95. return a->opt == b->opt;
  96. return true;
  97. }
  98. /**
  99. * @WPAN_PHY_FLAG_TRANSMIT_POWER: Indicates that transceiver will support
  100. * transmit power setting.
  101. * @WPAN_PHY_FLAG_CCA_ED_LEVEL: Indicates that transceiver will support cca ed
  102. * level setting.
  103. * @WPAN_PHY_FLAG_CCA_MODE: Indicates that transceiver will support cca mode
  104. * setting.
  105. */
  106. enum wpan_phy_flags {
  107. WPAN_PHY_FLAG_TXPOWER = BIT(1),
  108. WPAN_PHY_FLAG_CCA_ED_LEVEL = BIT(2),
  109. WPAN_PHY_FLAG_CCA_MODE = BIT(3),
  110. };
  111. struct wpan_phy {
  112. /* If multiple wpan_phys are registered and you're handed e.g.
  113. * a regular netdev with assigned ieee802154_ptr, you won't
  114. * know whether it points to a wpan_phy your driver has registered
  115. * or not. Assign this to something global to your driver to
  116. * help determine whether you own this wpan_phy or not.
  117. */
  118. const void *privid;
  119. u32 flags;
  120. /*
  121. * This is a PIB according to 802.15.4-2011.
  122. * We do not provide timing-related variables, as they
  123. * aren't used outside of driver
  124. */
  125. u8 current_channel;
  126. u8 current_page;
  127. struct wpan_phy_supported supported;
  128. /* current transmit_power in mBm */
  129. s32 transmit_power;
  130. struct wpan_phy_cca cca;
  131. __le64 perm_extended_addr;
  132. /* current cca ed threshold in mBm */
  133. s32 cca_ed_level;
  134. /* PHY depended MAC PIB values */
  135. /* 802.15.4 acronym: Tdsym in usec */
  136. u8 symbol_duration;
  137. /* lifs and sifs periods timing */
  138. u16 lifs_period;
  139. u16 sifs_period;
  140. struct device dev;
  141. char priv[0] __aligned(NETDEV_ALIGN);
  142. };
  143. struct wpan_dev {
  144. struct wpan_phy *wpan_phy;
  145. int iftype;
  146. /* the remainder of this struct should be private to cfg802154 */
  147. struct list_head list;
  148. struct net_device *netdev;
  149. u32 identifier;
  150. /* MAC PIB */
  151. __le16 pan_id;
  152. __le16 short_addr;
  153. __le64 extended_addr;
  154. /* MAC BSN field */
  155. atomic_t bsn;
  156. /* MAC DSN field */
  157. atomic_t dsn;
  158. u8 min_be;
  159. u8 max_be;
  160. u8 csma_retries;
  161. s8 frame_retries;
  162. bool lbt;
  163. bool promiscuous_mode;
  164. };
  165. #define to_phy(_dev) container_of(_dev, struct wpan_phy, dev)
  166. struct wpan_phy *
  167. wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size);
  168. static inline void wpan_phy_set_dev(struct wpan_phy *phy, struct device *dev)
  169. {
  170. phy->dev.parent = dev;
  171. }
  172. int wpan_phy_register(struct wpan_phy *phy);
  173. void wpan_phy_unregister(struct wpan_phy *phy);
  174. void wpan_phy_free(struct wpan_phy *phy);
  175. /* Same semantics as for class_for_each_device */
  176. int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data), void *data);
  177. static inline void *wpan_phy_priv(struct wpan_phy *phy)
  178. {
  179. BUG_ON(!phy);
  180. return &phy->priv;
  181. }
  182. struct wpan_phy *wpan_phy_find(const char *str);
  183. static inline void wpan_phy_put(struct wpan_phy *phy)
  184. {
  185. put_device(&phy->dev);
  186. }
  187. static inline const char *wpan_phy_name(struct wpan_phy *phy)
  188. {
  189. return dev_name(&phy->dev);
  190. }
  191. #endif /* __NET_CFG802154_H */