blob: da574bbdc33393fc927ba7297f7d67d0cf59b06e [file] [log] [blame]
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +00001/*
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 *
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000015 */
16#ifndef NET_MAC802154_H
17#define NET_MAC802154_H
18
19#include <net/af_ieee802154.h>
Alexander Aring239a84a2014-11-05 20:51:23 +010020#include <linux/ieee802154.h>
Phoebe Buckheister94b4f6c2014-03-14 21:24:00 +010021#include <linux/skbuff.h>
Alexander Aringba5bf172015-02-11 14:39:16 +010022#include <linux/unaligned/memmove.h>
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000023
Alexander Aring7fe9a382014-12-10 15:33:12 +010024#include <net/cfg802154.h>
25
Alexander Aring6b70a432015-06-06 17:30:47 +020026/**
27 * enum ieee802154_hw_addr_filt_flags - hardware address filtering flags
28 *
29 * The following flags are used to indicate changed address settings from
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000030 * the stack to the hardware.
Alexander Aring6b70a432015-06-06 17:30:47 +020031 *
32 * @IEEE802154_AFILT_SADDR_CHANGED: Indicates that the short address will be
33 * change.
34 *
35 * @IEEE802154_AFILT_IEEEADDR_CHANGED: Indicates that the extended address
36 * will be change.
37 *
38 * @IEEE802154_AFILT_PANID_CHANGED: Indicates that the pan id will be change.
39 *
40 * @IEEE802154_AFILT_PANC_CHANGED: Indicates that the address filter will
41 * do frame address filtering as a pan coordinator.
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000042 */
Alexander Aring6b70a432015-06-06 17:30:47 +020043enum ieee802154_hw_addr_filt_flags {
Alexander Aring70f36502015-06-12 09:24:00 +020044 IEEE802154_AFILT_SADDR_CHANGED = BIT(0),
45 IEEE802154_AFILT_IEEEADDR_CHANGED = BIT(1),
46 IEEE802154_AFILT_PANID_CHANGED = BIT(2),
47 IEEE802154_AFILT_PANC_CHANGED = BIT(3),
Alexander Aring6b70a432015-06-06 17:30:47 +020048};
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000049
Alexander Aringa0825b02015-06-06 17:30:52 +020050/**
51 * struct ieee802154_hw_addr_filt - hardware address filtering settings
52 *
53 * @pan_id: pan_id which should be set to the hardware address filter.
54 *
55 * @short_addr: short_addr which should be set to the hardware address filter.
56 *
57 * @ieee_addr: extended address which should be set to the hardware address
58 * filter.
59 *
60 * @pan_coord: boolean if hardware filtering should be operate as coordinator.
61 */
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000062struct ieee802154_hw_addr_filt {
Alexander Aringa0825b02015-06-06 17:30:52 +020063 __le16 pan_id;
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000064 __le16 short_addr;
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010065 __le64 ieee_addr;
Alexander Aring623c1232015-06-06 17:30:53 +020066 bool pan_coord;
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000067};
68
Alexander Aringa0825b02015-06-06 17:30:52 +020069/**
70 * struct ieee802154_hw - ieee802154 hardware
71 *
72 * @extra_tx_headroom: headroom to reserve in each transmit skb for use by the
73 * driver (e.g. for transmit headers.)
74 *
75 * @flags: hardware flags, see &enum ieee802154_hw_flags
76 *
77 * @parent: parent device of the hardware.
78 *
79 * @priv: pointer to private area that was allocated for driver use along with
80 * this structure.
81 *
82 * @phy: This points to the &struct wpan_phy allocated for this 802.15.4 PHY.
83 */
Alexander Aring5a504392014-10-25 17:16:34 +020084struct ieee802154_hw {
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000085 /* filled by the driver */
86 int extra_tx_headroom;
87 u32 flags;
88 struct device *parent;
Alexander Aringaf69a342015-06-06 17:30:51 +020089 void *priv;
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000090
91 /* filled by mac802154 core */
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000092 struct wpan_phy *phy;
93};
94
Alexander Aringbcbfd202015-06-06 17:30:49 +020095/**
96 * enum ieee802154_hw_flags - hardware flags
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000097 *
Alexander Aringbcbfd202015-06-06 17:30:49 +020098 * These flags are used to indicate hardware capabilities to
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +000099 * 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.
Alexander Aringbcbfd202015-06-06 17:30:49 +0200103 *
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.
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000126 */
Alexander Aringbcbfd202015-06-06 17:30:49 +0200127enum ieee802154_hw_flags {
Alexander Aring70f36502015-06-12 09:24:00 +0200128 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),
Alexander Aringbcbfd202015-06-06 17:30:49 +0200136};
Alexander Aring90386a72014-10-29 21:34:34 +0100137
138/* Indicates that receiver omits FCS and xmitter will add FCS on it's own. */
139#define IEEE802154_HW_OMIT_CKSUM (IEEE802154_HW_TX_OMIT_CKSUM | \
140 IEEE802154_HW_RX_OMIT_CKSUM)
Alexander Aring640985e2014-07-03 00:20:43 +0200141
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000142/* struct ieee802154_ops - callbacks from mac802154 to the driver
143 *
144 * This structure contains various callbacks that the driver may
145 * handle or, in some cases, must handle, for example to transmit
146 * a frame.
147 *
148 * start: Handler that 802.15.4 module calls for device initialization.
149 * This function is called before the first interface is attached.
150 *
151 * stop: Handler that 802.15.4 module calls for device cleanup.
152 * This function is called after the last interface is removed.
153 *
Alexander Aringed0a5dc2014-10-26 09:37:08 +0100154 * xmit_sync:
155 * Handler that 802.15.4 module calls for each transmitted frame.
156 * skb cntains the buffer starting from the IEEE 802.15.4 header.
157 * The low-level driver should send the frame based on available
158 * configuration. This is called by a workqueue and useful for
159 * synchronous 802.15.4 drivers.
160 * This function should return zero or negative errno.
161 *
Alexander Aring1e7283a2014-10-26 09:37:14 +0100162 * WARNING:
163 * This will be deprecated soon. We don't accept synced xmit callbacks
164 * drivers anymore.
165 *
Alexander Aringed0a5dc2014-10-26 09:37:08 +0100166 * xmit_async:
167 * Handler that 802.15.4 module calls for each transmitted frame.
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000168 * skb cntains the buffer starting from the IEEE 802.15.4 header.
169 * The low-level driver should send the frame based on available
170 * configuration.
Alexander Aringdc67c6b2014-10-26 09:37:04 +0100171 * This function should return zero or negative errno.
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000172 *
173 * ed: Handler that 802.15.4 module calls for Energy Detection.
174 * This function should place the value for detected energy
175 * (usually device-dependant) in the level pointer and return
176 * either zero or negative errno. Called with pib_lock held.
177 *
178 * set_channel:
179 * Set radio for listening on specific channel.
180 * Set the device for listening on specified channel.
181 * Returns either zero, or negative errno. Called with pib_lock held.
182 *
183 * set_hw_addr_filt:
184 * Set radio for listening on specific address.
185 * Set the device for listening on specified address.
186 * Returns either zero, or negative errno.
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +0100187 *
188 * set_txpower:
Alexander Aringe2eb1732015-05-17 21:44:40 +0200189 * Set radio transmit power in mBm. Called with pib_lock held.
Phoebe Buckheister9b2777d2014-02-17 11:34:08 +0100190 * Returns either zero, or negative errno.
Phoebe Buckheister84dda3c2014-02-17 11:34:10 +0100191 *
192 * set_lbt
193 * Enables or disables listen before talk on the device. Called with
194 * pib_lock held.
195 * Returns either zero, or negative errno.
Phoebe Buckheisterba08fea2014-02-17 11:34:11 +0100196 *
197 * set_cca_mode
198 * Sets the CCA mode used by the device. Called with pib_lock held.
199 * Returns either zero, or negative errno.
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100200 *
201 * set_cca_ed_level
Alexander Aring32b23552015-05-17 21:44:41 +0200202 * Sets the CCA energy detection threshold in mBm. Called with pib_lock
Phoebe Buckheister6ca00192014-02-17 11:34:12 +0100203 * held.
204 * Returns either zero, or negative errno.
Phoebe Buckheister4244db12014-02-17 11:34:14 +0100205 *
206 * set_csma_params
207 * Sets the CSMA parameter set for the PHY. Called with pib_lock held.
208 * Returns either zero, or negative errno.
209 *
210 * set_frame_retries
211 * Sets the retransmission attempt limit. Called with pib_lock held.
212 * Returns either zero, or negative errno.
Alexander Aring94b79222014-10-29 21:34:32 +0100213 *
214 * set_promiscuous_mode
215 * Enables or disable promiscuous mode.
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000216 */
217struct ieee802154_ops {
218 struct module *owner;
Alexander Aring5a504392014-10-25 17:16:34 +0200219 int (*start)(struct ieee802154_hw *hw);
220 void (*stop)(struct ieee802154_hw *hw);
Alexander Aringed0a5dc2014-10-26 09:37:08 +0100221 int (*xmit_sync)(struct ieee802154_hw *hw,
222 struct sk_buff *skb);
223 int (*xmit_async)(struct ieee802154_hw *hw,
224 struct sk_buff *skb);
Alexander Aring5a504392014-10-25 17:16:34 +0200225 int (*ed)(struct ieee802154_hw *hw, u8 *level);
Alexander Aringe37d2ec2014-10-28 18:21:19 +0100226 int (*set_channel)(struct ieee802154_hw *hw, u8 page,
227 u8 channel);
Alexander Aring5a504392014-10-25 17:16:34 +0200228 int (*set_hw_addr_filt)(struct ieee802154_hw *hw,
229 struct ieee802154_hw_addr_filt *filt,
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000230 unsigned long changed);
Alexander Aringe2eb1732015-05-17 21:44:40 +0200231 int (*set_txpower)(struct ieee802154_hw *hw, s32 mbm);
Alexander Aring5a504392014-10-25 17:16:34 +0200232 int (*set_lbt)(struct ieee802154_hw *hw, bool on);
Alexander Aring7fe9a382014-12-10 15:33:12 +0100233 int (*set_cca_mode)(struct ieee802154_hw *hw,
234 const struct wpan_phy_cca *cca);
Alexander Aring32b23552015-05-17 21:44:41 +0200235 int (*set_cca_ed_level)(struct ieee802154_hw *hw, s32 mbm);
Alexander Aring5a504392014-10-25 17:16:34 +0200236 int (*set_csma_params)(struct ieee802154_hw *hw,
Phoebe Buckheister4244db12014-02-17 11:34:14 +0100237 u8 min_be, u8 max_be, u8 retries);
Alexander Aring5a504392014-10-25 17:16:34 +0200238 int (*set_frame_retries)(struct ieee802154_hw *hw,
Phoebe Buckheister4244db12014-02-17 11:34:14 +0100239 s8 retries);
Alexander Aring94b79222014-10-29 21:34:32 +0100240 int (*set_promiscuous_mode)(struct ieee802154_hw *hw,
241 const bool on);
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000242};
243
Alexander Aringab24f502014-11-02 04:18:40 +0100244/**
Alexander Aring54552d02015-09-02 14:21:29 +0200245 * ieee802154_get_fc_from_skb - get the frame control field from an skb
246 * @skb: skb where the frame control field will be get from
247 */
248static inline __le16 ieee802154_get_fc_from_skb(const struct sk_buff *skb)
249{
250 /* return some invalid fc on failure */
Alexander Aring5f2ebb32015-09-28 19:59:48 +0200251 if (unlikely(skb->len < 2)) {
Alexander Aring54552d02015-09-02 14:21:29 +0200252 WARN_ON(1);
253 return cpu_to_le16(0);
254 }
255
256 return (__force __le16)__get_unaligned_memmove16(skb_mac_header(skb));
257}
258
259/**
Alexander Aring705cbbb2014-11-05 20:51:24 +0100260 * ieee802154_be64_to_le64 - copies and convert be64 to le64
261 * @le64_dst: le64 destination pointer
262 * @be64_src: be64 source pointer
Alexander Aringab24f502014-11-02 04:18:40 +0100263 */
Alexander Aring705cbbb2014-11-05 20:51:24 +0100264static inline void ieee802154_be64_to_le64(void *le64_dst, const void *be64_src)
Alexander Aringab24f502014-11-02 04:18:40 +0100265{
Alexander Aringba5bf172015-02-11 14:39:16 +0100266 __put_unaligned_memmove64(swab64p(be64_src), le64_dst);
Alexander Aringab24f502014-11-02 04:18:40 +0100267}
268
Alexander Aring239a84a2014-11-05 20:51:23 +0100269/**
270 * ieee802154_le64_to_be64 - copies and convert le64 to be64
271 * @be64_dst: be64 destination pointer
272 * @le64_src: le64 source pointer
273 */
274static inline void ieee802154_le64_to_be64(void *be64_dst, const void *le64_src)
275{
Alexander Aringb9767962015-02-11 14:39:17 +0100276 __put_unaligned_memmove64(swab64p(le64_src), be64_dst);
Alexander Aring239a84a2014-11-05 20:51:23 +0100277}
278
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200279/**
Alexander Aring8911d772015-10-13 13:42:58 +0200280 * ieee802154_le16_to_be16 - copies and convert le16 to be16
281 * @be16_dst: be16 destination pointer
282 * @le16_src: le16 source pointer
283 */
284static inline void ieee802154_le16_to_be16(void *be16_dst, const void *le16_src)
285{
286 __put_unaligned_memmove16(swab16p(le16_src), be16_dst);
287}
288
289/**
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200290 * ieee802154_alloc_hw - Allocate a new hardware device
291 *
292 * This must be called once for each hardware device. The returned pointer
293 * must be used to refer to this device when calling other functions.
294 * mac802154 allocates a private data area for the driver pointed to by
295 * @priv in &struct ieee802154_hw, the size of this area is given as
296 * @priv_data_len.
297 *
298 * @priv_data_len: length of private data
299 * @ops: callbacks for this device
300 *
301 * Return: A pointer to the new hardware device, or %NULL on error.
302 */
Alexander Aring5a504392014-10-25 17:16:34 +0200303struct ieee802154_hw *
Alexander Aring16301862014-10-28 18:21:18 +0100304ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops);
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200305
306/**
307 * ieee802154_free_hw - free hardware descriptor
308 *
309 * This function frees everything that was allocated, including the
310 * private data for the driver. You must call ieee802154_unregister_hw()
311 * before calling this function.
312 *
313 * @hw: the hardware to free
314 */
Alexander Aring5a504392014-10-25 17:16:34 +0200315void ieee802154_free_hw(struct ieee802154_hw *hw);
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200316
317/**
318 * ieee802154_register_hw - Register hardware device
319 *
320 * You must call this function before any other functions in
321 * mac802154. Note that before a hardware can be registered, you
322 * need to fill the contained wpan_phy's information.
323 *
324 * @hw: the device to register as returned by ieee802154_alloc_hw()
325 *
326 * Return: 0 on success. An error code otherwise.
327 */
Alexander Aring5a504392014-10-25 17:16:34 +0200328int ieee802154_register_hw(struct ieee802154_hw *hw);
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200329
330/**
331 * ieee802154_unregister_hw - Unregister a hardware device
332 *
333 * This function instructs mac802154 to free allocated resources
334 * and unregister netdevices from the networking subsystem.
335 *
336 * @hw: the hardware to unregister
337 */
Alexander Aring5a504392014-10-25 17:16:34 +0200338void ieee802154_unregister_hw(struct ieee802154_hw *hw);
alex.bluesman.smirnov@gmail.com1010f542012-05-15 20:50:20 +0000339
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200340/**
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200341 * ieee802154_rx_irqsafe - receive frame
342 *
343 * Like ieee802154_rx() but can be called in IRQ context
344 * (internally defers to a tasklet.)
345 *
346 * @hw: the hardware this frame came in on
347 * @skb: the buffer to receive, owned by mac802154 after this call
348 * @lqi: link quality indicator
349 */
Alexander Aring5a504392014-10-25 17:16:34 +0200350void ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb,
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +0000351 u8 lqi);
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200352/**
353 * ieee802154_wake_queue - wake ieee802154 queue
354 * @hw: pointer as obtained from ieee802154_alloc_hw().
355 *
356 * Drivers should use this function instead of netif_wake_queue.
357 */
Alexander Aringc2085102014-10-26 09:37:05 +0100358void ieee802154_wake_queue(struct ieee802154_hw *hw);
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200359
360/**
361 * ieee802154_stop_queue - stop ieee802154 queue
362 * @hw: pointer as obtained from ieee802154_alloc_hw().
363 *
364 * Drivers should use this function instead of netif_stop_queue.
365 */
Alexander Aringc2085102014-10-26 09:37:05 +0100366void ieee802154_stop_queue(struct ieee802154_hw *hw);
Varka Bhadram42fb23e2015-04-30 17:44:52 +0200367
368/**
369 * ieee802154_xmit_complete - frame transmission complete
370 *
371 * @hw: pointer as obtained from ieee802154_alloc_hw().
372 * @skb: buffer for transmission
373 * @ifs_handling: indicate interframe space handling
374 */
Alexander Aring61f2dcb2014-11-12 19:51:56 +0100375void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb,
376 bool ifs_handling);
Alexander Aringc2085102014-10-26 09:37:05 +0100377
alex.bluesman.smirnov@gmail.com0afd7ad2012-05-15 20:50:19 +0000378#endif /* NET_MAC802154_H */