mac802154.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * IEEE802.15.4-2003 specification
  3. *
  4. * Copyright (C) 2007-2012 Siemens AG
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #ifndef NET_MAC802154_H
  17. #define NET_MAC802154_H
  18. #include <net/af_ieee802154.h>
  19. #include <linux/ieee802154.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/unaligned/memmove.h>
  22. #include <net/cfg802154.h>
  23. /**
  24. * enum ieee802154_hw_addr_filt_flags - hardware address filtering flags
  25. *
  26. * The following flags are used to indicate changed address settings from
  27. * the stack to the hardware.
  28. *
  29. * @IEEE802154_AFILT_SADDR_CHANGED: Indicates that the short address will be
  30. * change.
  31. *
  32. * @IEEE802154_AFILT_IEEEADDR_CHANGED: Indicates that the extended address
  33. * will be change.
  34. *
  35. * @IEEE802154_AFILT_PANID_CHANGED: Indicates that the pan id will be change.
  36. *
  37. * @IEEE802154_AFILT_PANC_CHANGED: Indicates that the address filter will
  38. * do frame address filtering as a pan coordinator.
  39. */
  40. enum ieee802154_hw_addr_filt_flags {
  41. IEEE802154_AFILT_SADDR_CHANGED = BIT(0),
  42. IEEE802154_AFILT_IEEEADDR_CHANGED = BIT(1),
  43. IEEE802154_AFILT_PANID_CHANGED = BIT(2),
  44. IEEE802154_AFILT_PANC_CHANGED = BIT(3),
  45. };
  46. /**
  47. * struct ieee802154_hw_addr_filt - hardware address filtering settings
  48. *
  49. * @pan_id: pan_id which should be set to the hardware address filter.
  50. *
  51. * @short_addr: short_addr which should be set to the hardware address filter.
  52. *
  53. * @ieee_addr: extended address which should be set to the hardware address
  54. * filter.
  55. *
  56. * @pan_coord: boolean if hardware filtering should be operate as coordinator.
  57. */
  58. struct ieee802154_hw_addr_filt {
  59. __le16 pan_id;
  60. __le16 short_addr;
  61. __le64 ieee_addr;
  62. bool pan_coord;
  63. };
  64. /**
  65. * struct ieee802154_hw - ieee802154 hardware
  66. *
  67. * @extra_tx_headroom: headroom to reserve in each transmit skb for use by the
  68. * driver (e.g. for transmit headers.)
  69. *
  70. * @flags: hardware flags, see &enum ieee802154_hw_flags
  71. *
  72. * @parent: parent device of the hardware.
  73. *
  74. * @priv: pointer to private area that was allocated for driver use along with
  75. * this structure.
  76. *
  77. * @phy: This points to the &struct wpan_phy allocated for this 802.15.4 PHY.
  78. */
  79. struct ieee802154_hw {
  80. /* filled by the driver */
  81. int extra_tx_headroom;
  82. u32 flags;
  83. struct device *parent;
  84. void *priv;
  85. /* filled by mac802154 core */
  86. struct wpan_phy *phy;
  87. };
  88. /**
  89. * enum ieee802154_hw_flags - hardware flags
  90. *
  91. * These flags are used to indicate hardware capabilities to
  92. * the stack. Generally, flags here should have their meaning
  93. * done in a way that the simplest hardware doesn't need setting
  94. * any particular flags. There are some exceptions to this rule,
  95. * however, so you are advised to review these flags carefully.
  96. *
  97. * @IEEE802154_HW_TX_OMIT_CKSUM: Indicates that xmitter will add FCS on it's
  98. * own.
  99. *
  100. * @IEEE802154_HW_LBT: Indicates that transceiver will support listen before
  101. * transmit.
  102. *
  103. * @IEEE802154_HW_CSMA_PARAMS: Indicates that transceiver will support csma
  104. * parameters (max_be, min_be, backoff exponents).
  105. *
  106. * @IEEE802154_HW_FRAME_RETRIES: Indicates that transceiver will support ARET
  107. * frame retries setting.
  108. *
  109. * @IEEE802154_HW_AFILT: Indicates that transceiver will support hardware
  110. * address filter setting.
  111. *
  112. * @IEEE802154_HW_PROMISCUOUS: Indicates that transceiver will support
  113. * promiscuous mode setting.
  114. *
  115. * @IEEE802154_HW_RX_OMIT_CKSUM: Indicates that receiver omits FCS.
  116. *
  117. * @IEEE802154_HW_RX_DROP_BAD_CKSUM: Indicates that receiver will not filter
  118. * frames with bad checksum.
  119. */
  120. enum ieee802154_hw_flags {
  121. IEEE802154_HW_TX_OMIT_CKSUM = BIT(0),
  122. IEEE802154_HW_LBT = BIT(1),
  123. IEEE802154_HW_CSMA_PARAMS = BIT(2),
  124. IEEE802154_HW_FRAME_RETRIES = BIT(3),
  125. IEEE802154_HW_AFILT = BIT(4),
  126. IEEE802154_HW_PROMISCUOUS = BIT(5),
  127. IEEE802154_HW_RX_OMIT_CKSUM = BIT(6),
  128. IEEE802154_HW_RX_DROP_BAD_CKSUM = BIT(7),
  129. };
  130. /* Indicates that receiver omits FCS and xmitter will add FCS on it's own. */
  131. #define IEEE802154_HW_OMIT_CKSUM (IEEE802154_HW_TX_OMIT_CKSUM | \
  132. IEEE802154_HW_RX_OMIT_CKSUM)
  133. /* struct ieee802154_ops - callbacks from mac802154 to the driver
  134. *
  135. * This structure contains various callbacks that the driver may
  136. * handle or, in some cases, must handle, for example to transmit
  137. * a frame.
  138. *
  139. * start: Handler that 802.15.4 module calls for device initialization.
  140. * This function is called before the first interface is attached.
  141. *
  142. * stop: Handler that 802.15.4 module calls for device cleanup.
  143. * This function is called after the last interface is removed.
  144. *
  145. * xmit_sync:
  146. * Handler that 802.15.4 module calls for each transmitted frame.
  147. * skb cntains the buffer starting from the IEEE 802.15.4 header.
  148. * The low-level driver should send the frame based on available
  149. * configuration. This is called by a workqueue and useful for
  150. * synchronous 802.15.4 drivers.
  151. * This function should return zero or negative errno.
  152. *
  153. * WARNING:
  154. * This will be deprecated soon. We don't accept synced xmit callbacks
  155. * drivers anymore.
  156. *
  157. * xmit_async:
  158. * Handler that 802.15.4 module calls for each transmitted frame.
  159. * skb cntains the buffer starting from the IEEE 802.15.4 header.
  160. * The low-level driver should send the frame based on available
  161. * configuration.
  162. * This function should return zero or negative errno.
  163. *
  164. * ed: Handler that 802.15.4 module calls for Energy Detection.
  165. * This function should place the value for detected energy
  166. * (usually device-dependant) in the level pointer and return
  167. * either zero or negative errno. Called with pib_lock held.
  168. *
  169. * set_channel:
  170. * Set radio for listening on specific channel.
  171. * Set the device for listening on specified channel.
  172. * Returns either zero, or negative errno. Called with pib_lock held.
  173. *
  174. * set_hw_addr_filt:
  175. * Set radio for listening on specific address.
  176. * Set the device for listening on specified address.
  177. * Returns either zero, or negative errno.
  178. *
  179. * set_txpower:
  180. * Set radio transmit power in mBm. Called with pib_lock held.
  181. * Returns either zero, or negative errno.
  182. *
  183. * set_lbt
  184. * Enables or disables listen before talk on the device. Called with
  185. * pib_lock held.
  186. * Returns either zero, or negative errno.
  187. *
  188. * set_cca_mode
  189. * Sets the CCA mode used by the device. Called with pib_lock held.
  190. * Returns either zero, or negative errno.
  191. *
  192. * set_cca_ed_level
  193. * Sets the CCA energy detection threshold in mBm. Called with pib_lock
  194. * held.
  195. * Returns either zero, or negative errno.
  196. *
  197. * set_csma_params
  198. * Sets the CSMA parameter set for the PHY. Called with pib_lock held.
  199. * Returns either zero, or negative errno.
  200. *
  201. * set_frame_retries
  202. * Sets the retransmission attempt limit. Called with pib_lock held.
  203. * Returns either zero, or negative errno.
  204. *
  205. * set_promiscuous_mode
  206. * Enables or disable promiscuous mode.
  207. */
  208. struct ieee802154_ops {
  209. struct module *owner;
  210. int (*start)(struct ieee802154_hw *hw);
  211. void (*stop)(struct ieee802154_hw *hw);
  212. int (*xmit_sync)(struct ieee802154_hw *hw,
  213. struct sk_buff *skb);
  214. int (*xmit_async)(struct ieee802154_hw *hw,
  215. struct sk_buff *skb);
  216. int (*ed)(struct ieee802154_hw *hw, u8 *level);
  217. int (*set_channel)(struct ieee802154_hw *hw, u8 page,
  218. u8 channel);
  219. int (*set_hw_addr_filt)(struct ieee802154_hw *hw,
  220. struct ieee802154_hw_addr_filt *filt,
  221. unsigned long changed);
  222. int (*set_txpower)(struct ieee802154_hw *hw, s32 mbm);
  223. int (*set_lbt)(struct ieee802154_hw *hw, bool on);
  224. int (*set_cca_mode)(struct ieee802154_hw *hw,
  225. const struct wpan_phy_cca *cca);
  226. int (*set_cca_ed_level)(struct ieee802154_hw *hw, s32 mbm);
  227. int (*set_csma_params)(struct ieee802154_hw *hw,
  228. u8 min_be, u8 max_be, u8 retries);
  229. int (*set_frame_retries)(struct ieee802154_hw *hw,
  230. s8 retries);
  231. int (*set_promiscuous_mode)(struct ieee802154_hw *hw,
  232. const bool on);
  233. };
  234. /**
  235. * ieee802154_get_fc_from_skb - get the frame control field from an skb
  236. * @skb: skb where the frame control field will be get from
  237. */
  238. static inline __le16 ieee802154_get_fc_from_skb(const struct sk_buff *skb)
  239. {
  240. /* return some invalid fc on failure */
  241. if (unlikely(skb->len < 2)) {
  242. WARN_ON(1);
  243. return cpu_to_le16(0);
  244. }
  245. return (__force __le16)__get_unaligned_memmove16(skb_mac_header(skb));
  246. }
  247. /**
  248. * ieee802154_be64_to_le64 - copies and convert be64 to le64
  249. * @le64_dst: le64 destination pointer
  250. * @be64_src: be64 source pointer
  251. */
  252. static inline void ieee802154_be64_to_le64(void *le64_dst, const void *be64_src)
  253. {
  254. __put_unaligned_memmove64(swab64p(be64_src), le64_dst);
  255. }
  256. /**
  257. * ieee802154_le64_to_be64 - copies and convert le64 to be64
  258. * @be64_dst: be64 destination pointer
  259. * @le64_src: le64 source pointer
  260. */
  261. static inline void ieee802154_le64_to_be64(void *be64_dst, const void *le64_src)
  262. {
  263. __put_unaligned_memmove64(swab64p(le64_src), be64_dst);
  264. }
  265. /**
  266. * ieee802154_le16_to_be16 - copies and convert le16 to be16
  267. * @be16_dst: be16 destination pointer
  268. * @le16_src: le16 source pointer
  269. */
  270. static inline void ieee802154_le16_to_be16(void *be16_dst, const void *le16_src)
  271. {
  272. __put_unaligned_memmove16(swab16p(le16_src), be16_dst);
  273. }
  274. /**
  275. * ieee802154_alloc_hw - Allocate a new hardware device
  276. *
  277. * This must be called once for each hardware device. The returned pointer
  278. * must be used to refer to this device when calling other functions.
  279. * mac802154 allocates a private data area for the driver pointed to by
  280. * @priv in &struct ieee802154_hw, the size of this area is given as
  281. * @priv_data_len.
  282. *
  283. * @priv_data_len: length of private data
  284. * @ops: callbacks for this device
  285. *
  286. * Return: A pointer to the new hardware device, or %NULL on error.
  287. */
  288. struct ieee802154_hw *
  289. ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops);
  290. /**
  291. * ieee802154_free_hw - free hardware descriptor
  292. *
  293. * This function frees everything that was allocated, including the
  294. * private data for the driver. You must call ieee802154_unregister_hw()
  295. * before calling this function.
  296. *
  297. * @hw: the hardware to free
  298. */
  299. void ieee802154_free_hw(struct ieee802154_hw *hw);
  300. /**
  301. * ieee802154_register_hw - Register hardware device
  302. *
  303. * You must call this function before any other functions in
  304. * mac802154. Note that before a hardware can be registered, you
  305. * need to fill the contained wpan_phy's information.
  306. *
  307. * @hw: the device to register as returned by ieee802154_alloc_hw()
  308. *
  309. * Return: 0 on success. An error code otherwise.
  310. */
  311. int ieee802154_register_hw(struct ieee802154_hw *hw);
  312. /**
  313. * ieee802154_unregister_hw - Unregister a hardware device
  314. *
  315. * This function instructs mac802154 to free allocated resources
  316. * and unregister netdevices from the networking subsystem.
  317. *
  318. * @hw: the hardware to unregister
  319. */
  320. void ieee802154_unregister_hw(struct ieee802154_hw *hw);
  321. /**
  322. * ieee802154_rx_irqsafe - receive frame
  323. *
  324. * Like ieee802154_rx() but can be called in IRQ context
  325. * (internally defers to a tasklet.)
  326. *
  327. * @hw: the hardware this frame came in on
  328. * @skb: the buffer to receive, owned by mac802154 after this call
  329. * @lqi: link quality indicator
  330. */
  331. void ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb,
  332. u8 lqi);
  333. /**
  334. * ieee802154_wake_queue - wake ieee802154 queue
  335. * @hw: pointer as obtained from ieee802154_alloc_hw().
  336. *
  337. * Drivers should use this function instead of netif_wake_queue.
  338. */
  339. void ieee802154_wake_queue(struct ieee802154_hw *hw);
  340. /**
  341. * ieee802154_stop_queue - stop ieee802154 queue
  342. * @hw: pointer as obtained from ieee802154_alloc_hw().
  343. *
  344. * Drivers should use this function instead of netif_stop_queue.
  345. */
  346. void ieee802154_stop_queue(struct ieee802154_hw *hw);
  347. /**
  348. * ieee802154_xmit_complete - frame transmission complete
  349. *
  350. * @hw: pointer as obtained from ieee802154_alloc_hw().
  351. * @skb: buffer for transmission
  352. * @ifs_handling: indicate interframe space handling
  353. */
  354. void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb,
  355. bool ifs_handling);
  356. #endif /* NET_MAC802154_H */