cfg802154.h 6.0 KB

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