mac802154.h 12 KB

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