mac802154.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 <asm/unaligned.h>
  19. #include <net/af_ieee802154.h>
  20. #include <linux/ieee802154.h>
  21. #include <linux/skbuff.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. __le16 fc;
  241. /* check if we can fc at skb_mac_header of sk buffer */
  242. if (WARN_ON(!skb_mac_header_was_set(skb) ||
  243. (skb_tail_pointer(skb) -
  244. skb_mac_header(skb)) < IEEE802154_FC_LEN))
  245. return cpu_to_le16(0);
  246. memcpy(&fc, skb_mac_header(skb), IEEE802154_FC_LEN);
  247. return fc;
  248. }
  249. /**
  250. * ieee802154_skb_dst_pan - get the pointer to destination pan field
  251. * @fc: mac header frame control field
  252. * @skb: skb where the destination pan pointer will be get from
  253. */
  254. static inline unsigned char *ieee802154_skb_dst_pan(__le16 fc,
  255. const struct sk_buff *skb)
  256. {
  257. unsigned char *dst_pan;
  258. switch (ieee802154_daddr_mode(fc)) {
  259. case cpu_to_le16(IEEE802154_FCTL_ADDR_NONE):
  260. dst_pan = NULL;
  261. break;
  262. case cpu_to_le16(IEEE802154_FCTL_DADDR_SHORT):
  263. case cpu_to_le16(IEEE802154_FCTL_DADDR_EXTENDED):
  264. dst_pan = skb_mac_header(skb) +
  265. IEEE802154_FC_LEN +
  266. IEEE802154_SEQ_LEN;
  267. break;
  268. default:
  269. WARN_ONCE(1, "invalid addr mode detected");
  270. dst_pan = NULL;
  271. break;
  272. }
  273. return dst_pan;
  274. }
  275. /**
  276. * ieee802154_skb_src_pan - get the pointer to source pan field
  277. * @fc: mac header frame control field
  278. * @skb: skb where the source pan pointer will be get from
  279. */
  280. static inline unsigned char *ieee802154_skb_src_pan(__le16 fc,
  281. const struct sk_buff *skb)
  282. {
  283. unsigned char *src_pan;
  284. switch (ieee802154_saddr_mode(fc)) {
  285. case cpu_to_le16(IEEE802154_FCTL_ADDR_NONE):
  286. src_pan = NULL;
  287. break;
  288. case cpu_to_le16(IEEE802154_FCTL_SADDR_SHORT):
  289. case cpu_to_le16(IEEE802154_FCTL_SADDR_EXTENDED):
  290. /* if intra-pan and source addr mode is non none,
  291. * then source pan id is equal destination pan id.
  292. */
  293. if (ieee802154_is_intra_pan(fc)) {
  294. src_pan = ieee802154_skb_dst_pan(fc, skb);
  295. break;
  296. }
  297. switch (ieee802154_daddr_mode(fc)) {
  298. case cpu_to_le16(IEEE802154_FCTL_ADDR_NONE):
  299. src_pan = skb_mac_header(skb) +
  300. IEEE802154_FC_LEN +
  301. IEEE802154_SEQ_LEN;
  302. break;
  303. case cpu_to_le16(IEEE802154_FCTL_DADDR_SHORT):
  304. src_pan = skb_mac_header(skb) +
  305. IEEE802154_FC_LEN +
  306. IEEE802154_SEQ_LEN +
  307. IEEE802154_PAN_ID_LEN +
  308. IEEE802154_SHORT_ADDR_LEN;
  309. break;
  310. case cpu_to_le16(IEEE802154_FCTL_DADDR_EXTENDED):
  311. src_pan = skb_mac_header(skb) +
  312. IEEE802154_FC_LEN +
  313. IEEE802154_SEQ_LEN +
  314. IEEE802154_PAN_ID_LEN +
  315. IEEE802154_EXTENDED_ADDR_LEN;
  316. break;
  317. default:
  318. WARN_ONCE(1, "invalid addr mode detected");
  319. src_pan = NULL;
  320. break;
  321. }
  322. break;
  323. default:
  324. WARN_ONCE(1, "invalid addr mode detected");
  325. src_pan = NULL;
  326. break;
  327. }
  328. return src_pan;
  329. }
  330. /**
  331. * ieee802154_skb_is_intra_pan_addressing - checks whenever the mac addressing
  332. * is an intra pan communication
  333. * @fc: mac header frame control field
  334. * @skb: skb where the source and destination pan should be get from
  335. */
  336. static inline bool ieee802154_skb_is_intra_pan_addressing(__le16 fc,
  337. const struct sk_buff *skb)
  338. {
  339. unsigned char *dst_pan = ieee802154_skb_dst_pan(fc, skb),
  340. *src_pan = ieee802154_skb_src_pan(fc, skb);
  341. /* if one is NULL is no intra pan addressing */
  342. if (!dst_pan || !src_pan)
  343. return false;
  344. return !memcmp(dst_pan, src_pan, IEEE802154_PAN_ID_LEN);
  345. }
  346. /**
  347. * ieee802154_be64_to_le64 - copies and convert be64 to le64
  348. * @le64_dst: le64 destination pointer
  349. * @be64_src: be64 source pointer
  350. */
  351. static inline void ieee802154_be64_to_le64(void *le64_dst, const void *be64_src)
  352. {
  353. put_unaligned_le64(get_unaligned_be64(be64_src), le64_dst);
  354. }
  355. /**
  356. * ieee802154_le64_to_be64 - copies and convert le64 to be64
  357. * @be64_dst: be64 destination pointer
  358. * @le64_src: le64 source pointer
  359. */
  360. static inline void ieee802154_le64_to_be64(void *be64_dst, const void *le64_src)
  361. {
  362. put_unaligned_be64(get_unaligned_le64(le64_src), be64_dst);
  363. }
  364. /**
  365. * ieee802154_le16_to_be16 - copies and convert le16 to be16
  366. * @be16_dst: be16 destination pointer
  367. * @le16_src: le16 source pointer
  368. */
  369. static inline void ieee802154_le16_to_be16(void *be16_dst, const void *le16_src)
  370. {
  371. put_unaligned_be16(get_unaligned_le16(le16_src), be16_dst);
  372. }
  373. /**
  374. * ieee802154_be16_to_le16 - copies and convert be16 to le16
  375. * @le16_dst: le16 destination pointer
  376. * @be16_src: be16 source pointer
  377. */
  378. static inline void ieee802154_be16_to_le16(void *le16_dst, const void *be16_src)
  379. {
  380. put_unaligned_le16(get_unaligned_be16(be16_src), le16_dst);
  381. }
  382. /**
  383. * ieee802154_alloc_hw - Allocate a new hardware device
  384. *
  385. * This must be called once for each hardware device. The returned pointer
  386. * must be used to refer to this device when calling other functions.
  387. * mac802154 allocates a private data area for the driver pointed to by
  388. * @priv in &struct ieee802154_hw, the size of this area is given as
  389. * @priv_data_len.
  390. *
  391. * @priv_data_len: length of private data
  392. * @ops: callbacks for this device
  393. *
  394. * Return: A pointer to the new hardware device, or %NULL on error.
  395. */
  396. struct ieee802154_hw *
  397. ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops);
  398. /**
  399. * ieee802154_free_hw - free hardware descriptor
  400. *
  401. * This function frees everything that was allocated, including the
  402. * private data for the driver. You must call ieee802154_unregister_hw()
  403. * before calling this function.
  404. *
  405. * @hw: the hardware to free
  406. */
  407. void ieee802154_free_hw(struct ieee802154_hw *hw);
  408. /**
  409. * ieee802154_register_hw - Register hardware device
  410. *
  411. * You must call this function before any other functions in
  412. * mac802154. Note that before a hardware can be registered, you
  413. * need to fill the contained wpan_phy's information.
  414. *
  415. * @hw: the device to register as returned by ieee802154_alloc_hw()
  416. *
  417. * Return: 0 on success. An error code otherwise.
  418. */
  419. int ieee802154_register_hw(struct ieee802154_hw *hw);
  420. /**
  421. * ieee802154_unregister_hw - Unregister a hardware device
  422. *
  423. * This function instructs mac802154 to free allocated resources
  424. * and unregister netdevices from the networking subsystem.
  425. *
  426. * @hw: the hardware to unregister
  427. */
  428. void ieee802154_unregister_hw(struct ieee802154_hw *hw);
  429. /**
  430. * ieee802154_rx_irqsafe - receive frame
  431. *
  432. * Like ieee802154_rx() but can be called in IRQ context
  433. * (internally defers to a tasklet.)
  434. *
  435. * @hw: the hardware this frame came in on
  436. * @skb: the buffer to receive, owned by mac802154 after this call
  437. * @lqi: link quality indicator
  438. */
  439. void ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb,
  440. u8 lqi);
  441. /**
  442. * ieee802154_wake_queue - wake ieee802154 queue
  443. * @hw: pointer as obtained from ieee802154_alloc_hw().
  444. *
  445. * Drivers should use this function instead of netif_wake_queue.
  446. */
  447. void ieee802154_wake_queue(struct ieee802154_hw *hw);
  448. /**
  449. * ieee802154_stop_queue - stop ieee802154 queue
  450. * @hw: pointer as obtained from ieee802154_alloc_hw().
  451. *
  452. * Drivers should use this function instead of netif_stop_queue.
  453. */
  454. void ieee802154_stop_queue(struct ieee802154_hw *hw);
  455. /**
  456. * ieee802154_xmit_complete - frame transmission complete
  457. *
  458. * @hw: pointer as obtained from ieee802154_alloc_hw().
  459. * @skb: buffer for transmission
  460. * @ifs_handling: indicate interframe space handling
  461. */
  462. void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb,
  463. bool ifs_handling);
  464. #endif /* NET_MAC802154_H */