cfg802154.h 6.2 KB

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